moderators can override game restrictions and kick players when spectating; game host privileges shift when host leaves

This commit is contained in:
Max-Wilhelm Bruker 2011-11-13 13:13:41 +01:00
parent bca7b6414b
commit c7ef61f750
22 changed files with 165 additions and 90 deletions

View file

@ -48,40 +48,41 @@ ItemId_Event_ConnectionStateChanged = 1046,
ItemId_Event_Say = 1047,
ItemId_Event_Leave = 1048,
ItemId_Event_GameClosed = 1049,
ItemId_Event_Kicked = 1050,
ItemId_Event_Shuffle = 1051,
ItemId_Event_RollDie = 1052,
ItemId_Event_MoveCard = 1053,
ItemId_Event_FlipCard = 1054,
ItemId_Event_DestroyCard = 1055,
ItemId_Event_AttachCard = 1056,
ItemId_Event_CreateToken = 1057,
ItemId_Event_DeleteArrow = 1058,
ItemId_Event_SetCardAttr = 1059,
ItemId_Event_SetCardCounter = 1060,
ItemId_Event_SetCounter = 1061,
ItemId_Event_DelCounter = 1062,
ItemId_Event_SetActivePlayer = 1063,
ItemId_Event_SetActivePhase = 1064,
ItemId_Event_DumpZone = 1065,
ItemId_Event_StopDumpZone = 1066,
ItemId_Event_RemoveFromList = 1067,
ItemId_Event_ServerMessage = 1068,
ItemId_Event_ServerShutdown = 1069,
ItemId_Event_ConnectionClosed = 1070,
ItemId_Event_Message = 1071,
ItemId_Event_GameJoined = 1072,
ItemId_Event_UserLeft = 1073,
ItemId_Event_LeaveRoom = 1074,
ItemId_Event_RoomSay = 1075,
ItemId_Context_ReadyStart = 1076,
ItemId_Context_Concede = 1077,
ItemId_Context_DeckSelect = 1078,
ItemId_Context_UndoDraw = 1079,
ItemId_Context_MoveCard = 1080,
ItemId_Context_Mulligan = 1081,
ItemId_Command_UpdateServerMessage = 1082,
ItemId_Command_ShutdownServer = 1083,
ItemId_Command_BanFromServer = 1084,
ItemId_Other = 1085
ItemId_Event_GameHostChanged = 1050,
ItemId_Event_Kicked = 1051,
ItemId_Event_Shuffle = 1052,
ItemId_Event_RollDie = 1053,
ItemId_Event_MoveCard = 1054,
ItemId_Event_FlipCard = 1055,
ItemId_Event_DestroyCard = 1056,
ItemId_Event_AttachCard = 1057,
ItemId_Event_CreateToken = 1058,
ItemId_Event_DeleteArrow = 1059,
ItemId_Event_SetCardAttr = 1060,
ItemId_Event_SetCardCounter = 1061,
ItemId_Event_SetCounter = 1062,
ItemId_Event_DelCounter = 1063,
ItemId_Event_SetActivePlayer = 1064,
ItemId_Event_SetActivePhase = 1065,
ItemId_Event_DumpZone = 1066,
ItemId_Event_StopDumpZone = 1067,
ItemId_Event_RemoveFromList = 1068,
ItemId_Event_ServerMessage = 1069,
ItemId_Event_ServerShutdown = 1070,
ItemId_Event_ConnectionClosed = 1071,
ItemId_Event_Message = 1072,
ItemId_Event_GameJoined = 1073,
ItemId_Event_UserLeft = 1074,
ItemId_Event_LeaveRoom = 1075,
ItemId_Event_RoomSay = 1076,
ItemId_Context_ReadyStart = 1077,
ItemId_Context_Concede = 1078,
ItemId_Context_DeckSelect = 1079,
ItemId_Context_UndoDraw = 1080,
ItemId_Context_MoveCard = 1081,
ItemId_Context_Mulligan = 1082,
ItemId_Command_UpdateServerMessage = 1083,
ItemId_Command_ShutdownServer = 1084,
ItemId_Command_BanFromServer = 1085,
ItemId_Other = 1086
};

View file

@ -86,12 +86,13 @@ Command_RoomSay::Command_RoomSay(int _roomId, const QString &_message)
{
insertItem(new SerializableItem_String("message", _message));
}
Command_JoinGame::Command_JoinGame(int _roomId, int _gameId, const QString &_password, bool _spectator)
Command_JoinGame::Command_JoinGame(int _roomId, int _gameId, const QString &_password, bool _spectator, bool _overrideRestrictions)
: RoomCommand("join_game", _roomId)
{
insertItem(new SerializableItem_Int("game_id", _gameId));
insertItem(new SerializableItem_String("password", _password));
insertItem(new SerializableItem_Bool("spectator", _spectator));
insertItem(new SerializableItem_Bool("override_restrictions", _overrideRestrictions));
}
Command_KickFromGame::Command_KickFromGame(int _gameId, int _playerId)
: GameCommand("kick_from_game", _gameId)
@ -278,6 +279,10 @@ Event_GameClosed::Event_GameClosed(int _playerId)
: GameEvent("game_closed", _playerId)
{
}
Event_GameHostChanged::Event_GameHostChanged(int _playerId)
: GameEvent("game_host_changed", _playerId)
{
}
Event_Kicked::Event_Kicked(int _playerId)
: GameEvent("kicked", _playerId)
{
@ -426,11 +431,12 @@ Event_Message::Event_Message(const QString &_senderName, const QString &_receive
insertItem(new SerializableItem_String("receiver_name", _receiverName));
insertItem(new SerializableItem_String("text", _text));
}
Event_GameJoined::Event_GameJoined(int _gameId, const QString &_gameDescription, int _playerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming)
Event_GameJoined::Event_GameJoined(int _gameId, const QString &_gameDescription, int _hostId, int _playerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming)
: GenericEvent("game_joined")
{
insertItem(new SerializableItem_Int("game_id", _gameId));
insertItem(new SerializableItem_String("game_description", _gameDescription));
insertItem(new SerializableItem_Int("host_id", _hostId));
insertItem(new SerializableItem_Int("player_id", _playerId));
insertItem(new SerializableItem_Bool("spectator", _spectator));
insertItem(new SerializableItem_Bool("spectators_can_talk", _spectatorsCanTalk));
@ -548,6 +554,7 @@ void ProtocolItem::initializeHashAuto()
itemNameHash.insert("game_eventsay", Event_Say::newItem);
itemNameHash.insert("game_eventleave", Event_Leave::newItem);
itemNameHash.insert("game_eventgame_closed", Event_GameClosed::newItem);
itemNameHash.insert("game_eventgame_host_changed", Event_GameHostChanged::newItem);
itemNameHash.insert("game_eventkicked", Event_Kicked::newItem);
itemNameHash.insert("game_eventshuffle", Event_Shuffle::newItem);
itemNameHash.insert("game_eventroll_die", Event_RollDie::newItem);

View file

@ -15,7 +15,7 @@
0:join_room:i,room_id
1:leave_room
1:room_say:s,message
1:join_game:i,game_id:s,password:b,spectator
1:join_game:i,game_id:s,password:b,spectator:b,override_restrictions
2:kick_from_game:i,player_id
2:leave_game
2:say:s,message
@ -47,6 +47,7 @@
3:say:s,message
3:leave
3:game_closed
3:game_host_changed
3:kicked
3:shuffle
3:roll_die:i,sides:i,value
@ -69,7 +70,7 @@
4:server_shutdown:s,reason:i,minutes
4:connection_closed:s,reason
4:message:s,sender_name:s,receiver_name:s,text
4:game_joined:i,game_id:s,game_description:i,player_id:b,spectator:b,spectators_can_talk:b,spectators_see_everything:b,resuming
4:game_joined:i,game_id:s,game_description:i,host_id:i,player_id:b,spectator:b,spectators_can_talk:b,spectators_see_everything:b,resuming
4:user_left:s,user_name
5:leave_room:s,player_name
5:room_say:s,player_name:s,message
@ -81,4 +82,4 @@
6:mulligan:i,number
7:update_server_message
7:shutdown_server:s,reason:i,minutes
8:ban_from_server:s,user_name:s,address:i,minutes:s,reason
8:ban_from_server:s,user_name:s,address:i,minutes:s,reason

View file

@ -142,10 +142,11 @@ public:
class Command_JoinGame : public RoomCommand {
Q_OBJECT
public:
Command_JoinGame(int _roomId = -1, int _gameId = -1, const QString &_password = QString(), bool _spectator = false);
Command_JoinGame(int _roomId = -1, int _gameId = -1, const QString &_password = QString(), bool _spectator = false, bool _overrideRestrictions = false);
int getGameId() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->getData(); };
QString getPassword() const { return static_cast<SerializableItem_String *>(itemMap.value("password"))->getData(); };
bool getSpectator() const { return static_cast<SerializableItem_Bool *>(itemMap.value("spectator"))->getData(); };
bool getOverrideRestrictions() const { return static_cast<SerializableItem_Bool *>(itemMap.value("override_restrictions"))->getData(); };
static SerializableItem *newItem() { return new Command_JoinGame; }
int getItemId() const { return ItemId_Command_JoinGame; }
};
@ -427,6 +428,13 @@ public:
static SerializableItem *newItem() { return new Event_GameClosed; }
int getItemId() const { return ItemId_Event_GameClosed; }
};
class Event_GameHostChanged : public GameEvent {
Q_OBJECT
public:
Event_GameHostChanged(int _playerId = -1);
static SerializableItem *newItem() { return new Event_GameHostChanged; }
int getItemId() const { return ItemId_Event_GameHostChanged; }
};
class Event_Kicked : public GameEvent {
Q_OBJECT
public:
@ -644,9 +652,10 @@ public:
class Event_GameJoined : public GenericEvent {
Q_OBJECT
public:
Event_GameJoined(int _gameId = -1, const QString &_gameDescription = QString(), int _playerId = -1, bool _spectator = false, bool _spectatorsCanTalk = false, bool _spectatorsSeeEverything = false, bool _resuming = false);
Event_GameJoined(int _gameId = -1, const QString &_gameDescription = QString(), int _hostId = -1, int _playerId = -1, bool _spectator = false, bool _spectatorsCanTalk = false, bool _spectatorsSeeEverything = false, bool _resuming = false);
int getGameId() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->getData(); };
QString getGameDescription() const { return static_cast<SerializableItem_String *>(itemMap.value("game_description"))->getData(); };
int getHostId() const { return static_cast<SerializableItem_Int *>(itemMap.value("host_id"))->getData(); };
int getPlayerId() const { return static_cast<SerializableItem_Int *>(itemMap.value("player_id"))->getData(); };
bool getSpectator() const { return static_cast<SerializableItem_Bool *>(itemMap.value("spectator"))->getData(); };
bool getSpectatorsCanTalk() const { return static_cast<SerializableItem_Bool *>(itemMap.value("spectators_can_talk"))->getData(); };

View file

@ -29,7 +29,7 @@
#include <QDebug>
Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, const QList<int> &_gameTypes, bool _onlyBuddies, bool _onlyRegistered, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server_Room *_room)
: QObject(), room(_room), creatorInfo(new ServerInfo_User(_creator->getUserInfo())), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), gameTypes(_gameTypes), activePlayer(-1), activePhase(-1), onlyBuddies(_onlyBuddies), onlyRegistered(_onlyRegistered), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0), secondsElapsed(0), gameMutex(QMutex::Recursive)
: QObject(), room(_room), hostId(0), creatorInfo(new ServerInfo_User(_creator->getUserInfo())), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), gameTypes(_gameTypes), activePlayer(-1), activePhase(-1), onlyBuddies(_onlyBuddies), onlyRegistered(_onlyRegistered), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0), secondsElapsed(0), gameMutex(QMutex::Recursive)
{
connect(this, SIGNAL(sigStartGameIfReady()), this, SLOT(doStartGameIfReady()), Qt::QueuedConnection);
@ -201,21 +201,24 @@ void Server_Game::stopGameIfFinished()
}
}
ResponseCode Server_Game::checkJoin(ServerInfo_User *user, const QString &_password, bool spectator)
ResponseCode Server_Game::checkJoin(ServerInfo_User *user, const QString &_password, bool spectator, bool overrideRestrictions)
{
if ((_password != password) && !(spectator && !spectatorsNeedPassword))
return RespWrongPassword;
if (!(user->getUserLevel() & ServerInfo_User::IsRegistered) && onlyRegistered)
return RespUserLevelTooLow;
if (onlyBuddies)
if (!room->getServer()->getBuddyList(creatorInfo->getName()).contains(user->getName()))
return RespOnlyBuddies;
if (room->getServer()->getIgnoreList(creatorInfo->getName()).contains(user->getName()))
return RespInIgnoreList;
if (spectator) {
if (!spectatorsAllowed)
return RespSpectatorsNotAllowed;
} else if (gameStarted || (getPlayerCount() >= getMaxPlayers()))
if (!(overrideRestrictions && (user->getUserLevel() & ServerInfo_User::IsModerator))) {
if ((_password != password) && !(spectator && !spectatorsNeedPassword))
return RespWrongPassword;
if (!(user->getUserLevel() & ServerInfo_User::IsRegistered) && onlyRegistered)
return RespUserLevelTooLow;
if (onlyBuddies)
if (!room->getServer()->getBuddyList(creatorInfo->getName()).contains(user->getName()))
return RespOnlyBuddies;
if (room->getServer()->getIgnoreList(creatorInfo->getName()).contains(user->getName()))
return RespInIgnoreList;
if (spectator) {
if (!spectatorsAllowed)
return RespSpectatorsNotAllowed;
}
}
if (!spectator && (gameStarted || (getPlayerCount() >= getMaxPlayers())))
return RespGameFull;
return RespOk;
@ -260,12 +263,28 @@ void Server_Game::removePlayer(Server_Player *player)
sendGameEvent(new Event_Leave(player->getPlayerId()));
bool playerActive = activePlayer == player->getPlayerId();
bool playerHost = hostId == player->getPlayerId();
bool spectator = player->getSpectator();
player->prepareDestroy();
if (!getPlayerCount())
deleteLater();
else if (!spectator) {
if (playerHost) {
int newHostId = -1;
QMapIterator<int, Server_Player *> playerIterator(players);
while (playerIterator.hasNext()) {
Server_Player *p = playerIterator.next().value();
if (!p->getSpectator()) {
newHostId = p->getPlayerId();
break;
}
}
if (newHostId != -1) {
hostId = newHostId;
sendGameEvent(new Event_GameHostChanged(hostId));
}
}
stopGameIfFinished();
if (gameStarted && playerActive)
nextTurn();

View file

@ -35,6 +35,7 @@ class Server_Game : public QObject {
Q_OBJECT
private:
Server_Room *room;
int hostId;
ServerInfo_User *creatorInfo;
QMap<int, Server_Player *> players;
bool gameStarted;
@ -62,6 +63,7 @@ public:
Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, const QList<int> &_gameTypes, bool _onlyBuddies, bool _onlyRegistered, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, Server_Room *parent);
~Server_Game();
ServerInfo_Game *getInfo() const;
int getHostId() const { return hostId; }
ServerInfo_User *getCreatorInfo() const { return creatorInfo; }
bool getGameStarted() const { return gameStarted; }
int getPlayerCount() const;
@ -76,7 +78,7 @@ public:
bool getSpectatorsNeedPassword() const { return spectatorsNeedPassword; }
bool getSpectatorsCanTalk() const { return spectatorsCanTalk; }
bool getSpectatorsSeeEverything() const { return spectatorsSeeEverything; }
ResponseCode checkJoin(ServerInfo_User *user, const QString &_password, bool spectator);
ResponseCode checkJoin(ServerInfo_User *user, const QString &_password, bool spectator, bool overrideRestrictions);
bool containsUser(const QString &userName) const;
Server_Player *addPlayer(Server_ProtocolHandler *handler, bool spectator, bool broadcastUpdate = true);
void removePlayer(Server_Player *player);

View file

@ -327,7 +327,7 @@ ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *cmd, CommandContain
game->postConnectionStatusUpdate(gamePlayers[j], true);
games.insert(game->getGameId(), QPair<Server_Game *, Server_Player *>(game, gamePlayers[j]));
enqueueProtocolItem(new Event_GameJoined(game->getGameId(), game->getDescription(), gamePlayers[j]->getPlayerId(), gamePlayers[j]->getSpectator(), game->getSpectatorsCanTalk(), game->getSpectatorsSeeEverything(), true));
enqueueProtocolItem(new Event_GameJoined(game->getGameId(), game->getDescription(), game->getHostId(), gamePlayers[j]->getPlayerId(), gamePlayers[j]->getSpectator(), game->getSpectatorsCanTalk(), game->getSpectatorsSeeEverything(), true));
enqueueProtocolItem(GameEventContainer::makeNew(new Event_GameStateChanged(game->getGameStarted(), game->getActivePlayer(), game->getActivePhase(), game->getGameState(gamePlayers[j])), game->getGameId()));
break;
@ -527,7 +527,7 @@ ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd, Comm
QMutexLocker gameListLocker(&gameListMutex);
games.insert(game->getGameId(), QPair<Server_Game *, Server_Player *>(game, creator));
sendProtocolItem(new Event_GameJoined(game->getGameId(), game->getDescription(), creator->getPlayerId(), false, game->getSpectatorsCanTalk(), game->getSpectatorsSeeEverything(), false));
sendProtocolItem(new Event_GameJoined(game->getGameId(), game->getDescription(), creator->getPlayerId(), creator->getPlayerId(), false, game->getSpectatorsCanTalk(), game->getSpectatorsSeeEverything(), false));
sendProtocolItem(GameEventContainer::makeNew(new Event_GameStateChanged(game->getGameStarted(), game->getActivePlayer(), game->getActivePhase(), game->getGameState(creator)), game->getGameId()));
game->gameMutex.unlock();
@ -551,11 +551,11 @@ ResponseCode Server_ProtocolHandler::cmdJoinGame(Command_JoinGame *cmd, CommandC
QMutexLocker locker(&g->gameMutex);
ResponseCode result = g->checkJoin(userInfo, cmd->getPassword(), cmd->getSpectator());
ResponseCode result = g->checkJoin(userInfo, cmd->getPassword(), cmd->getSpectator(), cmd->getOverrideRestrictions());
if (result == RespOk) {
Server_Player *player = g->addPlayer(this, cmd->getSpectator());
games.insert(cmd->getGameId(), QPair<Server_Game *, Server_Player *>(g, player));
enqueueProtocolItem(new Event_GameJoined(cmd->getGameId(), g->getDescription(), player->getPlayerId(), cmd->getSpectator(), g->getSpectatorsCanTalk(), g->getSpectatorsSeeEverything(), false));
enqueueProtocolItem(new Event_GameJoined(cmd->getGameId(), g->getDescription(), g->getHostId(), player->getPlayerId(), cmd->getSpectator(), g->getSpectatorsCanTalk(), g->getSpectatorsSeeEverything(), false));
enqueueProtocolItem(GameEventContainer::makeNew(new Event_GameStateChanged(g->getGameStarted(), g->getActivePlayer(), g->getActivePhase(), g->getGameState(player)), cmd->getGameId()));
}
return result;
@ -569,7 +569,7 @@ ResponseCode Server_ProtocolHandler::cmdLeaveGame(Command_LeaveGame * /*cmd*/, C
ResponseCode Server_ProtocolHandler::cmdKickFromGame(Command_KickFromGame *cmd, CommandContainer * /*cont*/, Server_Game *game, Server_Player *player)
{
if (game->getCreatorInfo()->getName() != player->getUserInfo()->getName())
if ((game->getHostId() != player->getPlayerId()) && !(userInfo->getUserLevel() & ServerInfo_User::IsModerator))
return RespFunctionNotAllowed;
if (!game->kickPlayer(cmd->getPlayerId()))
@ -657,7 +657,7 @@ ResponseCode Server_ProtocolHandler::cmdReadyStart(Command_ReadyStart *cmd, Comm
ResponseCode Server_ProtocolHandler::cmdSay(Command_Say *cmd, CommandContainer * /*cont*/, Server_Game *game, Server_Player *player)
{
if (player->getSpectator() && !game->getSpectatorsCanTalk())
if (player->getSpectator() && !game->getSpectatorsCanTalk() && !(userInfo->getUserLevel() & ServerInfo_User::IsModerator))
return RespFunctionNotAllowed;
game->sendGameEvent(new Event_Say(player->getPlayerId(), cmd->getMessage()));