Documentation
¶
Index ¶
- Constants
- func Decode(data []byte, v any) error
- func Encode(v any) ([]byte, error)
- func Json(v Jsoner) string
- func PrintModes()
- func RegisterFactory(gameMode constant.GameMode, factory Factory)
- type Coder
- type Factory
- type Group
- type GroupBase
- func (g *GroupBase) AddInviteRecord(inviteeUID string, nowUnix int64)
- func (g *GroupBase) AddPlayer(p Player) error
- func (g *GroupBase) AllowNearbyJoin() bool
- func (g *GroupBase) AllowRecentJoin() bool
- func (g *GroupBase) Base() *GroupBase
- func (g *GroupBase) CanPlayTogether(info *pto.PlayerInfo) error
- func (g *GroupBase) CanStartMatch() error
- func (g *GroupBase) CheckState(valids ...GroupState) error
- func (g *GroupBase) ClearPlayers()
- func (g *GroupBase) DelInviteRecord(inviteeUID string)
- func (g *GroupBase) GetCaptain() string
- func (g *GroupBase) GetGroupInfo() *pto.GroupInfo
- func (g *GroupBase) GetInviteExpireTimeStamp(uid string) int64
- func (g *GroupBase) GetInviteRecords() map[string]int64
- func (g *GroupBase) GetPlayers() []string
- func (g *GroupBase) GetStartMatchTimeSec() int64
- func (g *GroupBase) GetState() GroupState
- func (g *GroupBase) GetStateWithLock() GroupState
- func (g *GroupBase) ID() int64
- func (g *GroupBase) IsFull() bool
- func (g *GroupBase) IsInviteExpired(uid string, nowUnix int64) bool
- func (g *GroupBase) IsMatchStrategySupported() bool
- func (g *GroupBase) Lock()
- func (g *GroupBase) PlayerExists(uid string) bool
- func (g *GroupBase) PlayerLimit() int
- func (g *GroupBase) RemovePlayer(p Player) (empty bool)
- func (g *GroupBase) SetAllowNearbyJoin(allow bool)
- func (g *GroupBase) SetAllowRecentJoin(allow bool)
- func (g *GroupBase) SetCaptain(uid string)
- func (g *GroupBase) SetStartMatchTimeSec(sec int64)
- func (g *GroupBase) SetState(s GroupState)
- func (g *GroupBase) SetStateWithLock(s GroupState)
- func (g *GroupBase) UIDs() []string
- func (g *GroupBase) Unlock()
- type GroupConfig
- type GroupMgr
- type GroupRole
- type GroupSettings
- type GroupState
- type Jsoner
- type Mgrs
- func (m *Mgrs) CreateAITeam(g Group) (t Team, err error)
- func (m *Mgrs) CreateGroup(playerLimit int, p Player) (g Group, err error)
- func (m *Mgrs) CreatePlayer(pInfo *pto.PlayerInfo) (p Player, err error)
- func (m *Mgrs) CreateRoom(teamLimit int, t Team) (r Room, err error)
- func (m *Mgrs) CreateTeam(g Group) (t Team, err error)
- type Player
- type PlayerBase
- func (p *PlayerBase) Base() *PlayerBase
- func (p *PlayerBase) CheckOnlineState(valids ...PlayerOnlineState) error
- func (p *PlayerBase) GetMatchStrategy() constant.MatchStrategy
- func (p *PlayerBase) GetMatchStrategyWithLock() constant.MatchStrategy
- func (p *PlayerBase) GetOnlineState() PlayerOnlineState
- func (p *PlayerBase) GetOnlineStateWithLock() PlayerOnlineState
- func (p *PlayerBase) GetPlayerInfo() *pto.PlayerInfo
- func (p *PlayerBase) GetVoiceState() PlayerVoiceState
- func (p *PlayerBase) Lock()
- func (p *PlayerBase) SetAttr(attr *pto.UploadPlayerAttr) error
- func (p *PlayerBase) SetMatchStrategy(s constant.MatchStrategy)
- func (p *PlayerBase) SetMatchStrategyWithLock(s constant.MatchStrategy)
- func (p *PlayerBase) SetOnlineState(s PlayerOnlineState)
- func (p *PlayerBase) SetOnlineStateWithLock(s PlayerOnlineState)
- func (p *PlayerBase) SetVoiceState(s PlayerVoiceState)
- func (p *PlayerBase) UID() string
- func (p *PlayerBase) Unlock()
- type PlayerMgr
- type PlayerOnlineState
- type PlayerVoiceState
- type Room
- type RoomBase
- func (r *RoomBase) AddEscapePlayer(uid string)
- func (r *RoomBase) AddTeam(t Team)
- func (r *RoomBase) Base() *RoomBase
- func (r *RoomBase) GetEscapePlayers() []string
- func (r *RoomBase) GetMatchInfo() *pto.MatchInfo
- func (r *RoomBase) GetTeams() []int64
- func (r *RoomBase) ID() int64
- func (r *RoomBase) Lock()
- func (r *RoomBase) NeedAI() bool
- func (r *RoomBase) RLock()
- func (r *RoomBase) RUnlock()
- func (r *RoomBase) RemoveTeam(id int64)
- func (r *RoomBase) Unlock()
- type RoomMgr
- type Team
- type TeamBase
- type TeamMgr
Constants ¶
const (
InviteExpireSec = 60 * 5
)
Variables ¶
This section is empty.
Functions ¶
func PrintModes ¶
func PrintModes()
func RegisterFactory ¶
Types ¶
type Coder ¶
type Coder interface {
// Encode encodes current object to a byte array
Encode() ([]byte, error)
// Decode decodes a byte array to current object
Decode(data []byte) error
}
Coder is a interface for encoding and decoding data. It should be implemented by the specific game mode player, group, room, team, etc.
Note: Do not implement this interface for the common entries or strategy entries.
type Factory ¶
type Factory interface {
CreateRoom(mgr *Mgrs, base *RoomBase) (Room, error)
CreatePlayer(mgr *Mgrs, base *PlayerBase, pInfo *pto.PlayerInfo) (Player, error)
CreateTeam(mgr *Mgrs, base *TeamBase) (Team, error)
CreateGroup(mgr *Mgrs, base *GroupBase) (Group, error)
}
func GetFactory ¶
type Group ¶
type Group interface {
Coder
Jsoner
// ID returns the unique group id.
ID() int64
// Base returns the base information of the group.
// Here we define a concrete struct `GroupBase`
// to hold the common fields to avoid lots getter and setter method.
Base() *GroupBase
// IsFull checks if the group is full.
IsFull() bool
// SetCaptain sets the captain of the group.
SetCaptain(string)
// GetCaptain returns the captain in the group.
GetCaptain() string
// CanPlayTogether checks if the player can play with the group's players.
CanPlayTogether(*pto.PlayerInfo) error
// GetPlayerInfos returns the player infos of the group.
// This method usually used for sync group info to client.
GetGroupInfo() *pto.GroupInfo
// CanStartMatch checks if the group can start to match.
// Maybe some game mode need to check if the group is full or not.
// Maybe some game mode need all players to be ready.
// If you have some special logics, please override this method.
CanStartMatch() error
// GetStartMatchTimeSec returns the start match time of the group.
GetStartMatchTimeSec() int64
// SetStartMatchTimeSec sets the start match time of the group.
SetStartMatchTimeSec(sec int64)
// Json returns the json string of the group.
// You may need to lock when marshal it to avoid data race,
// even if in print log.
//
// Note: it should be implemented by the specific game mode entry.
// TODO: any other greater way?
Json() string
}
Group represents a group of players.
type GroupBase ¶
type GroupBase struct {
// ReentrantLock is a reentrant L support multiple locks in the same goroutine.
// Use it to help avoid deadlock.
L *concurrent.ReentrantLock
// GroupID is the unique id of the group.
GroupID int64
// IsAI indicates if the group is an AI group.
IsAI bool
// GameMode is the game mode of the group.
GameMode constant.GameMode
// ModeVersion is the version of the game mode of the group.
// Only the same version of the players can be played together.
ModeVersion int64
// MatchStrategy is the current match strategy of the group.
MatchStrategy constant.MatchStrategy
// SupportMatchStrategies is the supported match strategies of the group.
SupportMatchStrategies []constant.MatchStrategy
// State is the current State of the group.
State GroupState
// Players holds the Players'ids in the group.
Players []string
// MatchID is a unique id to identify each match action.
MatchID string
// StartMatchTimeSec is the start match time of the group.
StartMatchTimeSec int64
// Roles holds the Roles of the players in the group.
Roles map[string]GroupRole
Configs GroupConfig
// ReadyPlayer holds the unready players in the group.
// In the project, we assume that the default does not
// require all players to prepare to start matching,
// so the map will be empty by default,
// and you will need to re-initialize this field in the game mode that requires preparation
UnReadyPlayer map[string]struct{}
// InviteRecords holds the invite records of the group.
// key: uid
// value: expire time (s)
InviteRecords map[string]int64
// Settings holds the settings of the group.
Settings GroupSettings
}
GroupBase holds the common fields of a Group for all kinds of game mode and match strategy.
func NewGroupBase ¶
func NewGroupBase( groupID int64, playerLimit int, playerBase *PlayerBase, ) *GroupBase
NewGroupBase creates a new GroupBase.
func (*GroupBase) AddInviteRecord ¶
func (*GroupBase) AllowNearbyJoin ¶
func (*GroupBase) AllowRecentJoin ¶
func (*GroupBase) CanPlayTogether ¶
func (g *GroupBase) CanPlayTogether(info *pto.PlayerInfo) error
func (*GroupBase) CanStartMatch ¶
func (*GroupBase) CheckState ¶
func (g *GroupBase) CheckState(valids ...GroupState) error
func (*GroupBase) ClearPlayers ¶
func (g *GroupBase) ClearPlayers()
func (*GroupBase) DelInviteRecord ¶
func (*GroupBase) GetCaptain ¶
func (*GroupBase) GetGroupInfo ¶
func (*GroupBase) GetInviteExpireTimeStamp ¶
func (*GroupBase) GetInviteRecords ¶
func (*GroupBase) GetPlayers ¶
func (*GroupBase) GetStartMatchTimeSec ¶
func (*GroupBase) GetState ¶
func (g *GroupBase) GetState() GroupState
func (*GroupBase) GetStateWithLock ¶
func (g *GroupBase) GetStateWithLock() GroupState
func (*GroupBase) IsInviteExpired ¶
func (*GroupBase) IsMatchStrategySupported ¶
IsMatchStrategySupported checks if the group supports the current match strategy.
func (*GroupBase) PlayerExists ¶
func (*GroupBase) PlayerLimit ¶
func (*GroupBase) RemovePlayer ¶
func (*GroupBase) SetAllowNearbyJoin ¶
func (*GroupBase) SetAllowRecentJoin ¶
func (*GroupBase) SetCaptain ¶
func (*GroupBase) SetStartMatchTimeSec ¶
func (*GroupBase) SetState ¶
func (g *GroupBase) SetState(s GroupState)
func (*GroupBase) SetStateWithLock ¶
func (g *GroupBase) SetStateWithLock(s GroupState)
type GroupConfig ¶
type GroupMgr ¶
type GroupMgr struct {
*collection.Manager[int64, Group]
// contains filtered or unexported fields
}
func NewGroupMgr ¶
NewGroupMgr creates a group repository, `groupIDStart`: the starting group ID.
func (*GroupMgr) Encode ¶
Encode encodes all groups into a map of game modes to their encoded bytes.
func (*GroupMgr) GenGroupID ¶
type GroupSettings ¶
type GroupSettings struct {
// NearbyJoinAllowed indicates whether nearby players can join the group.
NearbyJoinAllowed bool
// RecentJoinAllowed indicates whether recent players can join the group.
RecentJoinAllowed bool
}
GroupSettings defines the settings of a group.
type GroupState ¶
type GroupState int8
const ( GroupStateInvite GroupState = 0 GroupStateMatch GroupState = 1 GroupStateGame GroupState = 2 GroupStateDissolved GroupState = 3 )
type Mgrs ¶
func (*Mgrs) CreateGroup ¶
func (*Mgrs) CreatePlayer ¶
func (m *Mgrs) CreatePlayer(pInfo *pto.PlayerInfo) (p Player, err error)
type Player ¶
type Player interface {
Coder
Base() *PlayerBase
UID() string
GetPlayerInfo() *pto.PlayerInfo
SetAttr(attr *pto.UploadPlayerAttr) error
}
Player represents a player in a Group.
type PlayerBase ¶
type PlayerBase struct {
// ReentrantLock is a reentrant L support multiple locks in the same goroutine
// Use it to help avoid deadlock.
L *concurrent.ReentrantLock
IsAI bool
GroupID int64
MatchStrategy constant.MatchStrategy
OnlineState PlayerOnlineState
VoiceState PlayerVoiceState
// TODO: other common attributes
pto.PlayerInfo
pto.Attribute
}
PlayerBase holds the common fields of a Player for all kinds of game mode and match strategy.
func NewPlayerBase ¶
func NewPlayerBase(info *pto.PlayerInfo) *PlayerBase
func (*PlayerBase) Base ¶
func (p *PlayerBase) Base() *PlayerBase
func (*PlayerBase) CheckOnlineState ¶
func (p *PlayerBase) CheckOnlineState(valids ...PlayerOnlineState) error
CheckOnlineState checks if the player is in a valid online state.
func (*PlayerBase) GetMatchStrategy ¶
func (p *PlayerBase) GetMatchStrategy() constant.MatchStrategy
func (*PlayerBase) GetMatchStrategyWithLock ¶
func (p *PlayerBase) GetMatchStrategyWithLock() constant.MatchStrategy
func (*PlayerBase) GetOnlineState ¶
func (p *PlayerBase) GetOnlineState() PlayerOnlineState
func (*PlayerBase) GetOnlineStateWithLock ¶
func (p *PlayerBase) GetOnlineStateWithLock() PlayerOnlineState
func (*PlayerBase) GetPlayerInfo ¶
func (p *PlayerBase) GetPlayerInfo() *pto.PlayerInfo
func (*PlayerBase) GetVoiceState ¶
func (p *PlayerBase) GetVoiceState() PlayerVoiceState
func (*PlayerBase) Lock ¶
func (p *PlayerBase) Lock()
func (*PlayerBase) SetAttr ¶
func (p *PlayerBase) SetAttr(attr *pto.UploadPlayerAttr) error
func (*PlayerBase) SetMatchStrategy ¶
func (p *PlayerBase) SetMatchStrategy(s constant.MatchStrategy)
func (*PlayerBase) SetMatchStrategyWithLock ¶
func (p *PlayerBase) SetMatchStrategyWithLock(s constant.MatchStrategy)
func (*PlayerBase) SetOnlineState ¶
func (p *PlayerBase) SetOnlineState(s PlayerOnlineState)
func (*PlayerBase) SetOnlineStateWithLock ¶
func (p *PlayerBase) SetOnlineStateWithLock(s PlayerOnlineState)
func (*PlayerBase) SetVoiceState ¶
func (p *PlayerBase) SetVoiceState(s PlayerVoiceState)
func (*PlayerBase) UID ¶
func (p *PlayerBase) UID() string
func (*PlayerBase) Unlock ¶
func (p *PlayerBase) Unlock()
type PlayerMgr ¶
type PlayerMgr struct {
*collection.Manager[string, Player]
}
func NewPlayerMgr ¶
func NewPlayerMgr() *PlayerMgr
type PlayerOnlineState ¶
type PlayerOnlineState int8
PlayerOnlineState is the state of a player. TODO: try to use state machine to manage player state.
const ( PlayerOnlineStateOffline PlayerOnlineState = 0 PlayerOnlineStateOnline PlayerOnlineState = 1 PlayerOnlineStateInGroup PlayerOnlineState = 2 PlayerOnlineStateInMatch PlayerOnlineState = 3 PlayerOnlineStateInGame PlayerOnlineState = 4 PlayerOnlineStateInSettle PlayerOnlineState = 5 )
type PlayerVoiceState ¶
type PlayerVoiceState int8
PlayerVoiceState is the voice state of a player.
const ( PlayerVoiceStateMute PlayerVoiceState = 0 PlayerVoiceStateUnmute PlayerVoiceState = 1 )
type RoomBase ¶
type RoomBase struct {
RoomID int64
Teams map[int64]struct{}
TeamLimit int
GameMode constant.GameMode
MatchStrategy constant.MatchStrategy
ModeVersion int64
FinishMatchSec int64
EscapePlayer []string
GameServerInfo pto.GameServerInfo
// contains filtered or unexported fields
}
func (*RoomBase) AddEscapePlayer ¶
func (*RoomBase) GetEscapePlayers ¶
func (*RoomBase) GetMatchInfo ¶
func (*RoomBase) RemoveTeam ¶
type RoomMgr ¶
type RoomMgr struct {
*collection.Manager[int64, Room]
// contains filtered or unexported fields
}
type TeamBase ¶
type TeamBase struct {
UniqueID int64 // UniqueID is the global unique team id.
TeamID int // TeamID is the unique team id in one room, start from 1.
IsAI bool
Groups map[int64]struct{}
GameMode constant.GameMode
MatchStrategy constant.MatchStrategy
ModeVersion int64
// contains filtered or unexported fields
}
