samba/server/truco/room/trucoRoomRecord.go
2025-06-04 09:51:39 +08:00

143 lines
4.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package room
import (
"encoding/json"
"fmt"
"runtime/debug"
"samba/pkg/log"
"samba/pkg/servername"
"samba/proto"
. "samba/server/truco/service"
"samba/util/routingKey"
"samba/util/util"
"time"
)
var no int64
func makeClickHouseId() int64 {
if no > 9000000 {
no = 0
} else {
no++
}
return no
}
// 战绩回放
type TrucoGameRecord struct {
playerRecords []*proto.PlayerGameRecord
roomRecord proto.RoomGameRecord
tm time.Time
}
func NewTrucoGameRecord() *TrucoGameRecord {
return &TrucoGameRecord{playerRecords: make([]*proto.PlayerGameRecord, 0)}
}
func NewPlayerGameRecord(room *TrucoRoom, uid int64, takeCoin, playCoin int64, playerInfos []*proto.GameRecordPlayerInfo) *proto.PlayerGameRecord {
return &proto.PlayerGameRecord{UId: uid, RoomType: room.Type(), GameNo: room.GameNo,
StartTime: room.startTime.Unix(), StartTime1: room.startTime.Unix(),
EndTime: room.endTime.Unix(), EndTime1: room.endTime.Unix(),
TakeCoin: takeCoin, PlayCoin: playCoin, Favorite: 0, ClubId: room.ClubId(),
Blind: room.RoomCnf.Blind, RoomId: room.Id(), MaxGameNum: 1, GameNum: 1,
Disband: int(room.disband), PlayerInfos: playerInfos, PlayType: room.RoomCnf.PlayType, Members: len(room.Seats)}
}
func (t *TrucoGameRecord) AddPlayerRecord(room *TrucoRoom, uid int64, takeCoin, playCoin int64, playerInfos []*proto.GameRecordPlayerInfo) {
t.playerRecords = append(t.playerRecords, NewPlayerGameRecord(room, uid, takeCoin, playCoin, playerInfos))
if t.roomRecord.RoomId == 0 {
t.roomRecord.RoomId = room.Id()
t.roomRecord.RoomType = room.Type()
t.roomRecord.GameNo = room.GameNo
t.roomRecord.Blind = room.RoomCnf.Blind
t.roomRecord.StartTime = room.startTime.Unix()
t.roomRecord.EndTime = room.endTime.Unix()
t.roomRecord.ClubId = room.ClubId()
t.roomRecord.Members = len(room.Seats)
}
}
func (t *TrucoGameRecord) AddPlayers(room *TrucoRoom) {
for _, seat := range room.Seats {
t.roomRecord.Players = append(t.roomRecord.Players, room.makePlayerToProto(seat, false))
}
}
// 设置战绩里的解散房间状态
func (t *TrucoGameRecord) SetDisbandType(room *TrucoRoom) {
for _, pgRecord := range t.playerRecords {
pgRecord.Disband = int(room.disband)
}
}
func (t *TrucoGameRecord) NewGame() {
t.roomRecord.Actions = append(t.roomRecord.Actions, &proto.AGame{})
t.tm = time.Now()
if len(t.roomRecord.Actions) == 1 {
// 牌局回顾
ntf := &proto.NtfGameStart{Time: time.Now().Unix()}
t.AddAction(proto.NtfStartGameId, ntf)
}
}
func (t *TrucoGameRecord) AddAction(msgId string, iMsg interface{}) {
countdown := int(time.Now().Unix() - t.tm.Unix())
msg, _ := json.Marshal(iMsg)
last := t.roomRecord.Actions[len(t.roomRecord.Actions)-1]
action := &proto.GameAction{
Countdown: countdown,
MsgId: msgId,
Msg: string(msg),
}
if msgId == proto.RspPlayerActId {
strTm := time.Now().Format("2006-01-02 15:04:05")
action.Desc = fmt.Sprintf("时间:%v 消息:%v \n堆栈:%v", strTm, iMsg, string(debug.Stack()))
}
if len(last.GameAction) != 0 && last.GameAction[len(last.GameAction)-1].MsgId == action.MsgId && msgId != proto.NtfGameBigSettleId {
log.Error(fmt.Sprintf("上一个消息堆栈:%v本消息堆栈:%v", last.GameAction[len(last.GameAction)-1].Desc, action.Desc))
}
last.GameAction = append(last.GameAction, action)
}
//const writeClickHouseSqlAction = "write_clickhouse_sql"
//
//// {"k":"token","a":"write_clickhouse_sql","p":{"a":"sql"}}
//type writeClickHouseSql struct {
// Token string `json:"k"`
// MsgId string `json:"a"`
// Data map[string]string `json:"p"`
//}
func (t *TrucoGameRecord) send(data string) {
req := proto.NtfClickHouseSql{Sql: data}
msg := util.MakeMessage(proto.NtfClickHouseSqlId, req, 0, 0)
byteData, err := json.Marshal(msg)
if err != nil {
log.Error(err.Error())
}
//log.Debug(string(byteData))
err = TrucoService.Publish(util.Direct(servername.ClickHouse), routingKey.ClickHouseKey(makeClickHouseId()), byteData)
if err != nil {
log.Error(err.Error())
}
}
func (t *TrucoGameRecord) Flush() {
if sql, err := util.Struct2InsertSql("`samba`.`player_game_record`", t.playerRecords...); err == nil {
t.send(sql)
} else {
log.Error(err.Error())
}
if sql, err := util.Struct2InsertSql("`samba`.`room_game_record`", &t.roomRecord); err == nil {
t.send(sql)
} else {
log.Error(err.Error())
}
//for _, playerRecord := range t.playerRecords {
// model.AddPlayerGameRecord(playerRecord.UId, playerRecord)
//}
//model.AddRoomGameRecord(t.roomRecord.GetGameNo, &t.roomRecord)
}