core

package
v0.0.0-...-383283c Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScenarioNewQuestion  = "NEW_QUESTION"
	ScenarioSubmitAnswer = "SUBMIT_ANSWER"
	ScenarioGameOver     = "GAME_OVER"
)

Variables

View Source
var (
	ErrGameNotFound    = errors.New("game is not found")
	ErrInvalidScenario = errors.New("invalid scenario")
	ErrInvalidAPIKey   = errors.New("invalid api key")
)

Functions

This section is empty.

Types

type Auth

type Auth interface {
	ValidateAPIKey(ctx context.Context, apiKey string) error
}

func NewAuth

func NewAuth(cfg AuthConfig) (Auth, error)

type AuthConfig

type AuthConfig struct {
	APIKey string `validate:"nonzero"`
}

func (AuthConfig) Validate

func (c AuthConfig) Validate() error

type Clock

type Clock interface {
	// Now returns current time.
	Now() time.Time
}

type Game

type Game struct {
	GameID          string
	PlayerName      string
	Scenario        string
	Score           int
	CountCorrect    int
	CurrentQuestion *TimedQuestion
}

type GameStorage

type GameStorage interface {
	// PutGame is used for putting the game instance on storage. If game is already
	// exists on storage, it will be overwritten.
	PutGame(ctx context.Context, g Game) error

	// GetGame returns game instance stored on storage. Returns nil when game is not
	// found on storage.
	GetGame(ctx context.Context, gameID string) (*Game, error)
}

type NewGameInput

type NewGameInput struct {
	PlayerName string `validate:"nonzero"`
}

func (NewGameInput) Validate

func (i NewGameInput) Validate() error

type NewGameOutput

type NewGameOutput struct {
	GameID     string `json:"game_id"`
	PlayerName string `json:"player_name"`
	Scenario   string `json:"scenario"`
}

type NewQuestionInput

type NewQuestionInput struct {
	GameID string `validate:"nonzero"`
}

func (NewQuestionInput) Validate

func (i NewQuestionInput) Validate() error

type NewQuestionOutput

type NewQuestionOutput struct {
	GameID   string   `json:"game_id"`
	Scenario string   `json:"scenario"`
	Problem  string   `json:"problem"`
	Choices  []string `json:"choices"`
	Timeout  int      `json:"timeout"`
}

type Question

type Question struct {
	Problem      string   `json:"problem"`
	Choices      []string `json:"choices"`
	CorrectIndex int      `json:"correct_idx"`
}

type QuestionStorage

type QuestionStorage interface {
	// GetRandomQuestion returns random question from storage.
	GetRandomQuestion(ctx context.Context) (*Question, error)
}

type Scenario

type Scenario string

type Service

type Service interface {
	NewGame(ctx context.Context, input NewGameInput) (*NewGameOutput, error)
	NewQuestion(ctx context.Context, input NewQuestionInput) (*NewQuestionOutput, error)
	SubmitAnswer(ctx context.Context, input SubmitAnswerInput) (*SubmitAnswerOutput, error)
}

func NewService

func NewService(cfg ServiceConfig) (Service, error)

type ServiceConfig

type ServiceConfig struct {
	GameStorage       GameStorage       `validate:"nonnil"`
	QuestionStorage   QuestionStorage   `validate:"nonnil"`
	TimeoutCalculator TimeoutCalculator `validate:"nonnil"`
	Clock             Clock             `validate:"nonnil"`
	AddScore          int               `validate:"min=1"`
}

func (ServiceConfig) Validate

func (c ServiceConfig) Validate() error

type SubmitAnswerInput

type SubmitAnswerInput struct {
	GameID    string `validate:"nonzero"`
	AnswerIdx int    `validate:"min=0"`
	StartAt   int64  `validate:"nonzero"`
	SentAt    int64  `validate:"nonzero"`
}

func (SubmitAnswerInput) Validate

func (i SubmitAnswerInput) Validate() error

type SubmitAnswerOutput

type SubmitAnswerOutput struct {
	GameID       string `json:"game_id"`
	Scenario     string `json:"scenario"`
	AnswerIndex  int    `json:"answer_idx"`
	CorrectIndex int    `json:"correct_idx"`
	Duration     int    `json:"duration"`
	Timeout      int    `json:"timeout"`
	Score        int    `json:"score"`
}

type TimedQuestion

type TimedQuestion struct {
	Question
	Timeout int
}

type TimeoutCalculator

type TimeoutCalculator interface {
	// CalcTimeout returns proper duration to answer question for given number
	// of correct answers.
	CalcTimeout(ctx context.Context, countCorrect int) int
}

Jump to

Keyboard shortcuts

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