From 772b5113dec72211a6cc8a597669683fd63d1505 Mon Sep 17 00:00:00 2001 From: SpacialCircumstances Date: Mon, 22 Apr 2024 18:35:35 +0200 Subject: [PATCH 1/2] Do not throw error when adding player as spectator who is in the same game --- server/services/gameGalaxy.ts | 3 ++- server/services/gameList.ts | 2 ++ server/services/spectator.ts | 8 -------- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/server/services/gameGalaxy.ts b/server/services/gameGalaxy.ts index eb688804f..9d29e38b1 100644 --- a/server/services/gameGalaxy.ts +++ b/server/services/gameGalaxy.ts @@ -680,8 +680,9 @@ export default class GameGalaxyService { // If the user is spectating then they can see from the perspectives of all // players who they are spectating. + // If they are a player themselves, the spectator perspective is ignored if (userId && this.spectatorService.isSpectatingEnabled(game)) { - let spectating = this.spectatorService.listSpectatingPlayers(game, userId); + const spectating = this.spectatorService.listSpectatingPlayers(game, userId); if (spectating.length) { return spectating.map(p => p._id); diff --git a/server/services/gameList.ts b/server/services/gameList.ts index 30092e005..fbf03887b 100644 --- a/server/services/gameList.ts +++ b/server/services/gameList.ts @@ -178,6 +178,8 @@ export default class GameListService { } async listSpectating(userId: DBObjectId) { + // TODO: Verify user is not a player themselves + return await this.gameRepo.find({ 'state.endDate': { $eq: null }, // Game is in progress 'galaxy.players.spectators': { // User is spectating at least one player. diff --git a/server/services/spectator.ts b/server/services/spectator.ts index d4e4f2c2a..8dc026689 100644 --- a/server/services/spectator.ts +++ b/server/services/spectator.ts @@ -1,4 +1,3 @@ -import user from "../api/controllers/user"; import ValidationError from "../errors/validation"; import PlayerService from "./player"; import Repository from "./repository"; @@ -39,13 +38,6 @@ export default class SpectatorService { throw new ValidationError(`A player with the username ${username} does not exist.`); } - // Make sure the user isn't already playing in the game. - const existingPlayer = this.playerService.getByUserId(game, user._id); - - if (existingPlayer) { - throw new ValidationError(`The user ${username} is already playing in this game, they cannot be invited to spectate.`); - } - await this.gameRepo.updateOne({ _id: game._id, 'galaxy.players._id': player._id From b12d31ccf827f57efe1be8e62306e6dbcd45ae52 Mon Sep 17 00:00:00 2001 From: SpacialCircumstances Date: Thu, 25 Apr 2024 01:24:50 +0200 Subject: [PATCH 2/2] Do not display games to spectators when they are a player in that game --- server/services/gameList.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/server/services/gameList.ts b/server/services/gameList.ts index fbf03887b..9cf1caa04 100644 --- a/server/services/gameList.ts +++ b/server/services/gameList.ts @@ -178,15 +178,23 @@ export default class GameListService { } async listSpectating(userId: DBObjectId) { - // TODO: Verify user is not a player themselves - return await this.gameRepo.find({ 'state.endDate': { $eq: null }, // Game is in progress - 'galaxy.players.spectators': { // User is spectating at least one player. - $elemMatch: { - $in: [userId] + $and: [{ + 'galaxy.players.spectators': { // User is spectating at least one player. + $elemMatch: { + $in: [userId] + } + } + }, + { + 'galaxy.players': { + $not: { + $elemMatch: { userId } + } + } } - } + ] }, { 'settings.general.type': 1,