diff --git a/server/src/modules/player/player.service.ts b/server/src/modules/player/player.service.ts index d2d9caf..7c29383 100644 --- a/server/src/modules/player/player.service.ts +++ b/server/src/modules/player/player.service.ts @@ -150,21 +150,38 @@ export class PlayerService { } } - async getRandomUserAtLocation( + async getRandomOnlinePlayerAtLocation( excludeUserId: string, locationName: string, ): Promise { - const found = await this.players.aggregate([ + const currentTime = Date.now(); + + const found = await this.em.aggregate(Player, [ + { + $lookup: { + from: 'user', + let: { userId: '$userId' }, + pipeline: [ + { + $match: { + $expr: { + $eq: [{ $toObjectId: '$$userId' }, '$_id'], + }, + }, + }, + ], + as: 'user', + }, + }, { $match: { - userId: { $ne: excludeUserId }, 'location.current': locationName, + 'user.onlineUntil': { $gt: currentTime }, + userId: { $ne: excludeUserId }, }, }, { - $sample: { - size: 1, - }, + $sample: { size: 1 }, }, ]); @@ -177,7 +194,7 @@ export class PlayerService { if (waveRoll > waveChance) return; - const randomPlayer = await this.getRandomUserAtLocation( + const randomPlayer = await this.getRandomOnlinePlayerAtLocation( player.userId, player.location.current, );