dto-room.go
2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package roomrank
import (
"apigame/service-common/svconst"
"apigame/service-common/svmysql"
"apigame/util/util-lx/lxalilog"
"apigame/util/util-lx/lxtime"
"encoding/json"
"fmt"
"github.com/samber/lo"
)
// Room 房间排行持久数据
type Room struct {
Id int64 `gorm:"column:id;primaryKey;comment:房间唯一ID"`
ActivityId int64 `gorm:"comment:活动ID"`
ConfigId int `gorm:"comment:房间配置ID"`
Details *RoomDetails `gorm:"-"` // 详情
DetailsText string `gorm:"comment:详情封装"`
Closed bool `gorm:"comment:房间已关闭"`
CreateTime int64 `gorm:"comment:创建时间戳"`
UpdateTime int64 `gorm:"comment:修改时间戳"`
}
func (d *Room) MysqlInfo(suffix string) *svmysql.MysqlInfo {
tableName := svconst.MYSQL_TABLE_S_ROOMRANK_ROOM
return &svmysql.MysqlInfo{
DbMysql: svconst.DbCommon,
TableName: fmt.Sprintf("%s_%s_%d", tableName, suffix, d.ActivityId),
}
}
// RoomPlayer 房间玩家
type RoomPlayer struct {
Uid int64 // 玩家唯一ID
Score int64 // 玩家排行分数
JoinTime int64 // 加入时间
UserType int // 用户类型
RobotConfigId int // 0=玩家
}
func NewRoomRobot(robotConfigId int, userType int) *RoomPlayer {
d := &RoomPlayer{
Uid: 0,
Score: 0,
JoinTime: lxtime.NowUninx(),
UserType: userType,
RobotConfigId: robotConfigId,
}
return d
}
func NewRoomPlayer(player *Player) *RoomPlayer {
d := &RoomPlayer{
Uid: player.Uid,
Score: 0,
JoinTime: lxtime.NowUninx(),
UserType: player.UserType,
RobotConfigId: 0,
}
return d
}
// RoomDetails 详情
type RoomDetails struct {
Players []*RoomPlayer // 房间玩家列表
}
// Encode 打包数据
func (d *Room) Encode() {
details, err := json.Marshal(d.Details)
if err != nil {
lxalilog.Errors(err, "Player Encode Error", d.Id, d.ActivityId)
return
}
d.DetailsText = string(details)
}
// Decode 分包数据
func (d *Room) Decode() {
d.Details = &RoomDetails{
Players: make([]*RoomPlayer, 0),
}
err := json.Unmarshal([]byte(d.DetailsText), d.Details)
if err != nil {
lxalilog.Errors(err, "Player Decode Error", d.Id, d.ActivityId)
return
}
}
// FindPlayer 找到玩家排名 -1=没找到
func (d *Room) FindPlayer(playerUid int64) int {
count := len(d.Details.Players)
if count > 0 {
for i := 0; i < count; i++ {
p := d.Details.Players[i]
if p.Uid == playerUid {
return i
}
}
}
return -1
}
func (d *Room) GetPlayerTypeCount(userType int) int {
return lo.CountBy(d.Details.Players, func(p *RoomPlayer) bool {
return p.UserType == userType
})
}