Commit 60c545c8f58459d712d56444f780aabe07ce2fa8
1 parent
b3c9207e
Exists in
master
and in
1 other branch
feat✨:房间排行榜改为支持多个排行榜
Showing
12 changed files
with
65 additions
and
48 deletions
Show diff stats
configs/confcardholder/config.go
| ... | ... | @@ -49,8 +49,8 @@ func (c *ActivityConfig) ConfInfo(suffix string) *confbase.ConfInfo { |
| 49 | 49 | DbMysql: svconst.DbConfig, |
| 50 | 50 | TableName: fmt.Sprintf("%s_%s", tableName, suffix), |
| 51 | 51 | KeyName: "id", |
| 52 | - CurrentQuery: "start_time <= ? AND end_time >= ? AND status = ?", | |
| 53 | - CurrentArgs: []any{timeNow, timeNow, 1}, | |
| 52 | + CurrentQuery: "status = ? AND start_time <= ? AND end_time >= ?", | |
| 53 | + CurrentArgs: []any{1, timeNow, timeNow}, | |
| 54 | 54 | CacheKey: fmt.Sprintf("%s:%d", cacheKey, c.Id), |
| 55 | 55 | CacheCurrent: cacheKey + ":current", |
| 56 | 56 | CacheTime: 300, | ... | ... |
configs/confroomrank/config.go
| ... | ... | @@ -13,6 +13,7 @@ type ActivityConfig struct { |
| 13 | 13 | Raw *ActivityConfigRaw `json:"-"` |
| 14 | 14 | |
| 15 | 15 | Id int64 // ID |
| 16 | + Typ int // 排行榜类型 | |
| 16 | 17 | OpenLevel int // 开启等级 |
| 17 | 18 | PreviewTime int64 // 预告时间 |
| 18 | 19 | StartTime int64 // 开始时间 |
| ... | ... | @@ -43,10 +44,10 @@ func (c *ActivityConfig) ConfInfo(suffix string) *confbase.ConfInfo { |
| 43 | 44 | DbMysql: svconst.DbConfig, |
| 44 | 45 | TableName: fmt.Sprintf("%s_%s", tableName, suffix), |
| 45 | 46 | KeyName: "id", |
| 46 | - CurrentQuery: "start_time <= ? AND end_time >= ? AND status = ?", | |
| 47 | - CurrentArgs: []any{timeNow, timeNow, 1}, | |
| 47 | + CurrentQuery: "typ = ? AND status = ? AND start_time <= ? AND end_time >= ?", | |
| 48 | + CurrentArgs: []any{c.Typ, 1, timeNow, timeNow}, | |
| 48 | 49 | CacheKey: fmt.Sprintf("%s:%d", cacheKey, c.Id), |
| 49 | - CacheCurrent: cacheKey + ":current", | |
| 50 | + CacheCurrent: fmt.Sprintf("%s:%d:current", cacheKey, c.Typ), | |
| 50 | 51 | CacheTime: 300, |
| 51 | 52 | } |
| 52 | 53 | } | ... | ... |
configs/confroomrank/get.go
| ... | ... | @@ -3,8 +3,9 @@ package confroomrank |
| 3 | 3 | import "apigame/configs/confbase" |
| 4 | 4 | |
| 5 | 5 | // GetCurrent 获取 当前配置 |
| 6 | -func GetCurrent(gameId string) (conf *ActivityConfig, has bool) { | |
| 6 | +func GetCurrent(gameId string, topType int) (conf *ActivityConfig, has bool) { | |
| 7 | 7 | conf = new(ActivityConfig) |
| 8 | + conf.Typ = topType | |
| 8 | 9 | has = confbase.GetCurrent[*ActivityConfig, ActivityConfigRaw](gameId, conf) |
| 9 | 10 | |
| 10 | 11 | return | ... | ... |
configs/init.go
| ... | ... | @@ -4,7 +4,6 @@ import ( |
| 4 | 4 | "apigame/configs/confapi" |
| 5 | 5 | "apigame/configs/confcardholder" |
| 6 | 6 | "apigame/configs/confglobal" |
| 7 | - "apigame/configs/confroomrank" | |
| 8 | 7 | "apigame/service-common/svconst" |
| 9 | 8 | ) |
| 10 | 9 | |
| ... | ... | @@ -22,9 +21,9 @@ func Init() bool { |
| 22 | 21 | _, _ = confcardholder.GetCurrent(gameId) |
| 23 | 22 | } |
| 24 | 23 | |
| 25 | - for _, gameId := range svconst.GameListRoomRank { | |
| 26 | - _, _ = confroomrank.GetCurrent(gameId) | |
| 27 | - } | |
| 24 | + //for _, gameId := range svconst.GameListRoomRank { | |
| 25 | + // _, _ = confroomrank.GetCurrent(gameId) | |
| 26 | + //} | |
| 28 | 27 | |
| 29 | 28 | return true |
| 30 | 29 | } | ... | ... |
models/roomrank.go
| ... | ... | @@ -44,6 +44,7 @@ type RoomRankInfo struct { |
| 44 | 44 | type ReqRoomRankInfo struct { |
| 45 | 45 | BaseLoginInfo |
| 46 | 46 | BaseSign |
| 47 | + TopType int `form:"top_type" json:"top_type"` // 排行榜类型 | |
| 47 | 48 | PlayerLevel int `form:"player_level" json:"player_level"` // 玩家等级 |
| 48 | 49 | PlayerName string `form:"player_name" json:"player_name"` // 玩家名字 |
| 49 | 50 | PlayerIcon string `form:"player_icon" json:"player_icon"` // 玩家头像 |
| ... | ... | @@ -58,6 +59,7 @@ type RspRoomRankInfo struct { |
| 58 | 59 | type ReqRoomRankGetSettleAward struct { |
| 59 | 60 | BaseLoginInfo |
| 60 | 61 | BaseSign |
| 62 | + TopType int `form:"top_type" json:"top_type"` // 排行榜类型 | |
| 61 | 63 | PlayerLevel int `form:"player_level" json:"player_level"` // 玩家等级 |
| 62 | 64 | PlayerName string `form:"player_name" json:"player_name"` // 玩家名字 |
| 63 | 65 | PlayerIcon string `form:"player_icon" json:"player_icon"` // 玩家头像 |
| ... | ... | @@ -75,6 +77,7 @@ type RspRoomRankGetSettleAward struct { |
| 75 | 77 | type ReqRoomRankAddScore struct { |
| 76 | 78 | BaseLoginInfo |
| 77 | 79 | BaseSign |
| 80 | + TopType int `form:"top_type" json:"top_type"` // 排行榜类型 | |
| 78 | 81 | PlayerLevel int `form:"player_level" json:"player_level"` // 玩家等级 |
| 79 | 82 | PlayerName string `form:"player_name" json:"player_name"` // 玩家名字 |
| 80 | 83 | PlayerIcon string `form:"player_icon" json:"player_icon"` // 玩家头像 | ... | ... |
service/init.go
| ... | ... | @@ -4,7 +4,6 @@ import ( |
| 4 | 4 | "apigame/service-common/svconst" |
| 5 | 5 | "apigame/service-common/svmysql" |
| 6 | 6 | "apigame/service/cardholder" |
| 7 | - "apigame/service/roomrank" | |
| 8 | 7 | ) |
| 9 | 8 | |
| 10 | 9 | func Init() { |
| ... | ... | @@ -17,11 +16,5 @@ func Init() { |
| 17 | 16 | svmysql.InitTable(new(cardholder.RecordRewardAlbum), gameId, true) |
| 18 | 17 | svmysql.InitTable(new(cardholder.RecordRewardRound), gameId, true) |
| 19 | 18 | } |
| 20 | - // 房间排行 | |
| 21 | - for _, gameId := range svconst.GameListRoomRank { | |
| 22 | - svmysql.InitTable(new(roomrank.Player), gameId, true) | |
| 23 | - svmysql.InitTable(new(roomrank.RecordGetSettleAward), gameId, true) | |
| 24 | - svmysql.InitTable(new(roomrank.RecordAddScore), gameId, true) | |
| 25 | - } | |
| 26 | 19 | |
| 27 | 20 | } | ... | ... |
service/roomrank/dto-player.go
| ... | ... | @@ -10,6 +10,7 @@ import ( |
| 10 | 10 | // Player 房间排行持久数据 |
| 11 | 11 | type Player struct { |
| 12 | 12 | Uid int64 `gorm:"column:uid;primaryKey;comment:玩家唯一ID"` |
| 13 | + TopType int `gorm:"comment:排行榜类型"` | |
| 13 | 14 | Name string `gorm:"-"` // 玩家名字 |
| 14 | 15 | Icon string `gorm:"-"` // 玩家头像 |
| 15 | 16 | ActivityId int64 `gorm:"comment:活动ID"` |
| ... | ... | @@ -34,19 +35,21 @@ func (d *Player) MysqlInfo(suffix string) *svmysql.MysqlInfo { |
| 34 | 35 | tableName := svconst.MYSQL_TABLE_S_ROOMRANK_PLAYER |
| 35 | 36 | return &svmysql.MysqlInfo{ |
| 36 | 37 | DbMysql: svconst.DbCommon, |
| 37 | - TableName: fmt.Sprintf("%s_%s", tableName, suffix), | |
| 38 | + TableName: fmt.Sprintf("%s_%s_%d", tableName, suffix, d.TopType), | |
| 38 | 39 | } |
| 39 | 40 | } |
| 40 | 41 | |
| 41 | -func NewPlayer(uid int64) *Player { | |
| 42 | +func NewPlayer(uid int64, topType int) *Player { | |
| 42 | 43 | d := &Player{ |
| 43 | - Uid: uid, | |
| 44 | + Uid: uid, | |
| 45 | + TopType: topType, | |
| 44 | 46 | } |
| 45 | 47 | return d |
| 46 | 48 | } |
| 47 | 49 | |
| 48 | -func (d *Player) Init(uid int64) { | |
| 50 | +func (d *Player) Init(uid int64, topType int) { | |
| 49 | 51 | d.Uid = uid |
| 52 | + d.TopType = topType | |
| 50 | 53 | d.CreateTime = lxtime.NowUninx() |
| 51 | 54 | } |
| 52 | 55 | ... | ... |
service/roomrank/dto-record.go
| ... | ... | @@ -10,17 +10,19 @@ import ( |
| 10 | 10 | // RecordBase 日志公共 |
| 11 | 11 | type RecordBase struct { |
| 12 | 12 | Id int64 `gorm:"primaryKey;comment:日志ID"` |
| 13 | + TopType int `gorm:"comment:排行榜类型"` | |
| 13 | 14 | Uid int64 `gorm:"comment:玩家唯一ID"` |
| 14 | 15 | ActivityId int64 `gorm:"comment:当前活动ID"` |
| 15 | 16 | CreateTime int64 `gorm:"comment:创建时间戳"` |
| 16 | 17 | UpdateTime int64 `gorm:"comment:修改时间戳"` |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | -func NewRecordBase(uid int64, activityId int64) RecordBase { | |
| 20 | +func NewRecordBase(uid int64, topType int, activityId int64) RecordBase { | |
| 20 | 21 | secNow := lxtime.NowUninx() |
| 21 | 22 | return RecordBase{ |
| 22 | 23 | CreateTime: secNow, |
| 23 | 24 | UpdateTime: secNow, |
| 25 | + TopType: topType, | |
| 24 | 26 | Uid: uid, |
| 25 | 27 | ActivityId: activityId, |
| 26 | 28 | } |
| ... | ... | @@ -36,7 +38,7 @@ func (d *RecordGetSettleAward) MysqlInfo(suffix string) *svmysql.MysqlInfo { |
| 36 | 38 | tableName := svconst.MYSQL_TABLE_S_ROOMRANK_RECORD_SETTLEAWARD |
| 37 | 39 | return &svmysql.MysqlInfo{ |
| 38 | 40 | DbMysql: svconst.DbCommon, |
| 39 | - TableName: fmt.Sprintf("%s_%s", tableName, suffix), | |
| 41 | + TableName: fmt.Sprintf("%s_%s_%d", tableName, suffix, d.TopType), | |
| 40 | 42 | } |
| 41 | 43 | } |
| 42 | 44 | |
| ... | ... | @@ -60,7 +62,7 @@ func (d *RecordAddScore) MysqlInfo(suffix string) *svmysql.MysqlInfo { |
| 60 | 62 | tableName := svconst.MYSQL_TABLE_S_ROOMRANK_RECORD_ADDSCORE |
| 61 | 63 | return &svmysql.MysqlInfo{ |
| 62 | 64 | DbMysql: svconst.DbCommon, |
| 63 | - TableName: fmt.Sprintf("%s_%s", tableName, suffix), | |
| 65 | + TableName: fmt.Sprintf("%s_%s_%d", tableName, suffix, d.TopType), | |
| 64 | 66 | } |
| 65 | 67 | } |
| 66 | 68 | ... | ... |
service/roomrank/handle.go
| ... | ... | @@ -3,7 +3,6 @@ package roomrank |
| 3 | 3 | import ( |
| 4 | 4 | "apigame/configs/confroomrank" |
| 5 | 5 | "apigame/models" |
| 6 | - "apigame/service-common/svmysql" | |
| 7 | 6 | "apigame/service/code-msg" |
| 8 | 7 | "apigame/util/zredislock" |
| 9 | 8 | ) |
| ... | ... | @@ -15,7 +14,7 @@ func HandleGetConfig(req *models.ReqRoomRankGetConfig) (code string, rsp models. |
| 15 | 14 | gameId := req.GameID |
| 16 | 15 | |
| 17 | 16 | // 尝试更新配置 |
| 18 | - config, hasConfig := confroomrank.GetCurrent(gameId) | |
| 17 | + config, hasConfig := confroomrank.GetCurrent(gameId, 1) | |
| 19 | 18 | if !hasConfig { |
| 20 | 19 | code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR |
| 21 | 20 | return |
| ... | ... | @@ -41,7 +40,7 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan |
| 41 | 40 | playerLevel := req.PlayerLevel |
| 42 | 41 | |
| 43 | 42 | // 尝试更新配置 |
| 44 | - config, hasConfig := confroomrank.GetCurrent(gameId) | |
| 43 | + config, hasConfig := confroomrank.GetCurrent(gameId, req.TopType) | |
| 45 | 44 | if !hasConfig { |
| 46 | 45 | config = new(confroomrank.ActivityConfig) |
| 47 | 46 | code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR |
| ... | ... | @@ -49,7 +48,7 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan |
| 49 | 48 | } |
| 50 | 49 | |
| 51 | 50 | // 读取游戏数据 |
| 52 | - player := LoadPlayer(gameId, playerUid) | |
| 51 | + player := LoadPlayer(gameId, playerUid, req.TopType) | |
| 53 | 52 | player.Name = req.PlayerName |
| 54 | 53 | player.Icon = req.PlayerIcon |
| 55 | 54 | |
| ... | ... | @@ -103,7 +102,7 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r |
| 103 | 102 | playerUid := req.UID |
| 104 | 103 | |
| 105 | 104 | // 尝试更新配置 |
| 106 | - config, hasConfig := confroomrank.GetCurrent(gameId) | |
| 105 | + config, hasConfig := confroomrank.GetCurrent(gameId, req.TopType) | |
| 107 | 106 | if !hasConfig { |
| 108 | 107 | config = new(confroomrank.ActivityConfig) |
| 109 | 108 | code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR |
| ... | ... | @@ -111,7 +110,7 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r |
| 111 | 110 | } |
| 112 | 111 | |
| 113 | 112 | // 读取游戏数据 |
| 114 | - player := LoadPlayer(gameId, playerUid) | |
| 113 | + player := LoadPlayer(gameId, playerUid, req.TopType) | |
| 115 | 114 | player.Name = req.PlayerName |
| 116 | 115 | player.Icon = req.PlayerIcon |
| 117 | 116 | |
| ... | ... | @@ -144,10 +143,8 @@ func HandleGetSettleAward(req *models.ReqRoomRankGetSettleAward) (code string, r |
| 144 | 143 | SavePlayer(gameId, player) |
| 145 | 144 | |
| 146 | 145 | // 记录日志 |
| 147 | - recordBase := NewRecordBase(player.Uid, oldActivityId) | |
| 148 | - _ = svmysql.Create(NewRecordGetSettleAward(recordBase, | |
| 149 | - rsp.AwardText), | |
| 150 | - gameId) | |
| 146 | + recordBase := NewRecordBase(player.Uid, req.TopType, oldActivityId) | |
| 147 | + SaveRecordGetSettleAward(gameId, NewRecordGetSettleAward(recordBase, rsp.AwardText)) | |
| 151 | 148 | |
| 152 | 149 | return |
| 153 | 150 | } |
| ... | ... | @@ -161,7 +158,7 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs |
| 161 | 158 | playerLevel := req.PlayerLevel |
| 162 | 159 | |
| 163 | 160 | // 尝试更新配置 |
| 164 | - config, hasConfig := confroomrank.GetCurrent(gameId) | |
| 161 | + config, hasConfig := confroomrank.GetCurrent(gameId, req.TopType) | |
| 165 | 162 | if !hasConfig { |
| 166 | 163 | config = new(confroomrank.ActivityConfig) |
| 167 | 164 | code = code_msg.RECODE_MERGE_ROOMRANK_NOTOPEN_ERROR |
| ... | ... | @@ -169,7 +166,7 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs |
| 169 | 166 | } |
| 170 | 167 | |
| 171 | 168 | // 读取游戏数据 |
| 172 | - player := LoadPlayer(gameId, playerUid) | |
| 169 | + player := LoadPlayer(gameId, playerUid, req.TopType) | |
| 173 | 170 | player.Name = req.PlayerName |
| 174 | 171 | player.Icon = req.PlayerIcon |
| 175 | 172 | |
| ... | ... | @@ -218,10 +215,8 @@ func HandleAddScore(req *models.ReqRoomRankAddScore) (code string, rsp models.Rs |
| 218 | 215 | rsp.RoomRankInfo = GetInfoFromRoom(player, room, config) |
| 219 | 216 | |
| 220 | 217 | // 记录日志 |
| 221 | - recordBase := NewRecordBase(player.Uid, config.Id) | |
| 222 | - _ = svmysql.Create(NewRecordAddScore(recordBase, | |
| 223 | - req.AddScore, oldScore, newScore), | |
| 224 | - gameId) | |
| 218 | + recordBase := NewRecordBase(player.Uid, req.TopType, config.Id) | |
| 219 | + SaveRecordAddScore(gameId, NewRecordAddScore(recordBase, req.AddScore, oldScore, newScore)) | |
| 225 | 220 | |
| 226 | 221 | return |
| 227 | 222 | } | ... | ... |
service/roomrank/player.go
| ... | ... | @@ -11,10 +11,15 @@ import ( |
| 11 | 11 | "math/rand" |
| 12 | 12 | ) |
| 13 | 13 | |
| 14 | +func tryInitPlayer(gameId string, player *Player) { | |
| 15 | + svmysql.InitTable(player, gameId, false) | |
| 16 | +} | |
| 17 | + | |
| 14 | 18 | // SavePlayer 存储数据 |
| 15 | 19 | func SavePlayer(gameId string, player *Player) { |
| 16 | - player.UpdateTime = lxtime.NowUninx() | |
| 20 | + tryInitPlayer(gameId, player) | |
| 17 | 21 | |
| 22 | + player.UpdateTime = lxtime.NowUninx() | |
| 18 | 23 | err := svmysql.Save(player, gameId) |
| 19 | 24 | if err != nil { |
| 20 | 25 | lxalilog.Errors(err, "roomrank.SavePlayer error", gameId, player.Uid, player.ActivityId) |
| ... | ... | @@ -23,12 +28,14 @@ func SavePlayer(gameId string, player *Player) { |
| 23 | 28 | } |
| 24 | 29 | |
| 25 | 30 | // LoadPlayer 获取数据 外部接口 |
| 26 | -func LoadPlayer(gameId string, playerUid int64) (player *Player) { | |
| 27 | - player = NewPlayer(playerUid) | |
| 31 | +func LoadPlayer(gameId string, playerUid int64, topType int) (player *Player) { | |
| 32 | + player = NewPlayer(playerUid, topType) | |
| 33 | + tryInitPlayer(gameId, player) | |
| 34 | + | |
| 28 | 35 | has, err := svmysql.First(player, gameId) |
| 29 | 36 | if has { |
| 30 | 37 | } else { |
| 31 | - player.Init(playerUid) | |
| 38 | + player.Init(playerUid, topType) | |
| 32 | 39 | err = svmysql.Create(player, gameId) |
| 33 | 40 | if err != nil { |
| 34 | 41 | lxalilog.Errors(err, "roomrank._LoadPlayer Create error", gameId, player.Uid, player.ActivityId) | ... | ... |
| ... | ... | @@ -0,0 +1,13 @@ |
| 1 | +package roomrank | |
| 2 | + | |
| 3 | +import "apigame/service-common/svmysql" | |
| 4 | + | |
| 5 | +func SaveRecordGetSettleAward(gameId string, record *RecordGetSettleAward) { | |
| 6 | + svmysql.InitTable(record, gameId, false) | |
| 7 | + _ = svmysql.Create(record, gameId) | |
| 8 | +} | |
| 9 | + | |
| 10 | +func SaveRecordAddScore(gameId string, record *RecordAddScore) { | |
| 11 | + svmysql.InitTable(record, gameId, false) | |
| 12 | + _ = svmysql.Create(record, gameId) | |
| 13 | +} | ... | ... |
service/roomrank/room.go
| ... | ... | @@ -10,13 +10,13 @@ import ( |
| 10 | 10 | "sort" |
| 11 | 11 | ) |
| 12 | 12 | |
| 13 | -func tryInitTable(gameId string, room *Room) { | |
| 13 | +func tryInitRoom(gameId string, room *Room) { | |
| 14 | 14 | svmysql.InitTable(room, gameId, false) |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | // SaveRoom 存储数据 |
| 18 | 18 | func SaveRoom(gameId string, room *Room) { |
| 19 | - tryInitTable(gameId, room) | |
| 19 | + tryInitRoom(gameId, room) | |
| 20 | 20 | |
| 21 | 21 | room.UpdateTime = lxtime.NowUninx() |
| 22 | 22 | room.Encode() |
| ... | ... | @@ -35,7 +35,7 @@ func LoadRoom(gameId string, activityId int64, roomUid int64) (room *Room, has b |
| 35 | 35 | has = false |
| 36 | 36 | return |
| 37 | 37 | } |
| 38 | - tryInitTable(gameId, room) | |
| 38 | + tryInitRoom(gameId, room) | |
| 39 | 39 | |
| 40 | 40 | var err error |
| 41 | 41 | has, err = svmysql.First(room, gameId) |
| ... | ... | @@ -71,7 +71,7 @@ func CreateRoom(gameId string, activityId int64, roomConfigId int) (room *Room, |
| 71 | 71 | // FindRoom 查找 |
| 72 | 72 | func FindRoom(gameId string, activityId int64, roomConfigId int) (rooms []*Room, has bool) { |
| 73 | 73 | roomDemo := &Room{ActivityId: activityId} |
| 74 | - tryInitTable(gameId, roomDemo) | |
| 74 | + tryInitRoom(gameId, roomDemo) | |
| 75 | 75 | info := roomDemo.MysqlInfo(gameId) |
| 76 | 76 | |
| 77 | 77 | info.DbMysql = info.DbMysql.Where("config_id = ? AND closed = false", roomConfigId) | ... | ... |