249 lines
6.8 KiB
Go
249 lines
6.8 KiB
Go
package arena
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/lego/base"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"go_dreamfactory/lego/sys/mgo"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
)
|
|
|
|
var _ comm.IArena = (*Arena)(nil)
|
|
|
|
const moduleName = "竞技场"
|
|
|
|
/*
|
|
模块名:竞技场
|
|
描述:pvp相关业务
|
|
开发:李伟
|
|
*/
|
|
func NewModule() core.IModule {
|
|
m := new(Arena)
|
|
return m
|
|
}
|
|
|
|
type Arena struct {
|
|
modules.ModuleBase
|
|
service base.IRPCXService
|
|
mail comm.Imail
|
|
battle comm.IBattle
|
|
privilege comm.IPrivilege
|
|
api *apiComp
|
|
configure *configureComp
|
|
modelArena *modelArena
|
|
modelRank *modelRank
|
|
battlerecord comm.IBattleRecord // 战报模块
|
|
}
|
|
|
|
// 模块名
|
|
func (this *Arena) GetType() core.M_Modules {
|
|
return comm.ModuleArena
|
|
}
|
|
|
|
// 模块初始化接口 注册用户创建角色事件
|
|
func (this *Arena) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
|
if err = this.ModuleBase.Init(service, module, options); err != nil {
|
|
return
|
|
}
|
|
this.service = service.(base.IRPCXService)
|
|
return
|
|
}
|
|
func (this *Arena) Start() (err error) {
|
|
if err = this.ModuleBase.Start(); err != nil {
|
|
return
|
|
}
|
|
var module core.IModule
|
|
if module, err = this.service.GetModule(comm.ModuleBattle); err != nil {
|
|
return
|
|
}
|
|
this.battle = module.(comm.IBattle)
|
|
if module, err = this.service.GetModule(comm.ModuleMail); err != nil {
|
|
return
|
|
}
|
|
this.mail = module.(comm.Imail)
|
|
if module, err = this.service.GetModule(comm.ModulePrivilege); err != nil {
|
|
return
|
|
}
|
|
this.privilege = module.(comm.IPrivilege)
|
|
if module, err = this.service.GetModule(comm.ModuleBattleRecord); err != nil {
|
|
return
|
|
}
|
|
this.battlerecord = module.(comm.IBattleRecord)
|
|
this.service.RegisterFunctionName(string(comm.Rpc_ModuleArenaRaceSettlement), this.Rpc_ModuleArenaRaceSettlement)
|
|
this.service.RegisterFunctionName(string(comm.Rpc_ModuleArenaModifyIntegral), this.Rpc_ModuleArenaModifyIntegral)
|
|
return
|
|
}
|
|
|
|
// 装备组件
|
|
func (this *Arena) OnInstallComp() {
|
|
this.ModuleBase.OnInstallComp()
|
|
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
|
|
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
|
|
this.modelArena = this.RegisterComp(new(modelArena)).(*modelArena)
|
|
this.modelRank = this.RegisterComp(new(modelRank)).(*modelRank)
|
|
}
|
|
|
|
// 比赛结算
|
|
func (this *Arena) Rpc_ModuleArenaRaceSettlement(ctx context.Context, args *pb.EmptyReq, reply *pb.EmptyResp) (err error) {
|
|
this.Debug("Rpc_ModuleArenaRaceSettlement",
|
|
log.Field{Key: "args", Value: args.String()},
|
|
)
|
|
this.modelRank.raceSettlement()
|
|
return
|
|
}
|
|
|
|
// 修改用户积分
|
|
func (this *Arena) Rpc_ModuleArenaModifyIntegral(ctx context.Context, args *pb.RPCModifyIntegralReq, reply *pb.EmptyResp) (err error) {
|
|
this.Debug("Rpc_ModuleArenaModifyIntegral", log.Field{Key: "args", Value: args.String()})
|
|
var (
|
|
info *pb.DBArenaUser
|
|
)
|
|
//防止数据未初始化就修改数据
|
|
if info, err = this.modelArena.queryPlayerInfo(args.Uid); err != nil && err != mgo.MongodbNil {
|
|
this.Debug("Rpc_ModuleArenaModifyIntegral", log.Field{Key: "err", Value: err.Error()})
|
|
return
|
|
}
|
|
if err = this.modelArena.modifyIntegral(args.Uid, args.Integral); err != nil {
|
|
this.Debug("Rpc_ModuleArenaModifyIntegral", log.Field{Key: "err", Value: err.Error()})
|
|
return
|
|
}
|
|
err = this.modelRank.updateArenaRank(&pb.ArenaPlayer{
|
|
Uinfo: info.Uinfo,
|
|
Integral: args.Integral,
|
|
})
|
|
return
|
|
}
|
|
|
|
// 红点需求
|
|
func (this *Arena) Reddot(session comm.IUserSession, rid map[comm.ReddotType]struct{}) (items map[comm.ReddotType]*pb.ReddotItem) {
|
|
var (
|
|
selfrid []comm.ReddotType = []comm.ReddotType{comm.Reddot22100, comm.Reddot22102, comm.Reddot22202}
|
|
model *arenaModel
|
|
info *pb.DBArenaUser = &pb.DBArenaUser{}
|
|
activated bool
|
|
ticket int32
|
|
ok bool
|
|
err error
|
|
)
|
|
items = make(map[comm.ReddotType]*pb.ReddotItem)
|
|
for _, v := range selfrid {
|
|
if _, ok = rid[v]; ok {
|
|
break
|
|
}
|
|
}
|
|
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
if model, err = this.modelArena.getpandataModel(); err != nil {
|
|
this.Errorln(err)
|
|
return
|
|
}
|
|
|
|
if info, ticket, activated = model.reddot(session); info == nil {
|
|
return
|
|
}
|
|
|
|
for _, v := range selfrid {
|
|
if _, ok = rid[v]; ok {
|
|
switch v {
|
|
case comm.Reddot22100:
|
|
items[comm.Reddot22100] = &pb.ReddotItem{
|
|
Rid: int32(comm.Reddot22100),
|
|
}
|
|
if ticket == this.ModuleTools.GetGlobalConf().ArenaTicketCos.N {
|
|
items[comm.Reddot22100].Activated = true
|
|
}
|
|
break
|
|
case comm.Reddot22102:
|
|
items[comm.Reddot22102] = &pb.ReddotItem{
|
|
Rid: int32(comm.Reddot22102),
|
|
}
|
|
items[comm.Reddot22102].Activated = activated
|
|
break
|
|
|
|
case comm.Reddot22202:
|
|
items[comm.Reddot22202] = &pb.ReddotItem{
|
|
Rid: int32(comm.Reddot22202),
|
|
Activated: true,
|
|
Progress: ticket,
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *Arena) SetUserIntegral(session comm.IUserSession, Integral int32) (err error) {
|
|
if this.IsCross() {
|
|
err = this.modelArena.modifyIntegral(session.GetUserId(), Integral)
|
|
} else {
|
|
if _, err = this.service.AcrossClusterRpcGo(
|
|
context.Background(),
|
|
this.GetCrossTag(),
|
|
comm.Service_Worker,
|
|
string(comm.Rpc_ModuleArenaModifyIntegral),
|
|
&pb.RPCModifyIntegralReq{Uid: session.GetUserId(), Integral: Integral},
|
|
nil); err != nil {
|
|
this.Errorln(err)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// /获取竞技场匹配目标战斗阵型数据
|
|
func (this *Arena) GetMatcheBattleRoles(uid string) (captain int32, rules []*pb.BattleRole, err error) {
|
|
var (
|
|
global *cfg.GameGlobalData
|
|
info *pb.DBArenaUser
|
|
players []*pb.ArenaPlayer
|
|
ais []*pb.ArenaPlayer
|
|
errdata *pb.ErrorData
|
|
)
|
|
global = this.ModuleTools.GetGlobalConf()
|
|
if info, err = this.modelArena.queryPlayerInfo(uid); err != nil && err != mgo.MongodbNil {
|
|
return
|
|
}
|
|
if err == mgo.MongodbNil {
|
|
info = &pb.DBArenaUser{
|
|
Uinfo: &pb.BaseUserInfo{Uid: uid},
|
|
Integral: global.ArenaInitiaIntegral,
|
|
Isdef: false,
|
|
}
|
|
if info.Dan, err = this.modelArena.computedan(info.Integral); err != nil {
|
|
return
|
|
}
|
|
}
|
|
if players, err = this.modelArena.matchePlayer(info.Uinfo.Uid, info.Uinfo.Group, info.Dan, 1); err != nil && err != mgo.MongodbNil {
|
|
return
|
|
}
|
|
if len(players) > 0 {
|
|
captain = players[0].Defend.Leadpos
|
|
if rules, errdata = this.battle.CreateRolesByHeros(players[0].Defend.Formt); errdata != nil {
|
|
err = fmt.Errorf("Player CreateRolesByHeros fail:%v", errdata)
|
|
return
|
|
}
|
|
} else {
|
|
if ais, err = this.modelArena.matcheAI(info.Dan, 1); err != nil && err != mgo.MongodbNil {
|
|
return
|
|
}
|
|
if len(ais) > 0 {
|
|
captain = ais[0].Defend.Leadpos
|
|
if rules, errdata = this.battle.CreateRolesByHeros(ais[0].Defend.Formt); errdata != nil {
|
|
err = fmt.Errorf("AI CreateRolesByHeros fail:%v", errdata)
|
|
return
|
|
}
|
|
} else {
|
|
err = fmt.Errorf("matche fail!")
|
|
}
|
|
}
|
|
return
|
|
}
|