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.

89 lines
1.8 KiB
Go

package room
import (
"dcg/game/manager"
pbRoom "dcg/game/pb/room"
"git.noahlan.cn/northlan/ngs/component"
"git.noahlan.cn/northlan/ngs/session"
"git.noahlan.cn/northlan/ntools-go/uuid"
)
type (
Logic struct {
component.Base
}
)
func NewRoomLogic() *Logic {
return &Logic{}
}
func (m *Logic) CMD() string {
return "room"
}
func (m *Logic) Init() {
}
func (m *Logic) AfterInit() {
session.Lifetime.OnClosed(func(s *session.Session) {
manager.GameManager.LeaveRoom(s)
})
}
func (m *Logic) Shutdown() {
manager.GameManager.Clean()
}
func (m *Logic) Join(s *session.Session, msg *pbRoom.JoinRoomReq) error {
// uid - liveRoomId
err := s.Bind(uuid.NextId())
if err != nil {
return err
}
gameTypeStr := pbRoom.GameType_name[int32(msg.GameType)]
sd := manager.NewSessionData(gameTypeStr)
for _, room := range msg.LiveRooms {
sd.AddLiveRoom(manager.LiveRoom{
ID: room.LiveRoomId,
Platform: room.Platform,
})
}
sd.WithSession(s)
var duplicated bool
manager.GameManager.PeekAllSession(func(room *manager.Room, s *session.Session) bool {
if data, err := manager.GetSessionData(s); err == nil {
for _, liveRoom := range data.LiveRooms {
for _, req_liveRoom := range msg.LiveRooms {
if liveRoom.ID == req_liveRoom.LiveRoomId && liveRoom.Platform == req_liveRoom.Platform {
// 重复
duplicated = true
return false
}
}
}
}
return true
})
if duplicated {
return s.Response(&pbRoom.JoinRoomResp{
Code: 500,
Result: "当前客户端定义的直播间ID已经加入过游戏房间,请检查配置.",
})
}
if err = manager.GameManager.JoinRoom(s, gameTypeStr, msg.RoomId); err != nil {
return s.Response(&pbRoom.JoinRoomResp{
Code: 500,
Result: err.Error(),
})
}
return s.Response(&pbRoom.JoinRoomResp{
Code: 200,
Result: "success",
})
}