mirror of
https://github.com/tiennm99/noitu.git
synced 2026-10-05 14:14:53 +00:00
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.
82 lines
2.8 KiB
Go
82 lines
2.8 KiB
Go
package wsapi
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/coder/websocket"
|
|
noituv1 "github.com/tiennm99dev/noitu/server/gen/noitu/v1"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// maxFrameBytes caps what a client may send.
|
|
//
|
|
// The largest legitimate client frame is a Hello carrying a 20-rune nickname,
|
|
// well under 200 bytes. 4 KiB leaves room for the protocol to grow while still
|
|
// making a memory-exhaustion attempt fail at the socket rather than in the
|
|
// decoder.
|
|
const maxFrameBytes = 4096
|
|
|
|
// ErrNotBinary rejects text frames. The protocol is protobuf over binary
|
|
// frames only; a text frame means the peer is speaking something else, and
|
|
// guessing at it is how a parser becomes an attack surface.
|
|
var ErrNotBinary = errors.New("wsapi: expected a binary frame")
|
|
|
|
// Encode marshals a server message for the wire.
|
|
func Encode(m *noituv1.ServerMessage) ([]byte, error) {
|
|
raw, err := proto.Marshal(m)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("wsapi: encode: %w", err)
|
|
}
|
|
return raw, nil
|
|
}
|
|
|
|
// Decode parses a client frame.
|
|
//
|
|
// typ is checked here rather than at the call site so every entry point gets
|
|
// the same rejection.
|
|
func Decode(typ websocket.MessageType, raw []byte) (*noituv1.ClientMessage, error) {
|
|
if typ != websocket.MessageBinary {
|
|
return nil, ErrNotBinary
|
|
}
|
|
var m noituv1.ClientMessage
|
|
if err := proto.Unmarshal(raw, &m); err != nil {
|
|
return nil, fmt.Errorf("wsapi: decode: %w", err)
|
|
}
|
|
return &m, nil
|
|
}
|
|
|
|
// Message-building helpers. Every server message is constructed through one of
|
|
// these, so the oneof wrapper is written once per variant instead of at each
|
|
// call site.
|
|
|
|
func welcomeMsg(sessionID, resumeToken, nickname string) *noituv1.ServerMessage {
|
|
return &noituv1.ServerMessage{Payload: &noituv1.ServerMessage_Welcome{Welcome: &noituv1.Welcome{
|
|
SessionId: sessionID,
|
|
ResumeToken: resumeToken,
|
|
ProtocolVersion: ProtocolVersion,
|
|
AcceptedNickname: nickname,
|
|
}}}
|
|
}
|
|
|
|
func moveRejectedMsg(reason noituv1.RejectReason, word string, turnSeq uint32) *noituv1.ServerMessage {
|
|
return &noituv1.ServerMessage{Payload: &noituv1.ServerMessage_MoveRejected{
|
|
MoveRejected: &noituv1.MoveRejected{Reason: reason, Word: word, TurnSeq: turnSeq},
|
|
}}
|
|
}
|
|
|
|
// errorMsg carries a UI key, never prose and never an internal error string.
|
|
// The Vietnamese copy lives in the frontend so all wording stays in one place,
|
|
// and a raw error would leak server internals to anyone with a socket.
|
|
func errorMsg(code string) *noituv1.ServerMessage {
|
|
return &noituv1.ServerMessage{Payload: &noituv1.ServerMessage_Error{
|
|
Error: &noituv1.ServerError{Code: code, Message: code},
|
|
}}
|
|
}
|
|
|
|
func pongMsg(clientTimeMs, serverTimeMs int64) *noituv1.ServerMessage {
|
|
return &noituv1.ServerMessage{Payload: &noituv1.ServerMessage_Pong{
|
|
Pong: &noituv1.Pong{ClientTimeMs: clientTimeMs, ServerTimeMs: serverTimeMs},
|
|
}}
|
|
}
|