Skip to content

Commit

Permalink
- Changes way HLTB updates are triggered
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamarcke committed Oct 26, 2024
1 parent 58c7bb8 commit e3f67fa
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion server_swagger.json

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions src/game/game-repository/game-repository-create.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class GameRepositoryCreateService {
* @param gameEngineRepository
* @param gameEngineLogoRepository
* @param statisticsQueueService
* @param hltbSyncUpdateService
*/
constructor(
@InjectRepository(Game)
Expand Down Expand Up @@ -101,7 +100,6 @@ export class GameRepositoryCreateService {
@InjectRepository(GameEngineLogo)
private readonly gameEngineLogoRepository: Repository<GameEngineLogo>,
private readonly statisticsQueueService: StatisticsQueueService,
private readonly hltbSyncUpdateService: HltbSyncUpdateService,
) {}

/**
Expand Down Expand Up @@ -137,10 +135,6 @@ export class GameRepositoryCreateService {
sourceId: game.id,
sourceType: StatisticsSourceType.GAME,
});
this.hltbSyncUpdateService.registerUpdateRequest({
gameId: game.id,
name: game.name!,
});
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/game/game-repository/game-repository.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import { HltbSyncModule } from "../../sync/hltb/hltb-sync.module";
GamePlayerPerspective,
]),
forwardRef(() => StatisticsQueueModule),
HltbSyncModule,
],
providers: [GameRepositoryService, GameRepositoryCreateService],
exports: [GameRepositoryService, GameRepositoryCreateService],
Expand Down
2 changes: 1 addition & 1 deletion src/playtime/playtime.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export class PlaytimeController {

@Get(":gameId")
async findOneByGameId(@Param("gameId") gameId: number) {
return this.playtimeService.findOneByGameIdOrFail(gameId);
return this.playtimeService.findOneByGameIdAndRequestUpdate(gameId);
}
}
10 changes: 8 additions & 2 deletions src/playtime/playtime.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Module } from "@nestjs/common";
import { forwardRef, Module } from "@nestjs/common";
import { PlaytimeService } from "./playtime.service";
import { PlaytimeController } from "./playtime.controller";
import { TypeOrmModule } from "@nestjs/typeorm";
import { GamePlaytime } from "./entity/game-playtime.entity";
import { GameRepositoryModule } from "../game/game-repository/game-repository.module";
import { HltbSyncModule } from "../sync/hltb/hltb-sync.module";

@Module({
imports: [TypeOrmModule.forFeature([GamePlaytime])],
imports: [
TypeOrmModule.forFeature([GamePlaytime]),
GameRepositoryModule,
forwardRef(() => HltbSyncModule),
],
providers: [PlaytimeService],
controllers: [PlaytimeController],
exports: [PlaytimeService],
Expand Down
46 changes: 45 additions & 1 deletion src/playtime/playtime.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { HttpException, HttpStatus, Injectable, Logger } from "@nestjs/common";
import {
forwardRef,
HttpException,
HttpStatus,
Inject,
Injectable,
Logger,
} from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { GamePlaytime } from "./entity/game-playtime.entity";
import { DeepPartial, In, Repository } from "typeorm";
import { toMap } from "../utils/toMap";
import { HltbSyncUpdateService } from "../sync/hltb/hltb-sync-update.service";
import { GameRepositoryService } from "../game/game-repository/game-repository.service";

@Injectable()
export class PlaytimeService {
Expand All @@ -11,6 +20,9 @@ export class PlaytimeService {
constructor(
@InjectRepository(GamePlaytime)
private gamePlaytimeRepository: Repository<GamePlaytime>,
@Inject(forwardRef(() => HltbSyncUpdateService))
private readonly hltbSyncUpdateService: HltbSyncUpdateService,
private readonly gameRepositoryService: GameRepositoryService,
) {}

public async findOneByGameId(gameId: number) {
Expand All @@ -31,6 +43,38 @@ export class PlaytimeService {
return entity;
}

/**
* Triggers a HLTB sync update if no game playtime is found for a given game id.
* @param gameId
*/
public async findOneByGameIdAndRequestUpdate(gameId: number) {
try {
return await this.findOneByGameIdOrFail(gameId);
} catch (err: unknown) {
this.registerUpdateRequest(gameId);
throw err;
}
}

private registerUpdateRequest(gameId: number) {
this.gameRepositoryService
.findOneById(gameId)
.then((game) => {
if (game) {
this.hltbSyncUpdateService.registerUpdateRequest({
gameId: gameId,
name: game.name,
});
}
})
.catch((err) => {
this.logger.error(
`Failed to register update request for missing playtime for gameId: ${gameId}`,
);
this.logger.error(err);
});
}

public async findAllByGameIds(gameIds: number[]) {
return this.gamePlaytimeRepository.find({
where: {
Expand Down
6 changes: 3 additions & 3 deletions src/sync/hltb/hltb-sync-update.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Logger } from "@nestjs/common";
import { forwardRef, Inject, Injectable, Logger } from "@nestjs/common";
import {
HLTBResponseItem,
HLTBUpdateRequest,
Expand Down Expand Up @@ -29,6 +29,7 @@ export class HltbSyncUpdateService {

constructor(
private readonly amqpConnection: AmqpConnection,
@Inject(forwardRef(() => PlaytimeService))
private readonly playtimeService: PlaytimeService,
) {}

Expand Down Expand Up @@ -68,9 +69,8 @@ export class HltbSyncUpdateService {
routingKey: "sync.hltb.update.response",
queue: "sync.hltb.update.response",
name: "sync.hltb.update.response",
allowNonJsonMessages: true,
})
async receiveUpdateResponse(msg: HLTBUpdateResponse) {
async subscribe(msg: HLTBUpdateResponse) {
this.logger.log(`Received update response for gameId: ${msg.gameId}`);
const parsedResponse = parseResponse(msg.gameId, msg.match);
await this.playtimeService.save(parsedResponse);
Expand Down
4 changes: 2 additions & 2 deletions src/sync/hltb/hltb-sync.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Module } from "@nestjs/common";
import { forwardRef, Module } from "@nestjs/common";
import { HltbSyncUpdateService } from "./hltb-sync-update.service";
import { PlaytimeModule } from "../../playtime/playtime.module";

@Module({
imports: [PlaytimeModule],
imports: [forwardRef(() => PlaytimeModule)],
providers: [HltbSyncUpdateService],
exports: [HltbSyncUpdateService],
})
Expand Down

0 comments on commit e3f67fa

Please sign in to comment.