game

package
v0.0.0-...-d64fcf2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TimeFormat is the time format used for storing game updates
	TimeFormat = "2006-01-02T15:04:05"
	// IdleTimeout is the timeout after which a game will get terminated automatically after inactivity
	IdleTimeout = time.Minute * 60
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Player

type Player struct {
	ID     string
	Card   messaging.CardDeckCards
	IsHost bool
}

Player represents one connected client to a game server

func NewPlayer

func NewPlayer(id string, isHost bool) *Player

NewPlayer returns a new player object

type Server

type Server struct {
	ID                    string
	Title                 string
	CardDeck              messaging.CardDeckType
	Topic                 string
	TopicLink             string
	Players               map[string]*Player
	State                 messaging.GameStatus
	Round                 int32
	HighlightedPlayers    []string
	MajorityJoinedPlayers []string
	MajorityVotedCard     messaging.CardDeckCards
	UnanimousVote         bool
	IncomingMessage       chan []byte
	OutgoingMessage       chan *messaging.Container
	RedisClient           *redis.Client
	Log                   *log.Entry
	StartTS               time.Time
	LastUpdatedTS         time.Time
	// contains filtered or unexported fields
}

Server represents an instance of a game

func NewServer

func NewServer(title string, cardDeck messaging.CardDeckType, overrideID string, outgoingMessage chan *messaging.Container, redisClient *redis.Client, metricsCollector *instrumentation.MetricsCollector, sessionsURL string) *Server

NewServer creates a new game server object

func (*Server) BroadcastAndPersistStatusUpdate

func (g *Server) BroadcastAndPersistStatusUpdate()

BroadcastAndPersistStatusUpdate broadcasts a status update

func (*Server) BroadcastKickUser

func (g *Server) BroadcastKickUser(userID string)

BroadcastKickUser broadcasts a kicked user event to the gateway

func (*Server) ChangeTopic

func (g *Server) ChangeTopic(newTopic string, newTopicLink string)

ChangeTopic changes the topic of a game and starts a fresh round

func (*Server) GetHostIDs

func (g *Server) GetHostIDs() []string

GetHostIDs returns all IDs of a host

func (*Server) GetPlayerStatus

func (g *Server) GetPlayerStatus() map[string]messaging.CardDeckCards

GetPlayerStatus returns a map containing all player states

func (*Server) HandleDisconnect

func (g *Server) HandleDisconnect(playerID string, reason string) error

HandleDisconnect handles the disconnect event of a player

func (*Server) HandleIncomingHostUpdate

func (g *Server) HandleIncomingHostUpdate(msg *messaging.Container)

HandleIncomingHostUpdate handles an imcoming update of a host

func (*Server) HandleIncomingPlayerUpdate

func (g *Server) HandleIncomingPlayerUpdate(msg *messaging.Container)

HandleIncomingPlayerUpdate handles an incoming update of a player

func (*Server) HandleNewPlayer

func (g *Server) HandleNewPlayer(userID string, hosting bool)

HandleNewPlayer creates a new player for the game and broadcasts a status update

func (*Server) LoadFromState

func (g *Server) LoadFromState() error

LoadFromState loads a game state from redis

func (*Server) PersistGameState

func (g *Server) PersistGameState() error

PersistGameState persists the current game state to redis

func (*Server) PlayerExists

func (g *Server) PlayerExists(playerName string) bool

PlayerExists determines whether a player exists in the game

func (*Server) PlayerJoinedMajorityVote

func (g *Server) PlayerJoinedMajorityVote(msg *messaging.Container) bool

PlayerJoinedMajorityVote checks whether a player joined the majority and updates the chosen card accordingly

func (*Server) ResetPlayerCards

func (g *Server) ResetPlayerCards()

ResetPlayerCards sets all player cards to the not chosen state

func (*Server) Run

func (g *Server) Run()

Run orchestrates the main game logic via incoming messages

func (*Server) Shutdown

func (g *Server) Shutdown(reason string)

Shutdown triggers the shutdown of the game server

func (*Server) UpdateGameStatus

func (g *Server) UpdateGameStatus(playerJoinedMajority bool)

UpdateGameStatus calculates the status of a game

Jump to

Keyboard shortcuts

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