|
|
|
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 bool
|
|
|
|
room.PeekMembers(func(_ int64, s *session.Session) bool {
|
|
|
|
data, err := m.DataManager.SessionData(s)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if data.LiveRoomId() == liveRoomId {
|
|
|
|
exists = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if !exists {
|
|
|
|
return room.Add(s)
|
|
|
|
}
|
|
|
|
return errors.New(fmt.Sprintf("session [%v] 已在房间 [%d] 中,不能再次加入", s, room.ID()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
})
|
|
|
|
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("未找到直播间 [%s] 对应游戏房间", 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
|
|
|
|
}
|