package realarena import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/base" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" "sync" "go.mongodb.org/mongo-driver/bson/primitive" ) /* 模块名:实时竞技场 描述:实时竞技场 开发:李伟 */ func NewModule() core.IModule { m := new(RealArena) return m } type RealArena struct { modules.ModuleBase service base.IRPCXService pvp comm.IPvp apicomp *apiComp configure *configureComp model *modelComp modelRank *modelRank match *matchComp lock sync.RWMutex rooms map[string]*Room } // 模块名 func (this *RealArena) GetType() core.M_Modules { return comm.ModuleRealarena } // 模块初始化接口 注册用户创建角色事件 func (this *RealArena) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { if err = this.ModuleBase.Init(service, module, options); err != nil { return } this.rooms = make(map[string]*Room) this.service = service.(base.IRPCXService) return } func (this *RealArena) Start() (err error) { if err = this.ModuleBase.Start(); err != nil { return } var module core.IModule if module, err = this.service.GetModule(comm.ModulePvp); err != nil { return } this.pvp = module.(comm.IPvp) this.service.RegisterFunctionName(string(comm.RPC_RealarenaTrusteeship), this.endgame) return } // 装备组件 func (this *RealArena) OnInstallComp() { this.ModuleBase.OnInstallComp() this.apicomp = this.RegisterComp(new(apiComp)).(*apiComp) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) this.model = this.RegisterComp(new(modelComp)).(*modelComp) this.modelRank = this.RegisterComp(new(modelRank)).(*modelRank) this.match = this.RegisterComp(new(matchComp)).(*matchComp) } //创建房间 func (this *RealArena) createroom(red, bule *pb.DBRealArenaMember) (room *Room, err error) { var ( session comm.IUserSession ok bool ) room = &Room{ Id: primitive.NewObjectID().Hex(), module: this, ServicePath: fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()), sessions: make([]comm.IUserSession, 0), members: make([]*pb.DBRealArenaMember, 0), } room.members = append(room.members, red, bule) if session, ok = this.GetUserSession(red.User.Uid); ok { room.sessions = append(room.sessions, session) } else { err = fmt.Errorf("player:%s is Offline", red.User.Uid) return } if session, ok = this.GetUserSession(bule.User.Uid); ok { room.sessions = append(room.sessions, session) } else { err = fmt.Errorf("player:%s is Offline", bule.User.Uid) return } if err = this.model.change(red.User.Uid, map[string]interface{}{ "state": 2, "roomid": room.Id, "roompath": room.ServicePath, }); err != nil { this.Errorln(err) return } if err = this.model.change(bule.User.Uid, map[string]interface{}{ "state": 2, "roomid": room.Id, "roompath": room.ServicePath, }); err != nil { this.Errorln(err) return } this.lock.Lock() this.rooms[room.Id] = room this.lock.Unlock() return } // 用户离线处理 func (this *RealArena) useroffline(uid, sessionid string) { var ( info *pb.DBRealArena err error ) if info, err = this.model.getinfobyuid(uid); err != nil { this.Error("用户离线!", log.Field{Key: "err", Value: err.Error()}) return } if info.State == 1 { if err = this.match.CancelMatch(uid); err != nil { this.Error("用户离线! 处理数据", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err.Error()}) return } if err = this.model.change(uid, map[string]interface{}{ "state": 0, }); err != nil { this.Error("用户离线! 处理数据", log.Field{Key: "uid", Value: uid}, log.Field{Key: "err", Value: err.Error()}) return } } else if info.State == 2 { var ( lockpath string = fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()) ) if lockpath != info.Roompath { //在当前房间下 _, err = this.service.RpcGo( context.Background(), info.Roompath, string(comm.RPC_RealarenaTrusteeship), &pb.RPC_RealArenaTrusteeshipReq{RoomId: info.Roomid, OffUid: info.Uid}, nil) if err != nil { this.Errorln(err) return } } else { this.endgame(context.Background(), &pb.RPC_RealArenaTrusteeshipReq{RoomId: info.Roomid, OffUid: info.Uid}, &pb.RPC_RealArenaTrusteeshipResp{}) } } } func (this *RealArena) endgame(ctx context.Context, req *pb.RPC_RealArenaTrusteeshipReq, resp *pb.RPC_RealArenaTrusteeshipResp) (err error) { var ( room *Room ok bool ) this.lock.RLock() room, ok = this.rooms[req.RoomId] this.lock.RUnlock() if ok { room.UserOffline(req.OffUid) } return } //战斗结束 func (this *RealArena) ChallengeResults(roomid, red, bule string, winSide int32) { this.Debug("ChallengeResults", log.Field{Key: "roomid", Value: roomid}) var ( room *Room ok bool ) this.lock.Lock() room, ok = this.rooms[roomid] delete(this.rooms, roomid) this.lock.Unlock() if ok { room.ChallengeResults(winSide) } }