common

package
v0.0.0-...-bd4b760 Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Game states:
	// * The game starts off as GameNotStarted
	// * When the host starts the game, the state shifts to QuestionInProgress
	// * When the time is up or if all players have answered, the state shifts
	//   to Show Results
	// * When the host advances to the next question, the state shifts back to
	//   QuestionInProgress
	// * When the time is up or if all players have answered, the state shifts
	//   to Show Results
	// * After all the questions have been answered and the last result is
	//   shown, the state shifts to GameEnded - the UI can then show the
	//   the results of the game (the winners list)
	// * After that, the game can be deleted
	//
	GameNotStarted     = iota
	QuestionInProgress = iota
	ShowResults        = iota
	GameEnded          = iota
)

Variables

This section is empty.

Functions

func ConvertToJSON

func ConvertToJSON(input interface{}) (string, error)

Types

type AddPlayerToGameMessage

type AddPlayerToGameMessage struct {
	Sessionid string
	Name      string
	Pin       int
}

type AddQuizMessage

type AddQuizMessage struct {
	Quiz   Quiz
	Result chan error
}

type AnswersUpdate

type AnswersUpdate struct {
	AllAnswered  bool  `json:"allanswered"`
	Answered     int   `json:"answered"`
	TotalPlayers int   `json:"totalplayers"`
	Votes        []int `json:"votes"`
	TotalVotes   int   `json:"totalvotes"`
}

To be sent to the host when a player answers a question

type BindGameToSessionMessage

type BindGameToSessionMessage struct {
	Sessionid string
	Name      string
	Pin       int
}

type CancelGameMessage

type CancelGameMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type ClientErrorMessage

type ClientErrorMessage struct {
	Clientid   uint64
	Sessionid  string
	Message    string
	Nextscreen string
}

type ClientMessage

type ClientMessage struct {
	Clientid uint64
	Message  string
}

type DeleteGameByPin

type DeleteGameByPin struct {
	Pin int
}

used by REST API

type DeleteGameMessage

type DeleteGameMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

used by frontend

type DeleteQuizMessage

type DeleteQuizMessage struct {
	Quizid int
}

type DeleteSessionMessage

type DeleteSessionMessage struct {
	Sessionid string
}

type DeregisterClientMessage

type DeregisterClientMessage struct {
	Clientid uint64
}

type DeregisterGameFromSessionsMessage

type DeregisterGameFromSessionsMessage struct {
	Sessions []string
}

type ErrorToSessionMessage

type ErrorToSessionMessage struct {
	Sessionid  string
	Message    string
	Nextscreen string
}

type ExtendSessionExpiryMessage

type ExtendSessionExpiryMessage struct {
	Sessionid string
}

type Game

type Game struct {
	Pin              int                 `json:"pin"`
	Host             string              `json:"host"`    // session ID of game host
	Players          map[string]int      `json:"players"` // scores of players
	PlayerNames      map[string]string   `json:"playernames"`
	Quiz             Quiz                `json:"quiz"`
	QuestionIndex    int                 `json:"questionindex"`    // current question
	QuestionDeadline time.Time           `json:"questiondeadline"` // answers must come in at this time or before
	PlayersAnswered  map[string]struct{} `json:"playersanswered"`
	CorrectPlayers   map[string]struct{} `json:"correctplayers"` // players that answered current question correctly
	Votes            []int               `json:"votes"`          // number of players that answered each choice
	GameState        int                 `json:"gamestate"`
}

func UnmarshalGame

func UnmarshalGame(b []byte) (*Game, error)

func (*Game) AddPlayer

func (g *Game) AddPlayer(sessionid, name string) bool

Returns true if the player was added - false if the player is already in the game

func (*Game) Copy

func (g *Game) Copy() Game

func (*Game) DeletePlayer

func (g *Game) DeletePlayer(sessionid string)

func (*Game) GetCurrentQuestion

func (g *Game) GetCurrentQuestion() (bool, GameCurrentQuestion, error)

Returns true if state was changed

func (*Game) GetGameState

func (g *Game) GetGameState() int

func (*Game) GetPlayerNames

func (g *Game) GetPlayerNames() []string

func (*Game) GetPlayers

func (g *Game) GetPlayers() []string

func (*Game) GetQuestionResults

func (g *Game) GetQuestionResults() (QuestionResults, error)

func (*Game) GetWinners

func (g *Game) GetWinners() []PlayerScore

func (*Game) Marshal

func (g *Game) Marshal() ([]byte, error)

func (*Game) NameExistsInGame

func (g *Game) NameExistsInGame(name string) bool

name should be trimmed of leading and trailing spaces

func (*Game) NextState

func (g *Game) NextState() (int, error)

func (*Game) RegisterAnswer

func (g *Game) RegisterAnswer(sessionid string, answerIndex int) (bool, AnswersUpdate, error)

Returns true if changed

func (*Game) SetQuiz

func (g *Game) SetQuiz(quiz Quiz)

func (*Game) ShowResults

func (g *Game) ShowResults() error

type GameCurrentQuestion

type GameCurrentQuestion struct {
	QuestionIndex  int      `json:"questionindex"`
	TimeLeft       int      `json:"timeleft"`
	Answered       int      `json:"answered"`     // number of players that have answered
	TotalPlayers   int      `json:"totalplayers"` // number of players in this game
	Question       string   `json:"question"`
	Answers        []string `json:"answers"`
	Votes          []int    `json:"votes"`
	TotalVotes     int      `json:"totalvotes"`
	TotalQuestions int      `json:"totalquestions"`
}

Queried by the host - either when the host first displays the question or when the host reconnects

type GetGameMessage

type GetGameMessage struct {
	Pin    int
	Result chan GetGameResult
}

type GetGameResult

type GetGameResult struct {
	Game  Game
	Error error
}

type GetGamesMessage

type GetGamesMessage struct {
	Result chan []Game
}

type GetQuizMessage

type GetQuizMessage struct {
	Quizid int
	Result chan GetQuizResult
}

type GetQuizResult

type GetQuizResult struct {
	Quiz  Quiz
	Error error
}

type GetQuizzesMessage

type GetQuizzesMessage struct {
	Result chan []Quiz
}

type GetSessionMessage

type GetSessionMessage struct {
	Sessionid string
	Result    chan *Session
}

type GetSessionsMessage

type GetSessionsMessage struct {
	Result chan []Session
}

type HostGameLobbyMessage

type HostGameLobbyMessage struct {
	Clientid  uint64
	Sessionid string
	Quizid    int
}

type HostShowGameResultsMessage

type HostShowGameResultsMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type HostShowQuestionMessage

type HostShowQuestionMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type LookupQuizForGameMessage

type LookupQuizForGameMessage struct {
	Clientid  uint64
	Sessionid string
	Quizid    int
	Pin       int
}

type NameExistsInGameError

type NameExistsInGameError struct {
	Pin  int
	Name string
}

func NewNameExistsInGameError

func NewNameExistsInGameError(name string, pin int) *NameExistsInGameError

func (*NameExistsInGameError) Error

func (e *NameExistsInGameError) Error() string

type NextQuestionMessage

type NextQuestionMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type NoSuchGameError

type NoSuchGameError struct {
	Pin int
}

func NewNoSuchGameError

func NewNoSuchGameError(pin int) *NoSuchGameError

func (*NoSuchGameError) Error

func (e *NoSuchGameError) Error() string

type PlayerScore

type PlayerScore struct {
	Name  string `json:"name"`
	Score int    `json:"score"`
	// contains filtered or unexported fields
}

type PlayerScoreList

type PlayerScoreList []PlayerScore

func (PlayerScoreList) Len

func (p PlayerScoreList) Len() int

func (PlayerScoreList) Less

func (p PlayerScoreList) Less(i, j int) bool

func (PlayerScoreList) Swap

func (p PlayerScoreList) Swap(i, j int)

type QueryDisplayChoicesMessage

type QueryDisplayChoicesMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type QueryHostResultsMessage

type QueryHostResultsMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type QueryPlayerResultsMessage

type QueryPlayerResultsMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type QuestionResults

type QuestionResults struct {
	QuestionIndex  int           `json:"questionindex"`
	Question       string        `json:"question"`
	Answers        []string      `json:"answers"`
	Correct        int           `json:"correct"`
	Votes          []int         `json:"votes"`
	TotalVotes     int           `json:"totalvotes"`
	TotalQuestions int           `json:"totalquestions"`
	TotalPlayers   int           `json:"totalplayers"`
	TopScorers     []PlayerScore `json:"topscorers"`
}

type Quiz

type Quiz struct {
	Id               int            `json:"id"`
	Name             string         `json:"name"`
	QuestionDuration int            `json:"questionDuration"`
	ShuffleQuestions bool           `json:"shuffleQuestions"`
	ShuffleAnswers   bool           `json:"shuffleAnswers"`
	Questions        []QuizQuestion `json:"questions"`
}

func UnmarshalQuiz

func UnmarshalQuiz(r io.Reader) (Quiz, error)

Ingests a single Quiz object in JSON

func UnmarshalQuizzes

func UnmarshalQuizzes(r io.Reader) ([]Quiz, error)

Ingests an array of Quiz objects in JSON

func (Quiz) GetQuestion

func (q Quiz) GetQuestion(i int) (QuizQuestion, error)

func (Quiz) Marshal

func (q Quiz) Marshal() ([]byte, error)

func (Quiz) NumQuestions

func (q Quiz) NumQuestions() int

func (*Quiz) Shuffle

func (q *Quiz) Shuffle()

Shuffle questions

type QuizQuestion

type QuizQuestion struct {
	Question string   `json:"question"`
	Answers  []string `json:"answers"`
	Correct  int      `json:"correct"`
}

func (QuizQuestion) NumAnswers

func (q QuizQuestion) NumAnswers() int

func (QuizQuestion) ShuffleAnswers

func (q QuizQuestion) ShuffleAnswers() QuizQuestion

func (QuizQuestion) String

func (q QuizQuestion) String() string

type RegisterAnswerMessage

type RegisterAnswerMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
	Answer    int
}

type SendGameMetadataMessage

type SendGameMetadataMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type SendQuizzesToClientMessage

type SendQuizzesToClientMessage struct {
	Clientid  uint64
	Sessionid string
}

type Session

type Session struct {
	Id       string    `json:"id"`
	ClientId uint64    `json:"clientid"`
	Screen   string    `json:"screen"`
	Gamepin  int       `json:"gamepin"`
	Name     string    `json:"name"`
	Admin    bool      `json:"admin"`
	Expiry   time.Time `json:"expiry"`
}

func UnmarshalSession

func UnmarshalSession(b []byte) (*Session, error)

func (*Session) Copy

func (s *Session) Copy() Session

func (Session) Marshal

func (s Session) Marshal() ([]byte, error)

type SessionMessage

type SessionMessage struct {
	Sessionid string
	Message   string
}

type SessionToScreenMessage

type SessionToScreenMessage struct {
	Sessionid  string
	Nextscreen string
}

type SetQuizForGameMessage

type SetQuizForGameMessage struct {
	Pin  int
	Quiz Quiz
}

type SetSessionGamePinMessage

type SetSessionGamePinMessage struct {
	Sessionid string
	Pin       int
}

type SetSessionScreenMessage

type SetSessionScreenMessage struct {
	Sessionid  string
	Nextscreen string
}

type ShowResultsMessage

type ShowResultsMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type StartGameMessage

type StartGameMessage struct {
	Clientid  uint64
	Sessionid string
	Pin       int
}

type UnexpectedStateError

type UnexpectedStateError struct {
	CurrentState int
	Err          error
}

func NewUnexpectedStateError

func NewUnexpectedStateError(state int, message string) *UnexpectedStateError

func (*UnexpectedStateError) Error

func (e *UnexpectedStateError) Error() string

type UpdateGameMessage

type UpdateGameMessage struct {
	Game
}

type UpdateQuizMessage

type UpdateQuizMessage struct {
	Quiz   Quiz
	Result chan error
}

Jump to

Keyboard shortcuts

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