258 lines
6.6 KiB
Go
258 lines
6.6 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/lego/sys/timewheel"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"sync"
|
|
"time"
|
|
|
|
"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
|
|
ai *aiComp
|
|
configure *configureComp
|
|
parkourComp *ModelParkourComp
|
|
raceComp *ModelRaceComp
|
|
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.ai = this.RegisterComp(new(aiComp)).(*aiComp)
|
|
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
|
|
this.parkourComp = this.RegisterComp(new(ModelParkourComp)).(*ModelParkourComp)
|
|
this.raceComp = this.RegisterComp(new(ModelRaceComp)).(*ModelRaceComp)
|
|
}
|
|
|
|
//添加坐骑资源
|
|
func (this *Parkour) AddMounts(session comm.IUserSession, mounts map[string]int32, bPush bool) (code pb.ErrorCode) {
|
|
var (
|
|
result *pb.DBParkour
|
|
err error
|
|
)
|
|
if result, err = this.parkourComp.addUserMounts(session.GetUserId(), mounts); err != nil {
|
|
code = pb.ErrorCode_DBError
|
|
return
|
|
}
|
|
if bPush {
|
|
session.SendMsg(string(this.GetType()), "infochange", &pb.ParkourInfoChangePush{Info: result})
|
|
}
|
|
return
|
|
}
|
|
|
|
//匹配
|
|
func (this *Parkour) match(team *pb.DBParkour) (err error) {
|
|
err = this.service.RpcCall(
|
|
context.Background(),
|
|
comm.Service_Mainte,
|
|
string(comm.RPC_ParkourJoinMatch),
|
|
&pb.RPCParkourJoinMatchReq{Captainid: team.Captainid, Member: team.Member},
|
|
&pb.RPCParkourJoinMatchResp{})
|
|
if err != nil {
|
|
this.Errorln(err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//匹配成功 创建战斗
|
|
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)
|
|
)
|
|
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),
|
|
overtimer: timewheel.Add(time.Minute*3, this.overtimer, race.Id),
|
|
}
|
|
|
|
for _, v := range req.Red {
|
|
v.Hp = 6
|
|
if !v.Isai { //非AI
|
|
session, online := this.GetUserSession(v.Uid)
|
|
v.Isoff = !online
|
|
if online {
|
|
battle.RedSession = append(battle.RedSession, session)
|
|
}
|
|
}
|
|
|
|
}
|
|
sessions = append(sessions, battle.RedSession...)
|
|
battle.RedMember = req.Red
|
|
for _, v := range req.Bule {
|
|
v.Hp = 6
|
|
if !v.Isai { //非AI
|
|
session, online := this.GetUserSession(v.Uid)
|
|
v.Isoff = !online
|
|
if online {
|
|
battle.BuleSession = append(battle.BuleSession, session)
|
|
}
|
|
}
|
|
}
|
|
sessions = append(sessions, battle.BuleSession...)
|
|
battle.BuleMember = req.Bule
|
|
if err = this.raceComp.addrace(race); err != nil {
|
|
return
|
|
}
|
|
this.lock.Lock()
|
|
this.battles[race.Id] = battle
|
|
this.lock.Unlock()
|
|
if err = this.SendMsgToSession(string(this.GetType()), "racematchsucc", &pb.ParkourRaceMatchSuccPush{
|
|
Race: race,
|
|
}, sessions...); err != nil {
|
|
this.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *Parkour) startbattle(id string) {
|
|
this.Debug("startbattle", log.Field{Key: "id", Value: id})
|
|
var (
|
|
battle *RaceItem
|
|
ok bool
|
|
sessions []comm.IUserSession = make([]comm.IUserSession, 0)
|
|
err error
|
|
)
|
|
this.lock.RLock()
|
|
battle, ok = this.battles[id]
|
|
this.lock.RUnlock()
|
|
if ok {
|
|
sessions = append(sessions, battle.RedSession...)
|
|
sessions = append(sessions, battle.BuleSession...)
|
|
if err = this.SendMsgToSession(string(comm.ModulePvp), "finish", &pb.ParkourRaceStartPush{
|
|
Countdown: 3,
|
|
}, sessions...); err != nil {
|
|
this.Errorln(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *Parkour) shot(id string, uid string) {
|
|
this.Debug("shot", log.Field{Key: "id", Value: id})
|
|
var (
|
|
battle *RaceItem
|
|
ok bool
|
|
sessions []comm.IUserSession = make([]comm.IUserSession, 0)
|
|
err error
|
|
)
|
|
this.lock.RLock()
|
|
battle, ok = this.battles[id]
|
|
this.lock.RUnlock()
|
|
if ok {
|
|
sessions = append(sessions, battle.RedSession...)
|
|
sessions = append(sessions, battle.BuleSession...)
|
|
if err = this.SendMsgToSession(string(this.GetType()), "finish", &pb.ParkourRaceStartPush{
|
|
Countdown: 3,
|
|
}, sessions...); err != nil {
|
|
this.Errorln(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *Parkour) avoid(id string, uid string, dis int32) {
|
|
this.Debug("shot", log.Field{Key: "id", Value: id})
|
|
var (
|
|
battle *RaceItem
|
|
ok bool
|
|
sessions []comm.IUserSession = make([]comm.IUserSession, 0)
|
|
err error
|
|
)
|
|
this.lock.RLock()
|
|
battle, ok = this.battles[id]
|
|
this.lock.RUnlock()
|
|
if ok {
|
|
sessions = append(sessions, battle.RedSession...)
|
|
sessions = append(sessions, battle.BuleSession...)
|
|
if err = this.SendMsgToSession(string(this.GetType()), "finish", &pb.ParkourRaceStartPush{
|
|
Countdown: 3,
|
|
}, sessions...); err != nil {
|
|
this.Errorln(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
//战斗结束
|
|
func (this *Parkour) overtimer(task *timewheel.Task, args ...interface{}) {
|
|
this.Debug("shot", log.Field{Key: "id", Value: args})
|
|
var (
|
|
battle *RaceItem
|
|
ok bool
|
|
sessions []comm.IUserSession = make([]comm.IUserSession, 0)
|
|
err error
|
|
)
|
|
id := args[0].(string)
|
|
this.lock.RLock()
|
|
battle, ok = this.battles[id]
|
|
this.lock.RUnlock()
|
|
if ok {
|
|
this.lock.Lock()
|
|
delete(this.battles, id)
|
|
this.lock.Unlock()
|
|
this.raceComp.delrace(id)
|
|
sessions = append(sessions, battle.RedSession...)
|
|
sessions = append(sessions, battle.BuleSession...)
|
|
if err = this.SendMsgToSession(string(this.GetType()), "finish", &pb.ParkourRaceOverPush{
|
|
Race: &pb.DBRace{
|
|
Id: battle.Id,
|
|
},
|
|
}, sessions...); err != nil {
|
|
this.Errorln(err)
|
|
return
|
|
}
|
|
}
|
|
}
|