Documentation ¶
Index ¶
- Constants
- func IsGameOverEvent(evt Event) bool
- type Action
- type ActionCheckGameOver
- type ActionForceGameOver
- type ActionTick
- type Airplane
- func (a *Airplane) Copy() *Airplane
- func (a *Airplane) Destination() IslandID
- func (a *Airplane) Direction() float64
- func (a *Airplane) ID() AirplaneID
- func (a *Airplane) Position() FloatCoordinate
- func (a *Airplane) SetDirection(direction float64)
- func (a *Airplane) SetPosition(position FloatCoordinate)
- func (a *Airplane) SetSpeed(speed float64)
- func (a *Airplane) Speed() float64
- type AirplaneID
- type Army
- type Coordinate
- type Event
- type EventGameOver
- type EventGameStart
- type EventTick
- type FloatCoordinate
- type Game
- func (g *Game) AddAirplane(airplane *Airplane)
- func (g *Game) Airplanes() []*Airplane
- func (g *Game) Copy() *Game
- func (g *Game) ID() GameID
- func (g *Game) Island(id IslandID) *Island
- func (g *Game) Islands() []*Island
- func (g *Game) Player(id PlayerID) *Player
- func (g *Game) PlayerNeutral() *Player
- func (g *Game) Players() []*Player
- func (g *Game) RemoveAirplane(airplane *Airplane)
- func (g *Game) ReviveCount() int
- func (g *Game) SetReviveCount(reviveCount int)
- func (g *Game) Size() Coordinate
- type GameBuilder
- type GameID
- type ID
- type IllegalActionError
- type Island
- type IslandID
- type IslandSize
- type Player
- func (p *Player) Copy() *Player
- func (p *Player) Equals(other *Player) bool
- func (p *Player) FogOfWar() map[Coordinate]struct{}
- func (p *Player) ID() PlayerID
- func (p *Player) IsInFogOfWar(c Coordinate) bool
- func (p *Player) SetFogOfWar(fogOfWar map[Coordinate]struct{})
- func (p *Player) SetState(state PlayerState)
- func (p *Player) State() PlayerState
- type PlayerAction
- type PlayerActionLaunch
- type PlayerActionLeave
- type PlayerEvent
- type PlayerEventGameOver
- type PlayerEventGameStart
- type PlayerEventTick
- type PlayerID
- type PlayerState
Constants ¶
const AirplaneDefaultSpeed = 1 / float64(3*time.Second)
AirplaneDefaultSpeed is the default speed of an airplane tiles/ns.
const InvalidID = ID("INVALID_ID")
InvalidID represents an invalid model ID, guaranteed to never match the ID of any entry.
const IslandGrowthCap = 100.0
IslandGrowthCap is the army size where the island army stops growing, without factoring in the island size.
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 ¶
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.
type ActionForceGameOver ¶
type ActionForceGameOver struct { }
ActionForceGameOver is an action for triggering an unconditional GameOver event with no 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.
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) Destination ¶
Destination returns the IslandID of the airplane's target island.
func (*Airplane) Position ¶
func (a *Airplane) Position() FloatCoordinate
Position returns the current position of the airplane.
func (*Airplane) SetDirection ¶
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.
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 ¶
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) SetOwner ¶
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 ¶
SetStrength is a setter for the Army strength.
type Coordinate ¶
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 ¶
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 ¶
AddAirplane adds an airplane to the game instance. The method panics if the airplane being added is nil.
func (*Game) 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) 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 ¶
PlayerNeutral is a getter for the Player model representing the neutral player.
func (*Game) Players ¶
Players returns the players of the game instance. Does not include the neutral player.
func (*Game) RemoveAirplane ¶
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 ¶
ReviveCount returns how many times players have been revived this game.
func (*Game) SetReviveCount ¶
SetReviveCount sets how many times players have been revivied this 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 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) GrowthRemainder ¶
GrowthRemainder returns the remaining time from the last growth until a new growth would take place.
func (*Island) Position ¶
func (i *Island) Position() Coordinate
Position returns the coordinates for the position of the Island within the game.
func (*Island) SetGrowthRemainder ¶
SetGrowthRemainder sets the remaining time before a new growth would have taken place.
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 (*Player) Equals ¶
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) 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 ¶
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 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 )