go_dreamfactory/modules/hunting/module.go
2023-06-06 11:02:47 +08:00

210 lines
5.2 KiB
Go

package hunting
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"
"time"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type Hunting struct {
modules.ModuleBase
modelHunting *modelHunting
api *apiComp
configure *configureComp
modulerank *ModelRank
battle comm.IBattle
service core.IService
}
func NewModule() core.IModule {
return &Hunting{}
}
func (this *Hunting) GetType() core.M_Modules {
return comm.ModuleHunting
}
func (this *Hunting) 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 *Hunting) 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 *Hunting) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelHunting = this.RegisterComp(new(modelHunting)).(*modelHunting)
this.modulerank = this.RegisterComp(new(ModelRank)).(*ModelRank)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
// 接口信息
func (this *Hunting) ModifyHuntingData(uid string, data map[string]interface{}) (errdata *pb.ErrorData) {
err := this.modelHunting.modifyHuntingDataByObjId(uid, data)
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
}
return
}
func (this *Hunting) CheckUserBaseHuntingInfo(uid string) (data []*pb.DBHuntingRank) {
list, err := this.modelHunting.getHuntingList(uid)
if err != nil {
for k := range list.Boss {
_d := this.modulerank.getHuntingRankListByBossType(uid, k)
if _d != nil {
data = append(data, _d)
}
}
}
return
}
func (this *Hunting) CheckRank(uid string, boosID int32, difficulty int32, report *pb.BattleReport, userinfo *pb.DBUser) {
conn_, _ := db.Cross() // 获取跨服数据库对象
model := db.NewDBModel(comm.TableHuntingRank, time.Hour, 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.DBHuntingRank, 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.DBHuntingRank{
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 = "huntingRank" + strconv.Itoa(int(boosID))
strKey := "huntingRank:" + uid + "-" + objID
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 *Hunting) 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.Reddot32:
reddot[comm.Reddot32] = this.modelHunting.checkReddot32(session)
break
}
}
return
}
// 解锁远征所有难度
func (this *Hunting) CompleteAllLevel(session comm.IUserSession) (errdata *pb.ErrorData) {
list, err := this.modelHunting.getHuntingList(session.GetUserId())
if err != nil {
errdata = &pb.ErrorData{
Code: pb.ErrorCode_DBError,
Title: pb.ErrorCode_DBError.ToString(),
Message: err.Error(),
}
return
}
mapData := make(map[string]interface{}, 0)
// 查配置获取每个Boss的最大难度
for k := range list.Boss {
for i := 1; ; i++ {
conf, _ := this.configure.GetHuntingBossConfigData(k, int32(i))
if conf == nil {
list.Boss[k] = int32(i - 1)
}
}
}
mapData["boss"] = list.Boss
code = this.ModifyHuntingData(session.GetUserId(), mapData)
session.SendMsg(string(this.GetType()), HuntingGetListResp, &pb.HuntingGetListResp{Data: list})
return
}