game

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package game controls the logic to run the game.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Debug is a flag that causes the game to log the types messages that are read.
	Debug bool
	// TimeFunc is a function which should supply the current time since the unix epoch.
	// Used for the created at timestamp.
	TimeFunc func() int64
	// UserDao is used to increment the points for players when the game is finished.
	// MaxPlayers is the maximum number of players that can be part of the game.
	MaxPlayers int
	// PlayerCfg is used to create new players.
	PlayerCfg playerController.Config
	// NumNewTiles is the number of new tiles each player starts the game with.
	NumNewTiles int
	// TileLetters is a string of all the upper case letters that can be used in the game.
	// If not specified, the default 144 letters will be used.
	// If a letter should occur on multiple tiles, it sh be present multiple times.
	// For example, the TileLetters "AABCCC" will be used to initialize a game with two As, 1 B, and 3 Cs.
	TileLetters string
	// IdlePeriod is the amount of time that can pass between non-BoardRefresh messages before the game is idle and will delete itself.
	IdlePeriod time.Duration
	// ShuffleUnusedTilesFunc is used to shuffle unused tiles when initializing the game and after tiles are swapped.
	ShuffleUnusedTilesFunc func(tiles []tile.Tile)
	// ShufflePlayersFunc is used to shuffle the order of players when giving tiles after a snag
	// The snagging player should always get a new tile.  Other players will get a tile, if possible.
	ShufflePlayersFunc func(playerNames []player.Name)
	// Config is the nested coniguration for the specific game
	game.Config
}

Config contiains the properties to create similar games.

func (Config) NewGame

func (cfg Config) NewGame(log *log.Logger, id game.ID, WordValidator WordValidator, userDao UserDao) (*Game, error)

NewGame creates a new game and runs it.

type Game

type Game struct {
	WordValidator WordValidator

	Config
	// contains filtered or unexported fields
}

Game contains the logic to play a tile-base word-forming game between users.

func (*Game) Run

func (g *Game) Run(ctx context.Context, wg *sync.WaitGroup, in <-chan message.Message, out chan<- message.Message)

Run runs the game asynchronously until the context is closed.

type Runner

type Runner struct {

	// WordValidator is used to validate players' words when they try to finish the game.
	WordValidator WordValidator

	// RunnerConfig contains configuration properties of the Runner.
	RunnerConfig
	// contains filtered or unexported fields
}

Runner runs games.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup, in <-chan message.Message) <-chan message.Message

Run consumes messages from the "in" channel, processing them on a new goroutine until the "in" channel closes. The results of messages are sent on the "out" channel to be read by the subscriber.

type RunnerConfig

type RunnerConfig struct {
	// Debug is a flag that causes the game to log the types messages that are read.
	Debug bool
	// The maximum number of games.
	MaxGames int
	// The config for creating new games.
	GameConfig Config
}

RunnerConfig is used to create a game Runner.

func (RunnerConfig) NewRunner

func (cfg RunnerConfig) NewRunner(log *log.Logger, WordValidator WordValidator, userDao UserDao) (*Runner, error)

NewRunner creates a new game runner from the config.

type UserDao

type UserDao interface {
	// UpdatePointsIncrement increments points for the specified usernames based on the userPointsIncrementFunc
	UpdatePointsIncrement(ctx context.Context, userPoints map[string]int) error
}

UserDao makes changes to the stored state of users in the game

type WordValidator

type WordValidator interface {
	Validate(word string) bool
}

WordValidator checks if words are valid.

Directories

Path Synopsis
Package lobby handles players connecting to games and communication between games and players
Package lobby handles players connecting to games and communication between games and players
Package player controls the game for each player
Package player controls the game for each player
Package socket handles communication with a player using a websocket connection
Package socket handles communication with a player using a websocket connection
gorilla
Package gorilla implements a websocket connection by wrapping gorilla/websocket.
Package gorilla implements a websocket connection by wrapping gorilla/websocket.

Jump to

Keyboard shortcuts

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