138 lines
3.7 KiB
Go
138 lines
3.7 KiB
Go
package battle
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/pb"
|
|
"sync"
|
|
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/core/cbase"
|
|
"go_dreamfactory/lego/sys/log"
|
|
)
|
|
|
|
/*
|
|
战斗服务客户端组件
|
|
*/
|
|
type battleClientMgrComp struct {
|
|
cbase.ModuleCompBase
|
|
options *Options
|
|
service core.IService
|
|
module *Battle
|
|
mutexs []sync.Mutex
|
|
clinet *client
|
|
i int
|
|
}
|
|
|
|
//组件初始化接口
|
|
func (this *battleClientMgrComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.ModuleCompBase.Init(service, module, comp, options)
|
|
this.options = options.(*Options)
|
|
this.module = module.(*Battle)
|
|
this.mutexs = make([]sync.Mutex, len(this.options.BattleServerAddr))
|
|
return
|
|
}
|
|
|
|
func (this *battleClientMgrComp) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
if this.options.OpenCheck {
|
|
if this.clinet, err = newClient(this.options.BattleServerAddr, this, this.options.Log); err != nil {
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//关闭客户端连接对象
|
|
func (this *battleClientMgrComp) Shutdown(c *client) {
|
|
this.clinet = nil
|
|
this.module.Errorf("战斗校验服务被关闭!")
|
|
}
|
|
|
|
func (this *battleClientMgrComp) reconnect() (err error) {
|
|
if this.clinet != nil {
|
|
return
|
|
}
|
|
if this.clinet, err = newClient(this.options.BattleServerAddr, this, this.options.Log); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//Pvp 战斗指令输出
|
|
func (this *battleClientMgrComp) BattleOutCmd(out *pb.BattleOutCmdPush) {
|
|
this.module.pvp.PvpOutCmdPush(&pb.PvpOutCmdPush{
|
|
Battleid: out.Battleid,
|
|
Cmd: out.Cmd,
|
|
})
|
|
}
|
|
|
|
//Pvp 战斗指令输出
|
|
func (this *battleClientMgrComp) BattleFinish(out *pb.BattleFinishPush) {
|
|
this.module.pvp.PvpFinishPush(out)
|
|
}
|
|
|
|
//校验战斗结果
|
|
func (this *battleClientMgrComp) CheckBattle(ctx context.Context, req *pb.BattleReport) (reply *pb.BattleCheckResults, err error) {
|
|
reply = &pb.BattleCheckResults{}
|
|
if err = this.reconnect(); err != nil {
|
|
return
|
|
}
|
|
if err = this.clinet.callBattle(ctx, "Check", req, reply); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
//实时pvp战斗创建
|
|
func (this *battleClientMgrComp) GetInfo(ctx context.Context, req *pb.BattleGetInfoReq) (reply *pb.BattleGetInfoResp, err error) {
|
|
this.module.Debug("GetInfo", log.Field{Key: "req", Value: req})
|
|
if err = this.reconnect(); err != nil {
|
|
return
|
|
}
|
|
reply = &pb.BattleGetInfoResp{}
|
|
if err = this.clinet.callBattle(ctx, "GetInfo", req, reply); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
//实时pvp战斗创建
|
|
func (this *battleClientMgrComp) CreateBattle(ctx context.Context, req *pb.BattleCreateServerReq) (reply *pb.BattleCreateServerResp, err error) {
|
|
this.module.Debug("CreateBattle", log.Field{Key: "req", Value: req})
|
|
if err = this.reconnect(); err != nil {
|
|
return
|
|
}
|
|
reply = &pb.BattleCreateServerResp{}
|
|
if err = this.clinet.callBattle(ctx, "Create", req, reply); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
//实时pvp 输入指令输入
|
|
func (this *battleClientMgrComp) InCmdBattle(ctx context.Context, req *pb.BattleInCmdReq) (reply *pb.BattleInCmdResp, err error) {
|
|
this.module.Debug("InCmdBattle", log.Field{Key: "req", Value: req})
|
|
if err = this.reconnect(); err != nil {
|
|
return
|
|
}
|
|
reply = &pb.BattleInCmdResp{}
|
|
if err = this.clinet.callBattle(ctx, "InCmd", req, reply); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
//实时pvp 认输
|
|
func (this *battleClientMgrComp) ConcedeBattle(ctx context.Context, req *pb.BattleConcedeReq) (reply *pb.BattleConcedeResp, err error) {
|
|
this.module.Debug("ConcedeBattle", log.Field{Key: "req", Value: req})
|
|
if err = this.reconnect(); err != nil {
|
|
return
|
|
}
|
|
reply = &pb.BattleConcedeResp{}
|
|
if err = this.clinet.callBattle(ctx, "Concede", req, reply); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
return
|
|
}
|