import { Direction, MazeMap, MazeTile, } from "./MazeMap"; class GameState { public userID: string; public mapID: string; public tileID: string; constructor(userID: string, mapID: string, tileID: string) { this.userID = userID; this.mapID = mapID; this.tileID = tileID; } } export class GameServer { private mapList: MazeMap[]; private states: GameState[]; constructor() { this.mapList = []; this.states = []; } public addMap(newMap: MazeMap): boolean { const validationResult = newMap.validate(); if(validationResult.length > 0){ let message = "Cannot add invalid map:"; for (const e of validationResult) { message += "\n\t" + e; } throw new Error(message); } if(-1 === this.mapList.findIndex(m => m.id === newMap.id)) { this.mapList.push(newMap); return true; } else { return false; } } public startGame(userID: string, mapID: string): MazeTile { const map = this.mapList.find(m => m.id === mapID); if(map == null){ throw new Error(`Map "${mapID}" does not exist.`); } const start = map.getStartTile(); let state = this.states.find(s => s.userID === userID); if(state == null) { state = new GameState(userID, map.id, start.id); this.states.push(state); } else { state.mapID = map.id; state.tileID = start.id; } return start; } public navigate(userID: string, currentTile:string, direction:Direction): MazeTile { const newPosition = new MazeTile(""); newPosition.paths.push(Direction.left); return newPosition; } public currentPosition(userID: string): MazeTile { const state = this.states.find(s => s.userID === userID); if(state == null){ return null; } const map = this.mapList.find(m => m.id === state.mapID); const tile = map.getTile(state.tileID); return tile; } }