go_dreamfactory/modules/parkour/module.go
2023-04-18 15:52:46 +08:00

130 lines
3.3 KiB
Go

package parkour
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(Parkour)
return m
}
type Parkour struct {
modules.ModuleBase
service base.IRPCXService
api *apiComp
configure *configureComp
teamComp *ModelTeamComp
matchComp *ModelMatchComp
parkourComp *ModelParkourComp
lock sync.RWMutex
battles map[string]*RaceItem
}
//模块名
func (this *Parkour) GetType() core.M_Modules {
return comm.ModuleParkour
}
//模块初始化
func (this *Parkour) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service.(base.IRPCXService)
this.battles = map[string]*RaceItem{}
return
}
func (this *Parkour) Start() (err error) {
err = this.ModuleBase.Start()
this.service.RegisterFunctionName(string(comm.RPC_ParkourMatchSucc), this.createbattle)
return
}
//装备组件
func (this *Parkour) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
this.teamComp = this.RegisterComp(new(ModelTeamComp)).(*ModelTeamComp)
this.matchComp = this.RegisterComp(new(ModelMatchComp)).(*ModelMatchComp)
this.parkourComp = this.RegisterComp(new(ModelParkourComp)).(*ModelParkourComp)
}
//匹配成功 创建战斗
func (this *Parkour) createbattle(ctx context.Context, req *pb.RPCParkourMatchSuccReq, resp *pb.RPCParkourMatchSuccResp) (err error) {
var (
race *pb.DBRace
battle *RaceItem
sessions []comm.IUserSession = make([]comm.IUserSession, 0)
teams []*pb.DBRaceTeam
users []string = make([]string, 0)
)
this.Debug("createbattle", log.Field{Key: "req", Value: req.String()})
race = &pb.DBRace{
Id: primitive.NewObjectID().Hex(),
ServicePath: fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()),
Redmember: req.Red,
Bulemember: req.Bule,
}
battle = &RaceItem{
Id: race.Id,
RedSession: make([]comm.IUserSession, 0),
BuleSession: make([]comm.IUserSession, 0),
}
for _, v := range req.Red {
users = append(users, v.Uid)
session, online := this.GetUserSession(v.Uid)
v.Isoff = !online
if online {
battle.RedSession = append(battle.RedSession, session)
}
}
battle.RedMember = req.Red
for _, v := range req.Bule {
users = append(users, v.Uid)
session, online := this.GetUserSession(v.Uid)
v.Isoff = !online
if online {
battle.BuleSession = append(battle.BuleSession, session)
}
}
battle.BuleMember = req.Bule
if teams, err = this.teamComp.queryteams(users); err != nil {
return
}
for _, v := range teams {
v.State = pb.RaceTeamState_raceing
}
if err = this.parkourComp.addrace(race); err != nil {
return
}
this.lock.Lock()
this.battles[race.Id] = battle
this.lock.Unlock()
if err = this.SendMsgToSession(string(comm.ModulePvp), "racematchsucc", &pb.ParkourRaceMatchSuccPush{
Race: race,
}, sessions...); err != nil {
this.Errorln(err)
}
return
}