spellingbee

package
v0.0.0-...-fab11af Latest Latest
Warning

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

Go to latest
Published: May 4, 2020 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StateTTL = 4 * time.Hour

StateTTL determines how long a particular crossword's solve state should remain in redis in the absence of any activity.

Functions

func AddAnswer

func AddAnswer(pool *redis.Pool, registry *pubsub.Registry) http.HandlerFunc

AddAnswer applies an answer to the puzzle solve.

func ForceErrorDuringPuzzleLoad

func ForceErrorDuringPuzzleLoad(t *testing.T, err error)

ForceErrorDuringLoad sets up an error to be returned when an attempt is made to load a puzzle.

func ForceErrorDuringSettingsLoad

func ForceErrorDuringSettingsLoad(t *testing.T, err error)

ForceErrorDuringSettingsLoad sets up an error to be returned when an attempt is made to load settings.

func ForceErrorDuringSettingsSave

func ForceErrorDuringSettingsSave(t *testing.T, err error)

ForceErrorDuringSettingsSave sets up an error to be returned when an attempt is made to save settings.

func ForceErrorDuringStateLoad

func ForceErrorDuringStateLoad(t *testing.T, err error)

ForceErrorDuringStateLoad sets up an error to be returned when an attempt is made to load state.

func ForceErrorDuringStateSave

func ForceErrorDuringStateSave(t *testing.T, err error)

ForceErrorDuringStateSave sets up an error to be returned when an attempt is made to save state.

func ForcePuzzleToBeLoaded

func ForcePuzzleToBeLoaded(t *testing.T, filename string)

ForcePuzzleToBeLoaded sets up a cached version of a puzzle using a file from the testdata directory.

func GetAllChannels

func GetAllChannels(conn redis.Conn) ([]model.Channel, error)

GetAllChannels returns a slice of model.Channel instances for each spelling bee that contains state in the database. If there are no active channels then an empty slice is returned. This method does not update the expiration times of any state instance.

func GetEvents

func GetEvents(pool *redis.Pool, registry *pubsub.Registry) http.HandlerFunc

GetEvents establishes an event stream with a client. An event stream is server side event stream (SSE) with a client's browser that allows one way communication from the server to the client. Clients that call into this handler will keep an open connection open to the server waiting to receive events as JSON objects. The server can send events to all clients of a channel using the pubsub.Registry's Publish method.

func InferLetters

func InferLetters(words []string) (string, []string, error)

InferLetters looks at the list of words and determines which letter is the center letter (because it's used in every word) and which letters are the surrounding letters. If for some reason the letters can't be inferred then an error will be returned.

func NewEventSubscription

func NewEventSubscription(t *testing.T, registry *pubsub.Registry, channel string) <-chan pubsub.Event

NewEventSubscription will return a channel of events that are subscribed to the specified channel. The subscription will be configured to automatically unsubscribe when the test completes.

func NewRedisConnection

func NewRedisConnection(t *testing.T, pool *redis.Pool) redis.Conn

NewRedisConnection will return a connection to the provided connection pool. The returned connection will be configured to automatically close when the test completes.

func NewTestRouter

func NewTestRouter(t *testing.T) (chi.Router, *redis.Pool, *pubsub.Registry)

NewTestRouter will return a router configured with a redis pool and pubsub registry and wired together along with all of the routes for a spelling bee puzzle.

func RegisterRoutes

func RegisterRoutes(router chi.Router, pool *redis.Pool)

func RegisterRoutesWithRegistry

func RegisterRoutesWithRegistry(r chi.Router, pool *redis.Pool, registry *pubsub.Registry)

func SetSettings

func SetSettings(conn redis.Conn, channel string, settings Settings) error

SetSettings will write settings for the provided channel name. If the settings can't be properly written then an error will be returned.

func SetState

func SetState(conn redis.Conn, channel string, state State) error

SetState writes the state for a channel's spelling bee solve to redis. If the state can't be property written then an error will be returned.

func SettingsKey

func SettingsKey(name string) string

SettingsKey returns the key that should be used in redis to store a particular channel's settings.

func ShuffleLetters

func ShuffleLetters(pool *redis.Pool, registry *pubsub.Registry) http.HandlerFunc

ShuffleLetters changes the order of the letters in the puzzle.

func StateKey

func StateKey(name string) string

StateKey returns the key that should be used in redis to store a particular spelling bee solve's state.

func ToggleStatus

func ToggleStatus(pool *redis.Pool, registry *pubsub.Registry) http.HandlerFunc

ToggleStatus changes the status of the current puzzle solve to a new status. This effectively toggles between the solving and paused statuses as long as the solve is in a state that can be paused or resumed.

func UpdatePuzzle

func UpdatePuzzle(pool *redis.Pool, registry *pubsub.Registry) http.HandlerFunc

UpdatePuzzle changes the spelling bee puzzle that's currently being solved for a channel.

func UpdateSetting

func UpdateSetting(pool *redis.Pool, registry *pubsub.Registry) http.HandlerFunc

UpdateSetting changes a specified spelling bee setting to a new value.

Types

type Puzzle

type Puzzle struct {
	// The date that the spelling bee puzzle was published.
	PublishedDate time.Time `json:"published"`

	// The center letter of the spelling bee puzzle.  This letter must be used in
	// every answer.
	CenterLetter string `json:"center"`

	// The non-center letters in the spelling bee puzzle.  Each entry will
	// always be a single letter long and there will always be 6 entries.
	Letters []string `json:"letters"`

	// The list of official answers from The New York Times.
	OfficialAnswers []string `json:"official_answers,omitempty"`

	// The list of unofficial answers from NYTBee.com.
	UnofficialAnswers []string `json:"unofficial_answers,omitempty"`
}

Puzzle represents a spelling bee puzzle. The puzzle is comprised of a circular grid of 6 letters around a single letter. The goal is to use the letters to make words that all use the center letter and have length 4 or greater. Letters may be reused.

func InferPuzzle

func InferPuzzle(official, unofficial []string, unofficialRequired bool) (*Puzzle, error)

InferPuzzle looks at the list of words and determines the puzzle structure from them. In addition it verifies that the provided words have a valid structure for the puzzle.

func LoadFromNYTBee

func LoadFromNYTBee(date string) (*Puzzle, error)

LoadFromNYTBee loads a spelling bee puzzle from the NYTBee website for a particular date.

This method loads the HTML of the spelling bee page for a desired date and parses it to obtain the answer list(s) from it.

If the puzzle cannot be loaded or the HTML properly parsed then an error is returned.

func LoadTestPuzzle

func LoadTestPuzzle(t *testing.T, filename string) *Puzzle

LoadTestPuzzle loads a puzzle from the testdata directory.

func ParseNYTBeeResponse

func ParseNYTBeeResponse(in io.Reader) (*Puzzle, error)

ParseNYTBeeResponse converts an HTML page from nytbee.com into a puzzle object.

func (*Puzzle) WithoutAnswers

func (p *Puzzle) WithoutAnswers() *Puzzle

WithoutAnswers returns a copy of the puzzle that has the answers removed. This makes the resulting puzzle suitable to pass to a client that shouldn't know the answers to the puzzle.

type Settings

type Settings struct {
	// When enabled unofficial answers will be allowed.
	AllowUnofficialAnswers bool `json:"allow_unofficial_answers"`

	// What font size words should be rendered with.
	FontSize model.FontSize `json:"font_size"`
}

Settings represents the optional behaviors that can be enabled or disabled by a streamer for their channel's spelling bee solves.

func GetSettings

func GetSettings(conn redis.Conn, channel string) (Settings, error)

GetSettings will load settings for the provided channel name. If the settings can't be properly loaded then an error will be returned.

type State

type State struct {
	// The status of the channel's solve.
	Status model.Status `json:"status"`

	// The spelling bee puzzle that's being solved.  May not always be present,
	// for example when the state is being serialized to be sent to the browser.
	Puzzle *Puzzle `json:"puzzle,omitempty"`

	// The current order of the letters of the puzzle, not including the first
	// letter.
	Letters []string `json:"letters"`

	// The currently discovered words the puzzle.
	Words []string `json:"words"`

	// The current score of the solve.
	Score int `json:"score"`

	// The time that we last started or resumed solving the puzzle.  If the
	// channel has not yet started solving the puzzle or is in a non-playing state
	// this will be nil.
	LastStartTime *time.Time `json:"last_start_time,omitempty"`

	// The total time spent on solving the puzzle up to the last start time.
	TotalSolveDuration model.Duration `json:"total_solve_duration"`
}

State represents the state of an active channel that is attempting to solve a spelling bee puzzle.

func GetState

func GetState(conn redis.Conn, channel string) (State, error)

GetState loads the state for a spelling bee solve from redis. If the state can't be loaded then an error will be returned. If there is no state, then the zero value will be returned. After a state is read, its expiration time is automatically updated.

func NewState

func NewState(t *testing.T, filename string) State

NewState creates a new spelling bee puzzle state that has been properly initialized with the puzzle corresponding to the provided filename.

func (*State) ApplyAnswer

func (s *State) ApplyAnswer(answer string, allowUnofficial bool) error

ApplyAnswer applies an answer to the state. If the answer cannot be applied or is incorrect then an error is returned.

func (*State) ClearUnofficialAnswers

func (s *State) ClearUnofficialAnswers()

ClearUnofficialAnswers goes through all of the provided answers for a puzzle and removes any that are on the unofficial answers list.

Jump to

Keyboard shortcuts

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