-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathgame-engine.ts
More file actions
68 lines (62 loc) · 2.03 KB
/
Copy pathgame-engine.ts
File metadata and controls
68 lines (62 loc) · 2.03 KB
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
// The Game Engine
import Reports from './reports'
import Wallets from './wallets'
export type GameState = {
clock: number
gameOpen: boolean
entries: [string, number][]
}
export default class GameEngine {
static instance: GameEngine
#startTime = 0
#clock = 0
#entries: [string, number][] = []
#gameOpen = true
#reports = new Reports()
#wallets = new Wallets()
constructor() {
if (GameEngine.instance) {
return GameEngine.instance
}
this.#startTime = Math.floor(Date.now() / 1000)
this.#clock = 60
GameEngine.instance = this
}
getGameState(): GameState {
// Get a snapshot of the current game state
const now = Math.floor(Date.now() / 1000)
let timeRemaining = this.#startTime - now + this.#clock
console.log('getGameState ' + timeRemaining)
if (timeRemaining < 0) {
timeRemaining = 0
}
this.#gameOpen = false
return {
clock: timeRemaining,
gameOpen: this.#gameOpen,
entries: this.#entries,
} as GameState
}
submitEntry(userId: string, entry: number): boolean {
// Submit a new entry for the user in this game
const now = Math.floor(Date.now() / 1000)
const time_remaining = this.#startTime - now + this.#clock
if (time_remaining > 0) {
if (this.#wallets.getBalance(userId) > 1) {
if (this.#wallets.adjustBalance(userId, -1)) {
this.#entries.push([userId, entry])
this.#reports.logEvent(
`New entry '${entry}' submitted by '${userId}'`
)
return true
}
this.#reports.logEvent(`Problem adjusting balance for '${userId}'`)
return false
}
this.#reports.logEvent(`User Balance for '${userId}' to low`)
return false
}
this.#reports.logEvent('Game Closed')
return false
}
}