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.
226 lines
5.5 KiB
226 lines
5.5 KiB
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) |
|
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]*common.DecimalDigits) { // 特殊转盘达到了才能看见 |
|
// continue |
|
//} |
|
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 |
|
} |
|
} |
|
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) |
|
|
|
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]*common.DecimalDigits) { |
|
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 index, luckyAward := range luckyWheelCfg.LuckyAwards { |
|
value += luckyAward.Weights |
|
if valueRand <= value { |
|
award = &luckyAward |
|
resp.AwardIndex = index |
|
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 |
|
award.WheelLessCount = playerData.LuckyWheelMap[addKey] |
|
} |
|
if err != nil { |
|
log.Error("award err, %s", err.Error()) |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
call.SetLuckyWheelRecords(uid, req.LuckyType, award) |
|
|
|
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 |
|
} |
|
|
|
func LuckyWheelAwardRecords(c *gin.Context) { |
|
a := app.NewApp(c) |
|
defer func() { |
|
a.Response() |
|
}() |
|
|
|
req := new(values.LuckWheelAwardRecordsReq) |
|
if !a.S(req) { |
|
return |
|
} |
|
|
|
resp := &values.LuckWheelAwardRecordsResp{} |
|
records, count, totalAmount, err := call.GetLuckWheelRecords(req.Uid, a.UID, req.Page, req.PageSize) |
|
if err != nil { |
|
log.Error("get records err, %s", err.Error()) |
|
a.Code = values.CodeRetry |
|
return |
|
} |
|
resp.Records = records |
|
resp.Total = count |
|
resp.TotalAmount = totalAmount |
|
a.Data = resp |
|
}
|
|
|