game

package
v0.0.0-...-7b9b069 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package game contains all the logic for creating and manipulating a game

Index

Constants

This section is empty.

Variables

View Source
var ErrGameCompleted = errors.New("game is already completed")

ErrGameCompleted is an error representing an action that failed due to the game being completed.

View Source
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.

View Source
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

View Source
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 Challenge

type Challenge struct {
	ChallengerID string
	ChallengedID string
	GameID       string
	ChannelID    string
}

Challenge represents a challenge between two players

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 Color

type Color string

Color represents the game color (white/black)

const (
	White             Color         = "White"
	Black             Color         = "Black"
	TakebackThreshold time.Duration = time.Minute
)

White represents the color of the white set. Black represents the color of the black set. TakebackThreshold represents number of minutes to allow a takeback

type Game

type Game struct {
	ID string

	Players map[Color]Player
	// contains filtered or unexported fields
}

Game is the state of a game (active or not)

func NewGame

func NewGame(ID string, players ...Player) *Game

NewGame will create a new game with typical starting positions

func NewGameFromFEN

func NewGameFromFEN(ID string, fen string, players ...Player) (*Game, error)

NewGameFromFEN will create a new game with a given FEN starting position

func NewGameFromPGN

func NewGameFromPGN(ID string, pgn string, white Player, black Player) (*Game, error)

NewGameFromPGN will create a new game at the state expressed by a provided PGN

func (*Game) CheckedKing

func (g *Game) CheckedKing() chess.Square

CheckedKing returns the square of a checked king if there is indeed a king in check.

func (*Game) Export

func (g *Game) Export() string

Export a game in PGN format

func (*Game) FEN

func (g *Game) FEN() string

FEN serializer

func (*Game) IsExtendedTakebackAllowed

func (g *Game) IsExtendedTakebackAllowed(requestingPlayer *Player) bool

IsExtendedTakebackAllowed determines if a player may request a take back after the time threshold has expired

func (*Game) IsPastTakebackThreshold

func (g *Game) IsPastTakebackThreshold() bool

IsPastTakebackThreshold determines if the last move is within the allowable takeback time period.

func (*Game) LastMove

func (g *Game) LastMove() *chess.Move

LastMove returns the last move done of the game

func (*Game) LastMoved

func (g *Game) LastMoved() time.Time

LastMoved is the last time a move was made

func (*Game) Move

func (g *Game) Move(san string) (*chess.Move, error)

Move a Chess piece based on standard algabreic notation (d2d4, etc)

func (*Game) OtherPlayer

func (g *Game) OtherPlayer(player *Player) *Player

OtherPlayer returns ...the other player given a player

func (*Game) Outcome

func (g *Game) Outcome() chess.Outcome

Outcome determines the outcome of the game (or no outcome)

func (*Game) PGN

func (g *Game) PGN() string

PGN serializer

func (*Game) PlayerByID

func (g *Game) PlayerByID(ID string) (*Player, error)

PlayerByID returns a reference to a player given their ID

func (*Game) Resign

func (g *Game) Resign(resigner Player)

Resign will resign a player from the game

func (*Game) ResultText

func (g *Game) ResultText() string

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) Start

func (g *Game) Start()

Start indicates the game has been started

func (*Game) Started

func (g *Game) Started() bool

Started determines if the game has been started

func (*Game) String

func (g *Game) String() string

String representation of the current game state (draws an ascii board)

func (*Game) Takeback

func (g *Game) Takeback(requestingPlayer *Player) (*chess.Move, error)

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) Turn

func (g *Game) Turn() Color

Turn returns which color should move next

func (*Game) TurnPlayer

func (g *Game) TurnPlayer() Player

TurnPlayer returns which player should move next

func (*Game) ValidMoves

func (g *Game) ValidMoves() []*chess.Move

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

type Takeback struct {
	CurrentGame *Game
	FENSnapshot string
}

Takeback represents a takeback request

func NewTakeback

func NewTakeback(game *Game) *Takeback

NewTakeback constructs a new takeback request

func (*Takeback) IsValidTakeback

func (t *Takeback) IsValidTakeback() bool

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

type TimeProvider func() time.Time

TimeProvider is a closure that returns the current time as determined by the provider

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL