game

package
v0.0.0-...-843b749 Latest Latest
Warning

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

Go to latest
Published: May 21, 2020 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Package game implements the Blade II Online game server.

Index

Constants

View Source
const (

	// BufferSize is the size of each message queue's buffer.
	BufferSize = 2048
)
View Source
const (

	// SerializedCardsDelimiter is the delimiter for serialized cards objects.
	SerializedCardsDelimiter string = "."
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ActivePlayer

type ActivePlayer uint8

ActivePlayer is a uint8 typedef for the active player during a game end check

type B2MatchInstruction

type B2MatchInstruction uint8

B2MatchInstruction is an enum type that represents different different types of match instruction for Blade II Online.

const (

	// None is a special case used to indicate that this instruction should result in a noop.
	None B2MatchInstruction = 0

	// Instructions 1 through 11 indicate that a card was selected. The position from which it
	// should be taken, and where it should end up, is determined by the current board state.
	CardElliotsOrbalStaff   B2MatchInstruction = 1
	CardFiesTwinGunswords   B2MatchInstruction = 2
	CardAlisasOrbalBow      B2MatchInstruction = 3
	CardJusisSword          B2MatchInstruction = 4
	CardMachiasOrbalShotgun B2MatchInstruction = 5
	CardGaiusSpear          B2MatchInstruction = 6
	CardLaurasGreatsword    B2MatchInstruction = 7
	CardBolt                B2MatchInstruction = 8
	CardMirror              B2MatchInstruction = 9
	CardBlast               B2MatchInstruction = 10
	CardForce               B2MatchInstruction = 11

	// Messages that can be sent to and from the server.
	InstructionForfeit B2MatchInstruction = 12
	InstructionMessage B2MatchInstruction = 13

	// Messages that can only be received from the server.
	InstructionCards              B2MatchInstruction = 14
	InstructionPlayerData         B2MatchInstruction = 15
	InstructionOpponentData       B2MatchInstruction = 16
	InstructionConnectionProgress B2MatchInstruction = 17
	InstructionConnectionClosed   B2MatchInstruction = 18

	// Error messages from the server grouped so we can check for errors by equality (> the lowest value error).
	InstructionConnectionError    B2MatchInstruction = 19
	InstructionAuthError          B2MatchInstruction = 20
	InstructionMatchCheckError    B2MatchInstruction = 21
	InstructionMatchSetupError    B2MatchInstruction = 22
	InstructionMatchIllegalMove   B2MatchInstruction = 23
	InstructionMatchMutualTimeOut B2MatchInstruction = 24
	InstructionMatchTimeOut       B2MatchInstruction = 25
)

Definitions of server updates. Explicitly numbered so that their numerical value can be seen when hovering them in vscode.

func (B2MatchInstruction) ToCard

func (i B2MatchInstruction) ToCard() (card Card)

ToCard returns this instruction as a card. Invalid cards are returned with the default value of 0 (ElliotsOrbalStaff).

type Card

type Card uint8

Card is a typedef for different Blade II card types.

const (

	// Standard cards (basic).
	ElliotsOrbalStaff   Card = 0
	FiesTwinGunswords   Card = 1
	AlisasOrbalBow      Card = 2
	JusisSword          Card = 3
	MachiasOrbalShotgun Card = 4
	GaiusSpear          Card = 5
	LaurasGreatsword    Card = 6

	// Standard cards (effects).
	Bolt   Card = 7
	Mirror Card = 8
	Blast  Card = 9
	Force  Card = 10

	// Bolted Cards (basic).
	InactiveElliotsOrbalStaff   Card = 11
	InactiveFiesTwinGunswords   Card = 12
	InactiveAlisasOrbalBow      Card = 13
	InactiveJusisSword          Card = 14
	InactiveMachiasOrbalShotgun Card = 15
	InactiveGaiusSpear          Card = 16
	InactiveLaurasGreatsword    Card = 17

	// Bolted cards (effects).
	InactiveBolt   Card = 18
	InactiveMirror Card = 19
	InactiveBlast  Card = 20
	InactiveForce  Card = 21
)

Card enums - explicitly numbered so that their numerical value can be seen when hovering them in vscode.

func (Card) Value

func (c Card) Value() (value uint8)

Value returns the point value of the specified card, if it where to be played on the field. Effect cards always return 1.

type Cards

type Cards struct {
	Player1Deck    []Card
	Player1Hand    []Card
	Player1Field   []Card
	Player1Discard []Card

	Player2Deck    []Card
	Player2Hand    []Card
	Player2Field   []Card
	Player2Discard []Card
}

Cards is a container for all the cards on the field.

func GenerateCards

func GenerateCards() (cards Cards)

GenerateCards generates a new set of cards for a match - has additional checks to ensure that the match is not unwinnable from the first move etc.

func InitializeCards

func InitializeCards(inCards Cards) (outCards Cards)

InitializeCards simulates the first moves of the game until a playable state is reached.

Returns a COPY of the input cards.

func (Cards) Copy

func (c Cards) Copy() (outCards Cards)

Copy returns a deep copy of this cards object.

func (*Cards) Serialized

func (c *Cards) Serialized() string

Serialized returns the string representation of the DECKS ONLY, as the other data is never required to be sent.

The cards are serialized as hexadecimal numbers, with the following format:

NNNNNNNNNNNNNNN.NNNNNNNNNNNNNNN

Where each "N" is the hexadecimal representation of a card.

type DisconnectRequest

type DisconnectRequest struct {

	// A pointer to the client to remove.
	Client *GClient

	// The reason for removal.
	Reason protocol.B2Code

	// An optional to be sent, either to just the player, or both, depending on whether they are
	// in a match, and the state of the match etc..
	Message string
}

DisconnectRequest is a wrapper for the information required to remove a client from the game server.

type GClient

type GClient struct {

	// Database values for this client.
	DBID        uint64
	MatchID     uint64
	PublicID    string
	DisplayName string
	Avatar      uint8

	// Whether the server is currently expecting a move update from this client.
	WaitingForMove bool
	// contains filtered or unexported fields
}

GClient is a container for a websocket connection and its associated user data.

func NewClient

func NewClient(wsconn *websocket.Conn, databaseID uint64, publicID string, displayname string, matchID uint64, avatar uint8, gameServer *Server) *GClient

NewClient creates a and retruns a pointer to a new Client, and starts its message pumps in two seperate go routines.

func (*GClient) Close

func (client *GClient) Close(message protocol.Message)

Close sends a message to the client, and closes the connection after a delay. The delay is asynchronous, as it is wrapped in a goroutine.

func (*GClient) IsSameConnection

func (client *GClient) IsSameConnection(other *GClient) bool

IsSameConnection returns true if the specified client is the same as this one.

func (*GClient) SendMessage

func (client *GClient) SendMessage(message protocol.Message)

SendMessage adds a message to the outbound queue.

func (*GClient) StartEventLoop

func (client *GClient) StartEventLoop()

StartEventLoop starts the send and receive pumps for the client, with a separate goroutine for each.

type Match

type Match struct {

	// The Match ID
	ID uint64

	// Pointers to both players.
	Client1 *GClient
	Client2 *GClient

	// Match state
	State MatchState

	// A pointer to the game server.
	Server *Server
	// contains filtered or unexported fields
}

Match is a wrapper for a matches data and client connections etc

func NewMatch

func NewMatch(matchID uint64, client *GClient, server *Server) *Match

NewMatch creates and returns a pointer to a new match, setting the specified client as player 1.

func (*Match) BroadCast

func (match *Match) BroadCast(message protocol.Message)

BroadCast sends the specified message to both clients.

func (*Match) GetPhase

func (match *Match) GetPhase() Phase

GetPhase gets the match phase

func (*Match) SendCardData

func (match *Match) SendCardData(cards string)

SendCardData sends starting card data to each client.

func (*Match) SendOpponentData

func (match *Match) SendOpponentData()

SendOpponentData sends each player the opponents data.

func (*Match) SendPlayerData

func (match *Match) SendPlayerData()

SendPlayerData sends each player's (their own) name to the respective client.

func (*Match) SetMatchResult

func (match *Match) SetMatchResult()

SetMatchResult updates the database with the match result, and also updates the match stats for each player via the Blade II Online REST API.

Fails silently but logs errors.

Performed in a goroutine.

func (*Match) SetMatchStart

func (match *Match) SetMatchStart()

SetMatchStart sets the phase + start time for the current match.

Fails silently but logs errors.

Database update is performed in a goroutine to avoid a delay when updating the database.

func (*Match) SetPhase

func (match *Match) SetPhase(phase Phase)

SetPhase sets the match phase, using a mutex lock to protect the critical section, as multiple goroutines may be trying to read the matches phase.

func (*Match) Tick

func (match *Match) Tick()

Tick reads any incoming messages and passes outgoing messages to the queue, as well as handling any timed out players (players that did not make a move within the turn time limit).

type MatchState

type MatchState struct {

	// Winner is the database ID for the winner. A value of zero indicates that there is no player,
	// or the game ended in a draw.
	Winner uint64

	// Whos turn it currently is.
	Turn Player

	// The cards for this match.
	Cards Cards

	// Player scores.
	Player1Score uint16
	Player2Score uint16

	// Match phase (used by the server to determine whether to, for example, tick the match or not).
	Phase Phase
}

MatchState represents the current state of a match.

type Move

type Move struct {
	Instruction B2MatchInstruction
	Payload     string
}

Move represents a client match data packet.

func MoveFromString

func MoveFromString(moveString string) (move Move, err error)

MoveFromString attempts to parse a move from the specified move string Non nil error means something went wrong.

type Phase

type Phase uint8

Phase is a typedef for the different states that a match can be in.

const (
	WaitingForPlayers Phase = 0
	Play              Phase = 1
	Finished          Phase = 2
)

Match phase enums.

type Player

type Player uint8

Player is a typedef for the two different players

const (
	PlayerUndecided Player = 0
	Player1         Player = 1
	Player2         Player = 2
)

Player enums representing each type of player. PlayerUndecided is an edge case for indicating that there is no player specified, such as when the game just cleared all the cards from the field and is waiting to see when each player draw onto the field.

type Server

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

Server is the game server itself

func NewServer

func NewServer() *Server

NewServer creates and returns a pointer to a new game server.

func (*Server) AddClient

func (gs *Server) AddClient(wsconn *websocket.Conn, dbid uint64, pid string, displayname string, avatar uint8, matchID uint64)

AddClient takes a websocket connection various data, wraps them up and adds them to the game server as a client, to be processed later.

func (*Server) Init

func (gs *Server) Init()

Init initializes the game server including starting the internal loop.

func (*Server) MainLoop

func (gs *Server) MainLoop()

MainLoop is the main logic loop for the game server.

func (*Server) Remove

func (gs *Server) Remove(client *GClient, reason protocol.B2Code, message string)

Remove adds a client to the disconnect queue, to be disconnected later, along with a reason code and a message.

Jump to

Keyboard shortcuts

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