Skip to content

Commit f6d359f

Browse files
authored
Merge pull request #10 from Brawmario/feature/guest-ready-lobby
Add readyLobby and unreadyLobby actions with corresponding state management
2 parents 823ca34 + da2004e commit f6d359f

6 files changed

Lines changed: 52 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ lobbyInfo
124124

125125
---
126126

127+
readyLobby
128+
- Client is ready to start, host may start the game
129+
130+
---
131+
132+
unreadyLobby
133+
- Client is not ready to start, host has to wait to start the game
134+
135+
---
136+
127137
stopGame
128138
- Client is returning to lobby. Server should send other clients back to lobby as well.
129139

src/Client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Client {
2323
username = 'Guest'
2424
modHash = 'NULL'
2525
lobby: Lobby | null = null
26+
isReadyLobby = false
2627
/** Whether player is ready for next blind */
2728
isReady = false
2829
firstReady = false

src/Lobby.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Lobby {
4949
this.options = {};
5050

5151
host.setLobby(this);
52+
host.isReadyLobby = false;
5253
host.sendAction({
5354
action: "joinedLobby",
5455
code: this.code,
@@ -88,8 +89,11 @@ class Lobby {
8889
});
8990
return;
9091
}
92+
9193
this.guest = client;
94+
9295
client.setLobby(this);
96+
client.isReadyLobby = false;
9397
client.sendAction({
9498
action: "joinedLobby",
9599
code: this.code,
@@ -121,6 +125,7 @@ class Lobby {
121125
action.guest = this.guest.username;
122126
action.guestHash = this.guest.modHash;
123127
action.guestCached = this.guest.isCached;
128+
action.guestReady = this.guest.isReadyLobby;
124129
this.guest.sendAction(action);
125130
}
126131

src/actionHandlers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,35 @@ const lobbyInfoAction = (client: Client) => {
6868
client.lobby?.broadcastLobbyInfo();
6969
};
7070

71+
const readyLobbyAction = (client: Client) => {
72+
client.isReadyLobby = true;
73+
client.lobby?.broadcastLobbyInfo();
74+
}
75+
76+
const unreadyLobbyAction = (client: Client) => {
77+
client.isReadyLobby = false;
78+
client.lobby?.broadcastLobbyInfo();
79+
}
80+
7181
const keepAliveAction = (client: Client) => {
7282
// Send an ack back to the received keepAlive
7383
client.sendAction({ action: "keepAliveAck" });
7484
};
7585

7686
const startGameAction = (client: Client) => {
7787
const lobby = client.lobby;
88+
7889
// Only allow the host to start the game
7990
if (!lobby || lobby.host?.id !== client.id) {
8091
return;
8192
}
8293

94+
// Only start the game if guest is ready
95+
// TODO: Uncomment this when Client ready is released in the mod
96+
// if (!lobby.guest?.isReadyLobby) {
97+
// return;
98+
// }
99+
83100
const lives = lobby.options.starting_lives
84101
? Number.parseInt(lobby.options.starting_lives)
85102
: GameModes[lobby.gameMode].startingLives;
@@ -89,8 +106,14 @@ const startGameAction = (client: Client) => {
89106
deck: "c_multiplayer_1",
90107
seed: lobby.options.different_seeds ? undefined : generateSeed(),
91108
});
109+
92110
// Reset players' lives
93111
lobby.setPlayersLives(lives);
112+
113+
// Unready guest for next game
114+
if (lobby.guest) {
115+
lobby.guest.isReadyLobby = false;
116+
}
94117
};
95118

96119
const readyBlindAction = (client: Client) => {
@@ -494,6 +517,8 @@ export const actionHandlers = {
494517
joinLobby: joinLobbyAction,
495518
lobbyInfo: lobbyInfoAction,
496519
leaveLobby: leaveLobbyAction,
520+
readyLobby: readyLobbyAction,
521+
unreadyLobby: unreadyLobbyAction,
497522
keepAlive: keepAliveAction,
498523
startGame: startGameAction,
499524
readyBlind: readyBlindAction,

src/actions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type ActionLobbyInfo = {
1010
guest?: string
1111
guestHash?: string
1212
guestCached?: boolean
13+
guestReady?: boolean
1314
isHost: boolean
1415
}
1516
export type ActionStopGame = { action: 'stopGame' }
@@ -99,6 +100,8 @@ export type ActionUsername = { action: 'username'; username: string; modHash: st
99100
export type ActionCreateLobby = { action: 'createLobby'; gameMode: GameMode }
100101
export type ActionJoinLobby = { action: 'joinLobby'; code: string }
101102
export type ActionLeaveLobby = { action: 'leaveLobby' }
103+
export type ActionReadyLobby = { action: 'readyLobby' }
104+
export type ActionUnreadyLobby = { action: 'unreadyLobby' }
102105
export type ActionLobbyInfoRequest = { action: 'lobbyInfo' }
103106
export type ActionStopGameRequest = { action: 'stopGame' }
104107
export type ActionStartGameRequest = { action: 'startGame' }
@@ -144,6 +147,8 @@ export type ActionClientToServer =
144147
| ActionCreateLobby
145148
| ActionJoinLobby
146149
| ActionLeaveLobby
150+
| ActionReadyLobby
151+
| ActionUnreadyLobby
147152
| ActionLobbyInfoRequest
148153
| ActionStopGameRequest
149154
| ActionStartGameRequest

src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ const server = createServer((socket) => {
199199
case 'leaveLobby':
200200
actionHandlers.leaveLobby(client)
201201
break
202+
case 'readyLobby':
203+
actionHandlers.readyLobby(client)
204+
break
205+
case 'unreadyLobby':
206+
actionHandlers.unreadyLobby(client)
207+
break
202208
case 'startGame':
203209
actionHandlers.startGame(client)
204210
break

0 commit comments

Comments
 (0)