Skip to content

Commit

Permalink
Merge pull request #59 from After-the-End-of-All-Things/issue-50
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Jun 19, 2023
2 parents ffcb7b6 + 12123bb commit 4bf6144
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 @@ -150,21 +150,38 @@ 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 },
},
]);

Expand All @@ -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,
);
Expand Down

0 comments on commit 4bf6144

Please sign in to comment.