controller

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2018 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Stages include all available stages, ordered

Functions

This section is empty.

Types

type ApiServer

type ApiServer struct {
	Consumer EventConsumer
	// contains filtered or unexported fields
}

func (*ApiServer) PublishState

func (a *ApiServer) PublishState(state State)

func (*ApiServer) WsHandler

func (a *ApiServer) WsHandler(w http.ResponseWriter, r *http.Request)

WsHandler handles incoming web socket connections

type CardModification

type CardModification struct {
	CardID   int           `json:"cardId"`
	TimeLeft time.Duration `json:"timeLeft"`
}

CardModification to apply to a card

type CardOperation

type CardOperation string

CardOperation on a card

const (
	// CardOperationAdd add a card
	CardOperationAdd CardOperation = "add"
	// CardOperationRevoke revoke a card
	CardOperationRevoke CardOperation = "revoke"
	// CardOperationModify modify a card
	CardOperationModify CardOperation = "modify"
)

type CardType

type CardType string

CardType is one of yellow or red

const (
	// CardTypeYellow yellow card
	CardTypeYellow CardType = "yellow"
	// CardTypeRed red card
	CardTypeRed CardType = "red"
)

type Config

type Config struct {
	Publish ConfigPublish `yaml:"publish"`
	Game    ConfigGame    `yaml:"game"`
}

Config structure for the game controller

func DefaultConfig

func DefaultConfig() (c Config)

DefaultConfig creates a config with default values

func LoadConfig

func LoadConfig(fileName string) (config Config, err error)

LoadConfig loads a config from given file

type ConfigGame

type ConfigGame struct {
	YellowCardDuration time.Duration `yaml:"yellow-card-duration"`
	Normal             ConfigSpecial `yaml:"normal"`
	Overtime           ConfigSpecial `yaml:"overtime"`
}

ConfigGame holds configs that are valid for the whole game

type ConfigPublish

type ConfigPublish struct {
	Address string `yaml:"address"`
}

ConfigPublish holds configs for publishing the state and commands to the teams

type ConfigSpecial

type ConfigSpecial struct {
	HalfDuration     time.Duration `yaml:"half-duration"`
	HalfTimeDuration time.Duration `yaml:"half-time-duration"`
	TimeoutDuration  time.Duration `yaml:"timeout-duration"`
	Timeouts         int           `yaml:"timeouts"`
	BreakAfter       time.Duration `yaml:"break-after"`
}

ConfigSpecial holds configs that are different between normal and overtime halves

type Engine

type Engine struct {
	State          *State
	MatchTimeStart time.Time
	StageTimes     map[Stage]time.Duration

	StateHistory []State
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine(config ConfigGame) (e Engine)

func (*Engine) Process

func (e *Engine) Process(event Event) (*EventCommand, error)

func (*Engine) ResetGame

func (e *Engine) ResetGame()

func (*Engine) Tick

func (e *Engine) Tick(delta time.Duration)

Tick updates the times of the state and removes cards, if necessary

func (*Engine) UndoLastAction

func (e *Engine) UndoLastAction()

UndoLastAction restores the last state from internal history

type Event

type Event struct {
	Card    *EventCard        `json:"card"`
	Command *EventCommand     `json:"command"`
	Modify  *EventModifyValue `json:"modify"`
	Stage   *EventStage       `json:"stage"`
	Trigger *EventTrigger     `json:"trigger"`
}

Event holds all possible events. Only one at a time can be applied

func (Event) String

func (e Event) String() string

type EventCard

type EventCard struct {
	ForTeam      Team             `json:"forTeam"`
	Type         CardType         `json:"cardType"`
	Operation    CardOperation    `json:"operation"`
	Modification CardModification `json:"modification"`
}

EventCard is an event that can be applied

type EventCommand

type EventCommand struct {
	ForTeam *Team      `json:"forTeam"`
	Type    RefCommand `json:"commandType"`
}

EventCommand is an event that can be applied

func (EventCommand) String

func (c EventCommand) String() string

type EventConsumer

type EventConsumer interface {
	OnNewEvent(event Event)
}

type EventModifyCardTime

type EventModifyCardTime struct {
	CardID   int    `json:"cardId"`
	Duration string `json:"duration"`
}

EventModifyCardTime holds the duration for a certain yellow card duration

type EventModifyValue

type EventModifyValue struct {
	ForTeam Team `json:"forTeam"`

	Goals           *int                 `json:"goals"`
	Goalie          *int                 `json:"goalie"`
	YellowCards     *int                 `json:"yellowCards"`
	YellowCardTime  *EventModifyCardTime `json:"yellowCardTime"`
	RedCards        *int                 `json:"redCards"`
	TimeoutsLeft    *int                 `json:"timeoutsLeft"`
	TimeoutTimeLeft *string              `json:"timeoutTimeLeft"`
	OnPositiveHalf  *bool                `json:"onPositiveHalf"`
	TeamName        *string              `json:"teamName"`
}

EventModifyValue is an event that can be applied

func (EventModifyValue) String

func (m EventModifyValue) String() string

type EventStage

type EventStage struct {
	StageOperation StageOperation `json:"stageOperation"`
}

EventStage is an event that can be applied

type EventTrigger

type EventTrigger struct {
	Type TriggerType `json:"triggerType"`
}

EventTrigger is an event that can be applied

type GameController

type GameController struct {
	Config Config

	Publisher Publisher
	ApiServer ApiServer
	Engine    Engine
	// contains filtered or unexported fields
}

GameController controls a game

func NewGameController

func NewGameController() (r *GameController)

NewGameController creates a new RefBox

func (*GameController) OnNewEvent

func (r *GameController) OnNewEvent(event Event)

func (*GameController) Run

func (r *GameController) Run() (err error)

Run the GameController by starting an endless loop in the background

type GameState

type GameState string

GameState of a game

const (
	// GameStateHalted halted
	GameStateHalted GameState = "Halted"
	// GameStateStopped stopped
	GameStateStopped GameState = "Stopped"
	// GameStateRunning running
	GameStateRunning GameState = "Running"
	// GameStatePreKickoff kickoff
	GameStatePreKickoff GameState = "Prepare Kickoff"
	// GameStatePrePenalty penalty
	GameStatePrePenalty GameState = "Prepare Penalty"
	// GameStateTimeout timeout
	GameStateTimeout GameState = "Timeout"
	// GameStateBallPlacement ball placement
	GameStateBallPlacement GameState = "Ball Placement"
)

type Publisher

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

Publisher can publish state and commands to the teams

func NewPublisher

func NewPublisher(address string) (publisher Publisher, err error)

NewPublisher creates a new publisher that publishes referee messages via UDP to the teams

func (*Publisher) Publish

func (p *Publisher) Publish(state *State, command *EventCommand)

Publish the state and command

type RefCommand

type RefCommand string

RefCommand is a command to be send to the teams

const (
	// CommandHalt HALT
	CommandHalt RefCommand = "halt"
	// CommandStop STOP
	CommandStop RefCommand = "stop"
	// CommandNormalStart NORMAL_START
	CommandNormalStart RefCommand = "normalStart"
	// CommandForceStart FORCE_START
	CommandForceStart RefCommand = "forceStart"
	// CommandDirect DIRECT
	CommandDirect RefCommand = "direct"
	// CommandIndirect INDIRECT
	CommandIndirect RefCommand = "indirect"
	// CommandKickoff KICKOFF
	CommandKickoff RefCommand = "kickoff"
	// CommandPenalty PENALTY
	CommandPenalty RefCommand = "penalty"
	// CommandTimeout TIMEOUT
	CommandTimeout RefCommand = "timeout"
	// CommandBallPlacement BALL_PLACEMENT
	CommandBallPlacement RefCommand = "ballPlacement"
	// CommandGoal GOAL
	CommandGoal RefCommand = "goal"
)

type Stage

type Stage string

Stage represents the different stages of a game

const (
	// StagePreGame before game has started
	StagePreGame Stage = "Pre-First Half"
	// StageFirstHalf in first half
	StageFirstHalf Stage = "First Half"
	// StageHalfTime in half time
	StageHalfTime Stage = "Half Time"
	// StageSecondHalfPre before second half
	StageSecondHalfPre Stage = "Pre-Second Half"
	// StageSecondHalf in second half
	StageSecondHalf Stage = "Second Half"
	// StageOvertimeBreak in break to overtime
	StageOvertimeBreak Stage = "Overtime Break"
	// StageOvertimeFirstHalfPre before first overtime half
	StageOvertimeFirstHalfPre Stage = "Pre-Overtime First Half"
	// StageOvertimeFirstHalf in first overtime half
	StageOvertimeFirstHalf Stage = "Overtime First Half"
	// StageOvertimeHalfTime in overtime half time
	StageOvertimeHalfTime Stage = "Overtime Half Time"
	// StageOvertimeSecondHalfPre before second overtime half
	StageOvertimeSecondHalfPre Stage = "Pre-Overtime Second Half"
	// StageOvertimeSecondHalf in second overtime half
	StageOvertimeSecondHalf Stage = "Overtime Second Half"
	// StageShootoutBreak in break to shootout
	StageShootoutBreak Stage = "Shootout Break"
	// StageShootout in Shootout
	StageShootout Stage = "Shootout"
	// StagePostGame after game ended
	StagePostGame Stage = "End of Game"
)

func (Stage) IsPreStage

func (s Stage) IsPreStage() bool

func (Stage) Next

func (s Stage) Next() Stage

func (Stage) Previous

func (s Stage) Previous() Stage

type StageOperation

type StageOperation string

StageOperation to apply on the current stage

const (
	// StageNext next stage
	StageNext StageOperation = "next"
	// StagePrevious previous stage
	StagePrevious StageOperation = "previous"
)

type State

type State struct {
	Stage            Stage              `json:"stage"`
	GameState        GameState          `json:"gameState"`
	GameStateFor     *Team              `json:"gameStateForTeam"`
	StageTimeElapsed time.Duration      `json:"gameTimeElapsed"`
	StageTimeLeft    time.Duration      `json:"gameTimeLeft"`
	MatchDuration    time.Duration      `json:"matchDuration"`
	TeamState        map[Team]*TeamInfo `json:"teamState"`
}

State of the game

func NewState

func NewState() (s *State)

NewState creates a new state, initialized for the start of a new game

func (State) String

func (s State) String() string

type Team

type Team string

Team is one of Yellow or Blue

const (
	// TeamYellow is the yellow team
	TeamYellow Team = "Yellow"
	// TeamBlue is the blue team
	TeamBlue Team = "Blue"
)

func (Team) Opposite

func (t Team) Opposite() Team

Opposite returns the other team if the team is not Yellow or Blue, return the same team

func (Team) Unknown

func (t Team) Unknown() bool

Unknown returns true if the team is not blue or yellow

type TeamInfo

type TeamInfo struct {
	Name            string          `json:"name"`
	Goals           int             `json:"goals"`
	Goalie          int             `json:"goalie"`
	YellowCards     int             `json:"yellowCards"`
	YellowCardTimes []time.Duration `json:"yellowCardTimes"`
	RedCards        int             `json:"redCards"`
	TimeoutsLeft    int             `json:"timeoutsLeft"`
	TimeoutTimeLeft time.Duration   `json:"timeoutTimeLeft"`
	OnPositiveHalf  bool            `json:"onPositiveHalf"`
}

TeamInfo about a team

func (TeamInfo) String

func (t TeamInfo) String() string

type TriggerType

type TriggerType string

TriggerType is something that can be triggered

const (
	// TriggerResetMatch reset match
	TriggerResetMatch TriggerType = "resetMatch"
	// TriggerSwitchColor switch color
	TriggerSwitchColor TriggerType = "switchColor"
	// TriggerSwitchSides switch sides/goals (onPositiveHalf)
	TriggerSwitchSides TriggerType = "switchSides"
	// TriggerUndo undo last action
	TriggerUndo TriggerType = "undo"
)

Jump to

Keyboard shortcuts

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