Skip to content

Commit

Permalink
fix: Define spectators via empires and not members
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Aug 8, 2024
1 parent f3c86e5 commit bcfd2d2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
10 changes: 7 additions & 3 deletions src/empire/empire.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ export class EmpireController {
@Param('empire', ObjectIdPipe) id: Types.ObjectId,
): Promise<Empire | ReadEmpireDto | null> {
const empire = await this.empireService.find(id) ?? notFound(id);
return currentUser._id.equals(empire.user) || await this.memberService.isSpectator(empire.game, currentUser._id)
? empire
: this.empireService.mask(empire);
if (currentUser._id.equals(empire.user)) {
return empire;
}
if (await this.empireService.isSpectator(currentUser._id, empire.game)) {
return empire;
}
return this.empireService.mask(empire);
}

@Patch(':empire')
Expand Down
6 changes: 5 additions & 1 deletion src/empire/empire.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {BadRequestException, Injectable} from '@nestjs/common';
import {InjectModel} from '@nestjs/mongoose';
import {Document, Model} from 'mongoose';
import {Document, Model, Types} from 'mongoose';
import {EventRepository, EventService, MongooseRepository} from '@mean-stream/nestx';
import {Empire, EmpireDocument} from './empire.schema';
import {EmpireTemplate, ReadEmpireDto, UpdateEmpireDto} from './empire.dto';
Expand Down Expand Up @@ -41,6 +41,10 @@ export class EmpireService extends MongooseRepository<Empire> {
return rest;
}

async isSpectator(user: Types.ObjectId, game: Types.ObjectId): Promise<boolean> {
return !(await this.exists({game, user}));
}

updateEmpire(empire: EmpireDocument, dto: UpdateEmpireDto, free: boolean) {
const {resources, ...rest} = dto;
empire.set(rest);
Expand Down
6 changes: 1 addition & 5 deletions src/game-logic/game-logic.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import {Auth, AuthUser} from '../auth/auth.decorator';
import {User} from '../user/user.schema';
import {Types} from 'mongoose';
import {EmpireService} from '../empire/empire.service';
import {SystemService} from '../system/system.service';
import {Validated} from '../util/validated.decorator';
import {Throttled} from '../util/throttled.decorator';
import {ExplainedVariable, Variable} from './types';
import {explainVariable, getEmpireEffectSources} from './variables';
import {AggregateService} from './aggregate.service';
import {EmpireDocument} from '../empire/empire.schema';
import {MemberService} from '../member/member.service';

@Controller('games/:game/empires/:empire')
@ApiTags('Game Logic')
Expand All @@ -22,9 +20,7 @@ import {MemberService} from '../member/member.service';
@Throttled()
export class GameLogicController {
constructor(
private readonly memberService: MemberService,
private readonly empireService: EmpireService,
private readonly systemService: SystemService,
private readonly aggregateService: AggregateService,
) {
}
Expand Down Expand Up @@ -111,7 +107,7 @@ ${Object.entries(aggregate.optionalParams ?? {}).map(([param, desc]) => `- \`${p
if (currentUser._id.equals(empire.user)) {
return true;
}
if (await this.memberService.isSpectator(empire.game, currentUser._id)) {
if (await this.empireService.isSpectator(currentUser._id, empire.game)) {
// user is a spectator
return true;
}
Expand Down
4 changes: 1 addition & 3 deletions src/job/job.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {JobType} from './job-type.enum';
import {EmpireDocument} from '../empire/empire.schema';
import {SystemService} from '../system/system.service';
import {JobLogicService} from './job-logic.service';
import {MemberService} from '../member/member.service';
import {MONGO_ID_FORMAT} from '../util/schema';

@Controller('games/:game/empires/:empire/jobs')
Expand All @@ -45,7 +44,6 @@ export class JobController {
private readonly jobService: JobService,
private readonly jobLogicService: JobLogicService,
private readonly empireService: EmpireService,
private readonly memberService: MemberService,
private readonly systemService: SystemService,
) {
}
Expand Down Expand Up @@ -185,7 +183,7 @@ export class JobController {
if (requestedEmpire.user.equals(user._id)) {
return;
}
if (await this.memberService.isSpectator(requestedEmpire.game, user._id)) {
if (await this.empireService.isSpectator(user._id, requestedEmpire.game)) {
return;
}
throw new ForbiddenException('You can only read jobs for your own empire.');
Expand Down
7 changes: 1 addition & 6 deletions src/member/member.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Injectable} from '@nestjs/common';
import {InjectModel} from '@nestjs/mongoose';
import * as bcrypt from 'bcrypt';
import {Model, Types} from 'mongoose';
import {Model} from 'mongoose';
import {EventRepository, EventService, MongooseRepository} from '@mean-stream/nestx';

import {Game} from '../game/game.schema';
Expand All @@ -22,11 +22,6 @@ export class MemberService extends MongooseRepository<Member, never, MemberDocum
return bcrypt.compare(member.password, game.passwordHash);
}

async isSpectator(game: Types.ObjectId, user: Types.ObjectId): Promise<boolean> {
const member = await this.findOne({game, user});
return !!member && !member.empire;
}

private emit(event: string, member: Member): void {
this.eventEmitter.emit(`games.${member.game}.members.${member.user}.${event}`, member);
}
Expand Down

0 comments on commit bcfd2d2

Please sign in to comment.