crocodile

package
v0.0.0-...-6d75db2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrGameAlreadyStarted is error when user tries to start a game in chat, but there is the one already
	ErrGameAlreadyStarted = "Game already started"

	// ErrWaitingForWinnerRespond is error when the game have been played, but winner did't start a new one
	ErrWaitingForWinnerRespond = "Waiting for winner respond"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Tracef(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})
	Trace(args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})
	Traceln(args ...interface{})
	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Println(args ...interface{})
	Warnln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
	Fatalln(args ...interface{})
	Panicln(args ...interface{})
}

type Machine

type Machine struct {
	MesID int

	// ChatID where the game is started
	ChatID int64

	// ChatTitle where the game is started
	ChatTitle string

	// Word which users should guess
	Word string

	// UserID of the user that should explain the word
	Host     int
	HostName string

	// UserID of user who guessed the word
	Winner int

	StartedTime time.Time
	GuessedTime time.Time

	// Technical data
	Storage       Storage       `json:"-"`
	WordsProvider WordsProvider `json:"-"`
	FSM           *fsm.FSM      `json:"-"`
	Log           Logger        `json:"-"`

	// We have to set this explicitly for saving state in external storage
	State string
}

Machine stores state of game in one chat

func NewMachine

func NewMachine(storage Storage, wp WordsProvider, log Logger, chatID int64, mesID int) *Machine

NewMachine returns new Machine instance

func (*Machine) CheckWord

func (m *Machine) CheckWord(word string) bool

CheckWord checks if m.Word == provided word

func (*Machine) CheckWordAndSetWinner

func (m *Machine) CheckWordAndSetWinner(word string, potentialWinner int, winnerName string) (string, bool)

CheckWordAndSetWinner sets m.Winner and returns true if m.CheckWord() returns true, otherwise ret. false

func (*Machine) GetGuessedTime

func (m *Machine) GetGuessedTime() time.Time

GetGuessedTime is getter for m.GuessedTime

func (*Machine) GetHost

func (m *Machine) GetHost() int

GetHost is getter for m.Host

func (*Machine) GetStartedTime

func (m *Machine) GetStartedTime() time.Time

GetStartedTime is getter for m.StartedTime

func (*Machine) GetWinner

func (m *Machine) GetWinner() int

GetWinner is getter for m.Winner

func (*Machine) GetWord

func (m *Machine) GetWord() string

GetWord is getter for m.Word

func (*Machine) SetNewRandomWord

func (m *Machine) SetNewRandomWord() (string, error)

SetNewRandomWord generates new word

func (*Machine) StartNewGameAndReturnWord

func (m *Machine) StartNewGameAndReturnWord(host int, hostName string, chatTitle string) (string, error)

StartNewGameAndReturnWord sets m.Word to new words and returns it

func (*Machine) StopGame

func (m *Machine) StopGame() error

StopGame sends stop_game event to FSM

type MachineFabric

type MachineFabric struct {
	Storage       Storage
	WordsProvider WordsProvider
	Log           Logger
}

MachineFabric aims to produce new machines with freezed Storage and WordsProvider

func NewMachineFabric

func NewMachineFabric(storage Storage, wp WordsProvider, log Logger) *MachineFabric

NewMachineFabric returns MachineFabric

func (*MachineFabric) NewMachine

func (m *MachineFabric) NewMachine(chatID int64, mesID int) *Machine

NewMachine returns Machine with freezed Storage and WordsProvider

type Storage

type Storage interface {
	IncrementUserStats(model.Chat, ...model.UserInChat) error
	SaveMachineState(Machine) error
	LookupForMachine(*Machine) error
}

Storage aims to save FSM state somewhere (e.g. in Redis)

type WordsProvider

type WordsProvider interface {
	GetWord() (string, error)
}

WordsProvider should return random word

type WordsProviderReader

type WordsProviderReader struct {
	// contains filtered or unexported fields
}

WordsProviderReader takes content from reader, converts to string, splits by "\n" and returns random word

func NewWordsProviderReader

func NewWordsProviderReader(r io.Reader) (*WordsProviderReader, error)

NewWordsProviderReader returns new instance of WordsProviderReader

func (*WordsProviderReader) GetWord

func (w *WordsProviderReader) GetWord() (string, error)

GetWord returns random word

Jump to

Keyboard shortcuts

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