22// Use of this source code is governed by the MIT license, a copy of which can
33// be found in the LICENSE file.
44
5+ import { CommandBuilder } from 'components/command_manager/command_builder.js' ;
6+ import { FightGame } from 'features/fights/fight_game.js' ;
57import { FightLocationDescription } from 'features/fights/fight_location_description.js' ;
68import { FightLocation } from 'features/fights/fight_location.js' ;
9+ import { GameCommandParams } from 'features/games/game_command_params.js' ;
10+
11+ import { clone } from 'base/clone.js' ;
12+
13+ // Directory in which the fight commands aer located.
14+ const kCommandDirectory = 'data/fights/' ;
715
816// Directory in which the fight locations are located.
917const kLocationDirectory = 'data/fights/locations/' ;
1018
1119// Keeps track of the fight locations, commands and configuration available on the server. Is
1220// responsible for loading it, and doing the initial initialization.
1321export class FightRegistry {
22+ #commands_ = null ;
23+ #games_ = null ;
1424 #locations_ = null ;
1525
16- constructor ( ) {
26+ constructor ( games ) {
27+ this . #commands_ = new Set ( ) ;
28+ this . #games_ = games ;
1729 this . #locations_ = new Map ( ) ;
1830 }
1931
@@ -29,6 +41,7 @@ export class FightRegistry {
2941 // the Fights class for production usage, but has to be called manually when running tests.
3042 initialize ( ) {
3143 this . initializeLocations ( ) ;
44+ this . initializeCommands ( ) ;
3245 }
3346
3447 // Initializes the locations in which fights can take place. Each is based on a structured game
@@ -42,9 +55,59 @@ export class FightRegistry {
4255 }
4356 }
4457
58+ // Initializes the commands which act as shortcuts to the available fighting game configuration,
59+ // to make it easier for players to start a game.
60+ initializeCommands ( ) {
61+ for ( const filename of glob ( kCommandDirectory , '.*fights[\\\\/][^\\\\/]*\.json$' ) ) {
62+ const configuration = JSON . parse ( readFile ( kCommandDirectory + filename ) ) ;
63+ const settings = new Map ( ) ;
64+
65+ for ( const [ identifier , value ] of Object . entries ( configuration . settings ) )
66+ settings . set ( identifier , value ) ;
67+
68+ settings . set ( 'internal/goal' , configuration . goal ) ;
69+ settings . set ( 'internal/name' , configuration . name ) ;
70+
71+ this . registerCommand ( configuration . command , settings ) ;
72+ }
73+ }
74+
75+ // ---------------------------------------------------------------------------------------------
76+
77+ // Registers a command with the server, following the given configuration. Activating it will
78+ // immediately start the sign-up flow of the configured game. This mimics the command syntax
79+ // that the games features exposes by default.
80+ registerCommand ( command , settings ) {
81+ server . commandManager . buildCommand ( command )
82+ . sub ( 'custom' )
83+ . build ( FightRegistry . prototype . onCommand . bind ( this , settings , /* custom= */ true ) )
84+ . sub ( CommandBuilder . NUMBER_PARAMETER )
85+ . build ( FightRegistry . prototype . onCommand . bind ( this , settings , /* custom= */ false ) )
86+ . build ( FightRegistry . prototype . onCommand . bind ( this , settings , /* custom= */ false ) ) ;
87+
88+ this . #commands_. add ( command ) ;
89+ }
90+
91+ // Called when the |player| has issued a command. We'll construct the game command parameters
92+ // and use their API in order to formally request starting the game.
93+ onCommand ( settings , customise , player , registrationId ) {
94+ const params = new GameCommandParams ( ) ;
95+
96+ params . customise = ! ! customise ;
97+ params . registrationId = registrationId ;
98+ params . settings = clone ( settings ) ;
99+
100+ return this . #games_( ) . startGame ( FightGame , player , params ) ;
101+ }
102+
45103 // ---------------------------------------------------------------------------------------------
46104
47105 dispose ( ) {
106+ for ( const command of this . #commands_)
107+ server . commandManager . removeCommand ( command ) ;
108+
109+ this . #commands_ = null ;
110+
48111 this . #locations_. clear ( ) ;
49112 this . #locations_ = null ;
50113 }
0 commit comments