v0.2.0 transitioned to api-only, extended and improved features, docs and tests added

This commit is contained in:
2025-10-29 23:28:19 -04:00
parent b98ea83012
commit 0ad608293e
32 changed files with 3683 additions and 1670 deletions

53
internal/core/api.go Normal file
View File

@ -0,0 +1,53 @@
// FILE: internal/core/api.go
package core
// Request types
type CreateGameRequest struct {
White PlayerConfig `json:"white" validate:"required"`
Black PlayerConfig `json:"black" validate:"required"`
FEN string `json:"fen,omitempty" validate:"omitempty,max=100"`
}
type ConfigurePlayersRequest struct {
White PlayerConfig `json:"white" validate:"required"`
Black PlayerConfig `json:"black" validate:"required"`
}
type MoveRequest struct {
Move string `json:"move" validate:"required,min=4,max=5"` // "cccc" for computer move, 4-5 chars for UCI moves
}
type UndoRequest struct {
Count int `json:"count" validate:"required,min=1,max=300"` // Max based on longest games in history (272), theoretical max 5949
}
// Response types
type GameResponse struct {
GameID string `json:"gameId"`
FEN string `json:"fen"`
Turn string `json:"turn"` // "w" or "b"
State string `json:"state"` // "ongoing", "white_wins", etc
Moves []string `json:"moves"`
Players PlayersResponse `json:"players"`
LastMove *MoveInfo `json:"lastMove,omitempty"`
}
type MoveInfo struct {
Move string `json:"move"`
PlayerColor string `json:"playerColor"` // "w" or "b"
Score int `json:"score,omitempty"`
Depth int `json:"depth,omitempty"`
}
type BoardResponse struct {
FEN string `json:"fen"`
Board string `json:"board"` // ASCII representation
}
type ErrorResponse struct {
Error string `json:"error"`
Code string `json:"code"`
Details string `json:"details,omitempty"`
}

View File

@ -1,53 +0,0 @@
// FILE: internal/core/core.go
package core
type State int
const (
StateOngoing State = iota
StateWhiteWins
StateBlackWins
StateDraw
StateStalemate
)
func (s State) String() string {
switch s {
case StateWhiteWins:
return "White wins"
case StateBlackWins:
return "Black wins"
case StateDraw:
return "Draw"
case StateStalemate:
return "Stalemate"
default:
return "Ongoing"
}
}
type PlayerType int
const (
PlayerHuman PlayerType = iota
PlayerComputer
)
type Player struct {
ID string
Type PlayerType
}
type Color byte
const (
ColorWhite Color = 'w'
ColorBlack Color = 'b'
)
func OppositeColor(c Color) Color {
if c == ColorWhite {
return ColorBlack
}
return ColorWhite
}

15
internal/core/error.go Normal file
View File

@ -0,0 +1,15 @@
// FILE: internal/core/core.go
package core
// Error codes
const (
ErrGameNotFound = "GAME_NOT_FOUND"
ErrInvalidMove = "INVALID_MOVE"
ErrNotHumanTurn = "NOT_HUMAN_TURN"
ErrGameOver = "GAME_OVER"
ErrRateLimitExceeded = "RATE_LIMIT_EXCEEDED"
ErrInvalidContent = "INVALID_CONTENT_TYPE"
ErrInvalidRequest = "INVALID_REQUEST"
ErrInvalidFEN = "INVALID_FEN"
ErrInternalError = "INTERNAL_ERROR"
)

75
internal/core/player.go Normal file
View File

@ -0,0 +1,75 @@
// FILE: internal/core/player.go
package core
import (
"github.com/google/uuid"
)
type PlayerType int
const (
PlayerHuman PlayerType = iota + 1
PlayerComputer
)
// Player is the complete game entity with all state
type Player struct {
ID string `json:"id"`
Color Color `json:"color"`
Type PlayerType `json:"type"`
Level int `json:"level,omitempty"` // Only for computer
SearchTime int `json:"searchTime,omitempty"` // Only for computer
}
// PlayerConfig for API requests and configuration
type PlayerConfig struct {
Type PlayerType `json:"type" validate:"required,oneof=1 2"`
Level int `json:"level,omitempty" validate:"omitempty,min=0,max=20"`
SearchTime int `json:"searchTime,omitempty" validate:"omitempty,min=100,max=10000"` // Processor sets the min value
}
// PlayersResponse for API responses - now contains full Player structs
type PlayersResponse struct {
White *Player `json:"white"`
Black *Player `json:"black"`
}
// NewPlayer creates a Player from PlayerConfig
func NewPlayer(config PlayerConfig, color Color) *Player {
player := &Player{
ID: uuid.New().String(),
Color: color,
Type: config.Type,
}
if config.Type == PlayerComputer {
player.Level = config.Level
player.SearchTime = config.SearchTime
}
return player
}
type Color byte
const (
ColorWhite = iota + 1
ColorBlack
)
func (c Color) String() string {
if c == ColorWhite {
return "w"
} else if c == ColorBlack {
return "b"
} else {
return "-"
}
}
func OppositeColor(c Color) Color {
if c == ColorWhite {
return ColorBlack
}
return ColorWhite
}

35
internal/core/state.go Normal file
View File

@ -0,0 +1,35 @@
// FILE: internal/core/core.go
package core
type State int
const (
StateOngoing State = iota
StatePending // Computer is calculating a move
StateStuck // Computer is calculating a move
StateWhiteWins
StateBlackWins
StateDraw
StateStalemate
)
func (s State) String() string {
switch s {
case StatePending:
return "pending"
case StateStuck:
return "stuck"
case StateWhiteWins:
return "white wins"
case StateBlackWins:
return "black wins"
case StateDraw:
return "draw"
case StateStalemate:
return "stalemate"
case StateOngoing:
return "ongoing"
default:
return "unknown"
}
}