game_sync/gamesrv/base/gamedetail.go

256 lines
6.5 KiB
Go

package base
import (
"time"
"github.com/globalsign/mgo/bson"
"mongo.games.com/game/model"
"mongo.games.com/game/mq"
)
/*
记录游戏对局记录
*/
type SaveGameDetailedParam struct {
LogId string // 日志id
Detail string // 游戏详细信息
GameTime int64 // 游戏时长
Trend20Lately string // 最近20局开奖结果
CtrlType int // 调控类型 1控赢 2控输
PlayerPool map[int]int // 个人水池分
OnlyLog bool // 只返回日志,不保存
}
// SaveGameDetailedLog 保存游戏详细记录
func (this *Scene) SaveGameDetailedLog(param *SaveGameDetailedParam) *SaveGameDetailedCopy {
if this == nil || param == nil {
return nil
}
if param.GameTime <= 0 {
param.GameTime = int64(time.Now().Sub(this.GameNowTime).Seconds())
}
if param.GameTime < 0 {
param.GameTime = 0
}
now := time.Now()
var ret SaveGameDetailedCopy
ret.Param = param
f := func(plt string) {
log := &model.GameDetailedLog{
Id: bson.NewObjectId(),
LogId: param.LogId,
GameId: this.GameId,
Platform: plt,
MatchId: this.GetMatch().GetMatchSortId(),
SceneId: this.SceneId,
GameMode: this.GameMode,
GameFreeid: this.GetGameFreeId(),
PlayerCount: int32(len(this.Players)),
GameTiming: int32(param.GameTime),
GameBaseBet: this.GetBaseScore(),
GameDetailedNote: param.Detail,
GameDetailVer: GameDetailedVer[int(this.GameId)],
CpCtx: this.CpCtx,
Time: now,
Trend20Lately: param.Trend20Lately,
Ts: now.Unix(),
CtrlType: param.CtrlType,
PlayerPool: make(map[int]int),
CycleId: this.CycleID,
}
for k, v := range param.PlayerPool {
log.PlayerPool[k] = v
}
if param.OnlyLog {
ret.Log = append(ret.Log, log)
} else {
mq.Write(log)
}
}
switch {
case this.IsCoinScene():
mapPlatform := make(map[string]bool)
for _, v := range this.Players {
if v == nil {
continue
}
if _, ok := mapPlatform[v.Platform]; ok {
continue
}
mapPlatform[v.Platform] = true
f(v.Platform)
}
default:
f(this.Platform)
}
return &ret
}
type SaveGamePlayerListLogParam struct {
LogId string // 详情日志id
Platform string // 平台
Snid int32 // 玩家id
PlayerName string // 玩家名字
Channel string // 渠道
ChannelId string // 推广渠道
TotalIn int64 // 总投入
TotalOut int64 // 总产出(税前)
TaxCoin int64 // 总税收
BetAmount int64 // 下注量
WinAmountNoAnyTax int64 // 税后赢取额(净利润,正负值)
IsFirstGame bool // 是否第一次游戏
IsFree bool // 拉霸专用 是否免费
WinSmallGame int64 // 拉霸专用 小游戏奖励
WinTotal int64 // 拉霸专用 本局输赢
GameTime int64 // 游戏时长
OnlyLog bool // 只返回日志,不保存
}
// SaveGamePlayerListLog 保存玩家对局记录
func (this *Scene) SaveGamePlayerListLog(param *SaveGamePlayerListLogParam) *SaveGamePlayerListLogCopy {
if this == nil {
return nil
}
if param == nil {
return nil
}
p := this.GetPlayer(param.Snid)
if p == nil {
return nil
}
if param.PlayerName == "" {
param.PlayerName = p.Name
}
var ret SaveGamePlayerListLogCopy
ret.Param = param
baseScore := this.GetBaseScore()
// 上报玩家游戏记录
if !p.IsRob && (param.IsFree || param.TotalIn != 0 || param.TotalOut != 0) {
e := p.ReportGameEvent(&ReportGameEventParam{
Tax: param.TaxCoin,
Change: param.WinAmountNoAnyTax,
In: param.TotalIn,
Out: param.TotalOut,
GameTime: param.GameTime,
OnlyLog: param.OnlyLog,
})
if e != nil {
ret.UpLog = e
}
}
// 保存玩家游戏日志
now := time.Now()
log := &model.GamePlayerListLog{
LogId: bson.NewObjectId(),
SnId: p.SnId,
Name: param.PlayerName,
GameId: this.GameId,
BaseScore: baseScore,
TaxCoin: param.TaxCoin,
Platform: param.Platform,
Channel: param.Channel,
SceneId: this.SceneId,
GameMode: this.GameMode,
GameFreeid: this.GetGameFreeId(),
GameDetailedLogId: param.LogId,
IsFirstGame: param.IsFirstGame,
BetAmount: param.BetAmount,
WinAmountNoAnyTax: param.WinAmountNoAnyTax,
TotalIn: param.TotalIn,
TotalOut: param.TotalOut,
Time: now,
RoomType: this.SceneMode,
GameDif: this.GetDBGameFree().GetGameDif(),
GameClass: this.GetDBGameFree().GetGameClass(),
MatchId: this.GetMatch().GetMatchSortId(),
MatchType: int64(this.GetMatch().GetMatchType()),
Ts: now.Unix(),
IsFree: param.IsFree,
WinSmallGame: param.WinSmallGame,
WinTotal: param.WinTotal,
CycleId: this.CycleID,
}
if param.OnlyLog {
ret.Log = append(ret.Log, log)
} else {
mq.Write(log)
}
return &ret
}
// SaveGamePlayerListLogCopy 临时记录
// 为了拉霸统计游戏时长,需要临时缓存游戏记录
type SaveGamePlayerListLogCopy struct {
Param *SaveGamePlayerListLogParam
Log []*model.GamePlayerListLog
UpLog *ReportGameEventOnly // mq上报数据
}
func (s *SaveGamePlayerListLogCopy) Save() {
for _, v := range s.Log {
mq.Write(v)
}
if s.UpLog != nil {
for _, v := range s.UpLog.Log {
mq.Write(v, mq.BackGameRecord)
}
}
}
// SaveGameDetailedCopy 临时记录
// 为了拉霸统计游戏时长,需要临时缓存游戏记录
type SaveGameDetailedCopy struct {
Param *SaveGameDetailedParam
Log []*model.GameDetailedLog
}
func (s *SaveGameDetailedCopy) Save() {
for _, v := range s.Log {
mq.Write(v)
}
}
// LabaLog 拉霸缓存游戏记录
type LabaLog struct {
PlayerListLog *SaveGamePlayerListLogCopy
GameDetailLog *SaveGameDetailedCopy
}
// Cache 临时缓存
func (l *LabaLog) Cache(s *Scene, detailLog *SaveGameDetailedParam, playerListLog *SaveGamePlayerListLogParam) {
if s == nil {
return
}
detailLog.OnlyLog = true
playerListLog.OnlyLog = true
l.GameDetailLog = s.SaveGameDetailedLog(detailLog)
l.PlayerListLog = s.SaveGamePlayerListLog(playerListLog)
}
// Save 保存
func (l *LabaLog) Save(f func(log *LabaLog)) {
f(l)
l.PlayerListLog.Save()
l.GameDetailLog.Save()
l.Clear()
}
// Clear 清空
func (l *LabaLog) Clear() {
l.PlayerListLog = nil
l.GameDetailLog = nil
}