123 lines
3.5 KiB
Go
123 lines
3.5 KiB
Go
package room
|
|
|
|
import (
|
|
"encoding/json"
|
|
"samba/pkg/log"
|
|
"samba/pkg/servername"
|
|
"samba/proto"
|
|
. "samba/server/cacheta/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 *GameRoom, uid int64, takeCoin, playCoin int64, clubId int) *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: clubId}
|
|
}
|
|
|
|
func (t *TrucoGameRecord) AddPlayerRecord(room *GameRoom, uid int64, takeCoin, playCoin int64, clubId int) {
|
|
t.playerRecords = append(t.playerRecords, NewPlayerGameRecord(room, uid, takeCoin, playCoin, clubId))
|
|
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 = clubId
|
|
t.roomRecord.Members = len(room.Seats)
|
|
}
|
|
}
|
|
|
|
func (t *TrucoGameRecord) AddPlayers(room *GameRoom) {
|
|
for _, seat := range room.Seats {
|
|
t.roomRecord.Players = append(t.roomRecord.Players, room.makePlayerToProto(seat, false))
|
|
}
|
|
}
|
|
|
|
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]
|
|
last.GameAction = append(last.GameAction, &proto.GameAction{
|
|
Countdown: countdown,
|
|
MsgId: msgId,
|
|
Msg: string(msg),
|
|
})
|
|
}
|
|
|
|
//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 = CathetaService.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)
|
|
}
|