Create game as spectator (#4281)

* refactoring

* allow for creation of games as spectator

allow setting the amount of games per user to none
remove limit on amount of games when creating a game as judge as
spectator

* refactor common/server_player.cpp

* do not close games with spectating host automatically

* remove check that filters out 0 player games

this check didn't really do anything, deleted games are removed before
it would be reached

* don't transfer host to spectators

this seems to cause a bug, also present on master
This commit is contained in:
ebbit1q
2021-03-13 20:39:25 +01:00
committed by GitHub
parent b722864caf
commit 06bfc0291a
13 changed files with 153 additions and 140 deletions

View File

@@ -766,22 +766,28 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room
if (gameId == -1)
return Response::RespInternalError;
if (server->getMaxGamesPerUser() > 0)
if (room->getGamesCreatedByUser(QString::fromStdString(userInfo->name())) >= server->getMaxGamesPerUser())
return Response::RespContextError;
if (cmd.join_as_judge() && !server->permitCreateGameAsJudge() &&
!(userInfo->user_level() & ServerInfo_User::IsJudge)) {
auto level = userInfo->user_level();
bool isJudge = level & ServerInfo_User::IsJudge;
int maxGames = server->getMaxGamesPerUser();
bool asJudge = cmd.join_as_judge();
bool asSpectator = cmd.join_as_spectator();
// allow judges to open games as spectator without limit to facilitate bots etc, -1 means no limit
if (!(isJudge && asJudge && asSpectator) && maxGames >= 0 &&
room->getGamesCreatedByUser(QString::fromStdString(userInfo->name())) >= maxGames) {
return Response::RespContextError;
}
QList<int> gameTypes;
for (int i = cmd.game_type_ids_size() - 1; i >= 0; --i)
gameTypes.append(cmd.game_type_ids(i));
// if a non judge user tries to create a game as judge while not permitted, instead create a normal game
if (asJudge && !(server->permitCreateGameAsJudge() || isJudge)) {
asJudge = false;
}
QString description = QString::fromStdString(cmd.description());
if (description.size() > 60)
description = description.left(60);
QList<int> gameTypes;
for (int i = cmd.game_type_ids_size() - 1; i >= 0; --i) { // FIXME: why are these iterated in reverse?
gameTypes.append(cmd.game_type_ids(i));
}
QString description = QString::fromStdString(cmd.description()).left(60);
// When server doesn't permit registered users to exist, do not honor only-reg setting
bool onlyRegisteredUsers = cmd.only_registered() && (server->permitUnregisteredUsers());
@@ -790,7 +796,7 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room
cmd.only_buddies(), onlyRegisteredUsers, cmd.spectators_allowed(), cmd.spectators_need_password(),
cmd.spectators_can_talk(), cmd.spectators_see_everything(), room);
game->addPlayer(this, rc, false, cmd.join_as_judge(), false);
game->addPlayer(this, rc, asSpectator, asJudge, false);
room->addGame(game);
return Response::RespOk;