|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { TestEnvironment } from '@test/cqrs'; |
| 3 | +import { PostgreSqlContainer, StartedPostgreSqlContainer } from '@testcontainers/postgresql'; |
| 4 | +import FinishedMatchEntity from 'gameserver/model/finished-match.entity'; |
| 5 | +import { createFakeMatch, fillMatch } from '@test/create-fake-match'; |
| 6 | +import { MmrChangeLogEntity } from 'gameserver/model/mmr-change-log.entity'; |
| 7 | +import { NestApplication } from '@nestjs/core'; |
| 8 | +import { typeorm } from '@test/testcontainers'; |
| 9 | +import { VersionPlayerEntity } from 'gameserver/model/version-player.entity'; |
| 10 | +import { ProcessRankedMatchHandler } from 'gameserver/command/ProcessRankedMatch/process-ranked-match.handler'; |
| 11 | +import { GameServerService } from 'gameserver/gameserver.service'; |
| 12 | +import { GameSeasonEntity } from 'gameserver/model/game-season.entity'; |
| 13 | +import { Dota2Version } from 'gateway/shared-types/dota2version'; |
| 14 | +import { DotaTeam } from 'gateway/shared-types/dota-team'; |
| 15 | +import PlayerInMatchEntity from 'gameserver/model/player-in-match.entity'; |
| 16 | +import { MatchEntity } from 'gameserver/model/match.entity'; |
| 17 | +import { ProcessRankedMatchCommand } from 'gameserver/command/ProcessRankedMatch/process-ranked-match.command'; |
| 18 | +import { PlayerId } from 'gateway/shared-types/player-id'; |
| 19 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 20 | +import { Repository } from 'typeorm'; |
| 21 | +import { MatchmakingMode } from 'gateway/shared-types/matchmaking-mode'; |
| 22 | + |
| 23 | +describe("MatchController", () => { |
| 24 | + jest.setTimeout(60000); |
| 25 | + |
| 26 | + let container: StartedPostgreSqlContainer; |
| 27 | + let module: TestingModule; |
| 28 | + let app: NestApplication; |
| 29 | + |
| 30 | + let gsServiceMock = { |
| 31 | + getCurrentSeason() { |
| 32 | + const season = new GameSeasonEntity(); |
| 33 | + season.start_timestamp = new Date("2020-07-07 00:00:00.000000"); |
| 34 | + season.id = 1; |
| 35 | + season.version = Dota2Version.Dota_684; |
| 36 | + return Promise.resolve(season); |
| 37 | + }, |
| 38 | + getGamesPlayed: jest.fn(), |
| 39 | + }; |
| 40 | + |
| 41 | + |
| 42 | + const processRanked = async (fm: FinishedMatchEntity, pims: PlayerInMatchEntity[]) => { |
| 43 | + await app.get(ProcessRankedMatchHandler).execute( |
| 44 | + new ProcessRankedMatchCommand( |
| 45 | + fm.id, |
| 46 | + pims |
| 47 | + .filter((t) => t.team === DotaTeam.RADIANT) |
| 48 | + .map((it) => new PlayerId(it.playerId)), |
| 49 | + pims |
| 50 | + .filter((t) => t.team === DotaTeam.DIRE) |
| 51 | + .map((it) => new PlayerId(it.playerId)), |
| 52 | + fm.matchmaking_mode, |
| 53 | + ), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + beforeAll(async () => { |
| 58 | + container = await new PostgreSqlContainer() |
| 59 | + .withUsername("username") |
| 60 | + .withPassword("password") |
| 61 | + .start(); |
| 62 | + |
| 63 | + const Entities = [ |
| 64 | + VersionPlayerEntity, |
| 65 | + MatchEntity, |
| 66 | + FinishedMatchEntity, |
| 67 | + MmrChangeLogEntity, |
| 68 | + PlayerInMatchEntity, |
| 69 | + ]; |
| 70 | + |
| 71 | + module = await Test.createTestingModule({ |
| 72 | + imports: [...typeorm(container, Entities)], |
| 73 | + controllers: [], |
| 74 | + providers: [ |
| 75 | + ProcessRankedMatchHandler, |
| 76 | + ...TestEnvironment(), |
| 77 | + { |
| 78 | + provide: GameServerService, |
| 79 | + useValue: gsServiceMock, |
| 80 | + }, |
| 81 | + ], |
| 82 | + }).compile(); |
| 83 | + |
| 84 | + app = module.createNestApplication(); |
| 85 | + await app.init(); |
| 86 | + }); |
| 87 | + |
| 88 | + afterAll(async () => { |
| 89 | + await app.close(); |
| 90 | + await container.stop(); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should spin up", () => {}); |
| 94 | + |
| 95 | + it("should update mmr after ranked match", async () => { |
| 96 | + const fm = await createFakeMatch(module, DotaTeam.RADIANT); |
| 97 | + const pims = await fillMatch(module, fm, 10); |
| 98 | + |
| 99 | + gsServiceMock.getGamesPlayed = jest.fn( |
| 100 | + ( |
| 101 | + season: GameSeasonEntity, |
| 102 | + pid: PlayerId, |
| 103 | + modes: MatchmakingMode[] | undefined, |
| 104 | + beforeTimestamp: string, |
| 105 | + ) => { |
| 106 | + if (pims.findIndex((t) => t.playerId === pid.value) % 2 === 0) return 0; |
| 107 | + else return 100; |
| 108 | + }, |
| 109 | + ); |
| 110 | + |
| 111 | + await processRanked(fm, pims); |
| 112 | + |
| 113 | + const mmrRepo: Repository<MmrChangeLogEntity> = app.get( |
| 114 | + getRepositoryToken(MmrChangeLogEntity), |
| 115 | + ); |
| 116 | + const changes = await mmrRepo.find({ where: { matchId: fm.id } }); |
| 117 | + expect(changes).toHaveLength(10); |
| 118 | + |
| 119 | + changes.forEach(change => { |
| 120 | + expect(change.mmrBefore).toEqual(VersionPlayerEntity.STARTING_MMR) |
| 121 | + }) |
| 122 | + |
| 123 | + changes |
| 124 | + .filter((t) => t.winner) |
| 125 | + .forEach((winner) => { |
| 126 | + expect(winner.change).toBeGreaterThan(0); |
| 127 | + }); |
| 128 | + |
| 129 | + changes |
| 130 | + .filter((t) => !t.winner) |
| 131 | + .forEach((loser) => { |
| 132 | + expect(loser.change).toBeLessThan(0); |
| 133 | + }); |
| 134 | + |
| 135 | + |
| 136 | + const calib = changes.find(t => t.playerId === pims[0].playerId); |
| 137 | + expect(calib.change).toBeGreaterThan(90) |
| 138 | + |
| 139 | + const nonCalib = changes.find(t => t.playerId === pims[1].playerId); |
| 140 | + |
| 141 | + |
| 142 | + expect(nonCalib.change).toBeGreaterThan(10) |
| 143 | + expect(nonCalib.change).toBeLessThan(50) |
| 144 | + }); |
| 145 | + |
| 146 | + it('should always subtract mmr for leavers', async () => { |
| 147 | + const fm = await createFakeMatch(module, DotaTeam.RADIANT); |
| 148 | + const pims = await fillMatch(module, fm, 10); |
| 149 | + |
| 150 | + const pimRep = module.get(getRepositoryToken(PlayerInMatchEntity)); |
| 151 | + pims[3].abandoned = true |
| 152 | + pims[7].abandoned = true |
| 153 | + pimRep.save(pims) |
| 154 | + |
| 155 | + gsServiceMock.getGamesPlayed = jest.fn( |
| 156 | + ( |
| 157 | + season: GameSeasonEntity, |
| 158 | + pid: PlayerId, |
| 159 | + modes: MatchmakingMode[] | undefined, |
| 160 | + beforeTimestamp: string, |
| 161 | + ) => { |
| 162 | + return 100; |
| 163 | + }, |
| 164 | + ); |
| 165 | + |
| 166 | + await processRanked(fm, pims); |
| 167 | + |
| 168 | + |
| 169 | + const mmrRepo: Repository<MmrChangeLogEntity> = app.get( |
| 170 | + getRepositoryToken(MmrChangeLogEntity), |
| 171 | + ); |
| 172 | + const changes = await mmrRepo.find({ where: { matchId: fm.id } }); |
| 173 | + expect(changes).toHaveLength(10); |
| 174 | + |
| 175 | + |
| 176 | + expect(changes[0].change).toBeGreaterThan(0) |
| 177 | + expect(changes[1].change).toBeGreaterThan(0) |
| 178 | + expect(changes[2].change).toBeGreaterThan(0) |
| 179 | + expect(changes[3].change).toBeLessThan(0) |
| 180 | + expect(changes[4].change).toBeGreaterThan(0) |
| 181 | + |
| 182 | + expect(changes[5].change).toBeLessThan(0) |
| 183 | + expect(changes[6].change).toBeLessThan(0) |
| 184 | + expect(changes[7].change).toBeLessThan(0) |
| 185 | + expect(changes[8].change).toBeLessThan(0) |
| 186 | + expect(changes[9].change).toBeLessThan(0) |
| 187 | + |
| 188 | + |
| 189 | + }); |
| 190 | +}); |
0 commit comments