import { ServerMessage } from "../messages/ServerMessage" ; import * as WebSocket from "ws" ; import EntireGame from "../common/EntireGame" ; import { UserSettings } from "../messages/ClientMessage" ; import { observable } from "mobx" ; export default class User { id : string ; name : string ; @ observable settings : UserSettings ; entireGame : EntireGame ; connectedClients : WebSocket [ ] = [ ] ; @ observable otherUsersFromSameNetwork : string [ ] = [ ] ; @ observable connected : boolean ; constructor ( id , name , game , settings , connected = false , otherUsersFromSameNetwork = [ ] ) { this . id = id ; this . name = name ; this . settings = settings ; this . entireGame = game ; this . connected = connected ; this . otherUsersFromSameNetwork = otherUsersFromSameNetwork ; } send ( message ) { this . entireGame . sendMessageToClients ( [ this ] , message ) ; } syncSettings ( ) { this . entireGame . sendMessageToServer ( { type : "change-settings" , settings : this . settings } ) ; } updateConnectionStatus ( ) { const newConnected = this . connectedClients . length > 0 ; if ( newConnected != this . connected ) { this . connected = newConnected ; this . entireGame . broadcastToClients ( { type : "update-connection-status" , user : this . id , status : this . connected } ) ; } } serializeToClient ( ) { return { id : this . id , name : this . name , settings : this . settings , connected : this . connected , otherUsersFromSameNetwork : this . otherUsersFromSameNetwork } ; } static deserializeFromServer ( game , data ) { return new User ( data . id , data . name , game , data . settings , data . connected , data . otherUsersFromSameNetwork ) ; } } export interface SerializedUser { id : string ; name : string ; settings : UserSettings ; connected : boolean ; otherUsersFromSameNetwork : string [ ] ; }