Skip to content

Commit bcfd2d2

Browse files
committed
fix: Define spectators via empires and not members
1 parent f3c86e5 commit bcfd2d2

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

src/empire/empire.controller.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ export class EmpireController {
7171
@Param('empire', ObjectIdPipe) id: Types.ObjectId,
7272
): Promise<Empire | ReadEmpireDto | null> {
7373
const empire = await this.empireService.find(id) ?? notFound(id);
74-
return currentUser._id.equals(empire.user) || await this.memberService.isSpectator(empire.game, currentUser._id)
75-
? empire
76-
: this.empireService.mask(empire);
74+
if (currentUser._id.equals(empire.user)) {
75+
return empire;
76+
}
77+
if (await this.empireService.isSpectator(currentUser._id, empire.game)) {
78+
return empire;
79+
}
80+
return this.empireService.mask(empire);
7781
}
7882

7983
@Patch(':empire')

src/empire/empire.service.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {BadRequestException, Injectable} from '@nestjs/common';
22
import {InjectModel} from '@nestjs/mongoose';
3-
import {Document, Model} from 'mongoose';
3+
import {Document, Model, Types} from 'mongoose';
44
import {EventRepository, EventService, MongooseRepository} from '@mean-stream/nestx';
55
import {Empire, EmpireDocument} from './empire.schema';
66
import {EmpireTemplate, ReadEmpireDto, UpdateEmpireDto} from './empire.dto';
@@ -41,6 +41,10 @@ export class EmpireService extends MongooseRepository<Empire> {
4141
return rest;
4242
}
4343

44+
async isSpectator(user: Types.ObjectId, game: Types.ObjectId): Promise<boolean> {
45+
return !(await this.exists({game, user}));
46+
}
47+
4448
updateEmpire(empire: EmpireDocument, dto: UpdateEmpireDto, free: boolean) {
4549
const {resources, ...rest} = dto;
4650
empire.set(rest);

src/game-logic/game-logic.controller.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import {Auth, AuthUser} from '../auth/auth.decorator';
66
import {User} from '../user/user.schema';
77
import {Types} from 'mongoose';
88
import {EmpireService} from '../empire/empire.service';
9-
import {SystemService} from '../system/system.service';
109
import {Validated} from '../util/validated.decorator';
1110
import {Throttled} from '../util/throttled.decorator';
1211
import {ExplainedVariable, Variable} from './types';
1312
import {explainVariable, getEmpireEffectSources} from './variables';
1413
import {AggregateService} from './aggregate.service';
1514
import {EmpireDocument} from '../empire/empire.schema';
16-
import {MemberService} from '../member/member.service';
1715

1816
@Controller('games/:game/empires/:empire')
1917
@ApiTags('Game Logic')
@@ -22,9 +20,7 @@ import {MemberService} from '../member/member.service';
2220
@Throttled()
2321
export class GameLogicController {
2422
constructor(
25-
private readonly memberService: MemberService,
2623
private readonly empireService: EmpireService,
27-
private readonly systemService: SystemService,
2824
private readonly aggregateService: AggregateService,
2925
) {
3026
}
@@ -111,7 +107,7 @@ ${Object.entries(aggregate.optionalParams ?? {}).map(([param, desc]) => `- \`${p
111107
if (currentUser._id.equals(empire.user)) {
112108
return true;
113109
}
114-
if (await this.memberService.isSpectator(empire.game, currentUser._id)) {
110+
if (await this.empireService.isSpectator(currentUser._id, empire.game)) {
115111
// user is a spectator
116112
return true;
117113
}

src/job/job.controller.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {JobType} from './job-type.enum';
3333
import {EmpireDocument} from '../empire/empire.schema';
3434
import {SystemService} from '../system/system.service';
3535
import {JobLogicService} from './job-logic.service';
36-
import {MemberService} from '../member/member.service';
3736
import {MONGO_ID_FORMAT} from '../util/schema';
3837

3938
@Controller('games/:game/empires/:empire/jobs')
@@ -45,7 +44,6 @@ export class JobController {
4544
private readonly jobService: JobService,
4645
private readonly jobLogicService: JobLogicService,
4746
private readonly empireService: EmpireService,
48-
private readonly memberService: MemberService,
4947
private readonly systemService: SystemService,
5048
) {
5149
}
@@ -185,7 +183,7 @@ export class JobController {
185183
if (requestedEmpire.user.equals(user._id)) {
186184
return;
187185
}
188-
if (await this.memberService.isSpectator(requestedEmpire.game, user._id)) {
186+
if (await this.empireService.isSpectator(user._id, requestedEmpire.game)) {
189187
return;
190188
}
191189
throw new ForbiddenException('You can only read jobs for your own empire.');

src/member/member.service.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Injectable} from '@nestjs/common';
22
import {InjectModel} from '@nestjs/mongoose';
33
import * as bcrypt from 'bcrypt';
4-
import {Model, Types} from 'mongoose';
4+
import {Model} from 'mongoose';
55
import {EventRepository, EventService, MongooseRepository} from '@mean-stream/nestx';
66

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

25-
async isSpectator(game: Types.ObjectId, user: Types.ObjectId): Promise<boolean> {
26-
const member = await this.findOne({game, user});
27-
return !!member && !member.empire;
28-
}
29-
3025
private emit(event: string, member: Member): void {
3126
this.eventEmitter.emit(`games.${member.game}.members.${member.user}.${event}`, member);
3227
}

0 commit comments

Comments
 (0)