294 lines
7.5 KiB
Go
294 lines
7.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(),
|
||
Platform: plt,
|
||
LogId: param.LogId,
|
||
GameDif: this.GetDBGameFree().GetGameDif(),
|
||
GameId: this.GetDBGameFree().GetGameId(),
|
||
GameClass: this.GetDBGameFree().GetGameClass(),
|
||
GameMode: this.GetDBGameFree().GetGameMode(),
|
||
GameType: this.GetDBGameFree().GetGameType(),
|
||
GameFreeId: this.GetDBGameFree().GetId(),
|
||
MatchId: this.GetMatch().GetMatchSortId(),
|
||
SceneId: this.SceneId,
|
||
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 // 玩家名字
|
||
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(),
|
||
Platform: param.Platform,
|
||
GameDif: this.GetDBGameFree().GetGameDif(),
|
||
GameId: this.GetDBGameFree().GetGameId(),
|
||
GameClass: this.GetDBGameFree().GetGameClass(),
|
||
GameMode: this.GetDBGameFree().GetGameMode(),
|
||
GameType: this.GetDBGameFree().GetGameType(),
|
||
GameFreeId: this.GetGameFreeId(),
|
||
BaseScore: baseScore,
|
||
GameDetailedLogId: param.LogId,
|
||
Channel: p.Channel,
|
||
ChannelId: p.ChannelId,
|
||
RoomType: this.SceneMode,
|
||
Ts: now.Unix(),
|
||
Time: now,
|
||
CycleId: this.CycleID,
|
||
SnId: p.SnId,
|
||
Name: param.PlayerName,
|
||
SceneId: this.SceneId,
|
||
MatchId: this.GetMatch().GetMatchSortId(),
|
||
MatchType: int64(this.GetMatch().GetMatchType()),
|
||
WinAmountNoAnyTax: param.WinAmountNoAnyTax,
|
||
TaxCoin: param.TaxCoin,
|
||
BetAmount: param.BetAmount,
|
||
IsFirstGame: param.IsFirstGame,
|
||
TotalIn: param.TotalIn,
|
||
TotalOut: param.TotalOut,
|
||
IsFree: param.IsFree,
|
||
WinSmallGame: param.WinSmallGame,
|
||
WinTotal: param.WinTotal,
|
||
}
|
||
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
|
||
CacheTime time.Time // 缓存时间
|
||
noEmpty bool
|
||
}
|
||
|
||
// Cache 临时缓存
|
||
func (l *LabaLog) Cache(s *Scene, detailLog *SaveGameDetailedParam, playerListLog *SaveGamePlayerListLogParam) {
|
||
if s == nil {
|
||
return
|
||
}
|
||
|
||
l.Save(2) // 如果没有收到结束消息,算2秒游戏时长
|
||
|
||
detailLog.OnlyLog = true
|
||
playerListLog.OnlyLog = true
|
||
l.GameDetailLog = s.SaveGameDetailedLog(detailLog)
|
||
l.PlayerListLog = s.SaveGamePlayerListLog(playerListLog)
|
||
l.CacheTime = time.Now()
|
||
l.noEmpty = true
|
||
}
|
||
|
||
// Save 保存
|
||
// second 为0时,自动计算时间,从开始转动计时
|
||
func (l *LabaLog) Save(second int32) {
|
||
if !l.noEmpty {
|
||
return
|
||
}
|
||
if second <= 0 {
|
||
sub := time.Now().Sub(l.CacheTime).Milliseconds()
|
||
if sub <= 1000 {
|
||
second = 1
|
||
} else {
|
||
second = int32(sub / 1000)
|
||
}
|
||
}
|
||
|
||
if l.PlayerListLog != nil {
|
||
if l.PlayerListLog.UpLog != nil {
|
||
for _, v := range l.PlayerListLog.UpLog.Log {
|
||
v.GamingTime = second
|
||
}
|
||
}
|
||
l.PlayerListLog.Save()
|
||
}
|
||
|
||
if l.GameDetailLog != nil {
|
||
for _, v := range l.GameDetailLog.Log {
|
||
v.GameTiming = second
|
||
}
|
||
l.GameDetailLog.Save()
|
||
}
|
||
|
||
l.Clear()
|
||
}
|
||
|
||
// Clear 清空
|
||
func (l *LabaLog) Clear() {
|
||
l.noEmpty = false
|
||
l.PlayerListLog = nil
|
||
l.GameDetailLog = nil
|
||
l.CacheTime = time.Time{}
|
||
}
|