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.
201 lines
4.9 KiB
201 lines
4.9 KiB
|
3 months ago
|
package handler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/liangdas/mqant/log"
|
||
|
|
"math/rand"
|
||
|
|
"server/call"
|
||
|
|
"server/common"
|
||
|
|
"server/db"
|
||
|
|
"server/modules/web/app"
|
||
|
|
"server/modules/web/values"
|
||
|
|
)
|
||
|
|
|
||
|
|
func LuckyWheelCfg(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
|
||
|
|
req := new(values.LuckyWheelCfgReq)
|
||
|
|
if !a.S(req) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
uid := a.UID
|
||
|
|
|
||
|
|
if !db.Redis().Lock(common.GetRedisKeyLuckyWheel(uid)) {
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer func() {
|
||
|
|
db.Redis().UnLock(common.GetRedisKeyLuckyWheel(uid))
|
||
|
|
}()
|
||
|
|
|
||
|
|
resp := &values.LuckyWheelCfgResp{}
|
||
|
|
a.Data = resp
|
||
|
|
luckyWheel := call.GetConfigLuckyWheel()
|
||
|
|
if luckyWheel == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
rechargeInfo := call.GetRechargeInfo(uid)
|
||
|
|
playerData := call.GetPlayerData(uid)
|
||
|
|
log.Debug("get config:%+v", *luckyWheel)
|
||
|
|
log.Debug("rechargeInfo:%+v", *rechargeInfo)
|
||
|
|
log.Debug("playerData:%+v", *playerData)
|
||
|
|
if rechargeInfo.TotalRechargeCount <= int64(luckyWheel.RechargeCount) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var (
|
||
|
|
update bool
|
||
|
|
)
|
||
|
|
for _, wheelCfg := range luckyWheel.WheelCfgStr {
|
||
|
|
if wheelCfg.LuckyType == 4 &&
|
||
|
|
rechargeInfo.TotalRecharge < int64(wheelCfg.RechargeAmount[0]) { // 特殊转盘达到了才能看见
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
lessKey := fmt.Sprintf("l%d", wheelCfg.LuckyType)
|
||
|
|
for _, rechargeAmount := range wheelCfg.RechargeAmount {
|
||
|
|
freeKey := fmt.Sprintf("f%d_%d", wheelCfg.LuckyType, rechargeAmount)
|
||
|
|
if rechargeInfo.TotalRecharge >= int64(rechargeAmount) &&
|
||
|
|
playerData.LuckyWheelMap[freeKey] == 0 {
|
||
|
|
playerData.LuckyWheelMap[freeKey] = 1
|
||
|
|
playerData.LuckyWheelMap[lessKey] += 1
|
||
|
|
update = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
resp.LuckyWheel = append(resp.LuckyWheel, values.LuckyWheel{
|
||
|
|
LuckyWheel: wheelCfg,
|
||
|
|
LessDrawCount: playerData.LuckyWheelMap[lessKey],
|
||
|
|
})
|
||
|
|
}
|
||
|
|
if update {
|
||
|
|
luckyWheelBytes, err := json.Marshal(playerData.LuckyWheelMap)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("marshal err, %s", err.Error())
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
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())
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
a.Data = resp
|
||
|
|
}
|
||
|
|
|
||
|
|
func LuckyWheelLottery(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
|
||
|
|
req := new(values.LuckyWheelLotteryReq)
|
||
|
|
if !a.S(req) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
log.Debug("luckyWheelLottery req:%+v", req)
|
||
|
|
|
||
|
|
call.GetRedisRankKey()
|
||
|
|
uid := a.UID
|
||
|
|
resp := &values.LuckWheelLotteryResp{}
|
||
|
|
if !db.Redis().Lock(common.GetRedisKeyLuckyWheel(uid)) {
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer func() {
|
||
|
|
db.Redis().UnLock(common.GetRedisKeyLuckyWheel(uid))
|
||
|
|
}()
|
||
|
|
a.Data = resp
|
||
|
|
luckyWheel := call.GetConfigLuckyWheel()
|
||
|
|
if luckyWheel == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var luckyWheelCfg *common.LuckyWheel
|
||
|
|
rechargeInfo := call.GetRechargeInfo(uid)
|
||
|
|
for _, v := range luckyWheel.WheelCfgStr {
|
||
|
|
if v.LuckyType == req.LuckyType {
|
||
|
|
if rechargeInfo.TotalRecharge < int64(v.RechargeAmount[0]) {
|
||
|
|
a.Code = values.CodeParam
|
||
|
|
a.Msg = "Not enough recharge."
|
||
|
|
return
|
||
|
|
}
|
||
|
|
luckyWheelCfg = &v
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if luckyWheelCfg == nil {
|
||
|
|
a.Code = values.CodeParam
|
||
|
|
return
|
||
|
|
}
|
||
|
|
playerData := call.GetPlayerData(uid)
|
||
|
|
lessKey := fmt.Sprintf("l%d", luckyWheelCfg.LuckyType)
|
||
|
|
if lessTimes := playerData.LuckyWheelMap[lessKey]; lessTimes <= 0 {
|
||
|
|
a.Code = values.CodeParam
|
||
|
|
a.Msg = "Not enough lucky wheel draw times."
|
||
|
|
return
|
||
|
|
}
|
||
|
|
playerData.LuckyWheelMap[lessKey]--
|
||
|
|
valueRand := rand.Intn(luckyWheelCfg.WeightsSum)
|
||
|
|
value := 0
|
||
|
|
var award *common.LuckyAward
|
||
|
|
for _, luckyAward := range luckyWheelCfg.LuckyAwards {
|
||
|
|
value += luckyAward.Weights
|
||
|
|
if valueRand <= value {
|
||
|
|
award = &luckyAward
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if award == nil {
|
||
|
|
log.Error("get award err, %+v", *luckyWheelCfg)
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
var err error
|
||
|
|
switch award.AwardType {
|
||
|
|
case 1:
|
||
|
|
_, err = call.UpdateCurrencyPro(&common.UpdateCurrency{
|
||
|
|
CurrencyBalance: &common.CurrencyBalance{
|
||
|
|
UID: uid,
|
||
|
|
Value: award.Currency.Value,
|
||
|
|
Event: common.CurrencyEventLuckyWheel,
|
||
|
|
Type: common.CurrencyINR,
|
||
|
|
NeedBet: call.GetConfigCurrencyResourceNeedBet(common.CurrencyResourceBonus, award.Currency.Value, luckyWheel.BetMultiples),
|
||
|
|
},
|
||
|
|
})
|
||
|
|
case 2:
|
||
|
|
addKey := fmt.Sprintf("l%d", award.WheelType)
|
||
|
|
playerData.LuckyWheelMap[addKey] += award.WheelCount
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
log.Error("award err, %s", err.Error())
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
luckyWheelBytes, err := json.Marshal(playerData.LuckyWheelMap)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("marshal err, %s", err.Error())
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
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())
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
resp.LessTimes = playerData.LuckyWheelMap[lessKey]
|
||
|
|
resp.Award = *award
|
||
|
|
a.Data = resp
|
||
|
|
}
|