package catchbugs import ( "encoding/json" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/pb" cfg "go_dreamfactory/sys/configure/structs" "go.mongodb.org/mongo-driver/bson/primitive" ) func NewModule() core.IModule { m := new(CatchBugs) return m } /* 模块名称:捉虫子 */ type CatchBugs struct { modules.ModuleBase service comm.IService api *apiComp configure *configureComp model *modelComp rooms *roomsComp gameInvite comm.IGameInvite } // 模块名 func (this *CatchBugs) GetType() core.M_Modules { return comm.ModuleCatchbugs } // 模块初始化接口 注册用户创建角色事件 func (this *CatchBugs) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) { err = this.ModuleBase.Init(service, module, options) this.service = service.(comm.IService) return } func (this *CatchBugs) Start() (err error) { if err = this.ModuleBase.Start(); err != nil { return } var module core.IModule if module, err = this.service.GetModule(comm.ModuleGameInvite); err != nil { return } this.gameInvite = module.(comm.IGameInvite) return } func (this *CatchBugs) OnInstallComp() { this.ModuleBase.OnInstallComp() this.api = this.RegisterComp(new(apiComp)).(*apiComp) this.configure = this.RegisterComp(new(configureComp)).(*configureComp) this.model = this.RegisterComp(new(modelComp)).(*modelComp) this.rooms = this.RegisterComp(new(roomsComp)).(*roomsComp) } func (this *CatchBugs) CreateRoom(sessions []comm.IUserSession, rulesStr string) (roomid string, err error) { var ( rules *pb.DBCatchBugsRules = &pb.DBCatchBugsRules{} reduser, blueuser *pb.DBUser redinfo, blueinfo *pb.DBCatchBugs redplayer, blueplayer *pb.DBCatchBugsPlayer ) if err = json.Unmarshal([]byte(rulesStr), rules); err != nil { this.Error("解析规则json", log.Field{Key: "err", Value: err.Error()}) return } //发起者 red reduser, err = this.ModuleUser.GetUser(sessions[0].GetUserId()) if err != nil { this.Error("未找到红方信息", log.Field{Key: "uid", Value: sessions[0].GetUserId()}) return } blueuser, err = this.ModuleUser.GetUser(sessions[1].GetUserId()) if err != nil { this.Error("未找到蓝方信息", log.Field{Key: "uid", Value: sessions[1].GetUserId()}) return } if redinfo, err = this.model.getModel(sessions[0].GetUserId()); err != nil { this.Error("未找到红方信息", log.Field{Key: "uid", Value: sessions[0].GetUserId()}) return } if blueinfo, err = this.model.getModel(sessions[0].GetUserId()); err != nil { this.Error("未找到蓝方信息", log.Field{Key: "uid", Value: sessions[0].GetUserId()}) return } redplayer = &pb.DBCatchBugsPlayer{ Info: comm.GetUserBaseInfo(reduser), Integral: redinfo.Integral, Accruedintegral: redinfo.Accruedintegral, Books: redinfo.Books, } blueplayer = &pb.DBCatchBugsPlayer{ Info: comm.GetUserBaseInfo(blueuser), Integral: blueinfo.Integral, Accruedintegral: blueinfo.Accruedintegral, Books: blueinfo.Books, } roomid, err = this.createRoom(rules, redplayer, blueplayer, sessions) return } func (this *CatchBugs) createRoom(rules *pb.DBCatchBugsRules, red, blue *pb.DBCatchBugsPlayer, sessions []comm.IUserSession) (roomid string, err error) { var ( confs []*cfg.GameCatchbugLllustratedData cardsTemp []*pb.DBCatchBugsCard cards []*pb.DBCatchBugsCard backup []*pb.DBCatchBugsCard weights []int32 room *Room ) if confs, err = this.configure.getGameCatchbugLllustratedDatas(); err != nil { this.Error("配置未找到", log.Field{Key: "err", Value: err.Error()}) return } for _, v := range confs { weights = append(weights, v.Weights) } results := comm.GetRandWs(weights, 24) for i, v := range results { cardsTemp = append(cardsTemp, &pb.DBCatchBugsCard{ Id: int32(1000 + i*2), Cid: confs[v].Id, }, &pb.DBCatchBugsCard{ Id: int32(1001 + i*2), Cid: confs[v].Id, }) } indexs := comm.RandShuffle(24) cards = make([]*pb.DBCatchBugsCard, 24) for i, v := range indexs { cards[i] = cardsTemp[v] cards[i].Index = int32(i) } indexs = comm.RandShuffle(24) backup = make([]*pb.DBCatchBugsCard, 24) for i, v := range indexs { backup[i] = cardsTemp[24+v] backup[i].Index = int32(i) } roomid = primitive.NewObjectID().Hex() if room, err = this.rooms.newRoom(&pb.DBCatchBugsRoom{ Rid: roomid, Rules: rules, Red: red, Blue: blue, Backup: backup, Card: cards, }, sessions); err != nil { this.Error("创建房间错误", log.Field{Key: "err", Value: err.Error()}) return } go room.GameStart() return } //用户离线 func (this *CatchBugs) UserOffline(roomid string, uid string) (err error) { var ( room *Room ) if room, err = this.rooms.queryRoom(roomid); err != nil { return } err = room.PlayerOffline(uid) return } //用户离线 func (this *CatchBugs) AdmitDefeat(roomid string, uid string) (err error) { var ( room *Room ) if room, err = this.rooms.queryRoom(roomid); err != nil { return } err = room.PlayerOffline(uid) return } // 红点需求 func (this *CatchBugs) Reddot(session comm.IUserSession, rid map[comm.ReddotType]struct{}) (items map[comm.ReddotType]*pb.ReddotItem) { var ( selfrid []comm.ReddotType = []comm.ReddotType{comm.Reddot41101} confs []*cfg.GameCatchbugRewardData info *pb.DBCatchBugs err error ok bool ) items = make(map[comm.ReddotType]*pb.ReddotItem) for _, v := range selfrid { if _, ok = rid[v]; ok { break } } if !ok { return } if confs, err = this.configure.getGameGColorRewardDatas(); err != nil { return } if info, err = this.model.getCorssModel(session.GetUserId()); err != nil { return } for _, v := range selfrid { if _, ok = rid[v]; ok { switch v { case comm.Reddot41101: for _, conf := range confs { if conf.Type == 1 { if _, ok = info.Weekaward[conf.Key]; !ok && info.Integral >= conf.Condition { items[comm.Reddot41101] = &pb.ReddotItem{ Rid: int32(comm.Reddot41101), Activated: true, } break } } else { if _, ok = info.Allaward[conf.Key]; !ok && info.Accruedintegral >= conf.Condition { items[comm.Reddot41101] = &pb.ReddotItem{ Rid: int32(comm.Reddot41101), Activated: true, } break } } } break } } } return }