diff --git a/app/api/src/common/util/formatStats.ts b/app/api/src/common/util/formatStats.ts index 55e0ae4..ce83441 100644 --- a/app/api/src/common/util/formatStats.ts +++ b/app/api/src/common/util/formatStats.ts @@ -4,6 +4,7 @@ function formatPlayerStats(statObj: any) { return { ...statObj.stats, + teamScoreGame: Number(statObj.stats.teamScoreGame), masTG: Number(statObj.stats.masTG), cgMATG: Number(statObj.stats.cgMATG), kdrAvg: Number(statObj.stats.kdrAvg), diff --git a/app/api/src/game/game.service.ts b/app/api/src/game/game.service.ts index b8418de..672fb1e 100644 --- a/app/api/src/game/game.service.ts +++ b/app/api/src/game/game.service.ts @@ -10,150 +10,179 @@ import formatPlayerStats from '../common/util/formatStats'; @Injectable() export class GameService { - constructor( - private readonly connection: Connection, - private readonly configService: ConfigService, - @InjectRepository(Games) private readonly gamesRepository: Repository, - @InjectRepository(GameDetail) private readonly gameRepository: Repository - ) {} + constructor( + private readonly connection: Connection, + private readonly configService: ConfigService, + @InjectRepository(Games) + private readonly gamesRepository: Repository, + @InjectRepository(GameDetail) + private readonly gameRepository: Repository, + ) {} - async findOne(gameId: string) { - const query = await this.gameRepository.find({ - relations: [ 'game', 'playerGuid' ], - where: [ { game: { gameId: gameId } } ] - }); + async findOne(gameId: string) { + const query = await this.gameRepository.find({ + relations: ['game', 'playerGuid'], + where: [{ game: { gameId: gameId } }], + }); - if (!query.length) { - throw new NotFoundException(`Game ID: ${gameId} not found`); - } + if (!query.length) { + throw new NotFoundException(`Game ID: ${gameId} not found`); + } - const game: any = { - ...query[0].game - }; + const game: any = { + ...query[0].game, + }; - // Need to set return based off gameType - // Modify game object if not a CTF type game and return early - if (query[0].gametype !== 'CTFGame' && query[0].gametype !== 'SCtFGame') { - game['players'] = []; - for (const player of query) { - const { playerName } = player; - const stats = formatPlayerStats(player); + // Need to set return based off gameType + // Modify game object if not a CTF type game and return early + if (query[0].gametype !== 'CTFGame' && query[0].gametype !== 'SCtFGame') { + game['players'] = []; + for (const player of query) { + const { playerName } = player; + const stats = formatPlayerStats(player); - const p = { - playerGuid: player.playerGuid.playerGuid, - playerName, - stats - }; + const p = { + playerGuid: player.playerGuid.playerGuid, + playerName, + stats, + }; - game.players.push(p); - } + game.players.push(p); + } - return game; - } + return game; + } - // Team Based game stats (CTF/SCtF) - game['teams'] = { - obs: { score: 0, players: [] }, - storm: { score: 0, players: [] }, - inferno: { score: 0, players: [] } - }; + // Team Based game stats (CTF/SCtF) + game['teams'] = { + obs: { score: 0, players: [] }, + storm: { score: 0, players: [] }, + inferno: { score: 0, players: [] }, + }; - const teamZero = [], - teamOne = [], - teamTwo = []; + const teamZero = [], + teamOne = [], + teamTwo = []; - for (const player of query) { - const { playerName } = player; - const stats = formatPlayerStats(player); + for (const player of query) { + const { playerName } = player; + const stats = formatPlayerStats(player); - const p = { - playerGuid: player.playerGuid.playerGuid, - playerName, - stats - }; + const p = { + playerGuid: player.playerGuid.playerGuid, + playerName, + stats, + }; - const flagGrabsTG = parseInt(player.stats.flagGrabsTG[0]); - const flagCapsTG = parseInt(player.stats.flagCapsTG[0]) * 100; - const totalFlagScore = flagGrabsTG + flagCapsTG; + if (isNaN(player.stats.teamScoreGame)) { + // legacy calculations for game totals (not using the new teamScoreGame attribute) + const flagGrabsTG = parseInt(player.stats.flagGrabsTG[0]); + const flagCapsTG = parseInt(player.stats.flagCapsTG[0]) * 100; + const totalFlagScore = flagGrabsTG + flagCapsTG; - if (player.stats.dtTeamGame[0] === '1') { - // Storm - game.teams.storm.score += totalFlagScore; - teamOne.push(p); - } else if (player.stats.dtTeamGame[0] === '2') { - // Inferno - game.teams.inferno.score += totalFlagScore; - teamTwo.push(p); - } else { - // OBS - game.teams.obs.score += totalFlagScore; - teamZero.push(p); - } - } + if (player.stats.dtTeamGame[0] === '1') { + // Storm + game.teams.storm.score += totalFlagScore; + teamOne.push(p); + } else if (player.stats.dtTeamGame[0] === '2') { + // Inferno + game.teams.inferno.score += totalFlagScore; + teamTwo.push(p); + } else { + // OBS + game.teams.obs.score += totalFlagScore; + teamZero.push(p); + } + } else { + // Use new player.stats.teamScoreGame key + if (player.stats.dtTeamGame[0] === '1') { + // Storm + game.teams.storm.score = Number(player.stats.teamScoreGame); + teamOne.push(p); + } else if (player.stats.dtTeamGame[0] === '2') { + // Inferno + game.teams.inferno.score = Number(player.stats.teamScoreGame); + teamTwo.push(p); + } else { + // OBS + game.teams.obs.score = Number(player.stats.teamScoreGame); + teamZero.push(p); + } + } + } - game['teams']['obs']['players'] = teamZero.sort((a, b) => b.stats.scoreTG - a.stats.scoreTG); - game['teams']['storm']['players'] = teamOne.sort((a, b) => b.stats.scoreTG - a.stats.scoreTG); - game['teams']['inferno']['players'] = teamTwo.sort((a, b) => b.stats.scoreTG - a.stats.scoreTG); + game['teams']['obs']['players'] = teamZero.sort( + (a, b) => b.stats.scoreTG - a.stats.scoreTG, + ); + game['teams']['storm']['players'] = teamOne.sort( + (a, b) => b.stats.scoreTG - a.stats.scoreTG, + ); + game['teams']['inferno']['players'] = teamTwo.sort( + (a, b) => b.stats.scoreTG - a.stats.scoreTG, + ); - return game; - } + return game; + } - async findOneAbvSummary(gameId: string) { - const query = await this.gameRepository.find({ - relations: [ 'game', 'playerGuid' ], - where: [ { game: { gameId: gameId } } ] - }); + async findOneAbvSummary(gameId: string) { + const query = await this.gameRepository.find({ + relations: ['game', 'playerGuid'], + where: [{ game: { gameId: gameId } }], + }); - if (!query.length) { - throw new NotFoundException(`Game ID: ${gameId} not found`); - } + if (!query.length) { + throw new NotFoundException(`Game ID: ${gameId} not found`); + } - const game: any = { - ...query[0].game - }; + const game: any = { + ...query[0].game, + }; - // Need to set return based off gameType - // Modify game object if not a CTF type game and return early - if (query[0].gametype !== 'CTFGame' && query[0].gametype !== 'SCtFGame') { - game['totalScore'] = 0; + // Need to set return based off gameType + // Modify game object if not a CTF type game and return early + if (query[0].gametype !== 'CTFGame' && query[0].gametype !== 'SCtFGame') { + game['totalScore'] = 0; - for (const player of query) { - const stats = formatPlayerStats(player); + for (const player of query) { + const stats = formatPlayerStats(player); - game.totalScore += stats.scoreTG; - } + game.totalScore += stats.scoreTG; + } - return game; - } + return game; + } - // Team Based game stats (CTF/SCtF) - game['teams'] = { - obs: { score: 0, playerCount: 0 }, - storm: { score: 0, playerCount: 0 }, - inferno: { score: 0, playerCount: 0 } - }; + // Team Based game stats (CTF/SCtF) + game['teams'] = { + obs: { score: 0, playerCount: 0 }, + storm: { score: 0, playerCount: 0 }, + inferno: { score: 0, playerCount: 0 }, + }; - for (const player of query) { - const flagGrabsTG = parseInt(player.stats.flagGrabsTG[0]); - const flagCapsTG = parseInt(player.stats.flagCapsTG[0]) * 100; - const totalFlagScore = flagGrabsTG + flagCapsTG; + console.log(query); - if (player.stats.dtTeamGame[0] === '1') { - // Storm - game.teams.storm.score += totalFlagScore; - game.teams.storm.playerCount += 1; - } else if (player.stats.dtTeamGame[0] === '2') { - // Inferno - game.teams.inferno.score += totalFlagScore; - game.teams.inferno.playerCount += 1; - } else { - // OBS - game.teams.obs.score += totalFlagScore; - game.teams.obs.playerCount += 1; - } - } - game['totalScore'] = game.teams.storm.score + game.teams.inferno.score + game.teams.obs.score; + for (const player of query) { + const flagGrabsTG = parseInt(player.stats.flagGrabsTG[0]); + const flagCapsTG = parseInt(player.stats.flagCapsTG[0]) * 100; + const totalFlagScore = flagGrabsTG + flagCapsTG; - return game; - } + if (player.stats.dtTeamGame[0] === '1') { + // Storm + game.teams.storm.score += totalFlagScore; + game.teams.storm.playerCount += 1; + } else if (player.stats.dtTeamGame[0] === '2') { + // Inferno + game.teams.inferno.score += totalFlagScore; + game.teams.inferno.playerCount += 1; + } else { + // OBS + game.teams.obs.score += totalFlagScore; + game.teams.obs.playerCount += 1; + } + } + game['totalScore'] = + game.teams.storm.score + game.teams.inferno.score + game.teams.obs.score; + + return game; + } } diff --git a/docker-compose.yml b/docker-compose.yml index 6860e45..fddd7c6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -44,7 +44,7 @@ services: replicas: 1 api: - image: "amineo/t2-stats-api:v0.0.13" + image: "amineo/t2-stats-api:v0.0.14" build: context: . dockerfile: ./build/api/Dockerfile