-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathClient.ts
More file actions
114 lines (97 loc) · 2.74 KB
/
Client.ts
File metadata and controls
114 lines (97 loc) · 2.74 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
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
111
112
113
114
import { type AddressInfo } from 'node:net'
import { randomBytes } from 'node:crypto'
import { v4 as uuidv4 } from 'uuid'
import type Lobby from './Lobby.js'
import type { ActionServerToClient } from './actions.js'
import { InsaneInt } from './InsaneInt.js'
type SendFn = (action: ActionServerToClient) => void
type CloseConnFn = () => void
/* biome-ignore lint/complexity/noBannedTypes:
This is how the net module does it */
type Address = AddressInfo | {}
class Client {
// Connection info
id: string
// Could be useful later on to detect reconnects
address: Address
sendAction: SendFn
closeConnection: CloseConnFn
/** Token used to verify identity when reconnecting to a lobby */
reconnectToken: string
// Game info
username = 'Guest'
modHash = 'NULL'
lobby: Lobby | null = null
isReadyLobby = false
/** Whether player is ready for next blind */
isReady = false
firstReady = false
lives = 5
score = new InsaneInt(0, 0, 0)
handsLeft = 4
ante = 1
skips = 0
furthestBlind = 0
livesBlocker = false
location = 'loc_selecting'
isCached = true
constructor(address: Address, send: SendFn, closeConnection: CloseConnFn) {
this.id = uuidv4()
this.address = address
this.sendAction = send
this.closeConnection = closeConnection
this.reconnectToken = randomBytes(16).toString('hex')
}
setLocation = (location: string) => {
this.location = location
if (this.lobby) {
if (this.lobby.host === this) {
this.lobby.guest?.sendAction({ action: "enemyLocation", location: this.location })
} else {
this.lobby.host?.sendAction({ action: "enemyLocation", location: this.location })
}
}
}
setUsername = (username: string) => {
this.username = username
this.lobby?.broadcastLobbyInfo()
}
setModHash = (modHash: string) => {
this.modHash = modHash
this.lobby?.broadcastLobbyInfo()
}
setLobby = (lobby: Lobby | null) => {
this.lobby = lobby
}
resetBlocker = () => {
this.livesBlocker = false
}
loseLife = () => {
if (!this.livesBlocker) {
this.lives -= 1
this.livesBlocker = true
this.sendAction({ action: "playerInfo", lives: this.lives });
if (this.lobby && this.lobby.host && this.lobby.guest) {
const enemy = this.lobby.host === this ? this.lobby.guest : this.lobby.host
enemy.sendAction({
action: "enemyInfo",
handsLeft: this.handsLeft,
score: this.score.toString(),
skips: this.skips,
lives: this.lives,
});
}
}
}
setSkips = (skips: number) => {
this.skips = skips
}
/** Adopt the connection from another Client (used on reconnect) */
replaceConnection = (other: Client) => {
this.address = other.address
this.sendAction = other.sendAction
this.closeConnection = other.closeConnection
this.reconnectToken = other.reconnectToken
}
}
export default Client