You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.3 KiB
121 lines
3.3 KiB
package call |
|
|
|
import ( |
|
"encoding/json" |
|
"fmt" |
|
"github.com/liangdas/mqant/log" |
|
"server/common" |
|
"server/db" |
|
"server/pb" |
|
"time" |
|
) |
|
|
|
func SetLuckyWheelRecords(uid int, luckType int, award *common.LuckyAward) { |
|
userInfo, _ := GetUserInfo(uid) |
|
err := db.Mysql().C().Model(&common.LuckyWheelReward{}).Create(&common.LuckyWheelReward{ |
|
UID: uid, |
|
Nick: userInfo.Nick, |
|
LuckyType: luckType, |
|
AwardType: award.AwardType, |
|
AwardCount: int(award.Currency.Value), |
|
SpeAwardType: award.WheelType, |
|
SpeAwardCount: award.WheelCount, |
|
UpdatedAt: time.Now().Unix(), |
|
}).Error |
|
if err != nil { |
|
log.Error("create luckyWheel record err, %s", err.Error()) |
|
} |
|
} |
|
|
|
func GetLuckWheelRecords(uid, withoutUid, page, pageSize int) ( |
|
records []common.LuckyWheelReward, total, totalAmount int64, err error) { |
|
mdb := db.Mysql().C().Model(&common.LuckyWheelReward{}) |
|
if uid == 0 { // 查询全部 |
|
mdb = mdb.Where("uid != ?", withoutUid).Order("updated_at desc").Limit(10) |
|
err = mdb.Find(&records).Error |
|
if err != nil { |
|
log.Error("get luckyWheel records err, %s", err.Error()) |
|
return |
|
} |
|
} else { // 查询个人 |
|
mdb = mdb.Where("uid = ?", uid) |
|
mdb = mdb.Order("updated_at desc") |
|
err = mdb.Count(&total).Error |
|
if err != nil { |
|
log.Error("get luckyWheel reward count err, %s", err.Error()) |
|
return |
|
} |
|
if page != 0 && pageSize != 0 { |
|
mdb = mdb.Offset((page - 1) * pageSize).Limit(pageSize) |
|
} |
|
err = mdb.Find(&records).Error |
|
if err != nil { |
|
log.Error("get luckyWheel records err, %s", err.Error()) |
|
return |
|
} |
|
err = mdb.Select("COALESCE(SUM(award_count), 0) as totalAmount").Scan(&totalAmount).Error |
|
if err != nil { |
|
log.Error("get luckyWheel totalAmount err, %s", err.Error()) |
|
return |
|
} |
|
} |
|
return |
|
} |
|
|
|
func CheckLuckyWheel(uid int, unPush ...bool) { |
|
if !db.Redis().Lock(common.GetRedisKeyLuckyWheel(uid)) { |
|
return |
|
} |
|
defer func() { |
|
db.Redis().UnLock(common.GetRedisKeyLuckyWheel(uid)) |
|
}() |
|
luckyWheel := GetConfigLuckyWheel() |
|
if luckyWheel == nil { |
|
return |
|
} |
|
rechargeInfo := GetRechargeInfo(uid) |
|
playerData := GetPlayerData(uid) |
|
if rechargeInfo.TotalRechargeCount < int64(luckyWheel.RechargeCount) { |
|
return |
|
} |
|
var ( |
|
update bool |
|
lessCount int |
|
) |
|
for _, wheelCfg := range luckyWheel.WheelCfgStr { |
|
lessKey := fmt.Sprintf("l%d", wheelCfg.LuckyType) |
|
for _, rechargeAmount := range wheelCfg.RechargeAmount { |
|
if rechargeAmount == 0 { |
|
continue |
|
} |
|
freeKey := fmt.Sprintf("f%d_%d", wheelCfg.LuckyType, rechargeAmount) |
|
if rechargeInfo.TotalRecharge >= int64(rechargeAmount)*common.DecimalDigits && |
|
playerData.LuckyWheelMap[freeKey] == 0 { |
|
playerData.LuckyWheelMap[freeKey] = 1 |
|
playerData.LuckyWheelMap[lessKey] += 1 |
|
update = true |
|
} |
|
lessCount += playerData.LuckyWheelMap[lessKey] |
|
} |
|
} |
|
if update { |
|
luckyWheelBytes, err := json.Marshal(playerData.LuckyWheelMap) |
|
if err != nil { |
|
log.Error("marshal err, %s", err.Error()) |
|
return |
|
} |
|
err = db.Mysql().C().Model(&common.PlayerData{}).Where("uid = ?", uid).Updates(map[string]interface{}{ |
|
"lucky_wheel": string(luckyWheelBytes), |
|
}).Error |
|
if err != nil { |
|
log.Error("update err, %s", err.Error()) |
|
return |
|
} |
|
} |
|
if len(unPush) > 0 && unPush[0] { |
|
return |
|
} |
|
if lessCount > 0 { |
|
PushRed(uid, pb.RedPointModule_RedPointLuckyWheel, 1) |
|
} |
|
}
|
|
|