game

package
v0.0.0-...-53b8cb7 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package game represents all the game play for liar's dice.

Index

Constants

View Source
const (
	StatusNewGame    = "newgame"
	StatusPlaying    = "playing"
	StatusRoundOver  = "roundover"
	StatusGameOver   = "gameover"
	StatusReconciled = "reconciled"
)

Represents the different game status.

Variables

View Source
var ErrNotFound = errors.New("game not found")
View Source
var Tables = newTables()

Tables maintains the set of games in the system.

Functions

This section is empty.

Types

type Balance

type Balance struct {
	Player common.Address
	Amount *big.Float
}

Balance represents an individual balance for a player.

type BalanceFmt

type BalanceFmt struct {
	Player common.Address
	Amount string
}

BalanceFmt represents an individual formatted balance for a player.

type Banker

type Banker interface {
	AccountBalance(ctx context.Context, player common.Address) (GWei *big.Float, err error)
	Reconcile(ctx context.Context, winningPlayer common.Address, losingPlayers []common.Address, anteGWei *big.Float, gameFeeGWei *big.Float) (*types.Transaction, *types.Receipt, error)
}

Banker represents the ability to manage money for the game. Deposits and Withdrawls happen outside of game play.

type Bet

type Bet struct {
	Player common.Address
	Number int
	Suit   int
}

Bet represents a bet of dice made by a player.

type Cup

type Cup struct {
	Player   common.Address
	OrderIdx int
	Outs     int
	Dice     []int
}

Cup represents an individual cup being held by a player.

type Game

type Game struct {
	// contains filtered or unexported fields
}

Game represents a single game that is being played.

func New

func New(ctx context.Context, log *logger.Logger, converter *currency.Converter, storer Storer, banker Banker, player common.Address, anteUSD float64) (*Game, error)

New creates a new game.

func (*Game) AddAccount

func (g *Game) AddAccount(ctx context.Context, player common.Address) error

AddAccount adds a player to the game. If the account already exists, the function will return an error.

func (*Game) ApplyOut

func (g *Game) ApplyOut(ctx context.Context, player common.Address, outs int) error

ApplyOut will apply the specified number of outs to the account. If an account is out, it will check the number of active accounts, and end the round if there is only 1 left.

func (*Game) Bet

func (g *Game) Bet(ctx context.Context, player common.Address, number int, suit int) error

Bet accepts a bet from an account, but validates the bet is valid first. If the bet is valid, it's added to the list of bets for the game. Then the next player is determined and set.

func (*Game) CallLiar

func (g *Game) CallLiar(ctx context.Context, player common.Address) (winningPlayer common.Address, losingPlayer common.Address, err error)

CallLiar checks the last bet that was made and determines the winner and loser of the current round.

func (*Game) DateCreated

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

DateCreated returns the date/time the game was created.

func (*Game) ID

func (g *Game) ID() uuid.UUID

ID returns the game id.

func (*Game) NextRound

func (g *Game) NextRound(ctx context.Context) (int, error)

NextRound updates the game state for players who are out and determining which player goes next. The function returns the number of players left in the game.

func (*Game) NextTurn

func (g *Game) NextTurn(ctx context.Context) error

NextTurn determines which account makes the next move.

func (*Game) QueryState

func (g *Game) QueryState(ctx context.Context) (State, error)

QueryState retrieves the state for the current round of this game.

func (*Game) QueryStateByRound

func (g *Game) QueryStateByRound(ctx context.Context, round int) (State, error)

QueryStateByRound retrieves the state for the specified round of this game.

func (*Game) Reconcile

func (g *Game) Reconcile(ctx context.Context) (*types.Transaction, *types.Receipt, error)

Reconcile calculates the game pot and make the transfer to the winner.

func (*Game) RollDice

func (g *Game) RollDice(ctx context.Context, player common.Address, manualRole ...int) error

RollDice will generate 5 new random integers for the players cup. The caller can specific the dice if they choose.

func (*Game) StartGame

func (g *Game) StartGame(ctx context.Context) error

StartGame changes the status to Playing to allow the game to begin.

func (*Game) State

func (g *Game) State() State

State returns a copy of the game state.

func (*Game) Status

func (g *Game) Status() string

Status returns the current status of the game.

type State

type State struct {
	GameID          uuid.UUID
	GameName        string
	DateCreated     time.Time
	Round           int
	Status          string
	PlayerLastOut   common.Address
	PlayerLastWin   common.Address
	PlayerTurn      common.Address
	ExistingPlayers []common.Address
	Cups            map[common.Address]Cup
	Bets            []Bet
	Balances        []BalanceFmt
}

State represents a copy of the game state.

type Storer

type Storer interface {
	Create(ctx context.Context, g *Game) error
	InsertRound(ctx context.Context, state State) error
	QueryStateByID(ctx context.Context, gameID uuid.UUID, round int) (State, error)
}

Storer interface declares the behaviour this package needs to persist and retrieve data.

Directories

Path Synopsis
stores
gamedb
Package gamedb contains game related CRUD functionality.
Package gamedb contains game related CRUD functionality.

Jump to

Keyboard shortcuts

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