Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ lobbyInfo

---

readyLobby
- Client is ready to start, host may start the game

---

unreadyLobby
- Client is not ready to start, host has to wait to start the game

---

stopGame
- Client is returning to lobby. Server should send other clients back to lobby as well.

Expand Down
1 change: 1 addition & 0 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Client {
username = 'Guest'
modHash = 'NULL'
lobby: Lobby | null = null
isReadyLobby = false
/** Whether player is ready for next blind */
isReady = false
firstReady = false
Expand Down
5 changes: 5 additions & 0 deletions src/Lobby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Lobby {
this.options = {};

host.setLobby(this);
host.isReadyLobby = false;
host.sendAction({
action: "joinedLobby",
code: this.code,
Expand Down Expand Up @@ -88,8 +89,11 @@ class Lobby {
});
return;
}

this.guest = client;

client.setLobby(this);
client.isReadyLobby = false;
client.sendAction({
action: "joinedLobby",
code: this.code,
Expand Down Expand Up @@ -121,6 +125,7 @@ class Lobby {
action.guest = this.guest.username;
action.guestHash = this.guest.modHash;
action.guestCached = this.guest.isCached;
action.guestReady = this.guest.isReadyLobby;
this.guest.sendAction(action);
}

Expand Down
25 changes: 25 additions & 0 deletions src/actionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,35 @@ const lobbyInfoAction = (client: Client) => {
client.lobby?.broadcastLobbyInfo();
};

const readyLobbyAction = (client: Client) => {
client.isReadyLobby = true;
client.lobby?.broadcastLobbyInfo();
}

const unreadyLobbyAction = (client: Client) => {
client.isReadyLobby = false;
client.lobby?.broadcastLobbyInfo();
}

const keepAliveAction = (client: Client) => {
// Send an ack back to the received keepAlive
client.sendAction({ action: "keepAliveAck" });
};

const startGameAction = (client: Client) => {
const lobby = client.lobby;

// Only allow the host to start the game
if (!lobby || lobby.host?.id !== client.id) {
return;
}

// Only start the game if guest is ready
// TODO: Uncomment this when Client ready is released in the mod
// if (!lobby.guest?.isReadyLobby) {
// return;
// }

const lives = lobby.options.starting_lives
? Number.parseInt(lobby.options.starting_lives)
: GameModes[lobby.gameMode].startingLives;
Expand All @@ -89,8 +106,14 @@ const startGameAction = (client: Client) => {
deck: "c_multiplayer_1",
seed: lobby.options.different_seeds ? undefined : generateSeed(),
});

// Reset players' lives
lobby.setPlayersLives(lives);

// Unready guest for next game
if (lobby.guest) {
lobby.guest.isReadyLobby = false;
}
};

const readyBlindAction = (client: Client) => {
Expand Down Expand Up @@ -494,6 +517,8 @@ export const actionHandlers = {
joinLobby: joinLobbyAction,
lobbyInfo: lobbyInfoAction,
leaveLobby: leaveLobbyAction,
readyLobby: readyLobbyAction,
unreadyLobby: unreadyLobbyAction,
keepAlive: keepAliveAction,
startGame: startGameAction,
readyBlind: readyBlindAction,
Expand Down
5 changes: 5 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ActionLobbyInfo = {
guest?: string
guestHash?: string
guestCached?: boolean
guestReady?: boolean
isHost: boolean
}
export type ActionStopGame = { action: 'stopGame' }
Expand Down Expand Up @@ -99,6 +100,8 @@ export type ActionUsername = { action: 'username'; username: string; modHash: st
export type ActionCreateLobby = { action: 'createLobby'; gameMode: GameMode }
export type ActionJoinLobby = { action: 'joinLobby'; code: string }
export type ActionLeaveLobby = { action: 'leaveLobby' }
export type ActionReadyLobby = { action: 'readyLobby' }
export type ActionUnreadyLobby = { action: 'unreadyLobby' }
export type ActionLobbyInfoRequest = { action: 'lobbyInfo' }
export type ActionStopGameRequest = { action: 'stopGame' }
export type ActionStartGameRequest = { action: 'startGame' }
Expand Down Expand Up @@ -144,6 +147,8 @@ export type ActionClientToServer =
| ActionCreateLobby
| ActionJoinLobby
| ActionLeaveLobby
| ActionReadyLobby
| ActionUnreadyLobby
| ActionLobbyInfoRequest
| ActionStopGameRequest
| ActionStartGameRequest
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ const server = createServer((socket) => {
case 'leaveLobby':
actionHandlers.leaveLobby(client)
break
case 'readyLobby':
actionHandlers.readyLobby(client)
break
case 'unreadyLobby':
actionHandlers.unreadyLobby(client)
break
case 'startGame':
actionHandlers.startGame(client)
break
Expand Down