Documentation ¶
Overview ¶
Package quiz defines a state machine for quiz sessions, as well as a configuration of quiz questions. It uses a mochi-co/mqtt broker to publish questions to a set of defined quiz topics.
Index ¶
Constants ¶
const ( // Name of the MQTT topic where the server posts quiz questions for clients to see. QuestionTopic string = "coffeetalk/quiz/questions" // Name of the MQTT topic where the server posts answers to quiz questions. AnswerTopic string = "coffeetalk/quiz/answers" // Name of the MQTT topic where clients post to start a quiz, // and the server posts if the quiz is ended. QuizStatusTopic string = "coffeetalk/quiz/status" // The message posted on the MQTT quiz status topic to start a quiz. QuizStartMessage string = "start-quiz" // The message posted on the MQTT quiz status topic when a quiz ends. QuizEndMessage string = "end-quiz" )
Quiz-related constants for MQTT communication.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Question ¶
type Question struct { ID int `json:"id"` Question string `json:"question"` Answer string `json:"answer"` }
Questions and corresponding answers that make up the quiz. Includes an ID to check for question uniqueness, and json tags for reading from file.
type QuizMachine ¶
type QuizMachine struct {
// contains filtered or unexported fields
}
State machine for quiz sessions. Implements stm.StateMachine.
func NewMachine ¶
func NewMachine(broker *mqtt.Server) *QuizMachine
Returns a new quiz state machine, with all states, channels and lists initialized. Attaches the given broker to the machine, and assumes it is valid to send on.
func (*QuizMachine) Run ¶
func (machine *QuizMachine) Run() error
Runs the given quiz state machine. Keeps running through every configured state function, transitioning to new states as they return, until an error occurs.
func (*QuizMachine) StartQuizHandler ¶
func (machine *QuizMachine) StartQuizHandler() events.OnMessage
Returns a handler for listening to MQTT messages. When a start message is sent on the appropriate quiz topic, triggers the Start event on the given quiz state machine.
func (*QuizMachine) States ¶
func (machine *QuizMachine) States() stm.States[*QuizMachine]
Returns the quiz machine's configured states.