model

package
v0.0.0-...-2379a7f Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const AirplaneDefaultSpeed = 1 / float64(3*time.Second)

AirplaneDefaultSpeed is the default speed of an airplane tiles/ns.

View Source
const InvalidID = ID("INVALID_ID")

InvalidID represents an invalid model ID, guaranteed to never match the ID of any entry.

View Source
const IslandGrowthCap = 100.0

IslandGrowthCap is the army size where the island army stops growing, without factoring in the island size.

View Source
const IslandGrowthInterval = 2 * time.Second

IslandGrowthInterval is the interval between army size growth, without factoring in the island size.

Variables

This section is empty.

Functions

func IsGameOverEvent

func IsGameOverEvent(evt Event) bool

IsGameOverEvent tests if an event is a game over event.

Types

type Action

type Action interface {
	// Apply applies an action to a game, returning a slice of events created.
	// The Apply method is responsible for validating the action before it is
	// applied.
	//
	// If Apply returns an IllegalActionError, then the action was found
	// invalid before it was applied. Such actions have no effect on the game.
	// If any other type of error is returned, then the game should be considered
	// in an invalid state, meaning no further actions may be applied to the game.
	Apply(game *Game) ([]Event, error)
}

Action is an action to be applied on a game instance.

type ActionCheckGameOver

type ActionCheckGameOver struct {
}

ActionCheckGameOver is an action for triggering a check for if the game is over.

func (*ActionCheckGameOver) Apply

func (act *ActionCheckGameOver) Apply(game *Game) ([]Event, error)

Apply applies the ActionCheckGameOver to the game instance, returning a game over event if the game is over.

type ActionForceGameOver

type ActionForceGameOver struct {
}

ActionForceGameOver is an action for triggering an unconditional GameOver event with no winner.

func (*ActionForceGameOver) Apply

func (act *ActionForceGameOver) Apply(game *Game) ([]Event, error)

Apply unconditionally returns a game over event without a winner.

type ActionTick

type ActionTick struct {
	// Delta is the size of the time step that should be applied
	// to the game. Delta must be >= 0.
	Delta time.Duration
}

ActionTick is an action that performs a tick (i.e. update) on the game instance.

func (*ActionTick) Apply

func (at *ActionTick) Apply(game *Game) ([]Event, error)

Apply applies the ActionTick to the game instance.

type Airplane

type Airplane struct {
	*Army
	// contains filtered or unexported fields
}

Airplane represents an airplane in the game model. An Airplane is sent from one island to another, transporting an army to the destination.

func NewAirplane

func NewAirplane(origin Coordinate, destination *Island, owner *Player, strength int64) *Airplane

NewAirplane creates a new Airplane, starting at origin, targeting the destination Island. The Army contained on the Airplane is set to the provided strength, owned by the owner.

func (*Airplane) Copy

func (a *Airplane) Copy() *Airplane

Copy performs a deep copy of the airplane.

func (*Airplane) Destination

func (a *Airplane) Destination() IslandID

Destination returns the IslandID of the airplane's target island.

func (*Airplane) Direction

func (a *Airplane) Direction() float64

Direction returns the current direction of the airplane.

func (*Airplane) ID

func (a *Airplane) ID() AirplaneID

ID is a getter for the id of the airplane.

func (*Airplane) Position

func (a *Airplane) Position() FloatCoordinate

Position returns the current position of the airplane.

func (*Airplane) SetDirection

func (a *Airplane) SetDirection(direction float64)

SetDirection is a setter for the direction of the airplane.

func (*Airplane) SetPosition

func (a *Airplane) SetPosition(position FloatCoordinate)

SetPosition is a setter for the position of the airplane.

func (*Airplane) SetSpeed

func (a *Airplane) SetSpeed(speed float64)

SetSpeed is a setter for the speed of the airplane.

func (*Airplane) Speed

func (a *Airplane) Speed() float64

Speed returns the speed of the airplane. Speed is measured in game tiles / nanosecond.

type AirplaneID

type AirplaneID ID

AirplaneID is the id type for Airplanes.

type Army

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

Army is a representation of an Army owned by a player. The Army struct is not used directly, but instead embedded in Islands and Airplanes.

func NewArmy

func NewArmy(owner *Player, strength int64) *Army

NewArmy creates a new Army with the provided owner and strength. The provided owner must not be nil, or the method will panic.

func (*Army) Copy

func (a *Army) Copy() *Army

Copy performs a deep copy of the Army.

func (*Army) IsOwnedBy

func (a *Army) IsOwnedBy(player *Player) bool

IsOwnedBy tests if the owning Player equals the provided Player.

func (*Army) Owner

func (a *Army) Owner() *Player

Owner returns the Player owning the Army.

func (*Army) SetOwner

func (a *Army) SetOwner(owner *Player)

SetOwner changes the owner of the Army to the provided Player. The provided player must not be nil, or the method will panic.

func (*Army) SetStrength

func (a *Army) SetStrength(strength int64)

SetStrength is a setter for the Army strength.

func (*Army) Strength

func (a *Army) Strength() int64

Strength is a getter for the Army strength.

type Coordinate

type Coordinate struct {
	X int
	Y int
}

Coordinate is an (x, y) coordinate, representing a position on the game "board".

func (Coordinate) IsWithin

func (c Coordinate) IsWithin(boundary Coordinate) bool

IsWithin determines if the coordinate is within a boundary, seen as a rectangle from (0,0) to the boundary x and y coordinates.

func (Coordinate) String

func (c Coordinate) String() string

String represents the string representation of a Coordinate.

func (Coordinate) ToFloatCoordinate

func (c Coordinate) ToFloatCoordinate() FloatCoordinate

ToFloatCoordinate returns a FloatCoordinate representation of the Coordinate.

type Event

type Event interface {
	// Turns the event into a PlayerEvent for the specified
	// player.
	ToPlayerEvent(playerID PlayerID) PlayerEvent
}

Event is a (partially) created event that is to be sent to a player. As an event might depend on the player it is sent to, an Event is always transformed into a PlayerEvent for each player.

type EventGameOver

type EventGameOver struct {
	WinnerID PlayerID
}

EventGameOver is the Event representing game over. The event is sent once the game is over. If the game had a winner, the player id of the winner is included in the event.

func NewEventGameOver

func NewEventGameOver(winner *Player) *EventGameOver

NewEventGameOver creates a new EventGameOver with the WinnerID of the specified player, or InvalidID as WinnerID if winner is nil.

func (*EventGameOver) ToPlayerEvent

func (evt *EventGameOver) ToPlayerEvent(_ PlayerID) PlayerEvent

ToPlayerEvent returns a PlayerEventGameOver wrapper around the EventGameOver, representing the event for the specified.

type EventGameStart

type EventGameStart struct {
	// TickInterval is the (approximate) time between each
	// tick of the game on the server side
	TickInterval time.Duration
}

EventGameStart is the game start event.

func (*EventGameStart) ToPlayerEvent

func (evt *EventGameStart) ToPlayerEvent(playerID PlayerID) PlayerEvent

ToPlayerEvent turns the EventGameStart into a player specific PlayerEventGameStart.

type EventTick

type EventTick struct {
	// Game is a copy of the game instance at the time of this tick event.
	Game *Game
}

EventTick is the game tick event.

func (*EventTick) ToPlayerEvent

func (evt *EventTick) ToPlayerEvent(playerID PlayerID) PlayerEvent

ToPlayerEvent turn the EventTick into a PlayerEventTick.

type FloatCoordinate

type FloatCoordinate struct {
	X float64
	Y float64
}

FloatCoordinate is an (x, y) coordinate, approximately representing a position on the game "board".

func (FloatCoordinate) String

func (fc FloatCoordinate) String() string

String represents the string representation of a FloatCoordinate.

func (FloatCoordinate) ToCoordinate

func (fc FloatCoordinate) ToCoordinate() Coordinate

ToCoordinate returns a Coordinate representation of the FloatCoordinate. The Coordinate values are rounded to the nearest int away from zero.

type Game

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

Game represents a game instance, and acts as a container for other game models associated to the same game instance.

func CreateDummyGameEmpty

func CreateDummyGameEmpty() *Game

CreateDummyGameEmpty creates an empty 9x9 board

func CreateDummyGameSimple

func CreateDummyGameSimple() *Game

CreateDummyGameSimple Creates a 9x9 board with 3 islands:

  • (0,0) - player 1 island
  • (8,8) - player 2 island
  • (4,4) - neutral island
  • (0,8) - neutral island

All starting with a strength of 10 and a growth interval of 1/second

func (*Game) AddAirplane

func (g *Game) AddAirplane(airplane *Airplane)

AddAirplane adds an airplane to the game instance. The method panics if the airplane being added is nil.

func (*Game) Airplanes

func (g *Game) Airplanes() []*Airplane

Airplanes returns all the airplanes in the game instance.

func (*Game) Copy

func (g *Game) Copy() *Game

Copy performs a deep copy of the game, returning the copy.

func (*Game) ID

func (g *Game) ID() GameID

ID is a getter for the id of the game.

func (*Game) Island

func (g *Game) Island(id IslandID) *Island

Island returns an island by IslandID, or nil if an island with the given id could not be found in the game instance.

func (*Game) Islands

func (g *Game) Islands() []*Island

Islands returns all the islands in the game instance.

func (*Game) Player

func (g *Game) Player(id PlayerID) *Player

Player returns a player by PlayerID, or nil if a player with the given id could not be found in the game instance. Does not include the neutral player.

func (*Game) PlayerNeutral

func (g *Game) PlayerNeutral() *Player

PlayerNeutral is a getter for the Player model representing the neutral player.

func (*Game) Players

func (g *Game) Players() []*Player

Players returns the players of the game instance. Does not include the neutral player.

func (*Game) RemoveAirplane

func (g *Game) RemoveAirplane(airplane *Airplane)

RemoveAirplane removes an airplane from the game instance. The method panics if the airplane being removed is nil. The method is a no-op if the airplane is not found in the game instance.

func (*Game) ReviveCount

func (g *Game) ReviveCount() int

ReviveCount returns how many times players have been revived this game.

func (*Game) SetReviveCount

func (g *Game) SetReviveCount(reviveCount int)

SetReviveCount sets how many times players have been revivied this game.

func (*Game) Size

func (g *Game) Size() Coordinate

Size is a getter for the size of the game.

type GameBuilder

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

GameBuilder implements a builder patter for creating a new Game instance.

func NewGameBuilder

func NewGameBuilder(size Coordinate, playerNeutral *Player) *GameBuilder

NewGameBuilder creates a new GameBuilder with initial values. NewGameBuilder panics if size is < (1, 1), or if playerNeutral is nil.

func (*GameBuilder) AddIsland

func (gb *GameBuilder) AddIsland(island *Island) *GameBuilder

AddIsland adds an island to GameBuilder, to be later included in the game. AddIsland panics if island is nil.

func (*GameBuilder) AddPlayer

func (gb *GameBuilder) AddPlayer(player *Player) *GameBuilder

AddPlayer adds a player to GameBuilder, to be later included in the game. AddPlayer panics if player is nil.

func (*GameBuilder) Build

func (gb *GameBuilder) Build() (*Game, error)

Build creates a game from the GameBuilder parameters, first validating that the provided parameters are valid.

func (*GameBuilder) BuildOrPanic

func (gb *GameBuilder) BuildOrPanic() *Game

BuildOrPanic calls Build and panics if Build returns an error.

type GameID

type GameID ID

GameID is the id type for games.

type ID

type ID string

ID is the type used as ID for all models.

func NextModelID

func NextModelID() ID

NextModelID returns a unique id.

type IllegalActionError

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

IllegalActionError is an error used to represent an action not being applied due to it being invalid. IllegalActionError should only be used for errors that are due to user input (e.g. the target of a launch action) and not for errors that would indicate an error in the game server (e.g. the playerID of the action not existing in the game)

func NewIllegalActionError

func NewIllegalActionError(err error) *IllegalActionError

NewIllegalActionError returning a new IllegalActionError wrapping the provided error, or nil if the provided error was nil.

type Island

type Island struct {
	*Army
	// contains filtered or unexported fields
}

Island represents an Island in the game. An Island has a fixed position and an army controlled by some player.

func NewIsland

func NewIsland(position Coordinate, size IslandSize, strength int64, owner *Player) (*Island, error)

NewIsland creates a new Island from the provided values, automatically choosing an ID.

func NewIslandWithID

func NewIslandWithID(id IslandID, position Coordinate, size IslandSize, strength int64, owner *Player) (*Island, error)

NewIslandWithID creats a new Island from the provided values.

func (*Island) Copy

func (i *Island) Copy() *Island

Copy performs a deep copy of the Island, returning the copy.

func (*Island) GrowthRemainder

func (i *Island) GrowthRemainder() time.Duration

GrowthRemainder returns the remaining time from the last growth until a new growth would take place.

func (*Island) ID

func (i *Island) ID() IslandID

ID returns the IslandID of the Island.

func (*Island) Position

func (i *Island) Position() Coordinate

Position returns the coordinates for the position of the Island within the game.

func (*Island) SetGrowthRemainder

func (i *Island) SetGrowthRemainder(growthRemainder time.Duration)

SetGrowthRemainder sets the remaining time before a new growth would have taken place.

func (*Island) Size

func (i *Island) Size() IslandSize

Size returns the IslandSize of the island.

type IslandID

type IslandID ID

IslandID is the id type for Islands.

type IslandSize

type IslandSize float64

IslandSize is a type for size factors of an island, between 0.0 and 1.0.

const (
	IslandSizeTiny   IslandSize = 0.4
	IslandSizeSmall  IslandSize = 0.6
	IslandSizeMedium IslandSize = 0.8
	IslandSizeLarge  IslandSize = 1
)

Different sizes of islands. These should be preferred over creating custom IslandSize values.

type Player

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

Player represents a player in the game model.

func NewPlayer

func NewPlayer() (*Player, error)

NewPlayer creates a new Player with a new generated ID.

func (*Player) Copy

func (p *Player) Copy() *Player

Copy performs a deep copy of the Player, returning the copy.

func (*Player) Equals

func (p *Player) Equals(other *Player) bool

Equals compares two players for equality. Equality is determined by the ID of each player. Always returns false if other is nil.

func (*Player) FogOfWar

func (p *Player) FogOfWar() map[Coordinate]struct{}

FogOfWar returns the set of coordinates where the player has fog of war vision.

func (*Player) ID

func (p *Player) ID() PlayerID

ID returns the PlayerID of the Player.

func (*Player) IsInFogOfWar

func (p *Player) IsInFogOfWar(c Coordinate) bool

IsInFogOfWar tests if the given Coordinate is in fog of war for the player.

func (*Player) SetFogOfWar

func (p *Player) SetFogOfWar(fogOfWar map[Coordinate]struct{})

SetFogOfWar sets the coordinates where the player has fog of war vision.

func (*Player) SetState

func (p *Player) SetState(state PlayerState)

SetState sets the current state of the player.

func (*Player) State

func (p *Player) State() PlayerState

State returns the current state of the player.

type PlayerAction

type PlayerAction interface {
	// ToAction takes a PlayerID and returns an Action that can
	// be applied to a game.
	ToAction(playerID PlayerID) Action
}

PlayerAction is an action created by some player. A PlayerAction is turned into an Action by supplying it the PlayerID that the player represents in the game model.

type PlayerActionLaunch

type PlayerActionLaunch struct {
	From IslandID
	To   IslandID
}

PlayerActionLaunch is a player action for launching an airplane from an island to another island.

func (PlayerActionLaunch) ToAction

func (act PlayerActionLaunch) ToAction(playerID PlayerID) Action

ToAction transforms the PlayerActionLaunch, and a PlayerID, to a new Action. The returned action copies the PlayerActionLaunch struct values.

type PlayerActionLeave

type PlayerActionLeave struct{}

PlayerActionLeave is a player action for leaving the game. The action results in the neutral player taking ownership of any airplanes and islands currently owned by the leaving player.

func (PlayerActionLeave) ToAction

func (act PlayerActionLeave) ToAction(playerID PlayerID) Action

ToAction returns a new leave action associated with the provided playerID

type PlayerEvent

type PlayerEvent interface {
	// PlayerEventMarker is a dummy method used as marker for
	// things that implement the PlayerEvent interface.
	PlayerEventMarker()
}

PlayerEvent is an event that is meant for a specific player. The PlayerEvent is a marker interface used instead of an empty interface to better convey the intention, and allow IDEs to find implementations. It is still necessary for the PlayerEvent to be type switched before it can be used.

type PlayerEventGameOver

type PlayerEventGameOver struct {
	*EventGameOver
}

PlayerEventGameOver is the player-specific game over event. PlayerEventGameOver is only a thin wrapper around EventGameOver, as the game over event does not contain any player-specific properties.

func (*PlayerEventGameOver) PlayerEventMarker

func (evt *PlayerEventGameOver) PlayerEventMarker()

PlayerEventMarker is the marker implementation of PlayerEvent.

type PlayerEventGameStart

type PlayerEventGameStart struct {
	*EventGameStart
	// PlayerID is the PlayerID that the receiver represents, used for
	// all subsequent events for this particular game.
	PlayerID PlayerID
}

PlayerEventGameStart is the player-specific game start event.

func (*PlayerEventGameStart) PlayerEventMarker

func (evt *PlayerEventGameStart) PlayerEventMarker()

PlayerEventMarker is the marker implementation of PlayerEvent.

type PlayerEventTick

type PlayerEventTick struct {
	// Game is a player-specific copy of the game instance at the time of
	// this tick event.
	Game *Game
}

PlayerEventTick is the player-specific game tick event.

func (*PlayerEventTick) PlayerEventMarker

func (evt *PlayerEventTick) PlayerEventMarker()

PlayerEventMarker is the marker implementation of PlayerEvent.

type PlayerID

type PlayerID ID

PlayerID is the id type for players.

type PlayerState

type PlayerState int

A PlayerState represents the current exclusive state of a Player. During a game a Player transitions between PlayerStates:

Alive <-> PendingRevival -> Dead
|             v             |
---------> LeftGame <--------
const (
	// Alive is when still alive in the game.
	Alive PlayerState = iota
	// PendingRevival is when dead, but may be revived.
	PendingRevival
	// Dead is when dead and will remain dead.
	Dead
	// LeftGame is when the Player has left the game.
	LeftGame
)

Jump to

Keyboard shortcuts

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