feat(online)!: seat two to four players in a room

A room holds up to four people and needs two to start. Both numbers are
server constants sent to the client in RoomState, so the lobby draws
whatever the server allows and widening a room is a server change alone.

Failing a turn eliminates that player rather than ending the game. The
syllable and the used words survive them, the turn passes to whoever is
next, and the last player standing wins. Two seats is that same rule seen
from close up, which is why there is one implementation of it and not two.

A dead end still costs the first player to face it their own clock, as
before: they get their turn, and lose it. Everyone behind them has already
seen that board, so they go out together rather than each sitting out a turn
limit they cannot use — which leaves the player who closed the position
standing, the same outcome two players get.

A player who is knocked out keeps their seat. They watch the rest of the
game, chat included, with only the word input gone, and everybody lands back
in the same lobby when it ends. The result screen is the whole table, ranked
by who outlasted whom, with each score reported beside the place rather than
deciding it.

The turn clock is deliberately not paused for a seat that has dropped, so a
player who loses their connection on their own turn loses it the way anybody
else would. Their reconnect window decides only whether they are still in the
game afterwards. Any number of windows can be open at once, settled by one
timer armed for the nearest of them.

Starting waits for every guest, not merely the first: a room of four that
began on one yes would have dealt three people a turn they never agreed to.
Kicking names a seat and is still refused on a player who is ready, and on
the owner's own — leaving is what an owner who wants out does, and it hands
the room on. Joining stays a lobby thing: a room with a game running turns a
latecomer away even with seats going spare, because there is no way to hand
somebody a game already in progress.

BREAKING CHANGE: RoomState, TurnUpdate and GameOver lose the fields that
could only ever describe a second player, OpponentLeft is retired in favour
of presence on RoomState, and suggestions move to the new PlayerEliminated —
they describe the position that beat a player, which by the end of a longer
game is nobody else's position. ProtocolVersion goes to 2, so a client built
against 1 is refused with a readable error rather than decoding a frame that
now means something else.
This commit is contained in:
tiennm99 committed 2026-09-08 10:56:14 +07:00
1 parent 0b507c3537
commit 4e2e9e1433
54 files changed
+3692 -1129

No files matched your search

+51 -22
View File
@@ -51,7 +51,9 @@ func clientVariants() map[string]*noituv1.ClientMessage {
Ready: true,
}}},
"client_start_game": {Payload: &noituv1.ClientMessage_StartGame{StartGame: &noituv1.StartGame{}}},
"client_kick_player": {Payload: &noituv1.ClientMessage_KickPlayer{KickPlayer: &noituv1.KickPlayer{}}},
"client_kick_player": {Payload: &noituv1.ClientMessage_KickPlayer{KickPlayer: &noituv1.KickPlayer{
PlayerId: "p3",
}}},
"client_leave_room": {Payload: &noituv1.ClientMessage_LeaveRoom{LeaveRoom: &noituv1.LeaveRoom{}}},
"client_send_chat": {Payload: &noituv1.ClientMessage_SendChat{SendChat: &noituv1.SendChat{
Text: "Chào bạn, ván này khó thật!",
@@ -65,7 +67,7 @@ func serverVariants() map[string]*noituv1.ServerMessage {
"server_welcome": {Payload: &noituv1.ServerMessage_Welcome{Welcome: &noituv1.Welcome{
SessionId: "s-1a2b3c",
ResumeToken: "r-8f2c",
ProtocolVersion: 1,
ProtocolVersion: ProtocolVersion,
AcceptedNickname: "Người chơi ẩn danh",
}}},
"server_game_started": {Payload: &noituv1.ServerMessage_GameStarted{GameStarted: &noituv1.GameStarted{
@@ -75,6 +77,8 @@ func serverVariants() map[string]*noituv1.ServerMessage {
DeadlineUnixMs: 1756998020000,
TurnSeq: 1,
TurnLimitMs: 20000,
Players: fixtureScores(),
TurnPlayerId: "p1",
}}},
"server_turn_update": {Payload: &noituv1.ServerMessage_TurnUpdate{TurnUpdate: &noituv1.TurnUpdate{
Played: &noituv1.PlayedWord{
@@ -83,14 +87,15 @@ func serverVariants() map[string]*noituv1.ServerMessage {
ByMe: false,
Points: 2,
Syllables: 2,
PlayerId: "p2",
},
CurrentSyllable: "yên",
MyTurn: true,
DeadlineUnixMs: 1756998040000,
TurnSeq: 2,
MyScore: 3,
OpponentScore: 5,
ChainLength: 2,
Players: fixtureScores(),
TurnPlayerId: "p3",
}}},
"server_move_rejected": {Payload: &noituv1.ServerMessage_MoveRejected{MoveRejected: &noituv1.MoveRejected{
Reason: noituv1.RejectReason_REJECT_REASON_WRONG_LINK,
@@ -100,16 +105,26 @@ func serverVariants() map[string]*noituv1.ServerMessage {
"server_game_over": {Payload: &noituv1.ServerMessage_GameOver{GameOver: &noituv1.GameOver{
IWon: false,
Reason: noituv1.GameEndReason_GAME_END_REASON_NO_LEGAL_MOVE,
MyScore: 7,
ChainLength: 11,
// A repeated string of Vietnamese words: the one field in the
// contract whose encoding is neither a scalar nor a submessage.
Suggestions: []string{"sinh viên", "sinh sôi"},
}}},
"server_opponent_left": {Payload: &noituv1.ServerMessage_OpponentLeft{OpponentLeft: &noituv1.OpponentLeft{
CanReconnect: true,
GraceMs: 30000,
// Ranked, so a fixture that quietly reordered the table would show
// up as ranks that no longer match their positions.
Standings: []*noituv1.PlayerScore{
{PlayerId: "p2", Name: "Khách mời", Score: 42, Connected: true, Rank: 1},
{PlayerId: "p1", Name: "Người chơi", IsMe: true, Score: 7, Eliminated: true, Connected: true, Rank: 2},
{PlayerId: "p3", Name: "Khách 2", Score: 3, Eliminated: true, Rank: 3},
},
}}},
"server_player_eliminated": {Payload: &noituv1.ServerMessage_PlayerEliminated{
PlayerEliminated: &noituv1.PlayerEliminated{
PlayerId: "p1",
Name: "Người chơi",
IsMe: true,
Reason: noituv1.GameEndReason_GAME_END_REASON_TIMEOUT,
// A repeated string of Vietnamese words: the one field in the
// contract whose encoding is neither a scalar nor a submessage.
Suggestions: []string{"sinh viên", "sinh sôi"},
},
}},
"server_error": {Payload: &noituv1.ServerMessage_Error{Error: &noituv1.ServerError{
Code: "room_not_found",
Message: "room_not_found",
@@ -137,21 +152,35 @@ func serverVariants() map[string]*noituv1.ServerMessage {
{FromMe: false, Author: "", Text: "Tôi phải đi", SentUnixMs: 1756998000789},
},
}}},
// An owner looking at two guests, one ready and here and one ready but
// away. Every boolean in a slot is load-bearing in at least one row,
// and no two rows agree on all of them.
"server_room_state": {Payload: &noituv1.ServerMessage_RoomState{RoomState: &noituv1.RoomState{
RoomCode: "K7QX",
// An owner looking at a guest who is here, ready, and connected:
// the one combination in which every boolean is load-bearing.
IAmOwner: true,
CanStart: true,
IAmReady: false,
OpponentPresent: true,
OpponentName: "Khách mời",
OpponentReady: true,
OpponentConnected: true,
RoomCode: "K7QX",
CanStart: true,
MaxPlayers: 4,
MinPlayers: 2,
GraceMs: 30000,
Players: []*noituv1.PlayerSlot{
{PlayerId: "p1", Name: "Người chơi", IsMe: true, IsOwner: true, Connected: true},
{PlayerId: "p2", Name: "Khách mời", Ready: true, Connected: true},
{PlayerId: "p3", Name: "Khách 2", Ready: true},
},
}}},
}
}
// fixtureScores is the players table both in-game messages carry. Three seats
// in three different states, so a fixture that dropped or reordered a row
// cannot still decode to something plausible.
func fixtureScores() []*noituv1.PlayerScore {
return []*noituv1.PlayerScore{
{PlayerId: "p1", Name: "Người chơi", IsMe: true, Score: 7, Connected: true},
{PlayerId: "p2", Name: "Khách mời", Score: 42, Eliminated: true, Connected: true},
{PlayerId: "p3", Name: "Khách 2", Score: 3},
}
}
// TestRoundTripEveryVariant marshals and unmarshals each oneof arm. Equality
// alone is not enough: an empty arm such as CreateRoom encodes to a payload of
// zero bytes, so checking that the case survived is what proves the arm is