Documentation
¶
Index ¶
- Constants
- Variables
- type Board
- type Cell
- type Client
- type Direction
- type ErrorData
- type Game
- func (game *Game) AddPlayer(player *Player) bool
- func (g *Game) Cleanup()
- func (g *Game) Forfeit(loserIdx int)
- func (g *Game) GetPlayerIndex(id PlayerID) int
- func (g *Game) GetPlayers() [2]*Player
- func (g *Game) GetStatus() GameStatus
- func (g *Game) GetTimeRemaining() [2]time.Duration
- func (g *Game) HasPlayer(id PlayerID) bool
- func (g *Game) IsFull() bool
- func (g *Game) Play(playerIdx, col int) error
- func (g *Game) RequestReplay(playerIdx int) bool
- type GameCreatedData
- type GameOverData
- type GameResult
- type GameStartData
- type GameStateData
- type GameStatus
- type JoinGameData
- type LastMove
- type LoginData
- type Message
- type MessageType
- type MoveData
- type Node
- type PlayData
- type Player
- type PlayerID
- type PlayerInfo
- type QueueUpdateData
- type ReplayRequestData
- type Sender
- type WelcomeData
Constants ¶
const ( Rows = 6 Cols = 7 WinLength = 4 )
Variables ¶
var ( ErrGameNotPlaying = errors.New("game is not in playing state") ErrNotYourTurn = errors.New("not your turn") ErrInvalidMove = errors.New("invalid move") ErrGameNotFound = errors.New("game not found") ErrGameFull = errors.New("game is full") ErrPlayerNotFound = errors.New("player not found") ErrPlayerNotInGame = errors.New("player not in game") ErrPlayerAlreadyInGame = errors.New("player already in game") ErrInvalidUsername = errors.New("invalid username") )
Functions ¶
This section is empty.
Types ¶
type Board ¶
type Board struct {
// contains filtered or unexported fields
}
Board represents the game board as a graph of connected nodes
func (*Board) GetLastPlayedNode ¶
GetLastPlayedNode returns the node at the top of a column
type Direction ¶
type Direction uint8
Direction represents the 8 possible neighbor directions in the board graph
func (Direction) IsDiagonal ¶
IsDiagonal checks if direction is diagonal
func (Direction) IsHorizontal ¶
IsHorizontal checks if direction is horizontal (left or right)
func (Direction) IsVertical ¶
IsVertical checks if direction is vertical (up or down)
type Game ¶
type Game struct {
Code string
Board *Board
Status GameStatus
Result GameResult
Players [2]*Player
CurrentTurn int
MoveCount int
LastPlayedAt time.Time
CreatedAt time.Time
LastMove *LastMove
ReplayRequests [2]bool
// Timer management
InitialClock time.Duration // Store initial clock for resets
TimeRemaining [2]time.Duration
TurnStartedAt time.Time
Timer *time.Timer
TimerCallback func(string, int) // Called when timer expires with (gameCode, loserIdx)
// contains filtered or unexported fields
}
Game represents a Connect 4 game session
func (*Game) GetPlayerIndex ¶
GetPlayerIndex returns the index of the given player
func (*Game) GetPlayers ¶
GetPlayers returns the players in the game safely
func (*Game) GetStatus ¶
func (g *Game) GetStatus() GameStatus
GetStatus returns the current game status
func (*Game) GetTimeRemaining ¶
GetTimeRemaining returns remaining time for both players adjusted for current turn
func (*Game) RequestReplay ¶
RequestReplay marks a player's desire to replay
type GameCreatedData ¶
type GameCreatedData struct {
Code string `json:"code"`
}
GameCreatedData sent when game is created
type GameOverData ¶
type GameOverData struct {
Result GameResult `json:"result"`
Board [Rows][Cols]Cell `json:"board"`
}
GameOverData sent when game ends
type GameResult ¶
type GameResult uint8
GameResult represents the outcome of a finished game
const ( ResultNone GameResult = iota ResultPlayer0Win ResultPlayer1Win ResultDraw )
type GameStartData ¶
type GameStartData struct {
Code string `json:"code"`
CurrentTurn int `json:"current_turn"`
Players [2]PlayerInfo `json:"players"`
TimeRemaining [2]int64 `json:"time_remaining"` // milliseconds
}
GameStartData sent when game starts
type GameStateData ¶
type GameStateData struct {
Code string `json:"code"`
Status GameStatus `json:"status"`
Result GameResult `json:"result"`
Board [Rows][Cols]Cell `json:"board"`
Players [2]PlayerInfo `json:"players"`
PlayerIdx int `json:"player_idx"`
CurrentTurn int `json:"current_turn"`
MoveCount int `json:"move_count"`
TimeRemaining [2]int64 `json:"time_remaining"` // milliseconds
ReplayRequests [2]bool `json:"replay_requests"`
LastMove *LastMove `json:"last_move,omitempty"`
}
GameStateData contains full game state for reconnection
type GameStatus ¶
type GameStatus uint8
GameStatus represents the current state of a game
const ( StatusWaiting GameStatus = iota StatusPlaying StatusFinished )
type JoinGameData ¶
type JoinGameData struct {
Code string `json:"code"`
}
JoinGameData contains game join request
type LoginData ¶
type LoginData struct {
Username string `json:"username"`
PlayerID *PlayerID `json:"player_id,omitempty"` // for reconnection
}
LoginData contains login credentials
type Message ¶
type Message struct {
Type MessageType `json:"type"`
Data interface{} `json:"data,omitempty"`
}
Message represents a websocket message
type MessageType ¶
type MessageType string
MessageType identifies the type of websocket message
const ( // Client to Server MsgLogin MessageType = "login" MsgCreateGame MessageType = "create_game" MsgJoinGame MessageType = "join_game" MsgPlay MessageType = "play" MsgReplay MessageType = "replay" MsgForfeit MessageType = "forfeit" MsgLeaveLobby MessageType = "leave_lobby" MsgJoinMatchmaking MessageType = "join_matchmaking" MsgLeaveMatchmaking MessageType = "leave_matchmaking" // Server to Client MsgWelcome MessageType = "welcome" MsgGameCreated MessageType = "game_created" MsgGameStart MessageType = "game_start" MsgGameState MessageType = "game_state" MsgMove MessageType = "move" MsgGameOver MessageType = "game_over" MsgReplayReq MessageType = "replay_request" MsgError MessageType = "error" MsgMatchmakingSearching MessageType = "matchmaking_searching" MsgQueueUpdate MessageType = "queue_update" )
type MoveData ¶
type MoveData struct {
PlayerIdx int `json:"player_idx"`
Column int `json:"column"`
Row int `json:"row"`
Board [Rows][Cols]Cell `json:"board"`
NextTurn int `json:"next_turn"`
TimeRemaining [2]int64 `json:"time_remaining"` // milliseconds
}
MoveData broadcasts a move to both players
type Node ¶
Node represents a cell in the board as a graph node
func (*Node) GetNeighbor ¶
GetNeighbor returns the neighbor in the given direction, or nil if none
func (*Node) SetNeighbor ¶
SetNeighbor sets the neighbor in the given direction
type PlayData ¶
type PlayData struct {
Column int `json:"column"`
}
PlayData contains a move request
type Player ¶
type Player struct {
sync.RWMutex
ID PlayerID
Username string
Remaining time.Duration
// contains filtered or unexported fields
}
Player represents a connected player
func (*Player) IsConnected ¶
IsConnected checks if the player has an active sender
type PlayerInfo ¶
type PlayerInfo struct {
ID PlayerID `json:"id"`
Username string `json:"username"`
Connected bool `json:"connected"`
}
PlayerInfo contains public player information
type QueueUpdateData ¶
type QueueUpdateData struct {
PlayersInQueue int `json:"players_in_queue"`
}
QueueUpdateData contains matchmaking queue information
type ReplayRequestData ¶
type ReplayRequestData struct {
PlayerIdx int `json:"player_idx"`
}
ReplayRequestData sent when a player requests replay
type WelcomeData ¶
WelcomeData sent after successful login