go_dreamfactory/modules/battletest/battleclientmgr.go
2024-02-29 17:52:44 +08:00

73 lines
1.7 KiB
Go

package battletest
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 *BattleTest
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.(*BattleTest)
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
log.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 {
log.Errorln(err)
return
}
return
}
//校验战斗结果
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 {
log.Errorln(err)
}
return
}