@@ -20,7 +20,7 @@ import type { EmbedBuilder } from 'discord.js';
2020import type { Client , User } from 'ps-client' ;
2121import type { ReactElement } from 'react' ;
2222
23- const backupKeys = [ 'state' , 'started' , 'turn' , 'turns' , 'seed' , 'players' , 'log' , 'startedAt' , 'createdAt' ] as const ;
23+ const backupKeys = [ 'state' , 'started' , 'turn' , 'turns' , 'seed' , 'players' , 'theme' , ' log', 'startedAt' , 'createdAt' ] as const ;
2424
2525/**
2626 * This is the shared code for all games. To check the game-specific code, refer to the
@@ -63,6 +63,8 @@ export class Game<State extends BaseState> {
6363 players : Record < BaseState [ 'turn' ] , Player > = { } ;
6464 spectators : string [ ] = [ ] ;
6565
66+ theme ?: string ;
67+
6668 // Game-provided methods:
6769 render ( side : State [ 'turn' ] | null ) : ReactElement ;
6870 render ( ) {
@@ -114,6 +116,8 @@ export class Game<State extends BaseState> {
114116 this . timerLength = ctx . meta . timer ;
115117 this . pokeTimerLength = ctx . meta . pokeTimer ?? ctx . meta . timer ;
116118 }
119+
120+ if ( ctx . meta . defaultTheme ) this . theme = ctx . meta . defaultTheme ;
117121 }
118122 persist ( ctx : BaseContext ) {
119123 if ( ! PSGames [ this . meta . id ] ) PSGames [ this . meta . id ] = { } ;
@@ -208,6 +212,17 @@ export class Game<State extends BaseState> {
208212 gameCache . set ( { id : this . id , room : this . roomid , game : this . meta . id , backup } ) ;
209213 }
210214
215+ setTheme ( input : string ) : TranslatedText {
216+ if ( ! this . meta . themes ) this . throw ( 'GAME.NO_THEME_SUPPORT' , { game : this . meta . name } ) ;
217+ const themeId = toId ( input ) ;
218+ const allThemes = Object . values ( this . meta . themes ) ;
219+ const selectedTheme = allThemes . find ( theme => toId ( theme . name ) === themeId || theme . aliases . includes ( themeId ) ) ;
220+ if ( ! selectedTheme ) this . throw ( 'GAME.INVALID_THEME' , { themes : allThemes . map ( theme => theme . name ) . list ( this . $T ) } ) ;
221+ this . theme = selectedTheme . id ;
222+ this . update ( ) ;
223+ return this . $T ( 'GAME.SET_THEME' , { theme : selectedTheme . name } ) ;
224+ }
225+
211226 renderSignups ?( staff : boolean ) : ReactElement | null ;
212227 signups ( ) : void {
213228 if ( this . started ) this . throw ( 'GAME.ALREADY_STARTED' ) ;
0 commit comments