export interface RealtimeSubscribeHMMModuleAmongUsListener { serverGameStateUpdated : ( gameState : GameState ) => void ; } export const STATES = [ "LOBBY" , "TASKS" , "DISCUSSION" , "MENU" , ] ; export const REGIONS = [ "North America" , "Asia" , "Europe" ] ; export const ACTIONS = [ "JOIN" , "LEAVE" , "KILL" , "COLOR_CHANGE" , "FORCE_UPDATE" , "DISCONNECT" , "EXILE" , ] ; export const COLORS = [ "red" , "blue" , "green" , "pink" , "orange" , "yellow" , "black" , "white" , "purple" , "brown" , "cyan" , "lime" , "maroon" , "rose" , "banana" , "tan" , "sunset" , "sunset" , "sunset" ] ; export const COLORS_RGB = { red : "#BE0032" , blue : "#2252C1" , green : "#3BAF75" , pink : "#FF69B4" , orange : "#E67928" , yellow : "#F0E448" , black : "#000000" , white : "#FFFFFF" , purple : "#A757A8" , brown : "#6B3E08" , cyan : "#66ffcc" , lime : "#B9DD64" , maroon : "#7F4428" , rose : "#e83f5f" , banana : "#FFBF11" , tan : "#c1813f" , sunset : "#f6b483" , } ; export const ICONS_ALIVE = COLORS . map ( ( x ) => { return `resources/amongus/${ x }.png` ; } ) ; export const ICONS_DEAD = COLORS . map ( ( x ) => { return `resources/amongus/${ x }_dead.png` ; } ) ; type PlayerState = { name : string ; isDead : boolean ; isDeadDiscovered : boolean ; disconnected : boolean ; color : number ; action : number ; attendeeId ? : string ; chimeName ? : string ; } ; export type GameState = { hmmAttendeeId : string ; state : number ; lobbyCode : string ; gameRegion : number ; map : number ; connectCode : string ; players : PlayerState [ ] ; } ; const initialState = { hmmAttendeeId : "" , state : 3 , lobbyCode : "" , gameRegion : 0 , map : 0 , connectCode : "" , players : [ ] , } ; export class RealtimeSubscribeHMMModuleAmongUsServer { private _gameState : GameState = initialState ; private _hmmAttendeeId = "" ; private _realtimeSubscribeHMMModuleAmongUsListener : RealtimeSubscribeHMMModuleAmongUsListener | null = null ; constructor ( hmmAttendeeId ) { this . _hmmAttendeeId = hmmAttendeeId ; } setRealtimeSubscribeHMMModuleAmongUsListener ( l ) { this . _realtimeSubscribeHMMModuleAmongUsListener = l ; } updateGameState = ( ev , data ) => { const newGameState = JSON . parse ( data ) as GameState ; newGameState . hmmAttendeeId = this . _hmmAttendeeId ; this . _gameState . players . forEach ( ( x ) => { if ( x . attendeeId ) { const newPlayer = newGameState . players . find ( ( y ) => { return x . name === y . name ; } ) ; if ( newPlayer ) { newPlayer . attendeeId = x . attendeeId ; newPlayer . chimeName = x . chimeName ; } } } ) ; this . _gameState = newGameState ; this . _realtimeSubscribeHMMModuleAmongUsListener ?. serverGameStateUpdated ( this . _gameState ) ; } ; registerUserName = ( userName , attendeeId , chimeUserName ) => { const unregisterTarget = this . _gameState . players . find ( ( x ) => { return x . attendeeId === attendeeId ; } ) ; if ( unregisterTarget ) { unregisterTarget . attendeeId = undefined ; unregisterTarget . chimeName = undefined ; } console . log ( `Register User Name ${ JSON . stringify ( this . _gameState ) }` ) ; const targetPlayer = this . _gameState . players . find ( ( x ) => { return x . name === userName ; } ) ; if ( ! targetPlayer ) { console . log ( `user ${ userName } is not found` ) ; return ; } targetPlayer . attendeeId = attendeeId ; targetPlayer . chimeName = chimeUserName ; console . log ( `${ targetPlayer . name } is registaerd as ${ targetPlayer . attendeeId }` ) ; this . _realtimeSubscribeHMMModuleAmongUsListener ?. serverGameStateUpdated ( this . _gameState ) ; } ; }