You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package manager
|
|
|
|
import (
|
|
pbRoom "dcg/game/pb/room"
|
|
"dcg/game/svc"
|
|
"errors"
|
|
"fmt"
|
|
"git.noahlan.cn/northlan/ngs/session"
|
|
)
|
|
|
|
var GameManager *gameManager
|
|
|
|
type (
|
|
gameManager struct {
|
|
svcCtx *svc.ServiceContext // svcCtx
|
|
|
|
*RoomManager // 房间管理器
|
|
*DataManager // 数据管理器(Session)
|
|
}
|
|
)
|
|
|
|
func Init(svcCtx *svc.ServiceContext) {
|
|
GameManager = newGameManager(svcCtx)
|
|
}
|
|
|
|
func newGameManager(svcCtx *svc.ServiceContext) *gameManager {
|
|
return &gameManager{
|
|
svcCtx: svcCtx,
|
|
RoomManager: NewRoomManager(),
|
|
DataManager: NewDataManager(),
|
|
}
|
|
}
|
|
|
|
func (m *gameManager) JoinRoom(s *session.Session, gameType pbRoom.GameType, liveRoomId int64) error {
|
|
room := m.RoomManager.RetrieveRoomByType(gameType)
|
|
|
|
var exists *session.Session
|
|
room.PeekMembers(func(_ int64, s *session.Session) bool {
|
|
data, err := m.DataManager.SessionData(s)
|
|
if err != nil {
|
|
return true
|
|
}
|
|
if data.LiveRoomId() == liveRoomId {
|
|
|
|
exists = s
|
|
return false
|
|
}
|
|
return true
|
|
})
|
|
if exists != nil {
|
|
// 将原有session从房间移除
|
|
err := room.Leave(exists)
|
|
if err == nil {
|
|
exists = nil
|
|
}
|
|
}
|
|
if exists != nil {
|
|
return errors.New(fmt.Sprintf("session [%v] 已在房间 [%d] 中,不能再次加入", s, room.ID()))
|
|
} else {
|
|
return room.Add(s)
|
|
}
|
|
}
|
|
|
|
// SessionByDataFilter 通过 session数据过滤器 获取session,data
|
|
func (m *gameManager) SessionByDataFilter(filter DataFilter) (room *Room, sess *session.Session, data *Data, err error) {
|
|
m.RoomManager.PeekAllSession(func(r *Room, session *session.Session) bool {
|
|
data, err = m.DataManager.SessionData(session)
|
|
if err != nil {
|
|
return true
|
|
}
|
|
if filter(session, data) {
|
|
room = r
|
|
sess = session
|
|
return false
|
|
}
|
|
return true
|
|
})
|
|
if err == nil && room == nil {
|
|
err = errors.New("未找到相关客户端实例")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (m *gameManager) RoomByLiveRoomId(liveRoomId int64) (*Room, error) {
|
|
room, _, _, err := m.SessionByDataFilter(func(session *session.Session, data *Data) bool {
|
|
return data.LiveRoomId() == liveRoomId
|
|
})
|
|
if err != nil {
|
|
return nil, errors.New(fmt.Sprintf("未找到直播间 [%d] 对应游戏房间", liveRoomId))
|
|
}
|
|
return room, nil
|
|
}
|
|
|
|
func (m *gameManager) BattleIdByLiveRoomId(liveRoomId int64) int64 {
|
|
_, _, d, err := m.SessionByDataFilter(func(_ *session.Session, data *Data) bool {
|
|
return data.LiveRoomId() == liveRoomId
|
|
})
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return d.BattleId()
|
|
}
|
|
|
|
func (m *gameManager) BattleIdBySession(s *session.Session) int64 {
|
|
_, _, d, err := m.SessionByDataFilter(func(sess *session.Session, _ *Data) bool {
|
|
return sess.ID() == s.ID()
|
|
})
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return d.BattleId()
|
|
}
|
|
|
|
func (m *gameManager) RoomByBattleId(battleId int64) (*Room, error) {
|
|
room, _, _, err := m.SessionByDataFilter(func(_ *session.Session, data *Data) bool {
|
|
return data.BattleId() == battleId
|
|
})
|
|
if err != nil {
|
|
return nil, errors.New(fmt.Sprintf("未找到战局 [%d] 对应的游戏房间", battleId))
|
|
}
|
|
return room, nil
|
|
}
|