game

package
v0.0.0-...-1b9c3da Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LobbySettingBounds = &SettingBounds{
		MinDrawingTime:       60,
		MaxDrawingTime:       300,
		MinRounds:            1,
		MaxRounds:            20,
		MinMaxPlayers:        2,
		MaxMaxPlayers:        24,
		MinClientsPerIPLimit: 1,
		MaxClientsPerIPLimit: 24,
	}
	SupportedLanguages = map[string]string{
		"english": "English",
		"italian": "Italian",
		"german":  "German",
		"french":  "French",
		"dutch":   "Dutch",
	}
)
View Source
var SendDataToConnectedPlayers func(sender *Player, lobby *Lobby, data interface{})
View Source
var TriggerComplexUpdateEvent func(eventType string, data interface{}, lobby *Lobby)
View Source
var TriggerComplexUpdatePerPlayerEvent func(eventType string, data func(*Player) interface{}, lobby *Lobby)
View Source
var TriggerSimpleUpdateEvent func(eventType string, lobby *Lobby)
View Source
var WriteAsJSON func(player *Player, object interface{}) error
View Source
var WritePublicSystemMessage func(lobby *Lobby, text string)

Functions

func CreateLobby

func CreateLobby(playerName, language string, drawingTime, rounds, maxPlayers, customWordChance, clientsPerIPLimit int, customWords []string, enableVotekick bool) (*Player, *Lobby, error)

CreateLobby allows creating a lobby, optionally returning errors that occurred during creation.

func GeneratePlayerName

func GeneratePlayerName() string

GeneratePlayerName creates a new playername. A so called petname. It consists of an adverb, an adjective and a animal name. The result can generally be trusted to be sane.

func GetRandomWords

func GetRandomWords(lobby *Lobby) []string

GetRandomWords gets 3 random words for the passed Lobby. The words will be chosen from the custom words and the default dictionary, depending on the settings specified by the Lobby-Owner.

func HandleEvent

func HandleEvent(raw []byte, received *JSEvent, lobby *Lobby, player *Player) error

func OnConnected

func OnConnected(lobby *Lobby, player *Player)

func OnDisconnected

func OnDisconnected(lobby *Lobby, player *Player)

func RemoveLobby

func RemoveLobby(id string)

RemoveLobby deletes a lobby, not allowing anyone to connect to it again.

Types

type Fill

type Fill struct {
	X     float32 `json:"x"`
	Y     float32 `json:"y"`
	Color string  `json:"color"`
}

Fill represents the usage of the fill bucket.

type FillEvent

type FillEvent struct {
	Type string `json:"type"`
	Data *Fill  `json:"data"`
}

LineEvent is basically the same as JSEvent, but with a specific Data type. We use this for reparsing as soon as we know that the type is right. It's a bit unperformant, but will do for now.

type JSEvent

type JSEvent struct {
	Type string      `json:"type"`
	Data interface{} `json:"data"`
}

JSEvent contains an eventtype and optionally any data.

type Line

type Line struct {
	FromX     float32 `json:"fromX"`
	FromY     float32 `json:"fromY"`
	ToX       float32 `json:"toX"`
	ToY       float32 `json:"toY"`
	Color     string  `json:"color"`
	LineWidth float32 `json:"lineWidth"`
}

Line is the struct that a client send when drawing

type LineEvent

type LineEvent struct {
	Type string `json:"type"`
	Data *Line  `json:"data"`
}

LineEvent is basically the same as JSEvent, but with a specific Data type. We use this for reparsing as soon as we know that the type is right. It's a bit unperformant, but will do for now.

type Lobby

type Lobby struct {
	// ID uniquely identified the Lobby.
	ID string

	// DrawingTime is the amount of seconds that each player has available to
	// finish their drawing.
	DrawingTime int
	// MaxRounds defines how many iterations a lobby does before the game ends.
	// One iteration means every participant does one drawing.
	MaxRounds int
	// MaxPlayers defines the maximum amount of players in a single lobby.
	MaxPlayers int
	// CustomWords are additional words that will be used in addition to the
	// predefined words.
	CustomWords []string
	Words       []string

	// Players references all participants of the Lobby.
	Players []*Player

	// Drawer references the Player that is currently drawing.
	Drawer *Player
	// Owner references the Player that created the lobby.
	Owner *Player
	// CurrentWord represents the word that was last selected. If no word has
	// been selected yet or the round is already over, this should be empty.
	CurrentWord string
	// WordHints for the current word.
	WordHints []*WordHint
	// WordHintsShown are the same as WordHints with characters visible.
	WordHintsShown []*WordHint
	// Round is the round that the Lobby is currently in. This is a number
	// between 0 and MaxRounds. 0 indicates that it hasn't started yet.
	Round int
	// WordChoice represents the current choice of words.
	WordChoice []string
	// RoundEndTime represents the time at which the current round will end.
	// This is a UTC unix-timestamp in milliseconds.
	RoundEndTime int64

	CustomWordsChance int
	ClientsPerIPLimit int
	// CurrentDrawing represents the state of the current canvas. The elements
	// consist of LineEvent and FillEvent. Please do not modify the contents
	// of this array an only move AppendLine and AppendFill on the respective
	// lobby object.
	CurrentDrawing []interface{}
	EnableVotekick bool
	// contains filtered or unexported fields
}

Lobby represents a game session.

func GetLobby

func GetLobby(id string) *Lobby

GetLobby returns a Lobby that has a matching ID or no Lobby if none could be found.

func (*Lobby) AppendFill

func (lobby *Lobby) AppendFill(fill *FillEvent)

AppendFill adds a fill direction to the current drawing. This exists in order to prevent adding arbitrary elements to the drawing, as the backing array is an empty interface type.

func (*Lobby) AppendLine

func (lobby *Lobby) AppendLine(line *LineEvent)

AppendLine adds a line direction to the current drawing. This exists in order to prevent adding arbitrary elements to the drawing, as the backing array is an empty interface type.

func (*Lobby) ClearDrawing

func (lobby *Lobby) ClearDrawing()

func (*Lobby) GetAvailableWordHints

func (lobby *Lobby) GetAvailableWordHints(player *Player) []*WordHint

func (*Lobby) GetPlayer

func (lobby *Lobby) GetPlayer(userSession string) *Player

GetPlayer searches for a player, identifying them by usersession.

func (*Lobby) HasConnectedPlayers

func (lobby *Lobby) HasConnectedPlayers() bool

func (*Lobby) JoinPlayer

func (lobby *Lobby) JoinPlayer(playerName string) *Player

type Message

type Message struct {
	// Author is the player / thing that wrote the message
	Author string `json:"author"`
	// Content is the actual message text.
	Content string `json:"content"`
}

Message represents a message in the chatroom.

type NextTurn

type NextTurn struct {
	Round        int       `json:"round"`
	Players      []*Player `json:"players"`
	RoundEndTime int       `json:"roundEndTime"`
}

NextTurn represents the data necessary for displaying the lobby state right after a new turn started. Meaning that no word has been chosen yet and therefore there are no wordhints and no current drawing instructions.

type Player

type Player struct {

	// ID uniquely identified the Player.
	ID string `json:"id"`
	// Name is the players displayed name
	Name string `json:"name"`
	// Score is the points that the player got in the current Lobby.
	Score int `json:"score"`
	// Connected defines whether the players websocket connection is currently
	// established. This has previously been in state but has been moved out
	// in order to avoid losing the state on refreshing the page.
	// While checking the websocket against nil would be enough, we still need
	// this field for sending it via the APIs.
	Connected bool `json:"connected"`
	// Rank is the current ranking of the player in his Lobby
	LastScore int         `json:"lastScore"`
	Rank      int         `json:"rank"`
	State     PlayerState `json:"state"`
	// contains filtered or unexported fields
}

Player represents a participant in a Lobby.

func (*Player) GetLastKnownAddress

func (player *Player) GetLastKnownAddress() string

GetLastKnownAddress returns the last known IP-Address used for an HTTP request.

func (*Player) GetUserSession

func (player *Player) GetUserSession() string

GetUserSession returns the players current user session.

func (*Player) GetWebsocket

func (player *Player) GetWebsocket() *websocket.Conn

GetWebsocket simply returns the players websocket connection. This method exists to encapsulate the websocket field and prevent accidental sending the websocket data via the network.

func (*Player) GetWebsocketMutex

func (player *Player) GetWebsocketMutex() *sync.Mutex

GetWebsocketMutex returns a mutex for locking the websocket connection. Since gorilla websockets shits it self when two calls happen at the same time, we need a mutex per player, since each player has their own socket. This getter extends to prevent accidentally sending the mutex via the network.

func (*Player) SetLastKnownAddress

func (player *Player) SetLastKnownAddress(address string)

SetLastKnownAddress sets the last known IP-Address used for an HTTP request. Can be retrieved via GetLastKnownAddress().

func (*Player) SetWebsocket

func (player *Player) SetWebsocket(socket *websocket.Conn)

SetWebsocket sets the given connection as the players websocket connection.

type PlayerState

type PlayerState int
const (
	Guessing PlayerState = 0
	Drawing  PlayerState = 1
	Standby  PlayerState = 2
)

type Ready

type Ready struct {
	PlayerID   string `json:"playerId"`
	PlayerName string `json:"playerName"`
	Drawing    bool   `json:"drawing"`

	OwnerID        string        `json:"ownerId"`
	Round          int           `json:"round"`
	MaxRound       int           `json:"maxRounds"`
	RoundEndTime   int           `json:"roundEndTime"`
	WordHints      []*WordHint   `json:"wordHints"`
	Players        []*Player     `json:"players"`
	CurrentDrawing []interface{} `json:"currentDrawing"`
}

Ready represents the initial state that a user needs upon connection. This includes all the necessary things for properly running a client without receiving any more data.

type Rounds

type Rounds struct {
	Round     int `json:"round"`
	MaxRounds int `json:"maxRounds"`
}

type SettingBounds

type SettingBounds struct {
	MinDrawingTime       int64
	MaxDrawingTime       int64
	MinRounds            int64
	MaxRounds            int64
	MinMaxPlayers        int64
	MaxMaxPlayers        int64
	MinClientsPerIPLimit int64
	MaxClientsPerIPLimit int64
}

SettingBounds defines the lower and upper bounds for the user-specified lobby creation input.

type WordHint

type WordHint struct {
	Character rune `json:"character"`
	Underline bool `json:"underline"`
}

WordHint describes a character of the word that is to be guessed, whether the character should be shown and whether it should be underlined on the UI.

Jump to

Keyboard shortcuts

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