Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Jun 20, 2023
2 parents 99fa6fe + 4bf6144 commit 86f20cd
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions server/src/modules/player/player.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,46 @@ export class PlayerService {
}
}

async getRandomUserAtLocation(
async getRandomOnlinePlayerAtLocation(
excludeUserId: string,
locationName: string,
): Promise<Player> {
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 },
},
]);

return found[0];
}

async handleRandomWave(player: Player) {
const randomPlayer = await this.getRandomUserAtLocation(
const randomPlayer = await this.getRandomOnlinePlayerAtLocation(
player.userId,
player.location.current,
);
Expand Down

0 comments on commit 86f20cd

Please sign in to comment.