Documentation ¶
Overview ¶
Package game contains all the logic for creating and manipulating a game
Index ¶
- Variables
- type Challenge
- type ChallengeStorage
- type Color
- type Game
- func (g *Game) CheckedKing() chess.Square
- func (g *Game) Export() string
- func (g *Game) FEN() string
- func (g *Game) IsExtendedTakebackAllowed(requestingPlayer *Player) bool
- func (g *Game) IsPastTakebackThreshold() bool
- func (g *Game) LastMove() *chess.Move
- func (g *Game) LastMoved() time.Time
- func (g *Game) Move(san string) (*chess.Move, error)
- func (g *Game) OtherPlayer(player *Player) *Player
- func (g *Game) Outcome() chess.Outcome
- func (g *Game) PGN() string
- func (g *Game) PlayerByID(ID string) (*Player, error)
- func (g *Game) Resign(resigner Player)
- func (g *Game) ResultText() string
- func (g *Game) SetTimeProvider(provider TimeProvider)
- func (g *Game) Start()
- func (g *Game) Started() bool
- func (g *Game) String() string
- func (g *Game) Takeback(requestingPlayer *Player) (*chess.Move, error)
- func (g *Game) Turn() Color
- func (g *Game) TurnPlayer() Player
- func (g *Game) ValidMoves() []*chess.Move
- type GameStorage
- type MemoryStore
- func (m *MemoryStore) RemoveChallenge(challengerID string, challengedID string) error
- func (m *MemoryStore) RemoveTakeback(takeback *Takeback) error
- func (m *MemoryStore) RetrieveChallenge(challengerID string, challengedID string) (*Challenge, error)
- func (m *MemoryStore) RetrieveGame(ID string) (*Game, error)
- func (m *MemoryStore) RetrieveTakeback(gameID string) (*Takeback, error)
- func (m *MemoryStore) StoreChallenge(c *Challenge) error
- func (m *MemoryStore) StoreGame(ID string, game *Game) error
- func (m *MemoryStore) StoreTakeback(takeback *Takeback) error
- type Player
- type SqliteStore
- func (s *SqliteStore) RemoveChallenge(challengerID string, challengedID string) error
- func (s *SqliteStore) RemoveTakeback(takeback *Takeback) error
- func (s *SqliteStore) RetrieveChallenge(challengerID string, challengedID string) (*Challenge, error)
- func (s *SqliteStore) RetrieveGame(ID string) (*Game, error)
- func (s *SqliteStore) RetrieveTakeback(gameID string) (*Takeback, error)
- func (s *SqliteStore) StoreChallenge(challenge *Challenge) error
- func (s *SqliteStore) StoreGame(ID string, gm *Game) error
- func (s *SqliteStore) StoreTakeback(takeback *Takeback) error
- type Takeback
- type TakebackStorage
- type TimeProvider
Constants ¶
This section is empty.
Variables ¶
var ErrGameCompleted = errors.New("game is already completed")
ErrGameCompleted is an error representing an action that failed due to the game being completed.
var ErrGameHasNoMoves = errors.New("game has no moves yet")
ErrGameHasNoMoves is an error representing an action that failed due to the game having no moves yet.
var ErrPastTimeThreshold = fmt.Errorf("exceeded threshold of %v", TakebackThreshold)
ErrPastTimeThreshold is an error representing an action that failed due to taking place after a specific threshold
var ErrPlayerAlreadyMoved = errors.New("other player has already made a move")
ErrPlayerAlreadyMoved is an error representing an action that failed due to a another player move.
Functions ¶
This section is empty.
Types ¶
type ChallengeStorage ¶
type ChallengeStorage interface { RetrieveChallenge(challengerID string, challengedID string) (*Challenge, error) StoreChallenge(challenge *Challenge) error RemoveChallenge(challengerID string, challengedID string) error }
ChallengeStorage is an interface to be implemented for persisting challenges
type Game ¶
Game is the state of a game (active or not)
func NewGameFromFEN ¶
NewGameFromFEN will create a new game with a given FEN starting position
func NewGameFromPGN ¶
NewGameFromPGN will create a new game at the state expressed by a provided PGN
func (*Game) CheckedKing ¶
CheckedKing returns the square of a checked king if there is indeed a king in check.
func (*Game) IsExtendedTakebackAllowed ¶
IsExtendedTakebackAllowed determines if a player may request a take back after the time threshold has expired
func (*Game) IsPastTakebackThreshold ¶
IsPastTakebackThreshold determines if the last move is within the allowable takeback time period.
func (*Game) OtherPlayer ¶
OtherPlayer returns ...the other player given a player
func (*Game) PlayerByID ¶
PlayerByID returns a reference to a player given their ID
func (*Game) ResultText ¶
ResultText will show the outcome of the game in textual format
func (*Game) SetTimeProvider ¶
func (g *Game) SetTimeProvider(provider TimeProvider)
SetTimeProvider allows the time provider to be overwritten (exclusively for testing)
func (*Game) Takeback ¶
Takeback reverts the game to the previous move prior to the last move. Note: If the first move of the game is taken back, the resulting move will be nil
func (*Game) TurnPlayer ¶
TurnPlayer returns which player should move next
func (*Game) ValidMoves ¶
ValidMoves returns a list of all moves available to the current player's turn
type GameStorage ¶
type GameStorage interface { RetrieveGame(ID string) (*Game, error) StoreGame(ID string, game *Game) error }
GameStorage is an interface to be implemented for persisting a game
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore implements the Game and Challenge storage interfaces and holds all state in memory Once the MemoryStore instance is released, all data in that storage is lost
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore returns a MemoryStore pointer
func (*MemoryStore) RemoveChallenge ¶
func (m *MemoryStore) RemoveChallenge(challengerID string, challengedID string) error
RemoveChallenge deletes a challenge request
func (*MemoryStore) RemoveTakeback ¶
func (m *MemoryStore) RemoveTakeback(takeback *Takeback) error
RemoveTakeback removes a takeback request from storage
func (*MemoryStore) RetrieveChallenge ¶
func (m *MemoryStore) RetrieveChallenge(challengerID string, challengedID string) (*Challenge, error)
RetrieveChallenge will get a challenge request by challenger ID and challenged ID
func (*MemoryStore) RetrieveGame ¶
func (m *MemoryStore) RetrieveGame(ID string) (*Game, error)
RetrieveGame will get a game from storage by its ID
func (*MemoryStore) RetrieveTakeback ¶
func (m *MemoryStore) RetrieveTakeback(gameID string) (*Takeback, error)
RetrieveTakeback finds a takeback request by a game ID
func (*MemoryStore) StoreChallenge ¶
func (m *MemoryStore) StoreChallenge(c *Challenge) error
StoreChallenge will persist a challenge request
func (*MemoryStore) StoreGame ¶
func (m *MemoryStore) StoreGame(ID string, game *Game) error
StoreGame persists a game into memory
func (*MemoryStore) StoreTakeback ¶
func (m *MemoryStore) StoreTakeback(takeback *Takeback) error
StoreTakeback stores a takeback request
type Player ¶
type Player struct { ID string // contains filtered or unexported fields }
Player represents a human Chess player
type SqliteStore ¶
type SqliteStore struct {
// contains filtered or unexported fields
}
SqliteStore is an implementation of GameStorage and ChallengeStorage interfaces that persists using sqlite3
func NewSqliteStore ¶
func NewSqliteStore(path string) (*SqliteStore, error)
NewSqliteStore creates (if not exists) the DB file and structure at the path specified It implements the GameStorage and ChallengeStorage interface and is intended as a suitable perminent storage of games and challenges
func (*SqliteStore) RemoveChallenge ¶
func (s *SqliteStore) RemoveChallenge(challengerID string, challengedID string) error
RemoveChallenge removes a challenge from the DB
func (*SqliteStore) RemoveTakeback ¶
func (s *SqliteStore) RemoveTakeback(takeback *Takeback) error
RemoveTakeback removes a takeback request from storage
func (*SqliteStore) RetrieveChallenge ¶
func (s *SqliteStore) RetrieveChallenge(challengerID string, challengedID string) (*Challenge, error)
RetrieveChallenge retrives a challenge by the challenger and challenged ID
func (*SqliteStore) RetrieveGame ¶
func (s *SqliteStore) RetrieveGame(ID string) (*Game, error)
RetrieveGame retrieves a game by ID
func (*SqliteStore) RetrieveTakeback ¶
func (s *SqliteStore) RetrieveTakeback(gameID string) (*Takeback, error)
RetrieveTakeback finds a takeback request by a game ID
func (*SqliteStore) StoreChallenge ¶
func (s *SqliteStore) StoreChallenge(challenge *Challenge) error
StoreChallenge only supports inserting new challenges. Challenges should not be updated only inserted/removed
func (*SqliteStore) StoreGame ¶
func (s *SqliteStore) StoreGame(ID string, gm *Game) error
StoreGame stores a game by ID. If a game is already established, only the PGN log is updated
func (*SqliteStore) StoreTakeback ¶
func (s *SqliteStore) StoreTakeback(takeback *Takeback) error
StoreTakeback stores a takeback request Note: This will overwrite a takeback request if a previous request is left open
type Takeback ¶
Takeback represents a takeback request
func NewTakeback ¶
NewTakeback constructs a new takeback request
func (*Takeback) IsValidTakeback ¶
IsValidTakeback determines if this takeback request is valid
type TakebackStorage ¶
type TakebackStorage interface { RetrieveTakeback(gameID string) (*Takeback, error) StoreTakeback(takeback *Takeback) error RemoveTakeback(takeback *Takeback) error }
TakebackStorage is an interface to be implemented for persisting takeback requests.
type TimeProvider ¶
TimeProvider is a closure that returns the current time as determined by the provider