85 lines
1.9 KiB
Go
85 lines
1.9 KiB
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
|
|
ClaimedBy string `json:"claimedBy,omitempty"` // UserID that claimed this slot
|
|
}
|
|
|
|
// 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
|
|
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
|
|
}
|
|
|
|
// IsClaimed returns true if this slot has been claimed by a user
|
|
func (p *Player) IsClaimed() bool {
|
|
return p.ClaimedBy != ""
|
|
}
|
|
|
|
// CanBeClaimed returns true if this slot can be claimed
|
|
func (p *Player) CanBeClaimed() bool {
|
|
return p.Type == PlayerHuman && !p.IsClaimed()
|
|
}
|
|
|
|
type Color byte
|
|
|
|
const (
|
|
ColorWhite Color = 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
|
|
} |