controller

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 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) PublishRefereeEvents added in v0.6.0

func (a *ApiServer) PublishRefereeEvents(events []RefereeEvent)

func (*ApiServer) PublishState

func (a *ApiServer) PublishState(state State)

func (*ApiServer) PublishWrapper added in v0.3.0

func (a *ApiServer) PublishWrapper(wrapper MessageWrapper)

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
	RefereeEvents []RefereeEvent
	StageTimes    map[Stage]time.Duration

	TimeProvider func() time.Time
	History      History
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine(config ConfigGame) (e Engine)

func (*Engine) Continue added in v0.4.0

func (e *Engine) Continue() error

func (*Engine) LogCard added in v0.6.0

func (e *Engine) LogCard(card *EventCard)

func (*Engine) LogCommand added in v0.3.0

func (e *Engine) LogCommand()

func (*Engine) LogGameEvent added in v0.3.0

func (e *Engine) LogGameEvent()

func (*Engine) LogStage added in v0.6.0

func (e *Engine) LogStage(stage Stage)

func (*Engine) LogTime added in v0.6.0

func (e *Engine) LogTime(description string, forTeam Team)

func (*Engine) Process

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

func (*Engine) ResetGame

func (e *Engine) ResetGame()

func (*Engine) SendCommand added in v0.4.0

func (e *Engine) SendCommand(command RefCommand, forTeam Team)

func (*Engine) SendGameEvent added in v0.4.0

func (e *Engine) SendGameEvent(gameEvent GameEventType, forTeam Team)

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"`
	GameEvent *EventGameEvent   `json:"gameEvent"`
}

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 EventGameEvent added in v0.4.0

type EventGameEvent struct {
	BallLeftFieldTouchLine *EventGameEventBallLeftField `json:"ballLeftFieldTouchLine"`
	BallLeftFieldGoalLine  *EventGameEventBallLeftField `json:"ballLeftFieldGoalLine"`
}

func (EventGameEvent) String added in v0.4.0

func (e EventGameEvent) String() string

type EventGameEventBallLeftField added in v0.4.0

type EventGameEventBallLeftField struct {
	Team Team `json:"team"`
}

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"`
	BotCollisions         *int                 `json:"botCollisions"`
	BallPlacementFailures *int                 `json:"ballPlacementFailures"`
	BotSpeedInfringements *int                 `json:"botSpeedInfringements"`
}

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 (c *GameController) OnNewEvent(event Event)

func (*GameController) Run

func (c *GameController) Run()

Run the GameController by starting an endless loop in the background

type GameEventType added in v0.3.0

type GameEventType string
const (
	GameEventNone                   GameEventType = "none"
	GameEventBallLeftFieldTouchLine GameEventType = "ballLeftFieldTouchLine"
	GameEventBallLeftFieldGoalLine  GameEventType = "ballLeftFieldGoalLine"
)

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 History added in v0.3.0

type History []HistoryEntry

type HistoryEntry added in v0.3.0

type HistoryEntry struct {
	State         State
	RefereeEvents []RefereeEvent
}

type HistoryPreserver added in v0.3.0

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

func (*HistoryPreserver) Close added in v0.3.0

func (r *HistoryPreserver) Close()

Close closes the history file

func (*HistoryPreserver) Load added in v0.3.0

func (r *HistoryPreserver) Load() (*History, error)

Load loads the history from the filesystem

func (*HistoryPreserver) Open added in v0.3.0

func (r *HistoryPreserver) Open() error

Open opens the history file

func (*HistoryPreserver) Save added in v0.3.0

func (r *HistoryPreserver) Save(history History)

Save writes the current state into a file

type MessageWrapper added in v0.3.0

type MessageWrapper struct {
	State         *State          `json:"state"`
	RefereeEvents *[]RefereeEvent `json:"gameEvents"`
}

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)

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"
)

func (RefCommand) RunningState added in v0.4.0

func (c RefCommand) RunningState() bool

type RefereeEvent added in v0.3.0

type RefereeEvent struct {
	Timestamp   time.Time        `json:"timestamp"`
	StageTime   time.Duration    `json:"stageTime"`
	Type        RefereeEventType `json:"type"`
	Name        string           `json:"name"`
	Team        Team             `json:"team"`
	Description string           `json:"description"`
}

type RefereeEventType added in v0.3.0

type RefereeEventType string
const (
	RefereeEventCommand   RefereeEventType = "command"
	RefereeEventStage     RefereeEventType = "stage"
	RefereeEventCard      RefereeEventType = "card"
	RefereeEventTime      RefereeEventType = "time"
	RefereeEventGameEvent RefereeEventType = "gameEvent"
)

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) IsPausedStage added in v0.6.0

func (s Stage) IsPausedStage() bool

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"
	// StageEndGame ends the game
	StageEndGame StageOperation = "endGame"
)

type State

type State struct {
	Stage            Stage              `json:"stage"`
	Command          RefCommand         `json:"command"`
	CommandFor       Team               `json:"commandForTeam"`
	GameEvent        GameEventType      `json:"gameEvent"`
	GameEventFor     Team               `json:"gameEventForTeam"`
	StageTimeElapsed time.Duration      `json:"stageTimeElapsed"`
	StageTimeLeft    time.Duration      `json:"stageTimeLeft"`
	MatchTimeStart   time.Time          `json:"matchTimeStart"`
	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) GameState

func (s State) GameState() GameState

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) Known added in v0.4.0

func (t Team) Known() bool

Known returns true if the team is blue or yellow

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"`
	BotCollisions         int             `json:"botCollisions"`
	BallPlacementFailures int             `json:"ballPlacementFailures"`
	BotSpeedInfringements int             `json:"botSpeedInfringements"`
}

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"
	// TriggerContinue continues based on the current game event
	TriggerContinue TriggerType = "continue"
)

Jump to

Keyboard shortcuts

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