go_dreamfactory/modules/pvp/module.go
2023-02-08 18:45:36 +08:00

190 lines
4.8 KiB
Go

package pvp
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"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(Pvp)
return m
}
type Pvp struct {
modules.ModuleBase
service base.IRPCXService
battle comm.IBattle
api_comp *apiComp
lock sync.RWMutex
battles map[string]*BattleItem
}
//模块名
func (this *Pvp) GetType() core.M_Modules {
return comm.ModulePvp
}
//模块初始化接口 注册用户创建角色事件
func (this *Pvp) 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 = make(map[string]*BattleItem)
return
}
func (this *Pvp) Start() (err error) {
err = this.ModuleBase.Start()
var module core.IModule
if module, err = this.service.GetModule(comm.ModuleBattle); err != nil {
return
}
this.battle = module.(comm.IBattle)
return
}
//装备组件
func (this *Pvp) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp)
}
//创建Pvp
func (this *Pvp) CreatePvp(red, blue *pb.PvpUserInfo, ptype pb.PvpType) (battleId string, code pb.ErrorCode) {
var (
battle *BattleItem
err error
)
battleId = primitive.NewObjectID().Hex()
battle = &BattleItem{
Id: battleId,
Ptype: ptype,
State: pb.PvpState_ready,
Red: red,
Blue: blue,
readytimer: timewheel.Add(time.Second*15, this.readyTimeOut, battleId),
}
this.lock.Lock()
this.battles[battle.Id] = battle
this.lock.Unlock()
if err = this.SendMsgToUsers(string(comm.ModulePvp), "ready", &pb.PvpReadyPush{
ServicePath: fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()),
Battleid: battle.Id,
Red: battle.Red,
Blue: battle.Blue,
Countdown: 15,
}, red.Uid, blue.Uid); err != nil {
this.Errorln(err)
code = pb.ErrorCode_RpcFuncExecutionError
}
return
}
//推送战斗输出指令
func (this *Pvp) PvpOutCmdPush(out *pb.PvpOutCmdPush) {
}
//推送战斗结束
func (this *Pvp) PvpFinishPush(battleId string) {
}
//准备超时 取消战斗
func (this *Pvp) readyTimeOut(task *timewheel.Task, args ...interface{}) {
var (
id string
battle *BattleItem
ok bool
err error
)
id = args[0].(string)
this.lock.RLock()
battle, ok = this.battles[id]
this.lock.RUnlock()
if ok && battle.State == pb.PvpState_ready {
battle.lock.Lock()
battle.State = pb.PvpState_cancel
battle.lock.Unlock()
this.lock.Lock()
delete(this.battles, id)
this.lock.Unlock()
if err = this.SendMsgToUsers(string(comm.ModulePvp), "cancel", &pb.PvpCancelPush{
ServicePath: fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()),
Battleid: battle.Id,
}, battle.Red.Uid, battle.Blue.Uid); err != nil {
this.Errorln(err)
}
}
}
//开始战斗
func (this *Pvp) startBattle(battle *BattleItem) {
var (
record *pb.DBBattleRecord
info *pb.BattleInfo
code pb.ErrorCode
err error
)
if code, record = this.battle.CreateRtPvpBattle(&pb.BattleRTPVPReq{
Ptype: pb.PlayType_friendsmeet,
Title: "",
RedCompId: battle.Red.Uid,
Redformat: []*pb.BattleFormation{battle.Redformation},
BlueCompId: battle.Blue.Uid,
Bulefformat: []*pb.BattleFormation{battle.Blueformation},
}); code != pb.ErrorCode_Success {
this.lock.Lock()
delete(this.battles, battle.Id)
this.lock.Unlock()
if err = this.SendMsgToUsers(string(comm.ModulePvp), "cancel", &pb.PvpCancelPush{
ServicePath: fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()),
Battleid: battle.Id,
}, battle.Red.Uid, battle.Blue.Uid); err != nil {
this.Errorln(err)
}
} else {
record.Id = battle.Id
info = &pb.BattleInfo{
Id: record.Id,
Title: record.Title,
Btype: record.Btype,
Ptype: record.Ptype,
RedCompId: record.RedCompId,
Redflist: record.Redflist,
BlueCompId: record.BlueCompId,
Buleflist: record.Buleflist,
}
if code = this.battle.CreateBattleServer(info); code != pb.ErrorCode_Success {
this.lock.Lock()
delete(this.battles, battle.Id)
this.lock.Unlock()
if err = this.SendMsgToUsers(string(comm.ModulePvp), "cancel", &pb.PvpCancelPush{
ServicePath: fmt.Sprintf("%s/%s", this.service.GetType(), this.service.GetId()),
Battleid: battle.Id,
}, battle.Red.Uid, battle.Blue.Uid); err != nil {
this.Errorln(err)
}
} else {
if err = this.SendMsgToUsers(string(comm.ModulePvp), "start", &pb.PvpStartPush{
Info: info,
}, battle.Red.Uid, battle.Blue.Uid); err != nil {
this.Errorln(err)
}
}
}
}