model package - github.com/Qwiri/GYF/backend/pkg/model - Go Packages

model

package
v0.0.0-...-c40e111 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PermissionListTopics = 1 << iota
	PermissionCreateTopics
	PermissionDeleteTopics
)

Variables

View Source
var DefaultPreferences = GamePreferences{
	AutoSkip:      true,
	MinPlayers:    3,
	MaxPlayers:    100,
	MinTopics:     1,
	MaxTopics:     30,
	ShuffleTopics: true,
	Permissions:   0,
}
View Source
var ErrConnectionNil = errors.New("websocket connection was nil")

Functions

func WrapVoteResults

func WrapVoteResults(result ...*SubmissionResult) (res []interface{})

Types

type Client

type Client struct {
	Name       string
	Connection *websocket.Conn
	Leader     bool
}

func NewClient

func NewClient(conn *websocket.Conn, username string) (c *Client)

type ClientArray

type ClientArray []*Client

func (ClientArray) Contains

func (A ClientArray) Contains(client *Client) bool

func (*ClientArray) Delete

func (A *ClientArray) Delete(client *Client)

func (ClientArray) Names

func (A ClientArray) Names() (res []string)

type ClientMap

type ClientMap map[string]*Client

func (ClientMap) Array

func (M ClientMap) Array() (res ClientArray)

func (ClientMap) Delete

func (M ClientMap) Delete(client *Client)

type EnhancedPermission

type EnhancedPermission uint8

type Game

type Game struct {
	ID           string
	Clients      ClientMap
	Topics       TopicArray
	CurrentTopic *Topic

	LastInteraction time.Time
	Preferences     *GamePreferences
	Password        string
	// contains filtered or unexported fields
}

func NewGame

func NewGame(id string) (game *Game)

NewGame returns a new game object with default values

func (*Game) Broadcast

func (g *Game) Broadcast(response *Response)

Broadcast sends a response to all clients

func (*Game) BroadcastExcept

func (g *Game) BroadcastExcept(conn *websocket.Conn, response *Response)

BroadcastExcept sends a response to all clients except to a specific connection

func (*Game) BroadcastTopicListToLeaders

func (g *Game) BroadcastTopicListToLeaders()

func (*Game) BroadcastWaitingFor

func (g *Game) BroadcastWaitingFor()

func (*Game) BroadcastWaitingForAll

func (g *Game) BroadcastWaitingForAll()

func (*Game) BroadcastWaitingForNone

func (g *Game) BroadcastWaitingForNone()

func (*Game) CheckCycle

func (g *Game) CheckCycle(checkAutoSkip, force bool) (err error)

CheckCycle checks if we're waiting for clients and if not, continue the game

func (*Game) ClientByConnection

func (g *Game) ClientByConnection(conn *websocket.Conn) *Client

func (*Game) FindNonLeaderClient

func (g *Game) FindNonLeaderClient() *Client

func (*Game) ForceEndGame

func (g *Game) ForceEndGame(reason string) (err error)

ForceEndGame ends the game

func (*Game) ForceNextRound

func (g *Game) ForceNextRound() (err error)

ForceNextRound starts the next round if a topic is available

func (*Game) ForceShowVoteResults

func (g *Game) ForceShowVoteResults() (err error)

ForceShowVoteResults sends the voting results to all clients

func (*Game) ForceStartVote

func (g *Game) ForceStartVote() (err error)

ForceStartVote starts the vote process of the previous submissions

func (*Game) IsEmpty

func (g *Game) IsEmpty() bool

IsEmpty returns if there are clients in the current game

func (*Game) KickClient

func (g *Game) KickClient(client *Client)

func (*Game) LeaveClient

func (g *Game) LeaveClient(client *Client, reason string)

LeaveClient removes a client from the game and sends a PLAYER_LEAVE message to all other players

func (*Game) Reset

func (g *Game) Reset(purgeClients bool)

Reset resets the Topic.Played values, Game.CurrentTopic, Game.State() and purges (if purgeClients was set to true) all clients

func (*Game) SetLeader

func (g *Game) SetLeader(client *Client)

SetLeader sets the client as the sole leader and sends a CHANGE_ROLE message to all players with the old and the new leader.

func (*Game) SetState

func (g *Game) SetState(state util.GameState)

SetState sets the current GameState and broadcasts the change to all clients

func (*Game) State

func (g *Game) State() util.GameState

State returns the current GameState

func (*Game) StatsForUser

func (g *Game) StatsForUser(user string) (res int)

func (*Game) WaitingForGIFSubmission

func (g *Game) WaitingForGIFSubmission(topic *Topic) (res ClientArray)

WaitingForGIFSubmission returns all players we're currently waiting for

func (*Game) WaitingForVote

func (g *Game) WaitingForVote(topic *Topic) (res ClientArray)

type GamePreferences

type GamePreferences struct {
	AutoSkip      bool
	MinPlayers    int
	MaxPlayers    int
	MinTopics     int
	MaxTopics     int
	ShuffleTopics bool
	Permissions   EnhancedPermission
}

type Response

type Response struct {
	Command   string        `json:"cmd"`
	Args      []interface{} `json:"args"`
	Warn      string        `json:"warn"`
	Success   bool          `json:"_s"`
	Timestamp int64         `json:"_ts"`
}

func NewResponse

func NewResponse(command string, args ...interface{}) *Response

func NewResponseWithError

func NewResponseWithError(command string, err error, args ...interface{}) *Response

func NewResponseWithWarn

func NewResponseWithWarn(command, warn string, args ...interface{}) *Response

func PChangeRole

func PChangeRole(client *Client, role string) *Response

func PChat

func PChat(client *Client, message string) *Response

func PGameEnd

func PGameEnd(reason string) *Response

func PJoin

func PJoin(client *Client, game *Game) *Response

func PList

func PList(clients ClientMap) *Response

func PNextRound

func PNextRound(description string, currentTopic, totalTopics int) *Response

func PPlayerLeave

func PPlayerLeave(client *Client, reason string) *Response

func PPreferences

func PPreferences(pref *GamePreferences) *Response

func PStart

func PStart() *Response

func PState

func PState(state util.GameState) *Response

func PStats

func PStats(game *Game) *Response

func PSubmitGIF

func PSubmitGIF(client *Client) *Response

func PTopicAdd

func PTopicAdd(topic string) *Response

func PTopicList

func PTopicList(game *Game) *Response

func PTopicRemove

func PTopicRemove(topic string) *Response

func PVersion

func PVersion() *Response

func PVoteResults

func PVoteResults(results ...*SubmissionResult) *Response

func PVoteStart

func PVoteStart(urls ...string) *Response

func PWaitingFor

func PWaitingFor(waitingFor ClientArray) *Response

func (*Response) Marshal

func (r *Response) Marshal() (res []byte)

func (*Response) Respond

func (r *Response) Respond(conn *websocket.Conn) (err error)

func (*Response) RespondTo

func (r *Response) RespondTo(client *Client) (err error)

func (*Response) String

func (r *Response) String() string

type Submission

type Submission struct {
	Creator *Client
	Voters  ClientArray
	URL     string
}

func NewSubmission

func NewSubmission(creator *Client, url string) *Submission

type SubmissionArray

type SubmissionArray []*Submission

func (SubmissionArray) Results

func (A SubmissionArray) Results() (res []*SubmissionResult)

func (SubmissionArray) URLs

func (A SubmissionArray) URLs() (res []string)

type SubmissionMap

type SubmissionMap map[string]*Submission

func (SubmissionMap) AllExceptFrom

func (M SubmissionMap) AllExceptFrom(client *Client) (res SubmissionArray)

func (SubmissionMap) AsArray

func (M SubmissionMap) AsArray() (res SubmissionArray)

func (SubmissionMap) ByURL

func (M SubmissionMap) ByURL(url string) (*Submission, bool)

func (SubmissionMap) ByURLLoose

func (M SubmissionMap) ByURLLoose(url string) (*Submission, bool, error)

func (SubmissionMap) HasSubmittedGIF

func (M SubmissionMap) HasSubmittedGIF(client *Client) (ok bool)

func (SubmissionMap) HasVoted

func (M SubmissionMap) HasVoted(client *Client) bool

type SubmissionResult

type SubmissionResult struct {
	URL     string   `json:"url"`
	Creator string   `json:"creator"`
	Voters  []string `json:"voters"`
}

type Topic

type Topic struct {
	Description string        `json:"description,omitempty"`
	Submissions SubmissionMap `json:"submissions,omitempty"`
	Played      bool          `json:"played"`
}

func NewTopic

func NewTopic(description string) *Topic

type TopicArray

type TopicArray []*Topic

func (*TopicArray) Add

func (T *TopicArray) Add(topic string)

func (*TopicArray) Clear

func (T *TopicArray) Clear()

func (*TopicArray) Delete

func (T *TopicArray) Delete(topic string)

func (TopicArray) Exists

func (T TopicArray) Exists(topic string) bool

func (TopicArray) NextTopic

func (T TopicArray) NextTopic() (*Topic, error)

func (TopicArray) PlayedTopicsCount

func (T TopicArray) PlayedTopicsCount() (count int)

func (TopicArray) RandomTopic

func (T TopicArray) RandomTopic() (*Topic, error)

func (TopicArray) Strings

func (T TopicArray) Strings() (res []string)

Jump to

Keyboard shortcuts

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