go_dreamfactory/modules/sociaty/model_sociatyboss.go
2023-01-12 15:56:45 +08:00

200 lines
4.6 KiB
Go

package sociaty
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/lego/sys/redis/pipe"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type ModelSociatyBoss struct {
modules.MCompModel
moduleSociaty *Sociaty
service core.IService
}
func (this *ModelSociatyBoss) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.TableName = comm.TableSociatyBoss
err = this.MCompModel.Init(service, module, comp, options)
this.moduleSociaty = module.(*Sociaty)
this.service = service
return
}
// 设置阵容
func (s *ModelSociatyBoss) setFormation(sociaty *pb.DBSociaty, uid string, teams map[int32]*pb.ChallengeTeam) error {
if !s.moduleSociaty.modelSociaty.isMember(uid, sociaty) {
return comm.NewCustomError(pb.ErrorCode_SociatyBelongTo)
}
for _, m := range sociaty.Members {
if m.Uid == uid {
m.Teams = teams
}
}
update := map[string]interface{}{
"members": sociaty.Members,
}
return s.moduleSociaty.modelSociaty.updateSociaty(sociaty.Id, update)
}
//挑战开始
func (s *ModelSociatyBoss) challengestart(session comm.IUserSession, sociaty *pb.DBSociaty) error {
//阵容
var formations []*pb.BattleFormation
m := s.moduleSociaty.modelSociaty.getMemberInfo(sociaty, session.GetUserId())
for _, v := range m.Teams {
formations = append(formations, v.Formation)
}
if len(formations) == 0 {
return comm.NewCustomError(pb.ErrorCode_SociatyNoFormation)
}
iBattle, err := s.service.GetModule(comm.ModuleBattle)
if err != nil {
return err
}
if b, y := iBattle.(comm.IBattle); y {
b.CreatePvbBattle(session, &pb.BattlePVBReq{
Ptype: pb.PlayType_sociaty,
Title: "公会BOSS",
Format: formations,
})
}
return nil
}
// 挑战结束
func (s *ModelSociatyBoss) challengefinish(sociaty *pb.DBSociaty, uid string, report *pb.BattleReport) error {
// BOSS无伤害
if report.Harm == 0 {
return nil
}
//保存挑战记录
if err := s.addChallengeRecord(uid, &pb.DBSociatyBossRecord{
Uid: uid,
SociatyId: sociaty.Id,
Integral: s.transIntegral(report.Harm),
Duration: report.Costtime,
}); err != nil {
return err
}
return nil
}
// 挑战记录 作为阵容推荐的依据;
func (s *ModelSociatyBoss) addChallengeRecord(uid string, record *pb.DBSociatyBossRecord) error {
id := primitive.NewObjectID().Hex()
if err := s.AddList(uid, id, record); err != nil {
return err
}
return nil
}
// 记入排行
func (s *ModelSociatyBoss) addRank(uid string, integral int32) error {
var (
pipe *pipe.RedisPipe = s.Redis.RedisPipe(context.TODO())
m *redis.Z
)
m = &redis.Z{Score: float64(integral), Member: uid}
if cmd := pipe.ZAdd(s.TableName, m); cmd != nil {
_, err := cmd.Result()
if err != nil {
return err
}
}
if _, err := pipe.Exec(); err != nil {
return err
}
return nil
}
// 伤害转换积分
func (s *ModelSociatyBoss) transIntegral(harm int32) int64 {
return int64(harm)
}
// 查询排行榜
func (this *ModelSociatyBoss) queryRankUid(count int64) (ranks []string, err error) {
var (
result []string
)
if result, err = this.DBModel.Redis.ZRevRange(this.TableName, 0, count).Result(); err != nil {
return
}
ranks = make([]string, 0)
for i := 0; i < len(result); i += 1 {
ranks = append(ranks, result[i])
}
return
}
// 取积分
func (this *ModelSociatyBoss) getScoreByUid(uid string) int64 {
result, err := this.DBModel.Redis.ZScore(this.TableName, uid)
if err != nil {
return 0
}
return int64(result)
}
// 取排名
func (this *ModelSociatyBoss) getRankingByUid(uid string) int64 {
result, err := this.DBModel.Redis.ZRevRank(this.TableName, uid)
if err != nil {
return 0
}
return (result + 1)
}
// 排行榜
func (s *ModelSociatyBoss) rank(sociatyName string, rankType int32) (res []*pb.SociatyRankInfo) {
var rankCount int64
switch rankType {
case 1: //个人
rankCount = 1000
case 2: //成员
rankCount = 50
case 3: //公会
rankCount = 50
}
// 所有排行记录
rankUids, err := s.queryRankUid(rankCount)
if err != nil {
return nil
}
for _, uid := range rankUids {
imodule, err := s.service.GetModule(comm.ModuleUser)
if err != nil {
return nil
}
if iuser, ok := imodule.(comm.IUser); ok {
if user := iuser.GetUser(uid); user != nil && user.Uid != "" {
res = append(res, &pb.SociatyRankInfo{
SociatyName: sociatyName,
Name: user.Name,
Head: user.Avatar,
Lv: user.Lv,
Ranking: s.getRankingByUid(user.Uid),
Integral: s.getScoreByUid(user.Uid),
})
}
}
}
return nil
}