From bc838717eb3b7d3908f8c3a8230f13275a52053d Mon Sep 17 00:00:00 2001 From: hujiebin Date: Mon, 21 Aug 2023 12:46:33 +0800 Subject: [PATCH] feat:redis queue send gift --- cron/cron.go | 7 +- cron/gift_cron/send_gift_redis.go | 90 ++++++++++++++++++ domain/cache/gift_c/gift_event.go | 34 +++++++ domain/service/event_s/event_init.go | 133 +++++++++++++-------------- 4 files changed, 193 insertions(+), 71 deletions(-) create mode 100644 cron/gift_cron/send_gift_redis.go create mode 100644 domain/cache/gift_c/gift_event.go diff --git a/cron/cron.go b/cron/cron.go index 9ca7183..b55c58f 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -11,12 +11,13 @@ func Init() { if !config.IsMaster() { return } - gift_cron.SendGiftEventInit() // 礼物消息 - gift_cron.GiftRemark() // 礼物消息补偿 + //gift_cron.SendGiftEventInit() // 礼物消息 + //gift_cron.GiftRemark() // 礼物消息补偿 + gift_cron.SendGiftEvent() // 礼物事件 mic_cron.OnMicCheck() // 检查上麦 group_cron.GroupPowerExpClear() // 清理家族经验/等级 group_cron.GroupPowerMonthRankAct() //group_cron.GroupInEventInit() // 进房事件 - group_cron.GroupPowerGradeExp() // 家族升级 + group_cron.GroupPowerGradeExp() // 家族升级(上麦事件会用到) todo group_cron.CreateGroup() // } diff --git a/cron/gift_cron/send_gift_redis.go b/cron/gift_cron/send_gift_redis.go new file mode 100644 index 0000000..cc9f581 --- /dev/null +++ b/cron/gift_cron/send_gift_redis.go @@ -0,0 +1,90 @@ +package gift_cron + +import ( + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/resource/mysql" + "hilo-group/_const/enum/groupPower_e" + "hilo-group/domain/cache/gift_c" + "hilo-group/domain/event/gift_ev" + "hilo-group/domain/model/groupPower_m" +) + +// 送礼事件 +func SendGiftEvent() { + //if !config.IsMaster() { + // return + //} + go func() { + for true { + model := domain.CreateModelNil() + if sendGiftEvent := gift_c.BLPopQueueSendGift(model); sendGiftEvent != nil { + groupPowerGrade(model, sendGiftEvent) // 家族经验 + groupPowerStar(model, sendGiftEvent) // 家族之星 + } + } + }() +} + +// 群组势力经验 +func groupPowerGrade(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) { + model.Log.Infof("AddSendGiftEventAsync %+v", sendGiftEvent) + if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow { + return + } + exist, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, sendGiftEvent.SendUserId) + if err != nil { + model.Log.Infof("CheckGroupPowerUser fail %+v", err) + return + } + if exist { + exp := sendGiftEvent.GiftN * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) * sendGiftEvent.ResGift.DiamondNum + //return model.Transaction(func(model *domain.Model) error { + // return groupPower_m.IncrGroupPowerExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼") + //}) + //return groupPower_c.QueueGroupPowerGradeExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼") + if err := model.Transaction(func(model *domain.Model) error { + return groupPower_m.IncrGroupPowerExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼") + }); err != nil { + model.Log.Errorf("IncrGroupPowerExp fail,data:%v-err:%v", *sendGiftEvent, err) + } else { + model.Log.Infof("IncrGroupPowerExp success,data:%v", *sendGiftEvent) + } + } +} + +// 家族之星 +func groupPowerStar(model *domain.Model, sendGiftEvent *gift_ev.SendGiftEvent) { + if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow { + return + } + var userIds = []mysql.ID{sendGiftEvent.SendUserId} + userIds = append(userIds, sendGiftEvent.ReceiveUserIds...) + groupPowers, err := groupPower_m.BatchGetGroupPowerUser(model, userIds) + if err != nil { + model.Log.Errorf("AddSendGiftEventAsync fail:%v", err) + return + } + // 送礼加分 + if data, ok := groupPowers[sendGiftEvent.SendUserId]; ok { + diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) + if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { + model.Log.Errorf("IncrGroupPowerMonthStarScore famous fail:%v", err) + } + if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { + model.Log.Errorf("IncrGroupPowerDayStarScore famous fail:%v", err) + } + } + // 收礼加分 + for _, userId := range sendGiftEvent.ReceiveUserIds { + if data, ok := groupPowers[userId]; ok { + diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum + if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { + model.Log.Errorf("IncrGroupPowerMonthStarScore charm fail:%v", err) + } + if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { + model.Log.Errorf("IncrGroupPowerDayStarScore charm fail:%v", err) + } + } + } + return +} diff --git a/domain/cache/gift_c/gift_event.go b/domain/cache/gift_c/gift_event.go new file mode 100644 index 0000000..5e9868b --- /dev/null +++ b/domain/cache/gift_c/gift_event.go @@ -0,0 +1,34 @@ +package gift_c + +import ( + "encoding/json" + "git.hilo.cn/hilo-common/domain" + "github.com/go-redis/redis/v8" + "hilo-group/domain/event/gift_ev" + "time" +) + +const EventSendGiftHiloGroupQueue = "send:gift:queue:hilo_group" + +// redis pop event sendGift +func BLPopQueueSendGift(model *domain.Model) *gift_ev.SendGiftEvent { + var res *gift_ev.SendGiftEvent + queue := EventSendGiftHiloGroupQueue + strs, err := model.Redis.BLPop(model, time.Second, queue).Result() + if err != nil { + if err != redis.Nil { + model.Log.Errorf("BLPopQueueSendGift fail:%v", err) + } + return nil + } + if len(strs) >= 2 { + content := strs[1] + res = new(gift_ev.SendGiftEvent) + if err := json.Unmarshal([]byte(content), res); err != nil { + model.Log.Errorf("BLPopQueueSendGift json fail:%v", err) + return nil + } + return res + } + return nil +} diff --git a/domain/service/event_s/event_init.go b/domain/service/event_s/event_init.go index d64f653..6f9d894 100644 --- a/domain/service/event_s/event_init.go +++ b/domain/service/event_s/event_init.go @@ -6,13 +6,10 @@ import ( "git.hilo.cn/hilo-common/resource/mysql" "git.hilo.cn/hilo-common/rpc" "git.hilo.cn/hilo-common/sdk/tencentyun" - "hilo-group/_const/enum/groupPower_e" "hilo-group/_const/enum/group_e" "hilo-group/_const/enum/msg_e" "hilo-group/_const/enum/task_e" - "hilo-group/domain/cache/groupPower_c" "hilo-group/domain/cache/group_c" - "hilo-group/domain/event/gift_ev" "hilo-group/domain/event/group_ev" "hilo-group/domain/event/group_power_ev" "hilo-group/domain/event/mic_ev" @@ -35,7 +32,7 @@ func EventInit() { GroupEvents() GroupImMass() GroupTheme() - SendGift() // 送礼事件 + //SendGift() // 送礼事件 OnMic() // 在麦上事件 GroupInMicChangeEvent() // 用户进房推送mic位置信息 } @@ -472,71 +469,71 @@ func FlushHiloInfo(extId string, isVip bool, isPrettyCode bool, medals []uint32, func SendGift() { // 送礼事件-势力经验 - gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error { - sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent) - if !ok { - model.Log.Errorf("AddSendGiftEventAsync event type err") - return nil - } - model.Log.Infof("AddSendGiftEventAsync %+v", sendGiftEvent) - if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow { - return nil - } - exist, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, sendGiftEvent.SendUserId) - if err != nil { - model.Log.Infof("CheckGroupPowerUser fail %+v", err) - return err - } - if exist { - exp := sendGiftEvent.GiftN * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) * sendGiftEvent.ResGift.DiamondNum - //return model.Transaction(func(model *domain.Model) error { - // return groupPower_m.IncrGroupPowerExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼") - //}) - return groupPower_c.QueueGroupPowerGradeExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼") - } - return nil - }) + //gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error { + // sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent) + // if !ok { + // model.Log.Errorf("AddSendGiftEventAsync event type err") + // return nil + // } + // model.Log.Infof("AddSendGiftEventAsync %+v", sendGiftEvent) + // if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow { + // return nil + // } + // exist, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, sendGiftEvent.SendUserId) + // if err != nil { + // model.Log.Infof("CheckGroupPowerUser fail %+v", err) + // return err + // } + // if exist { + // exp := sendGiftEvent.GiftN * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) * sendGiftEvent.ResGift.DiamondNum + // //return model.Transaction(func(model *domain.Model) error { + // // return groupPower_m.IncrGroupPowerExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼") + // //}) + // return groupPower_c.QueueGroupPowerGradeExp(model, groupPowerId, exp, sendGiftEvent.SendUserId, "送礼") + // } + // return nil + //}) // 家族之星-送礼/收礼 - gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error { - sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent) - if !ok { - model.Log.Errorf("AddSendGiftEventAsync event type err") - return nil - } - if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow { - return nil - } - var userIds = []mysql.ID{sendGiftEvent.SendUserId} - userIds = append(userIds, sendGiftEvent.ReceiveUserIds...) - groupPowers, err := groupPower_m.BatchGetGroupPowerUser(model, userIds) - if err != nil { - model.Log.Errorf("AddSendGiftEventAsync fail:%v", err) - return err - } - // 送礼加分 - if data, ok := groupPowers[sendGiftEvent.SendUserId]; ok { - diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) - if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { - model.Log.Errorf("IncrGroupPowerMonthStarScore famous fail:%v", err) - } - if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { - model.Log.Errorf("IncrGroupPowerDayStarScore famous fail:%v", err) - } - } - // 收礼加分 - for _, userId := range sendGiftEvent.ReceiveUserIds { - if data, ok := groupPowers[userId]; ok { - diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum - if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { - model.Log.Errorf("IncrGroupPowerMonthStarScore charm fail:%v", err) - } - if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { - model.Log.Errorf("IncrGroupPowerDayStarScore charm fail:%v", err) - } - } - } - return nil - }) + //gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error { + // sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent) + // if !ok { + // model.Log.Errorf("AddSendGiftEventAsync event type err") + // return nil + // } + // if sendGiftEvent.ResGift.GiftType != mysql.DiamondYellow { + // return nil + // } + // var userIds = []mysql.ID{sendGiftEvent.SendUserId} + // userIds = append(userIds, sendGiftEvent.ReceiveUserIds...) + // groupPowers, err := groupPower_m.BatchGetGroupPowerUser(model, userIds) + // if err != nil { + // model.Log.Errorf("AddSendGiftEventAsync fail:%v", err) + // return err + // } + // // 送礼加分 + // if data, ok := groupPowers[sendGiftEvent.SendUserId]; ok { + // diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) + // if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { + // model.Log.Errorf("IncrGroupPowerMonthStarScore famous fail:%v", err) + // } + // if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { + // model.Log.Errorf("IncrGroupPowerDayStarScore famous fail:%v", err) + // } + // } + // // 收礼加分 + // for _, userId := range sendGiftEvent.ReceiveUserIds { + // if data, ok := groupPowers[userId]; ok { + // diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum + // if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { + // model.Log.Errorf("IncrGroupPowerMonthStarScore charm fail:%v", err) + // } + // if err := groupPower_m.IncrGroupPowerDayStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { + // model.Log.Errorf("IncrGroupPowerDayStarScore charm fail:%v", err) + // } + // } + // } + // return nil + //}) } func OnMic() { -- 2.22.0