game

package
v0.0.0-...-ebe70a3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FlagOffline indicates the client is disconnected, shall be remove after the game is over
	FlagOffline = 0x1
	// FlagKicked indicates the client is safe to removed from game
	FlagKicked = 0x2
)
View Source
const (
	StageIdle     = 0 // wait for enough players to start the game
	StageWait     = 1 // the game is about to start in few seconds
	StagePlaying  = 2 // the game is running
	StageGameOver = 3 // the game is just finished yet, will change to wait in few seconds
	StagePrepare  = 4 // hidden stage for client to show the game start animations, will change to StagePlaying in a few seconds
)

Stages

Variables

View Source
var (
	// Register channel for register client to game server
	Register chan *RegisterRequest
	// Unregister channel for unregister client from game server
	Unregister chan uint64
)

Functions

func AddBot

func AddBot(t *Table)

AddBot to table

func CreateFakeRPCInterface

func CreateFakeRPCInterface()

CreateFakeRPCInterface creates two channel as fake rpc interface

func DestroyFakeRPCInterface

func DestroyFakeRPCInterface()

DestroyFakeRPCInterface deletes the two channels

func HideCardsForUID

func HideCardsForUID(state *pb.TableState, uid uint64) *pb.TableState

HideCardsForUID will hide other player's cards for player with given uid

func SetTableConfig

func SetTableConfig(config *TableConfig)

SetTableConfig from outside

func Start

func Start()

Start game server

func UseLogger

func UseLogger(l *zap.Logger)

UseLogger set logger for this sub-system

func WaitGameServerShutdown

func WaitGameServerShutdown()

WaitGameServerShutdown waits for game server to shutdown

Types

type Client

type Client struct {
	// UID user unique ID
	UID uint64
	// In channel for receiving frames from session in agent
	In chan pb.Frame
	// Out channel for sending frames to session in agent
	Out chan pb.Frame
	// TID ID of the table this client play on
	TID uint64
	// Die channel as signal to indicate game server to remove this client
	Die chan struct{}
	// contains filtered or unexported fields
}

Client holds client's uid and channels for communicate

func (*Client) ClearFlagKicked

func (c *Client) ClearFlagKicked()

ClearFlagKicked clears the FlagKicked bit

func (*Client) ClearFlagOffline

func (c *Client) ClearFlagOffline()

ClearFlagOffline clears the FlagOffline bit

func (Client) IsFlagKickedSet

func (c Client) IsFlagKickedSet() bool

IsFlagKickedSet returns true if the FlagKicked is set

func (Client) IsFlagOfflineSet

func (c Client) IsFlagOfflineSet() bool

IsFlagOfflineSet returns true if the FlagOffline is set

func (*Client) SendOut

func (c *Client) SendOut(frame pb.Frame)

func (*Client) SetFlagKicked

func (c *Client) SetFlagKicked()

SetFlagKicked sets the FlagKicked bit

func (*Client) SetFlagOffline

func (c *Client) SetFlagOffline()

SetFlagOffline sets the FlagOffline bit

func (*Client) String

func (c *Client) String() string

String interface

type Flag

type Flag uint32

Flag client status

type FrameHandler

type FrameHandler struct {
	ReqCmd  pb.GameCmd
	RespCmd pb.GameCmd
	Handler handlerFunc
}

FrameHandler process client frames

func (*FrameHandler) String

func (h *FrameHandler) String() string

type HandleResult

type HandleResult struct {
	Body     []byte
	Status   int32
	Events   []*pb.SingleEvent
	GameOver bool
	Msg      string
	Err      error
}

HandleResult wraps up handler function results

type InFrame

type InFrame struct {
	C *Client
	F *pb.Frame
}

InFrame pair of client and incoming frame

type PlayerState

type PlayerState struct {
	UID     uint64  // uid
	Flag    int32   // flags
	Cards   []uint8 // cards
	Timeout bool    // timeout
	Score   int32   // player score
}

PlayerState holds player status, for it is more related to table not client

func NewPlayerState

func NewPlayerState(uid uint64) *PlayerState

NewPlayerState creates and returns pointer to a new PlayerState instance

func (PlayerState) CalculateScore

func (p PlayerState) CalculateScore() int32

CalculateScore convert cards to score after game over

func (PlayerState) CardIndex

func (p PlayerState) CardIndex(card uint8) int

CardIndex return index of the card, if not exist, -1 will be returned

func (*PlayerState) ClearFlag

func (p *PlayerState) ClearFlag(flag int32)

ClearFlag clear flag bit

func (PlayerState) Dump

func (p PlayerState) Dump() *pb.UnoPlayer

Dump convert PlayerState to pb.UnoPlayer

func (PlayerState) IsFlagSet

func (p PlayerState) IsFlagSet(flag int32) bool

IsFlagSet returns true if flag is set

func (*PlayerState) ResetForNewGame

func (p *PlayerState) ResetForNewGame()

ResetForNewGame reset player state for new game

func (*PlayerState) SetFlag

func (p *PlayerState) SetFlag(flag int32)

SetFlag set flag bit

func (PlayerState) String

func (p PlayerState) String() string

type RegisterRequest

type RegisterRequest struct {
	// UID user unique ID
	UID uint64
	// ClientEntry to receive client instance
	ClientEntry chan *Client
}

RegisterRequest ...

type Table

type Table struct {
	// TID table ID
	TID uint64
	// Stage see stage constants
	Stage int
	// Clockwise indicates the current order is clockwise, otherwise CCW
	Clockwise bool
	// Current color
	Color uint8
	// ChallengeColor for challenge
	ChallengeColor uint8
	// LastPlayer UID of last player
	LastPlayer uint64
	// CurrentPlayer UID of current player
	CurrentPlayer uint64
	// Deck on this table
	Deck *uno.Deck
	// Discard holds uno cards that has been discard
	Discard []uint8
	// DeckRecycled indicates if the deck has been recycled
	DeckRecycled bool
	// Timeout for the stage, in seconds
	Timeout int
	// TimeLeft for seconds left
	TimeLeft int
	// States array of player state
	States []*PlayerState
	// Clients array of clients
	Clients []*Client
	// Register channel for client
	Register chan *RegisterRequest
	// InFrames from clients
	InFrames chan *InFrame
	// contains filtered or unexported fields
}

Table holds state of an uno game

func NewTable

func NewTable() *Table

NewTable creates and returns pointer to a new table instance

func (Table) DumpState

func (t Table) DumpState() *pb.TableState

DumpState dumps table status and returns in TableState

func (*Table) String

func (t *Table) String() string

type TableConfig

type TableConfig struct {
	TurnTimeout           int // timeout of a turn
	GameOverTimeout       int // timeout of game over
	IdleTimeout           int // timeout before adding bots
	WaitTimeout           int // timeout before starting a game
	PrepareTimeout        int // timeout before playing
	AIAssistantMinTimeout int // minimum timeout for AI to kick in
	AIAssistantMaxTimeout int // maximum timeout for AI to kick in
	MinPlayers            int // minimum players to start the game
	MaxPlayers            int // maximum players a game can play with
	FrameQueueSize        int // input frame queue size
}

TableConfig holds table timeout configs

Jump to

Keyboard shortcuts

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