matchserver

package
v0.0.0-...-ed91d75 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NullT is the WS response type for a null response
	NullT = WebsocketResponseType(iota)
	// RequestSyncT is the WS request type for a sync request
	RequestSyncT = WebsocketRequestType(iota)
	// RequestAsyncT is the WS request type for an async request
	RequestAsyncT = WebsocketRequestType(iota)
	// ResponseSyncT is the WS response type for a sync response
	ResponseSyncT = WebsocketResponseType(iota)
	// ResponseAsyncT is the WS response type for an async response
	ResponseAsyncT = WebsocketResponseType(iota)
	// OpponentPlayedMoveT is the WS response type for an opponent's move
	OpponentPlayedMoveT = WebsocketResponseType(iota)
)

Variables

View Source
var (
	// PollingDefaultTimeout is the default timeout for http requests
	PollingDefaultTimeout time.Duration = 10 * time.Second
)

Functions

This section is empty.

Types

type Match

type Match struct {
	Game *model.Game
	// contains filtered or unexported fields
}

Match is a struct representing a game between two players

func DefaultMatchGenerator

func DefaultMatchGenerator(p1 *Player, p2 *Player) Match

DefaultMatchGenerator default match generator

func NewMatch

func NewMatch(black *Player, white *Player, maxTimeMs int64) Match

NewMatch create a new match between two players

func (*Match) GameOver

func (match *Match) GameOver() bool

GameOver check if the game is over

func (*Match) GetRequestedDraw

func (match *Match) GetRequestedDraw() *Player

GetRequestedDraw get the current player who has requested a draw

func (*Match) MaxTimeMs

func (match *Match) MaxTimeMs() int64

MaxTimeMs get match max time

func (*Match) PlayerName

func (match *Match) PlayerName(color model.Color) string

PlayerName get the player name corresponding to the input color

func (*Match) PlayerRemainingTimeMs

func (match *Match) PlayerRemainingTimeMs(color model.Color) int64

PlayerRemainingTimeMs get the player corresponding to the input color

func (*Match) SetRequestedDraw

func (match *Match) SetRequestedDraw(player *Player)

SetRequestedDraw store the fact that the player has requested a draw

type MatchDetails

type MatchDetails struct {
	Color        model.Color
	OpponentName string
	MaxTimeMs    int64
}

MatchDetails is a struct for the matched response

type MatchGenerator

type MatchGenerator func(black *Player, white *Player) Match

MatchGenerator takes two players and creates a match

func CreateCustomMatchGenerator

func CreateCustomMatchGenerator(matchPlayerTimeSeconds int) MatchGenerator

CreateCustomMatchGenerator create a generator that creates matches with a custom match length in seconds

type MatchingServer

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

MatchingServer handles matching players and carrying out the game

func NewMatchingServer

func NewMatchingServer() MatchingServer

NewMatchingServer create a matching server with no engine

func NewMatchingServerWithEngine

func NewMatchingServerWithEngine(
	engineAddr string, maxMatchingDuration time.Duration,
	engineConnTimeout time.Duration,
) MatchingServer

NewMatchingServerWithEngine create a matching server with an engine

func (*MatchingServer) LiveMatches

func (matchingServer *MatchingServer) LiveMatches() []*Match

LiveMatches current matches being played

func (*MatchingServer) MatchPlayer

func (matchingServer *MatchingServer) MatchPlayer(player *Player)

MatchPlayer queues the player for matching

func (*MatchingServer) StartCustomMatchServers

func (matchingServer *MatchingServer) StartCustomMatchServers(
	maxConcurrentGames int, matchGenerator MatchGenerator, quit chan bool,
)

StartCustomMatchServers using custom match generator

func (*MatchingServer) StartMatchServers

func (matchingServer *MatchingServer) StartMatchServers(
	maxConcurrentGames int, quit chan bool,
)

StartMatchServers using default match generator

type Player

type Player struct {
	ResponseChanSync   chan ResponseSync
	RequestChanAsync   chan RequestAsync
	ResponseChanAsync  chan ResponseAsync
	OpponentPlayedMove chan model.MoveRequest
	// contains filtered or unexported fields
}

Player is a struct representing a matchserver client, containing channels for communications between the client and the the matchserver

func NewPlayer

func NewPlayer(name string) *Player

NewPlayer create a new player

func (*Player) ClientConnectToPlayer

func (player *Player) ClientConnectToPlayer()

ClientConnectToPlayer ensures that only one client is connected to the player at a time (even if two client's have the session token)

func (*Player) ClientDisconnectFromPlayer

func (player *Player) ClientDisconnectFromPlayer()

ClientDisconnectFromPlayer ensures that only one client is connected to the player at a time (even if two client's have the session token)

func (*Player) Color

func (player *Player) Color() model.Color

Color returns player color

func (*Player) GetAsyncUpdate

func (player *Player) GetAsyncUpdate() *ResponseAsync

GetAsyncUpdate get the next async update for a player

func (*Player) GetMatch

func (player *Player) GetMatch() *Match

GetMatch get the player's match

func (*Player) GetSearchingForMatch

func (player *Player) GetSearchingForMatch() bool

GetSearchingForMatch get searching for match

func (*Player) GetSyncUpdate

func (player *Player) GetSyncUpdate() *model.MoveRequest

GetSyncUpdate get the next sync update for a player

func (*Player) HasMatchStarted

func (player *Player) HasMatchStarted(ctx context.Context) bool

HasMatchStarted player checks if their match has started

func (*Player) MakeMove

func (player *Player) MakeMove(pieceMove model.MoveRequest) bool

MakeMove player makes a move

func (*Player) MakeMoveWS

func (player *Player) MakeMoveWS(pieceMove model.MoveRequest)

MakeMoveWS player (websocket client) make a move

func (*Player) MatchMaxTimeMs

func (player *Player) MatchMaxTimeMs() int64

MatchMaxTimeMs returns players max time in ms

func (*Player) MatchedOpponentName

func (player *Player) MatchedOpponentName() string

MatchedOpponentName returns the matched opponent name

func (*Player) Name

func (player *Player) Name() string

Name get the player's name

func (*Player) RequestAsync

func (player *Player) RequestAsync(requestAsync RequestAsync)

RequestAsync player makes an async request

func (*Player) Reset

func (player *Player) Reset()

Reset a player for their next match

func (*Player) SetMatch

func (player *Player) SetMatch(match *Match)

SetMatch set the player's match

func (*Player) SetSearchingForMatch

func (player *Player) SetSearchingForMatch(searchingForMatch bool)

SetSearchingForMatch set searching for match

func (*Player) WaitForMatchOver

func (player *Player) WaitForMatchOver()

WaitForMatchOver used by client servers to synchronize around match ending

func (*Player) WaitForMatchStart

func (player *Player) WaitForMatchStart() error

WaitForMatchStart player waits for match start

type RequestAsync

type RequestAsync struct {
	Match, RequestToDraw, Resign bool
}

RequestAsync represents a request from the client unrelated to a move

type ResponseAsync

type ResponseAsync struct {
	GameOver, RequestToDraw, Draw, Resignation, Timeout, Matched bool
	Winner                                                       string
	MatchDetails                                                 MatchDetails
}

ResponseAsync represents a response to the client unrelated to a move

type ResponseSync

type ResponseSync struct {
	MoveSuccess       bool
	ElapsedMs         int
	ElapsedMsOpponent int
}

ResponseSync represents a response to the client related to a move

type WebsocketRequest

type WebsocketRequest struct {
	WebsocketRequestType WebsocketRequestType
	RequestSync          model.MoveRequest
	RequestAsync         RequestAsync
}

WebsocketRequest is a struct for a request over the WS conn

type WebsocketRequestType

type WebsocketRequestType uint8

WebsocketRequestType represents the different type of requests supported over the WS conn

type WebsocketResponse

type WebsocketResponse struct {
	WebsocketResponseType WebsocketResponseType
	ResponseSync          ResponseSync
	ResponseAsync         ResponseAsync
	OpponentPlayedMove    model.MoveRequest
}

WebsocketResponse is a struct for a response over the WS conn

type WebsocketResponseType

type WebsocketResponseType uint8

WebsocketResponseType represents the different type of responses supported over the WS conn

Jump to

Keyboard shortcuts

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