-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto-close games with too many substitute requests
- Loading branch information
1 parent
2f85d55
commit 55d8a41
Showing
6 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import fp from 'fastify-plugin' | ||
import { events } from '../../events' | ||
import { safe } from '../../utils/safe' | ||
import { GameState, type GameModel } from '../../database/models/game.model' | ||
import { configuration } from '../../configuration' | ||
import { SlotStatus } from '../../database/models/game-slot.model' | ||
import { debounce } from 'lodash-es' | ||
import { logger } from '../../logger' | ||
import { forceEnd } from '../force-end' | ||
import { GameEndedReason } from '../../database/models/game-event.model' | ||
|
||
export default fp( | ||
async () => { | ||
const maybeCloseGame = debounce(async (game: GameModel) => { | ||
if ( | ||
![ | ||
GameState.created, | ||
GameState.configuring, | ||
GameState.launching, | ||
GameState.started, | ||
].includes(game.state) | ||
) { | ||
return | ||
} | ||
|
||
const threshold = await configuration.get('games.auto_force_end_threshold') | ||
if (threshold <= 0) { | ||
return | ||
} | ||
|
||
const subRequests = game.slots.filter( | ||
slot => slot.status === SlotStatus.waitingForSubstitute, | ||
).length | ||
if (subRequests >= threshold) { | ||
logger.info( | ||
{ game, subRequestCount: subRequests, threshold }, | ||
`game #${game.number} has too many substitute requests; the game will be force-ended`, | ||
) | ||
await forceEnd(game.number, 'bot', GameEndedReason.tooManySubstituteRequests) | ||
} | ||
}, 100) | ||
|
||
events.on( | ||
'game:substituteRequested', | ||
safe(async ({ game }) => { | ||
await maybeCloseGame(game) | ||
}), | ||
) | ||
}, | ||
{ | ||
name: 'auto close games with too many subsitute requests', | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { expect, launchGame as test } from '../fixtures/launch-game' | ||
|
||
test('auto-closes games with too many substitute requests', async ({ gameNumber, users }) => { | ||
const page = await users.getAdmin().gamePage(gameNumber) | ||
await page.requestSubstitute('Mayflower') | ||
await page.requestSubstitute('Polemic') | ||
await page.requestSubstitute('Shadowhunter') | ||
await page.requestSubstitute('MoonMan') | ||
|
||
await expect.poll(() => page.isLive()).toBe(false) | ||
await expect(page.gameEvent('Game interrupted (too many substitute requests)')).toBeVisible() | ||
}) |