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.

69 lines
1.4 KiB
Go

package manager
import (
"dcg/game/svc"
"errors"
"fmt"
"git.noahlan.cn/northlan/ngs/session"
)
var GameManager *gameManager
type (
gameManager struct {
svcCtx *svc.ServiceContext // svcCtx
*RoomManager // 房间管理器
}
)
func Init(svcCtx *svc.ServiceContext) {
GameManager = newGameManager(svcCtx)
}
func newGameManager(svcCtx *svc.ServiceContext) *gameManager {
gameCfg := svcCtx.Config.Game.DefaultRooms
roomMetas := make([]RoomMeta, 0, len(gameCfg))
for _, s := range gameCfg {
roomMetas = append(roomMetas, RoomMeta{
ID: s.ID,
GameType: s.GameType,
IsDefault: true,
})
}
return &gameManager{
svcCtx: svcCtx,
RoomManager: newRoomManager(WithDefaultRoom(roomMetas)),
}
}
func (m *gameManager) RoomByLiveRoom(liveRoomId int64, platform string) (*Room, error) {
var resp *Room
m.PeekAllSession(func(room *Room, s *session.Session) bool {
data, err := GetSessionData(s)
if err != nil {
return true
}
for _, liveRoom := range data.LiveRooms {
if liveRoom.ID == liveRoomId && liveRoom.Platform == platform {
resp = room
return false
}
}
return true
})
if resp == nil {
return nil, errors.New(fmt.Sprintf("未找到直播间 [%d] 对应游戏房间", liveRoomId))
}
return resp, nil
}
func (m *gameManager) BattleIdByLiveRoom(liveRoomId int64, platform string) int64 {
room, err := m.RoomByLiveRoom(liveRoomId, platform)
if err != nil {
return 0
}
return room.BattleId
}