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.
223 lines
5.9 KiB
223 lines
5.9 KiB
|
3 months ago
|
package jin2
|
||
|
|
|
||
|
|
import (
|
||
|
|
"server/call"
|
||
|
|
"server/common"
|
||
|
|
"server/db"
|
||
|
|
"server/modules/web/app"
|
||
|
|
"server/modules/web/providers/base"
|
||
|
|
"server/util"
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/liangdas/mqant/log"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Jin2(e *gin.RouterGroup) {
|
||
|
|
e.POST("/Cash/Get", GetBalance)
|
||
|
|
e.POST("/Cash/TransferInOut", GameBet)
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetGameID(providerID int, gameCode string) int {
|
||
|
|
game := call.GetConfigGameListByCode(providerID, gameCode)
|
||
|
|
if game != nil {
|
||
|
|
return game.GameID
|
||
|
|
}
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetBalance(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.ResponseB()
|
||
|
|
}()
|
||
|
|
req := &GetBalanceReq{}
|
||
|
|
resp := &GetBalanceResp{}
|
||
|
|
a.RetData = resp
|
||
|
|
if !a.S(req) {
|
||
|
|
resp.Code = CodeRequestInvalidParams
|
||
|
|
return
|
||
|
|
}
|
||
|
|
log.Debug("GetBalanceReq:%+v", req)
|
||
|
|
if a.ShouldRoute(req, "UserID", common.ProviderAPITypeJson) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
uids := req.UserID
|
||
|
|
uid, err := strconv.Atoi(uids)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
resp.Code = CodeOperationFailed
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
currency, err := db.Redis().GetInt(common.GetRedisKeyGameCurrency(uid))
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
resp.Code = CodeOperationFailed
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
resp.Data.Balance = call.GetUserCurrencyFloat(uid, common.CurrencyType(currency), 2)
|
||
|
|
resp.Data.Level = call.GetProviderGameRtp(uid)
|
||
|
|
log.Debug("GetBalanceResp:%+v", resp)
|
||
|
|
}
|
||
|
|
|
||
|
|
func GameBet(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.ResponseB()
|
||
|
|
}()
|
||
|
|
req := &GameBetReq{}
|
||
|
|
resp := &GameBetResp{}
|
||
|
|
a.RetData = resp
|
||
|
|
if !a.S(req) {
|
||
|
|
resp.Code = CodeRequestInvalidParams
|
||
|
|
return
|
||
|
|
}
|
||
|
|
log.Debug("GameBet:%+v", req)
|
||
|
|
if a.ShouldRoute(req, "UserID", common.ProviderAPITypeJson) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
uid, err := strconv.Atoi(req.UserID)
|
||
|
|
if err != nil {
|
||
|
|
resp.Code = CodeOperationFailed
|
||
|
|
return
|
||
|
|
}
|
||
|
|
provider := call.GetConfigGameProvider(common.ProviderJin2)
|
||
|
|
now := time.Now().Unix()
|
||
|
|
if req.Reason == "bet" || req.Reason == "win" {
|
||
|
|
betReq := &base.BetReq{
|
||
|
|
UID: uid,
|
||
|
|
SessionType: base.SessionTypeBet,
|
||
|
|
GameID: GetGameID(common.ProviderJin2, req.GameID),
|
||
|
|
GameName: req.GameID,
|
||
|
|
Provider: provider,
|
||
|
|
BetID: req.TransactionID,
|
||
|
|
SessionID: req.RoundID,
|
||
|
|
Time: now,
|
||
|
|
}
|
||
|
|
if req.Reason == "bet" {
|
||
|
|
betReq.BetAmount = util.Abs(int64(req.Amount * common.DecimalDigits))
|
||
|
|
betReq.TurnOver = util.Abs(int64(req.Amount * common.DecimalDigits))
|
||
|
|
//betReq.Rtp = call.GetProviderGameRtp(uid)
|
||
|
|
} else if req.Reason == "win" {
|
||
|
|
// 先判断结算是否合法
|
||
|
|
record := &common.ProviderBetRecord{
|
||
|
|
UID: uid,
|
||
|
|
Provider: provider.ProviderID,
|
||
|
|
Type: base.SessionTypeBet,
|
||
|
|
SessionID: betReq.SessionID,
|
||
|
|
}
|
||
|
|
db.Mysql().Get(record)
|
||
|
|
if record.ID == 0 {
|
||
|
|
resp.Code = CodeOperationFailed
|
||
|
|
currency, err := db.Redis().GetInt(common.GetRedisKeyGameCurrency(uid))
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
resp.Code = CodeOperationFailed
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
resp.Data.Balance = call.GetUserCurrencyFloat(uid, common.CurrencyType(currency), 2)
|
||
|
|
|
||
|
|
resp.Data.Level = call.GetProviderGameRtp(uid)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
betReq.SettleAmount = int64(req.Amount * common.DecimalDigits)
|
||
|
|
// 免费游戏的情况
|
||
|
|
if betReq.SettleAmount == 0 && !req.IsEnd {
|
||
|
|
currency, err := db.Redis().GetInt(common.GetRedisKeyGameCurrency(uid))
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
resp.Code = CodeOperationFailed
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
resp.Data.Balance = call.GetUserCurrencyFloat(uid, common.CurrencyType(currency), 2)
|
||
|
|
|
||
|
|
resp.Data.Level = call.GetProviderGameRtp(uid)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
record = &common.ProviderBetRecord{
|
||
|
|
UID: uid,
|
||
|
|
Provider: provider.ProviderID,
|
||
|
|
Type: base.SessionTypeFree,
|
||
|
|
SessionID: betReq.SessionID,
|
||
|
|
}
|
||
|
|
db.Mysql().Get(record)
|
||
|
|
if !req.IsEnd {
|
||
|
|
if record.ID == 0 {
|
||
|
|
record = &common.ProviderBetRecord{
|
||
|
|
UID: uid,
|
||
|
|
Provider: provider.ProviderID,
|
||
|
|
GameID: betReq.GameID,
|
||
|
|
GameName: betReq.GameName,
|
||
|
|
UUID: betReq.BetID,
|
||
|
|
MyUUID: call.SnowNode().Generate().Int64(),
|
||
|
|
Type: base.SessionTypeFree,
|
||
|
|
Time: betReq.Time,
|
||
|
|
Settle: betReq.SettleAmount,
|
||
|
|
SessionID: betReq.SessionID,
|
||
|
|
Esi: base.SessionSuccess,
|
||
|
|
}
|
||
|
|
db.Mysql().Create(record)
|
||
|
|
} else {
|
||
|
|
db.Mysql().Update(&common.ProviderBetRecord{ID: record.ID},
|
||
|
|
map[string]interface{}{"Settle": gorm.Expr("Settle + ?", betReq.SettleAmount)})
|
||
|
|
}
|
||
|
|
currency, err := db.Redis().GetInt(common.GetRedisKeyGameCurrency(uid))
|
||
|
|
if err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
resp.Code = CodeOperationFailed
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
resp.Data.Balance = call.GetUserCurrencyFloat(uid, common.CurrencyType(currency), 2)
|
||
|
|
resp.Data.Level = call.GetProviderGameRtp(uid)
|
||
|
|
return
|
||
|
|
} else {
|
||
|
|
if record.ID >= 0 {
|
||
|
|
betReq.SettleAmount += record.Settle
|
||
|
|
}
|
||
|
|
}
|
||
|
|
betReq.SessionType = base.SessionTypeSettle
|
||
|
|
}
|
||
|
|
betResp := base.SessionBet(betReq)
|
||
|
|
if betResp.Code != base.CodeOk {
|
||
|
|
resp.Code = CodeSuccess
|
||
|
|
if betResp.Code == base.CodeAccepted {
|
||
|
|
resp.Code = CodeDuplicateOrder
|
||
|
|
} else if betResp.Code == base.CodeNotEnoughAmount {
|
||
|
|
resp.Code = CodeInsufficientBalance
|
||
|
|
}
|
||
|
|
resp.Error = "operation failed."
|
||
|
|
log.Error("GameBetResp err:%v", resp.Code)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
resp.Data.Balance = util.Decimal(float64(betResp.Balance)/common.DecimalDigits, 2)
|
||
|
|
} else if req.Reason == "refund" {
|
||
|
|
adjustReq := &base.AdjustmentReq{
|
||
|
|
UID: uid,
|
||
|
|
Amount: int64(req.Amount * common.DecimalDigits),
|
||
|
|
GameID: GetGameID(common.ProviderJin2, req.GameID),
|
||
|
|
GameName: req.GameID,
|
||
|
|
Provider: provider,
|
||
|
|
BetID: req.RoundID,
|
||
|
|
SessionID: req.TransactionID,
|
||
|
|
Time: now,
|
||
|
|
Type: base.SessionTypeAdjustment,
|
||
|
|
Ess: req.Reason,
|
||
|
|
}
|
||
|
|
adjustResp := base.Adjustment(adjustReq)
|
||
|
|
if adjustResp.Code != CodeSuccess {
|
||
|
|
resp.Error = "operation failed."
|
||
|
|
}
|
||
|
|
resp.Data.Balance = util.Decimal(float64(adjustResp.Balance)/common.DecimalDigits, 2)
|
||
|
|
}
|
||
|
|
resp.Data.Level = call.GetProviderGameRtp(uid)
|
||
|
|
log.Debug("GameBetResp:%+v", resp)
|
||
|
|
a.Data = resp
|
||
|
|
}
|