go_dreamfactory/modules/battle/module.go
2023-02-11 00:09:10 +08:00

304 lines
7.9 KiB
Go

package battle
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"time"
)
/*
模块名:战斗模块
描述:处理游侠战斗相关业务
开发:李伟
*/
func NewModule() core.IModule {
m := new(Battle)
return m
}
type Battle struct {
modules.ModuleBase
service base.IRPCXService
options *Options
friend comm.IFriend //好友系统
moonfantasy comm.IMoonFantasy //月之秘境模块
pvp comm.IPvp //实时pvp
api_comp *apiComp
configure *configureComp
modelBattle *modelBattleComp
clientmgr *battleClientMgrComp //c#战斗客户端端管理
}
//模块名
func (this *Battle) GetType() core.M_Modules {
return comm.ModuleBattle
}
func (this *Battle) NewOptions() (options core.IModuleOptions) {
return new(Options)
}
//模块初始化接口 注册用户创建角色事件
func (this *Battle) 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.options = options.(*Options)
return
}
func (this *Battle) Start() (err error) {
err = this.ModuleBase.Start()
var module core.IModule
if module, err = this.service.GetModule(comm.ModuleFriend); err != nil {
return
}
this.friend = module.(comm.IFriend)
if module, err = this.service.GetModule(comm.ModuleMoonfantasy); err != nil {
return
}
this.moonfantasy = module.(comm.IMoonFantasy)
if module, err = this.service.GetModule(comm.ModulePvp); err != nil {
return
}
this.pvp = module.(comm.IPvp)
return
}
//装备组件
func (this *Battle) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
this.modelBattle = this.RegisterComp(new(modelBattleComp)).(*modelBattleComp)
this.clientmgr = this.RegisterComp(new(battleClientMgrComp)).(*battleClientMgrComp)
}
//查询战斗记录
func (this *Battle) QueryBattleRecord(oid string) (code pb.ErrorCode, record *pb.DBBattleRecord) {
var err error
if record, err = this.modelBattle.queryrecord(oid); err != nil {
code = pb.ErrorCode_BattleNoFoundRecord
return
}
return
}
//创建pve战斗
func (this *Battle) CreateEveBattle(session comm.IUserSession, req *pb.BattleEVEReq) (code pb.ErrorCode, record *pb.DBBattleRecord) {
var (
conn *db.DBConn
err error
)
if !this.IsCross() {
conn, err = db.Local()
} else {
conn, err = db.ServerDBConn(session.GetServiecTag())
}
if err != nil {
code = pb.ErrorCode_DBError
this.Errorf("session:%v err:", session, err)
return
}
if record, code = this.modelBattle.createeve(session, conn, pb.BattleType_eve, req); code != pb.ErrorCode_Success {
return
}
return
}
//创建pve战斗
func (this *Battle) CreatePveBattle(session comm.IUserSession, req *pb.BattlePVEReq) (code pb.ErrorCode, record *pb.DBBattleRecord) {
var (
conn *db.DBConn
err error
)
if !this.IsCross() {
conn, err = db.Local()
} else {
conn, err = db.ServerDBConn(session.GetServiecTag())
}
if err != nil {
code = pb.ErrorCode_DBError
this.Errorf("session:%v err:", session, err)
return
}
if req.Format == nil || req.Format.Format == nil || len(req.Format.Format) != 5 {
code = pb.ErrorCode_ReqParameterError
return
}
if req.Format.Friendformat != nil && len(req.Format.Friendformat) != 5 {
code = pb.ErrorCode_ReqParameterError
}
for _, v := range req.Format.Friendformat {
if v != "" {
this.ModuleRtask.SendToRtask(session, comm.Rtype108, 1)
break
}
}
if record, code = this.modelBattle.createpve(session, conn, pb.BattleType_pve, req); code != pb.ErrorCode_Success {
return
}
return
}
//创建pve战斗
func (this *Battle) CreatePvbBattle(session comm.IUserSession, req *pb.BattlePVBReq) (code pb.ErrorCode, record *pb.DBBattleRecord) {
var (
conn *db.DBConn
err error
)
if !this.IsCross() {
conn, err = db.Local()
} else {
conn, err = db.ServerDBConn(session.GetServiecTag())
}
if err != nil {
code = pb.ErrorCode_DBError
this.Errorf("session:%v err:", session, err)
return
}
if req.Format == nil || len(req.Format) == 0 {
code = pb.ErrorCode_ReqParameterError
return
}
if record, code = this.modelBattle.createpvb(session, conn, pb.BattleType_pvb, req); code != pb.ErrorCode_Success {
return
}
return
}
//创建pve战斗
func (this *Battle) CreatePvpBattle(session comm.IUserSession, req *pb.BattlePVPReq) (code pb.ErrorCode, record *pb.DBBattleRecord) {
var (
conn *db.DBConn
err error
)
if !this.IsCross() {
conn, err = db.Local()
} else {
conn, err = db.ServerDBConn(session.GetServiecTag())
}
if err != nil {
code = pb.ErrorCode_DBError
this.Errorf("session:%v err:", session, err)
return
}
if record, code = this.modelBattle.createpvp(session, conn, pb.BattleType_pvp, req); code != pb.ErrorCode_Success {
return
}
return
}
//只有跨服环境下才可使用
func (this *Battle) CreateRtPvpBattle(req *pb.BattleRTPVPReq) (code pb.ErrorCode, record *pb.DBBattleRecord) {
var (
redmodel *db.DBModel
bluemodel *db.DBModel
err error
)
if !this.IsCross() {
code = pb.ErrorCode_Exception
return
}
if redmodel, err = this.GetDBModuleByUid(req.RedCompId, comm.TableHero, time.Hour); err != nil {
code = pb.ErrorCode_DBError
return
}
if bluemodel, err = this.GetDBModuleByUid(req.BlueCompId, comm.TableHero, time.Hour); err != nil {
code = pb.ErrorCode_DBError
return
}
if record, code = this.modelBattle.creatertpvp(redmodel, bluemodel, pb.BattleType_rtpvp, req); code != pb.ErrorCode_Success {
return
}
return
}
//创建战斗服务
func (this *Battle) CreateBattleServer(req *pb.BattleInfo) (code pb.ErrorCode) {
this.Debug("CreateBattleServer", log.Field{Key: "req", Value: req})
var (
resp *pb.BattleCreateServerResp
err error
)
if resp, err = this.clientmgr.CreateBattle(context.Background(), &pb.BattleCreateServerReq{Info: req}); err != nil {
this.Errorln(err)
code = pb.ErrorCode_RpcFuncExecutionError
}
if !resp.Issucc {
code = pb.ErrorCode_BattleCreateFailed
}
return
}
func (this *Battle) InCmdBattle(req *pb.BattleInCmdReq) (code pb.ErrorCode) {
var (
resp *pb.BattleInCmdResp
err error
)
if resp, err = this.clientmgr.InCmdBattle(context.Background(), req); err != nil {
this.Errorln(err)
code = pb.ErrorCode_RpcFuncExecutionError
}
if !resp.Issucc {
code = pb.ErrorCode_BattleInCmdFailed
}
return
}
//pvp认输
func (this *Battle) ConcedeBattle(req *pb.BattleConcedeReq) (code pb.ErrorCode) {
var (
resp *pb.BattleConcedeResp
err error
)
if resp, err = this.clientmgr.ConcedeBattle(context.Background(), req); err != nil {
this.Errorln(err)
code = pb.ErrorCode_RpcFuncExecutionError
}
if !resp.Issucc {
code = pb.ErrorCode_BattleInCmdFailed
}
return
}
//校验战报是否成功
func (this *Battle) CheckBattleReport(session comm.IUserSession, report *pb.BattleReport) (code pb.ErrorCode, iswin bool) {
var (
reply *pb.BattleCheckResults
err error
)
if report == nil {
iswin = false
return
}
if this.options.OpenCheck {
stime := time.Now()
if reply, err = this.clientmgr.CheckBattle(context.Background(), report); err != nil || !reply.Ischeck {
code = pb.ErrorCode_BattleValidationFailed
this.Error("[Battle Check]",
log.Field{Key: "t", Value: time.Since(stime).Milliseconds()},
log.Field{Key: "reply", Value: reply.String()},
log.Field{Key: "err", Value: err.Error()},
)
return
}
this.Debug("[Battle Check]",
log.Field{Key: "t", Value: time.Since(stime).Milliseconds()},
log.Field{Key: "reply", Value: reply.String()},
)
}
this.moonfantasy.Trigger(session, report)
for _, v := range report.Completetask {
this.ModuleRtask.SendToRtask(session, comm.Rtype157, int32(report.Info.Ptype), v)
}
return pb.ErrorCode_Success, true
}