1
+ import { logger } from '../../../../../../config' ;
1
2
import type { User } from '../../../../../../models' ;
2
3
import { findLivingPlayersByGame } from '../../../../../../models/ArenaPlayer' ;
3
4
import {
@@ -11,81 +12,118 @@ import {
11
12
findRingSystemZonesToDeactivate ,
12
13
ringDeactivationSystem ,
13
14
} from '../../../../../../models/ArenaZone' ;
15
+ import { gameResponseToSlackHandler } from '../../../../../../modules/slack/utils' ;
14
16
import { ONE , THREE , ZERO } from '../../../../../consts/global' ;
15
17
import { adminAction , getGameError , getGameResponse } from '../../../../../utils' ;
16
- import { processWinner , publishArenaMessage , withArenaTransaction } from '../../../../utils' ;
18
+ import {
19
+ processWinner ,
20
+ publishArenaMessage ,
21
+ withArenaTransaction ,
22
+ arenaNotifyEphemeral ,
23
+ } from '../../../../utils' ;
17
24
import { ArenaEngine } from '../../engine' ;
18
25
import { arenaCommandReply } from '../../replies' ;
19
26
20
27
const arenaGameEngine = ArenaEngine . getInstance ( ) ;
21
28
22
29
export async function startRoundCommand ( userRequesting : User ) {
23
- return withArenaTransaction ( async ( transaction ) => {
24
- const isAdmin = adminAction ( userRequesting ) ;
25
- if ( ! isAdmin ) {
26
- return getGameError ( arenaCommandReply . adminsOnly ( ) ) ;
27
- }
28
- const round = await findActiveRound ( true , transaction ) ;
29
- if ( ! round ) {
30
- return getGameError ( arenaCommandReply . noActiveRound ( ) ) ;
31
- }
32
- const isEveryoneVisible = round . isEveryoneVisible ;
33
- // Lock the round so no one can send more commands
34
- arenaGameEngine . setRoundState ( true ) ;
35
- await publishArenaMessage ( arenaCommandReply . channelEndingTheRound ( ) , true ) ;
30
+ void withArenaTransaction ( async ( transaction ) => {
31
+ try {
32
+ const isAdmin = adminAction ( userRequesting ) ;
33
+ if ( ! isAdmin ) {
34
+ return getGameError ( arenaCommandReply . adminsOnly ( ) ) ;
35
+ }
36
+ const round = await findActiveRound ( true , transaction ) ;
37
+ if ( ! round ) {
38
+ return getGameError ( arenaCommandReply . noActiveRound ( ) ) ;
39
+ }
40
+ const isEveryoneVisible = round . isEveryoneVisible ;
41
+ // Lock the round so no one can send more commands
42
+ arenaGameEngine . setRoundState ( true ) ;
43
+ await publishArenaMessage ( arenaCommandReply . channelEndingTheRound ( ) , true ) ;
36
44
37
- // 0. Set IDLE players to "Stay on location" action
38
- await arenaGameEngine . assignZoneActionToIdlePlayers ( round , transaction ) ;
39
- await round . customReload ( true , transaction ) ;
40
- // 1. Actions (Per Zone)
41
- const zonesWithPlayers = await findArenaZonesWithPlayers ( transaction ) ;
42
- for ( const zone of zonesWithPlayers ) {
43
- await arenaGameEngine . runRound ( zone , round , transaction ) ;
44
- }
45
- // 2. Movement: Players Location
46
- await arenaGameEngine . processChangeLocation ( round . _actions ?? [ ] , transaction ) ;
45
+ // 0. Set IDLE players to "Stay on location" action
46
+ await arenaGameEngine . assignZoneActionToIdlePlayers ( round , transaction ) ;
47
+ await round . customReload ( true , transaction ) ;
48
+ // 1. Actions (Per Zone)
49
+ const zonesWithPlayers = await findArenaZonesWithPlayers ( transaction ) ;
50
+ for ( const zone of zonesWithPlayers ) {
51
+ await arenaGameEngine . runRound ( zone , round , transaction ) ;
52
+ }
53
+ // 2. Movement: Players Location
54
+ await arenaGameEngine . processChangeLocation ( round . _actions ?? [ ] , transaction ) ;
47
55
48
- const playersAlive = await findLivingPlayersByGame (
49
- round . _arenaGame ?. _gameId ! ,
50
- false ,
51
- transaction
52
- ) ;
53
- const amountOfPlayersAlive = playersAlive . length ;
56
+ const playersAlive = await findLivingPlayersByGame (
57
+ round . _arenaGame ?. _gameId ! ,
58
+ false ,
59
+ transaction
60
+ ) ;
61
+ const amountOfPlayersAlive = playersAlive . length ;
54
62
55
- await publishArenaMessage ( arenaCommandReply . channelTotalPlayersAlive ( amountOfPlayersAlive ) ) ;
63
+ await publishArenaMessage ( arenaCommandReply . channelTotalPlayersAlive ( amountOfPlayersAlive ) ) ;
56
64
57
- // Search for a winner
58
- await processWinner ( round , transaction ) ;
59
- await startRound ( round . _gameId , userRequesting . id , isEveryoneVisible , transaction ) ;
60
- const roundsCompleted = await countRoundsCompleted ( transaction ) ;
65
+ // Search for a winner
66
+ await processWinner ( round , transaction ) ;
67
+ await startRound ( round . _gameId , userRequesting . id , isEveryoneVisible , transaction ) ;
68
+ const roundsCompleted = await countRoundsCompleted ( transaction ) ;
61
69
62
- const activeZonesForCurrentRound = await findActiveArenaZones ( transaction ) ;
70
+ const activeZonesForCurrentRound = await findActiveArenaZones ( transaction ) ;
63
71
64
- if ( round . _arenaGame ?. hasZoneDeactivation && activeZonesForCurrentRound . length > ONE ) {
65
- if ( ( roundsCompleted + ONE ) % THREE === ZERO ) {
66
- const zonesToDeactivate = await findRingSystemZonesToDeactivate (
67
- {
68
- ringSystemAlgorithm : round . _arenaGame ?. ringSystemAlgorithm ,
69
- currentRingDeactivation : round . _arenaGame ?. currentRingDeactivation ,
70
- } ,
71
- transaction
72
- ) ;
73
- await publishArenaMessage ( arenaCommandReply . channelZonesToDeactivate ( zonesToDeactivate ) ) ;
72
+ if ( round . _arenaGame ?. hasZoneDeactivation && activeZonesForCurrentRound . length > ONE ) {
73
+ if ( ( roundsCompleted + ONE ) % THREE === ZERO ) {
74
+ const zonesToDeactivate = await findRingSystemZonesToDeactivate (
75
+ {
76
+ ringSystemAlgorithm : round . _arenaGame ?. ringSystemAlgorithm ,
77
+ currentRingDeactivation : round . _arenaGame ?. currentRingDeactivation ,
78
+ } ,
79
+ transaction
80
+ ) ;
81
+ await publishArenaMessage ( arenaCommandReply . channelZonesToDeactivate ( zonesToDeactivate ) ) ;
82
+ }
83
+
84
+ if ( roundsCompleted % THREE === ZERO ) {
85
+ await publishArenaMessage ( arenaCommandReply . channelRunningRingSystem ( ) ) ;
86
+ await ringDeactivationSystem ( round . _arenaGame , transaction ) ;
87
+ }
74
88
}
75
89
76
- if ( roundsCompleted % THREE === ZERO ) {
77
- await publishArenaMessage ( arenaCommandReply . channelRunningRingSystem ( ) ) ;
78
- await ringDeactivationSystem ( round . _arenaGame , transaction ) ;
90
+ const activeZonesForNextRound = await findActiveArenaZones ( transaction ) ;
91
+ await publishArenaMessage ( arenaCommandReply . channelActiveZones ( activeZonesForNextRound ) ) ;
92
+ if ( activeZonesForNextRound . length > ONE ) {
93
+ await publishArenaMessage ( arenaCommandReply . playersMoving ( ) ) ;
79
94
}
80
- }
95
+ // unlock the round so everyone can send more commands
96
+ arenaGameEngine . setRoundState ( false ) ;
97
+
98
+ const reply = getGameResponse ( arenaCommandReply . adminFinishedRound ( ) ) ;
99
+
100
+ const slackResponseBody = gameResponseToSlackHandler ( reply ) ;
101
+ arenaNotifyEphemeral (
102
+ slackResponseBody . text ?? 'Something went wrong' ,
103
+ userRequesting . slackId ! ,
104
+ userRequesting . slackId !
105
+ ) . catch ( ( e ) => {
106
+ onError ( e , userRequesting ) ;
107
+ } ) ;
81
108
82
- const activeZonesForNextRound = await findActiveArenaZones ( transaction ) ;
83
- await publishArenaMessage ( arenaCommandReply . channelActiveZones ( activeZonesForNextRound ) ) ;
84
- if ( activeZonesForNextRound . length > ONE ) {
85
- await publishArenaMessage ( arenaCommandReply . playersMoving ( ) ) ;
109
+ return reply ;
110
+ } catch ( e ) {
111
+ onError ( e , userRequesting ) ;
112
+ return getGameError ( 'Something went wrong' ) ;
86
113
}
87
- // unlock the round so everyone can send more commands
88
- arenaGameEngine . setRoundState ( false ) ;
89
- return getGameResponse ( arenaCommandReply . adminFinishedRound ( ) ) ;
90
114
} ) ;
115
+
116
+ return getGameResponse ( arenaCommandReply . adminStartedNewRound ( ) ) ;
91
117
}
118
+
119
+ const onError = ( error : any , userRequesting : User ) => {
120
+ // return
121
+ logger . error ( 'onError:' , error ) ;
122
+ arenaNotifyEphemeral (
123
+ 'Something went wrong' ,
124
+ userRequesting . slackId ! ,
125
+ userRequesting . slackId !
126
+ ) . catch ( ( e ) => {
127
+ logger . error ( 'Something went wrong' , e ) ;
128
+ } ) ;
129
+ } ;
0 commit comments