go_dreamfactory/modules/viking/module.go
2023-05-25 17:11:36 +08:00

348 lines
10 KiB
Go

/*
模块名:viking
描述:维京远征
开发:梅雄风
*/
package viking
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/redis/pipe"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"math"
"strconv"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type Viking struct {
modules.ModuleBase
modelViking *modelViking
api *apiComp
configure *configureComp
modulerank *ModelRank
battle comm.IBattle
service core.IService
}
func NewModule() core.IModule {
return &Viking{}
}
func (this *Viking) GetType() core.M_Modules {
return comm.ModuleViking
}
func (this *Viking) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service
return
}
func (this *Viking) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelViking = this.RegisterComp(new(modelViking)).(*modelViking)
this.modulerank = this.RegisterComp(new(ModelRank)).(*ModelRank)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
// 接口信息
func (this *Viking) ModifyVikingData(uid string, data map[string]interface{}) (code pb.ErrorCode) {
err := this.modelViking.modifyVikingDataByObjId(uid, data)
if err != nil {
code = pb.ErrorCode_DBError
}
return
}
func (this *Viking) 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 *Viking) CheckUserBaseVikingInfo(uid string) (data []*pb.DBVikingRank) {
list, err := this.modelViking.getVikingList(uid)
if err != nil {
for k := range list.Boss {
_d := this.modulerank.getVikingRankListByBossType(uid, k)
if _d != nil {
data = append(data, _d)
}
}
}
return
}
// 记录数据存在跨服
func (this *Viking) CheckRank(uid string, boosID int32, difficulty int32, report *pb.BattleReport, userinfo *pb.DBUser) {
conn_, _ := db.Cross() // 获取跨服数据库对象
model := db.NewDBModel(comm.TableVikingRank, 0, conn_)
costTime := report.Costtime
szLine := make([]*pb.LineUp, len(report.Info.Redflist[0].Team))
Leadpos := 0
if report != nil && report.Info != nil && len(report.Info.Redflist) > 0 {
costTime = report.Costtime
Leadpos = int(report.Info.Redflist[0].Leadpos)
for i, v := range report.Info.Redflist[0].Team {
if v != nil {
szLine[i] = &pb.LineUp{
Cid: v.HeroID,
Star: v.Star,
Lv: v.Lv,
}
}
}
}
// 写入排行榜
objID := ""
bFind := false
ranks := make([]*pb.DBVikingRank, 0)
model.GetList(uid, &ranks)
for _, v := range ranks {
if v.Bosstype == boosID {
mapRankData := make(map[string]interface{}, 0)
mapRankData["difficulty"] = difficulty
mapRankData["bosstype"] = boosID
mapRankData["Leadpos"] = Leadpos
mapRankData["line"] = szLine
mapRankData["costTime"] = costTime
model.ChangeList(uid, v.Id, mapRankData)
objID = v.Id
bFind = true
break
}
}
if !bFind {
new := &pb.DBVikingRank{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Difficulty: difficulty,
Bosstype: boosID,
Nickname: userinfo.Name,
Icon: "",
Lv: userinfo.Lv,
Leadpos: int32(Leadpos),
Line: szLine,
CostTime: costTime,
}
objID = new.Id
model.AddList(uid, new.Id, new)
}
var (
pipe *pipe.RedisPipe = conn_.Redis.RedisPipe(context.TODO())
menbers *redis.Z
tableName string
score int64
)
score = int64(difficulty)<<31 + int64(math.MaxInt32-costTime)
tableName = "vikingRank" + strconv.Itoa(int(boosID))
strKey := "vikingrank:" + uid + "-" + objID // 自定义key
menbers = &redis.Z{Score: float64(score), Member: strKey}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {
dock, err1 := cmd.Result()
if err1 != nil {
this.Errorln(dock, err1)
}
}
if _, err := pipe.Exec(); err != nil {
this.Errorln(err)
return
}
}
//红点查询
func (this *Viking) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (reddot map[comm.ReddotType]bool) {
reddot = make(map[comm.ReddotType]bool)
for _, v := range rid {
switch v {
case comm.Reddot31:
reddot[comm.Reddot31] = this.modelViking.checkReddot31(session)
break
}
}
return
}
// 解锁远征所有难度
func (this *Viking) CompleteAllLevel(session comm.IUserSession) (code pb.ErrorCode) {
list, err := this.modelViking.getVikingList(session.GetUserId())
if err != nil {
code = pb.ErrorCode_DBError
return
}
mapData := make(map[string]interface{}, 0)
// 查配置获取每个Boss的最大难度
for k := range list.Boss {
for i := 1; ; i++ {
conf := this.configure.GetVikingBossConfigData(k, int32(i))
if conf == nil {
list.Boss[k] = int32(i - 1)
}
}
}
mapData["boss"] = list.Boss
code = this.ModifyVikingData(session.GetUserId(), mapData)
session.SendMsg(string(this.GetType()), VikingGetListResp, &pb.VikingGetListResp{Data: list})
return
}
func (this *Viking) AutoBuyTicket(session comm.IUserSession, bossId, difficulty int32) (code pb.ErrorCode) {
conf := this.configure.GetVikingBossConfigData(bossId, difficulty)
if code := this.CheckRes(session, conf.PsConsume); code != pb.ErrorCode_Success {
amount := int32(this.ModuleItems.QueryItemAmount(session.GetUserId(), conf.PsConsume[0].T)) // 获取当前数量
if amount < conf.PsConsume[0].N { // 数量不足直接购买
code, _ = this.api.Buy(session, &pb.VikingBuyReq{
Count: conf.PsConsume[0].N - amount,
})
}
}
return
}
func (this *Viking) AutoBattleInfo(session comm.IUserSession, battle *pb.BattleFormation, bossId, difficulty int32) (code pb.ErrorCode, battleInfo *pb.BattleInfo) {
cfgData := this.configure.GetVikingBossConfigData(bossId, difficulty)
if cfgData == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
code, record := this.battle.CreatePveBattle(session, &pb.BattlePVEReq{
Ptype: pb.PlayType_viking,
Title: "",
Format: battle,
Mformat: cfgData.Boss,
})
if code == pb.ErrorCode_Success {
battleInfo = &pb.BattleInfo{
Id: record.Id,
Title: record.Title,
Rulesid: cfgData.BattleReadyID,
Btype: record.Btype,
Ptype: record.Ptype, RedCompId: record.RedCompId,
Redflist: record.Redflist,
BlueCompId: record.BlueCompId,
Buleflist: record.Buleflist,
}
}
return
}
// 自动战斗完成
func (this *Viking) AutoBattleOver(session comm.IUserSession, Report *pb.BattleReport, autoBattle *pb.DBAutoBattle) (code pb.ErrorCode, atno []*pb.UserAtno) {
bossId := autoBattle.BossId
difficulty := autoBattle.Difficulty
atno = make([]*pb.UserAtno, 0)
conf := this.configure.GetVikingBossConfigData(bossId, difficulty)
tasks := make([]*comm.TaskParam, 0)
// costRes := this.ModuleTools.GetGlobalConf().VikingExpeditionCos
// if costRes == nil {
// code = pb.ErrorCode_ConfigNoFound
// return
// }
if code = this.ConsumeRes(session, conf.PsConsume, true); code != pb.ErrorCode_Success {
return
}
viking, err := this.modelViking.getVikingList(session.GetUserId())
if err != nil {
code = pb.ErrorCode_VikingBoosType
return
}
code, bWin := this.battle.CheckBattleReport(session, Report)
if !bWin { // 战斗失败了 直接返回
code = pb.ErrorCode_BattleNoWin
return
}
if autoBattle != nil {
if autoBattle.AutoBuy {
code = this.AutoBuyTicket(session, bossId, difficulty)
}
}
mapData := make(map[string]interface{}, 0)
vikingCfg := this.configure.GetVikingBossConfigData(bossId, difficulty)
if vikingCfg == nil {
code = pb.ErrorCode_ConfigNoFound
return
}
key := strconv.Itoa(int(bossId)) + "_" + strconv.Itoa(int(difficulty))
if viking.BossTime[key] == 0 { // 新关卡挑战通过 发放首通奖励
viking.Boss[bossId]++
mapData["boss"] = viking.Boss
if c, res1 := this.DispenseAtno(session, vikingCfg.Firstprize, true); c == pb.ErrorCode_Success {
atno = append(atno, res1...)
} else {
code = c
return
}
}
userinfo := this.ModuleUser.GetUser(session.GetUserId())
this.CheckRank(session.GetUserId(), bossId, difficulty, Report, userinfo)
reward := this.configure.GetDropReward(vikingCfg.Drop) // 获取掉落奖励
if c, res1 := this.DispenseAtno(session, reward, true); c == pb.ErrorCode_Success {
atno = append(atno, res1...)
} else {
code = c
return
}
mapData["bossTime"] = viking.BossTime // 更新时间
code = this.ModifyVikingData(session.GetUserId(), mapData)
if user := this.ModuleUser.GetUser(session.GetUserId()); user != nil {
if module, err := this.service.GetModule(comm.ModuleChat); err == nil {
module.(comm.IChat).SendSysChatToWorld(comm.ChatSystem14, nil, bossId, difficulty, user.Name)
}
}
// 随机任务统计
// this.ModuleRtask.SendToRtask(session, comm.Rtype73, difficulty, bossId, 1)
// this.ModuleRtask.SendToRtask(session, comm.Rtype74, difficulty, bossId)
// this.ModuleRtask.SendToRtask(session, comm.Rtype78, difficulty, bossId, Report.Costtime)
tasks = append(tasks, comm.GettaskParam(comm.Rtype73, 1, bossId, difficulty))
tasks = append(tasks, comm.GettaskParam(comm.Rtype74, difficulty, bossId))
tasks = append(tasks, comm.GettaskParam(comm.Rtype78, difficulty, bossId, Report.Costtime))
if Report != nil && Report.Info != nil && len(Report.Info.Redflist) > 0 {
for _, v := range Report.Info.Redflist[0].Team {
if v.Ishelp { // 判断是否有助战
// this.ModuleRtask.SendToRtask(session, comm.Rtype79, difficulty, bossId)
tasks = append(tasks, comm.GettaskParam(comm.Rtype78, difficulty, bossId, Report.Costtime))
break
}
}
}
this.ModuleRtask.TriggerTask(session.GetUserId(), tasks...)
return
}
func (this *Viking) CheckBattelParameter(session comm.IUserSession, battle *pb.BattleFormation, bossid, difficulty int32) (code pb.ErrorCode) {
code, _ = this.api.Challenge(session, &pb.VikingChallengeReq{
BossId: bossid,
Difficulty: difficulty,
Battle: battle,
})
return
}