game

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChooseText

func ChooseText() (string, error)

ChooseText randomly chooses a text from the dataset

func Compose added in v0.1.2

func Compose(t MessageType, payload string) []byte

Compose creates a data string for connections

Types

type Client added in v0.1.2

type Client struct {
	// State stores State logic
	State *State
	// Name is the username
	Name string
	// Conn holds the connection to the server
	Conn net.Conn
}

Client is a state manager for the client

func (*Client) ConfirmUsername added in v0.1.2

func (c *Client) ConfirmUsername(username string)

ConfirmUsername sets a username and informs the server about it

func (*Client) Listen added in v0.1.2

func (c *Client) Listen()

Listen listens for messages

type MessageType added in v0.1.2

type MessageType string

MessageType defines available commands

const (
	// ChangeName is for changing usernames
	// payload = new username
	ChangeName MessageType = "change-name"
	// ExitPlayer is for players leaving the game
	// payload = nil
	ExitPlayer MessageType = "exit-player"
	// StartGame is for starting the game
	// payload = unix timestamp of when it starts
	StartGame MessageType = "start-game"
)

func Parse added in v0.1.2

func Parse(payload string) (MessageType, string)

Parse returns message type and message itself from a connection

type Other added in v0.1.2

type Other struct {
	// Conn holds the connection to the user
	Conn net.Conn
	// Name is the username
	Name string
	// Progress indicates how far is the user
	Progress float64
}

Other contains all information needed about other players

type Server added in v0.1.2

type Server struct {
	// State stores State logic
	State *State
	// Name is the username
	Name string
	// Server is an instance of net.Listener to control server logic
	Server net.Listener
	// Others has connection to clients
	Others []*Other
	// Callback is fired when a message comes
	Callback func(t MessageType)
}

Server is a state manager for the server

func (*Server) Accept added in v0.1.2

func (s *Server) Accept()

Accept listens for connections

func (*Server) Inform added in v0.1.2

func (s *Server) Inform(msg []byte)

Inform messages all clients about something

func (*Server) Listen added in v0.1.2

func (s *Server) Listen(other *Other)

Listen listens to messages from client

func (*Server) StartGame added in v0.1.2

func (s *Server) StartGame()

StartGame informs client the game is starting

func (*Server) Subscribe added in v0.1.2

func (s *Server) Subscribe(cb func(t MessageType))

Subscribe creates a callback

type State

type State struct {
	// CurrWord is an index to State.Words
	CurrWord int
	// Words contains the text split by spaces
	Words []string
	// StartTime is a timestamp of the first keystroke
	StartTime time.Time
	// contains filtered or unexported fields
}

State describes the state of a game

func NewState

func NewState(text string) *State

NewState initializes State

func (*State) End

func (s *State) End()

End ends the mechanism

func (*State) IncError

func (s *State) IncError()

IncError increments the error count

func (*State) NextWord

func (s *State) NextWord()

NextWord saves stats of the current word and increments the counter

func (State) Progress added in v0.1.2

func (s State) Progress() float64

Progress returns a float in the (0;1) range represending the progress made

func (*State) Start

func (s *State) Start()

Start starts the mechanism

func (State) Wpm

func (s State) Wpm() float64

Wpm is the words per minute

Jump to

Keyboard shortcuts

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