go_dreamfactory/modules/catchbugs/module.go
2023-11-09 17:00:23 +08:00

160 lines
4.3 KiB
Go

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,
}
blueplayer = &pb.DBCatchBugsPlayer{
Info: comm.GetUserBaseInfo(blueuser),
Integral: blueinfo.Integral,
}
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
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)
}
roomid = primitive.NewObjectID().Hex()
if room, err = this.rooms.newRoom(&pb.DBCatchBugsRoom{
Rid: roomid,
Rules: rules,
Red: red,
Blue: blue,
Backup: cardsTemp[24:],
Card: cards,
}, sessions); err != nil {
this.Error("创建房间错误", log.Field{Key: "err", Value: err.Error()})
return
}
go room.GameStart()
return
}