From 170688b4075c103ec09ceabce8b9f10f3ce71231 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Tue, 7 Mar 2023 19:50:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=A3=80=E6=9F=A5=E4=B8=8A=E9=BA=A6?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/cron.go | 2 + cron/mic_cron/on_mic.go | 39 +++++++++ domain/model/groupPower_m/groupPower.go | 19 +++++ domain/model/groupPower_m/group_grade.go | 101 +++++++++++++++++++++++ domain/service/group_mic_s/group_mic.go | 30 +++++++ 5 files changed, 191 insertions(+) create mode 100644 cron/mic_cron/on_mic.go diff --git a/cron/cron.go b/cron/cron.go index c739d21..edccdec 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -3,6 +3,7 @@ package cron import ( "git.hilo.cn/hilo-common/resource/config" "hilo-group/cron/gift_cron" + "hilo-group/cron/mic_cron" ) func Init() { @@ -11,4 +12,5 @@ func Init() { } gift_cron.SendGiftEventInit() // 礼物消息 gift_cron.GiftRemark() // 礼物消息补偿 + mic_cron.OnMicCheck() // 检查上麦 } diff --git a/cron/mic_cron/on_mic.go b/cron/mic_cron/on_mic.go new file mode 100644 index 0000000..3e84cf6 --- /dev/null +++ b/cron/mic_cron/on_mic.go @@ -0,0 +1,39 @@ +package mic_cron + +import ( + "git.hilo.cn/hilo-common/domain" + "github.com/robfig/cron" + "hilo-group/domain/model/group_m" + "hilo-group/domain/service/group_mic_s" +) + +func OnMicCheck() { + c := cron.New() + //一分钟执行一次 + spec := "0 */1 * * * ?" + _ = c.AddFunc(spec, func() { + //获取麦上的所有人 + var model = domain.CreateModelNil() + groupIds, err := group_m.GetMicHasInGroups() + if err != nil { + model.Log.Errorf("cron micIn GetMicHasInGroups err:%v", err) + return + } + for _, groupId := range groupIds { + //遍历每个麦位 + for i := 1; i <= 30; i++ { + micUser, err := group_m.GetMicUser(model, groupId, i) + if err != nil { + model.Log.Errorf("cron micIn group_m.GetMicUser err:%v", err) + } + if micUser != nil { + if err := group_mic_s.NewGroupPowerService(model.MyContext).GroupPowerOnMicExp(groupId, micUser.UserId, micUser.Timestamp); err != nil { + model.Log.Errorf("cron micIn GroupPowerOnMicExp err:%v", err) + } else { + model.Log.Infof("cron micIn GroupPowerOnMicExp success, groupId:%v, userId:%v", groupId, micUser.UserId) + } + } + } + } + }) +} diff --git a/domain/model/groupPower_m/groupPower.go b/domain/model/groupPower_m/groupPower.go index c0a71a8..f15a751 100644 --- a/domain/model/groupPower_m/groupPower.go +++ b/domain/model/groupPower_m/groupPower.go @@ -648,3 +648,22 @@ func GetUserGroupPower(model *domain.Model, userId uint64) (uint64, string, erro } return gpu.GroupPowerId, powerName, nil } + +// 获取势力下的所有群组 +func GetGroupPowerGroups(model *domain.Model, groupPowerId mysql.ID) ([]*group_m.GroupInfo, error) { + var res []*group_m.GroupInfo + var ownerIds []mysql.ID + if err := model.DB().Model(GroupPowerUser{}).Where("group_power_id = ?", groupPowerId).Scan(&ownerIds).Error; err != nil { + model.Log.Errorf("GetGroupPowerGroups fail:%v", err) + return nil, err + } + if len(ownerIds) <= 0 { + return res, nil + } + if err := model.DB().Model(group_m.GroupInfo{}).Where("owner in ?", ownerIds).Find(&res).Error; err != nil { + model.Log.Errorf("GetGroupPowerGroups fail:%v", err) + return nil, err + } + return res, nil +} + diff --git a/domain/model/groupPower_m/group_grade.go b/domain/model/groupPower_m/group_grade.go index 1b9a766..faf5d1f 100644 --- a/domain/model/groupPower_m/group_grade.go +++ b/domain/model/groupPower_m/group_grade.go @@ -28,6 +28,25 @@ type GroupPowerGrade struct { UpdatedTime time.Time `gorm:"->"` } +type GroupPowerOnMic struct { + Date string + GroupPowerId mysql.ID + UserId mysql.ID + Seconds int64 + LastCalTs int64 + CreatedTime time.Time `gorm:"->"` + UpdatedTime time.Time `gorm:"->"` +} + +type GroupPowerOnMicDetail struct { + Date string + GroupPowerId mysql.ID + UserId mysql.ID + MinuteTimes int // 第几个10分钟 + CreatedTime time.Time `gorm:"->"` + UpdatedTime time.Time `gorm:"->"` +} + // 增加家族经验 // 达到经验值之后升级 // 单进程同步执行,不考虑并发 @@ -85,3 +104,85 @@ func IncrGroupPowerExp(txModel *domain.Model, groupPowerId mysql.ID, exp int64) } return nil } + +// 获取势力用户上麦加经验记录 +func GetGroupPowerUserOnMicDetails(model *domain.Model, groupPowerId, userId mysql.ID) ([]*GroupPowerOnMicDetail, error) { + var res []*GroupPowerOnMicDetail + date := time.Now().Format("2006-01-02") + if err := model.DB().Model(GroupPowerOnMicDetail{}).Where("date = ? AND group_power_id = ? AND user_id = ?", date, groupPowerId, userId).Find(&res).Error; err != nil { + return res, err + } + return res, nil +} + +// 获取势力用户上麦记录 +func GetGroupPowerOnMic(model *domain.Model, groupPowerId, userId mysql.ID) (*GroupPowerOnMic, error) { + gpom := new(GroupPowerOnMic) + date := time.Now().Format("2006-01-02") + if err := model.DB().Model(GroupPowerOnMic{}).Where("date = ? AND group_power_id = ? AND user_id = ?", date, groupPowerId, userId).First(gpom).Error; err != nil { + if err == gorm.ErrRecordNotFound { + return &GroupPowerOnMic{Date: date, GroupPowerId: groupPowerId, UserId: userId}, nil + } + return nil, err + } + return gpom, nil +} + +const MaxMinuteTimes = 18 + +// 增加势力上麦经验 +func IncrGroupPowerExpOnMic(model *domain.Model, groupPowerId, userId mysql.ID, joinMicTimestamp int64) error { + // 获取用户上麦奖励历史 + onMicDetails, err := GetGroupPowerUserOnMicDetails(model, groupPowerId, userId) + if err != nil { + return err + } + numDetails := len(onMicDetails) + if numDetails >= MaxMinuteTimes { + // 上麦经验贡献值最多1800,1分钟100 + return nil + } + onMic, err := GetGroupPowerOnMic(model, groupPowerId, userId) + if err != nil { + return err + } + onMicSeconds := time.Now().Unix() - joinMicTimestamp + if onMic.LastCalTs == joinMicTimestamp { + onMicSeconds = onMicSeconds - int64(numDetails*600) // 扣除之前加过的时间 + } + var moreDetails []*GroupPowerOnMicDetail + totalMinuteTimes := int((onMic.Seconds + onMicSeconds) % 600) // 今天实际能加经验次数 + if totalMinuteTimes >= MaxMinuteTimes { + totalMinuteTimes = MaxMinuteTimes + } + if totalMinuteTimes > numDetails { + for minute := numDetails + 1; minute <= totalMinuteTimes; minute++ { + moreDetails = append(moreDetails, &GroupPowerOnMicDetail{ + Date: time.Now().Format("2006-01-02"), + GroupPowerId: groupPowerId, + UserId: userId, + MinuteTimes: minute, + }) + } + } + // 有更多麦上10分钟,可以加经验 + if len(moreDetails) > 0 { + for _, detail := range moreDetails { + // 添加明细,避免重复计算 + if err := model.DB().Model(GroupPowerOnMicDetail{}).Create(detail).Error; err != nil { + return err + } + // 每10分钟增加100点经验 + if err := IncrGroupPowerExp(model, groupPowerId, 100); err != nil { + return err + } + } + // 更新micExp信息 + onMic.Seconds = onMic.Seconds + onMicSeconds + onMic.LastCalTs = joinMicTimestamp + if err := model.DB().Save(onMic).Error; err != nil { + return err + } + } + return nil +} diff --git a/domain/service/group_mic_s/group_mic.go b/domain/service/group_mic_s/group_mic.go index f9a54ad..134daad 100644 --- a/domain/service/group_mic_s/group_mic.go +++ b/domain/service/group_mic_s/group_mic.go @@ -10,6 +10,7 @@ import ( "hilo-group/_const/enum/group_e" "hilo-group/_const/redis_key" "hilo-group/domain/event/group_ev" + "hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/group_m" "hilo-group/domain/service/signal_s" "hilo-group/myerr" @@ -287,3 +288,32 @@ func (s *GroupMicService) GroupIMMassByMgr(groupId string, userId uint64, extern }) }) } + +// 增加势力上麦经验 +func (s *GroupMicService) GroupPowerOnMicExp(groupId string, userId uint64, joinMicTimestamp int64) error { + var model = domain.CreateModelContext(s.svc.MyContext) + exists, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, userId) + if err != nil { + return err + } + if !exists { + return nil + } + // 获取势力下所有群组 + groups, err := groupPower_m.GetGroupPowerGroups(model, groupPowerId) + if err != nil { + return err + } + inGroup := false + for _, group := range groups { + if group.ImGroupId == groupId { + inGroup = true + break + } + } + if !inGroup { + return nil + } + // 增加势力上麦经验 + return groupPower_m.IncrGroupPowerExpOnMic(model, groupPowerId, userId, joinMicTimestamp) +} -- 2.22.0