This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.ts
110 lines (90 loc) · 3.11 KB
/
GameManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { GameCreationOptions } from '../rules/GameCreationOptions'
import { GameManagerWorkerInterface } from './GameManagerWorkerInterface'
import { Move } from '../rules/CurrentGame'
import { GameInfo } from './GameInfo'
export class GameManager {
private gmwi: GameManagerWorkerInterface
private nextID: number
private gameInfos: Map<number, GameInfo>
constructor() {
this.gmwi = new GameManagerWorkerInterface()
this.gameInfos = new Map<number, GameInfo>()
this.nextID = 0
}
public createGameId(gameName: string, isReplay: boolean): number {
let newGameId = this.nextID++
this.gameInfos.set(newGameId, {
id: newGameId,
name: gameName,
isReplay: isReplay,
currentTurn: 0,
})
console.log('Created game', newGameId, this.getGameInfo(newGameId))
return newGameId
}
public getGameId(gameName: string): number {
this.gameInfos.forEach((value, key, map) => {
if (value.name == gameName) {
return key
}
})
throw Error('A Game with name \'' + gameName + '\' does not exist!')
}
/** Returns the buffered GameInfo for the game with this id */
public getGameInfo(gameId: number): GameInfo {
return this.gameInfos.get(gameId)
}
/**
* Creates a game with the given options
* @returns a Promise containing the information of the created game
*/
public createGame(options: GameCreationOptions): Promise<GameInfo> {
return this.gmwi.createGameWithOptions(options).then(id => this.getGameInfo(id))
}
public saveReplayOfGame(gameId: number, path: string) {
this.gmwi.saveReplayOfGame(gameId, path)
}
/** Gets the state for the game with the id corresponding to the name and the specific turn */
private getState(gameName: string, turn: number) {
return this.gmwi.getState(this.getGameId(gameName), turn)
}
/** Returns a list of GameInfos */
public getGameInfos(): GameInfo[] {
return Array.from(this.gameInfos.values())
}
/** returns whether a game with that name exists */
public hasGame(name: string) {
const games_list = this.getGameInfos()
let hasGame = games_list.map(game => game.name).includes(name)
console.log('hasGame ' + name + ':', hasGame)
return hasGame
}
public setCurrentDisplayStateOnGame(gameId: number, turn: number) {
this.getGameInfo(gameId).currentTurn = turn
}
public getCurrentDisplayStateOnGame(gameId: number) {
return this.getGameInfo(gameId).currentTurn
}
public getGameStatus(gameId: number) {
return this.gmwi.getStatus(gameId)
}
public sendMove(gameId: number, id: number, move: Move) {
return this.gmwi.sendMove(gameId, id, move)
}
public getGameState(gameId: number, turn: number) {
return this.gmwi.getState(gameId, turn)
}
public renameGame(gameId: number, newName: string) {
this.gameInfos[gameId].name = newName
}
public deleteGame(gameId: number) {
this.gameInfos.delete(gameId)
this.gmwi.deleteGame(gameId)
}
public stop() {
this.gmwi.stop()
}
public getGameServerStatus() {
return this.gmwi.getGameServerStatus()
}
}