Commit b2de8adde0accee4bc53665805465165c8042ad2
1 parent
afd7d6f2
Exists in
master
and in
1 other branch
feat✨:房间排行活动:增加玩家名字头像等
Showing
7 changed files
with
97 additions
and
38 deletions
Show diff stats
models/roomrank.go
| ... | ... | @@ -17,17 +17,30 @@ type RspRoomRankGetConfig struct { |
| 17 | 17 | Config any `form:"config" json:"config"` // 活动配置对象 |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | +// RoomRankTopNode 排行玩家 | |
| 21 | +type RoomRankTopNode struct { | |
| 22 | + Rank int `form:"rank" json:"rank"` // 排名 | |
| 23 | + Uid int64 `form:"uid" json:"uid"` // 玩家唯一ID | |
| 24 | + Score int64 `form:"score" json:"score"` // 玩家排行分数 | |
| 25 | + Name string `form:"name" json:"name"` // 名字 | |
| 26 | + Icon string `form:"icon" json:"icon"` // 头像 | |
| 27 | +} | |
| 28 | + | |
| 20 | 29 | // RoomRankInfo 房间排行信息 |
| 21 | 30 | type RoomRankInfo struct { |
| 22 | - SettleHas bool `form:"settle_has" json:"settle_has"` // 有结算内容未领取 | |
| 23 | - SettleAward string `form:"settle_award" json:"settle_award"` // 结算奖励内容 | |
| 31 | + SettleHas bool `form:"settle_has" json:"settle_has"` // 有结算内容未领取 | |
| 32 | + SettleAward string `form:"settle_award" json:"settle_award"` // 结算奖励内容 | |
| 33 | + RankSelf int `form:"rank_self" json:"rank_self"` // 自己排名 | |
| 34 | + Tops []RoomRankTopNode `form:"tops" json:"tops"` // 排行榜数据列表 | |
| 24 | 35 | } |
| 25 | 36 | |
| 26 | 37 | // ReqRoomRankInfo 请求 房间排行信息 |
| 27 | 38 | type ReqRoomRankInfo struct { |
| 28 | 39 | BaseLoginInfo |
| 29 | 40 | BaseSign |
| 30 | - PlayerLevel int `form:"player_level" json:"player_level"` // 玩家等级 | |
| 41 | + PlayerLevel int `form:"player_level" json:"player_level"` // 玩家等级 | |
| 42 | + PlayerName string `form:"player_name" json:"player_name"` // 玩家名字 | |
| 43 | + PlayerIcon string `form:"player_icon" json:"player_icon"` // 玩家头像 | |
| 31 | 44 | } |
| 32 | 45 | |
| 33 | 46 | // RspRoomRankInfo 返回 房间排行信息 | ... | ... |
service-common/svmysql/dto.go
| ... | ... | @@ -3,7 +3,7 @@ package svmysql |
| 3 | 3 | func InitTable(obj IMysqlData, suffix string, notify bool) { |
| 4 | 4 | info := obj.MysqlInfo(suffix) |
| 5 | 5 | db := info.DbMysql |
| 6 | - _ = db.Table(info.TableName).AutoMigrate(obj) | |
| 6 | + _ = db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").Table(info.TableName).AutoMigrate(obj) | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | func Create(obj IMysqlData, suffix string) (err error) { | ... | ... |
service/roomrank/dto-player.go
| ... | ... | @@ -9,8 +9,10 @@ import ( |
| 9 | 9 | |
| 10 | 10 | // Player 房间排行持久数据 |
| 11 | 11 | type Player struct { |
| 12 | - Uid int64 `gorm:"column:uid;primaryKey;comment:玩家唯一ID"` | |
| 13 | - ActivityId int64 `gorm:"comment:活动ID"` | |
| 12 | + Uid int64 `gorm:"column:uid;primaryKey;comment:玩家唯一ID"` | |
| 13 | + Name string `gorm:"-"` // 玩家名字 | |
| 14 | + Icon string `gorm:"-"` // 玩家头像 | |
| 15 | + ActivityId int64 `gorm:"comment:活动ID"` | |
| 14 | 16 | |
| 15 | 17 | UserType int `gorm:"comment:用户类型"` // 0=新手用户 1=优质用户 2=普通用户 3=垃圾用户 |
| 16 | 18 | UserScore int `gorm:"comment:用户评级分"` | ... | ... |
service/roomrank/dto-room.go
| ... | ... | @@ -4,11 +4,9 @@ import ( |
| 4 | 4 | "apigame/service-common/svconst" |
| 5 | 5 | "apigame/service-common/svmysql" |
| 6 | 6 | "apigame/util/util-lx/lxalilog" |
| 7 | - "apigame/util/util-lx/lxtime" | |
| 8 | 7 | "encoding/json" |
| 9 | 8 | "fmt" |
| 10 | 9 | "github.com/samber/lo" |
| 11 | - "math/rand" | |
| 12 | 10 | ) |
| 13 | 11 | |
| 14 | 12 | // Room 房间排行持久数据 |
| ... | ... | @@ -38,33 +36,13 @@ func (d *Room) MysqlInfo(suffix string) *svmysql.MysqlInfo { |
| 38 | 36 | |
| 39 | 37 | // RoomPlayer 房间玩家 |
| 40 | 38 | type RoomPlayer struct { |
| 41 | - Uid int64 // 玩家唯一ID | |
| 42 | - Score int64 // 玩家排行分数 | |
| 43 | - JoinTime int64 // 加入时间 | |
| 44 | - UserType int // 用户类型 -1=原生配置机器人 | |
| 45 | - RobotConfigId int // 0=玩家 | |
| 46 | -} | |
| 47 | - | |
| 48 | -func NewRoomRobot(robotConfigId int, userType int) *RoomPlayer { | |
| 49 | - d := &RoomPlayer{ | |
| 50 | - Uid: rand.Int63(), | |
| 51 | - Score: 0, | |
| 52 | - JoinTime: lxtime.NowUninx(), | |
| 53 | - UserType: userType, | |
| 54 | - RobotConfigId: robotConfigId, | |
| 55 | - } | |
| 56 | - return d | |
| 57 | -} | |
| 58 | - | |
| 59 | -func NewRoomPlayer(player *Player) *RoomPlayer { | |
| 60 | - d := &RoomPlayer{ | |
| 61 | - Uid: player.Uid, | |
| 62 | - Score: 0, | |
| 63 | - JoinTime: lxtime.NowUninx(), | |
| 64 | - UserType: player.UserType, | |
| 65 | - RobotConfigId: 0, | |
| 66 | - } | |
| 67 | - return d | |
| 39 | + Uid int64 // 玩家唯一ID | |
| 40 | + Name string // 玩家名字 | |
| 41 | + Icon string // 玩家头像 | |
| 42 | + Score int64 // 玩家排行分数 | |
| 43 | + JoinTime int64 // 加入时间 | |
| 44 | + UserType int // 用户类型 -1=原生配置机器人 | |
| 45 | + RobotConfigId int // 0=玩家 | |
| 68 | 46 | } |
| 69 | 47 | |
| 70 | 48 | // RoomDetails 详情 | ... | ... |
service/roomrank/handle.go
| ... | ... | @@ -54,13 +54,15 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan |
| 54 | 54 | |
| 55 | 55 | // 读取游戏数据 |
| 56 | 56 | player := LoadPlayer(gameId, playerUid) |
| 57 | + player.Name = req.PlayerName | |
| 58 | + player.Icon = req.PlayerIcon | |
| 57 | 59 | |
| 58 | 60 | // 尝试判断结算 |
| 59 | 61 | hasSettle := TrySettle(gameId, player, config) |
| 60 | 62 | |
| 61 | 63 | if hasSettle { |
| 62 | 64 | SavePlayer(gameId, player) |
| 63 | - rsp.RoomRankInfo = GetInfo(player, config) | |
| 65 | + rsp.RoomRankInfo = GetInfoFromSettle(player, config) | |
| 64 | 66 | return |
| 65 | 67 | } |
| 66 | 68 | |
| ... | ... | @@ -74,6 +76,8 @@ func HandleInfo(req *models.ReqRoomRankInfo) (code string, rsp models.RspRoomRan |
| 74 | 76 | SaveRoom(gameId, room) |
| 75 | 77 | } |
| 76 | 78 | |
| 79 | + rsp.RoomRankInfo = GetInfoFromRoom(player, room) | |
| 80 | + | |
| 77 | 81 | fmt.Println("dwjw🐸 len(room.Details.Players) 1:", len(room.Details.Players)) |
| 78 | 82 | |
| 79 | 83 | return | ... | ... |
service/roomrank/player.go
| ... | ... | @@ -3,9 +3,11 @@ package roomrank |
| 3 | 3 | import ( |
| 4 | 4 | "apigame/configs/confroomrank" |
| 5 | 5 | "apigame/models" |
| 6 | + "apigame/service-common/svcommon" | |
| 6 | 7 | "apigame/service-common/svmysql" |
| 7 | 8 | "apigame/util/util-lx/lxalilog" |
| 8 | 9 | "apigame/util/util-lx/lxtime" |
| 10 | + "math/rand" | |
| 9 | 11 | ) |
| 10 | 12 | |
| 11 | 13 | // SavePlayer 存储数据 |
| ... | ... | @@ -35,8 +37,36 @@ func LoadPlayer(gameId string, playerUid int64) (player *Player) { |
| 35 | 37 | return |
| 36 | 38 | } |
| 37 | 39 | |
| 38 | -// GetInfo 活动信息 | |
| 39 | -func GetInfo(player *Player, conf *confroomrank.ActivityConfig) models.RoomRankInfo { | |
| 40 | +// NewRoomRobot 新建房间机器人 | |
| 41 | +func NewRoomRobot(robotConfigId int, userType int) *RoomPlayer { | |
| 42 | + d := &RoomPlayer{ | |
| 43 | + Uid: rand.Int63(), | |
| 44 | + Name: svcommon.GetName(), | |
| 45 | + Icon: svcommon.GetIcon(), | |
| 46 | + Score: 0, | |
| 47 | + JoinTime: lxtime.NowUninx(), | |
| 48 | + UserType: userType, | |
| 49 | + RobotConfigId: robotConfigId, | |
| 50 | + } | |
| 51 | + return d | |
| 52 | +} | |
| 53 | + | |
| 54 | +// NewRoomPlayer 新建房间玩家 | |
| 55 | +func NewRoomPlayer(player *Player) *RoomPlayer { | |
| 56 | + d := &RoomPlayer{ | |
| 57 | + Uid: player.Uid, | |
| 58 | + Name: player.Name, | |
| 59 | + Icon: player.Icon, | |
| 60 | + Score: 0, | |
| 61 | + JoinTime: lxtime.NowUninx(), | |
| 62 | + UserType: player.UserType, | |
| 63 | + RobotConfigId: 0, | |
| 64 | + } | |
| 65 | + return d | |
| 66 | +} | |
| 67 | + | |
| 68 | +// GetInfoFromSettle 活动信息 从结算 | |
| 69 | +func GetInfoFromSettle(player *Player, conf *confroomrank.ActivityConfig) models.RoomRankInfo { | |
| 40 | 70 | info := models.RoomRankInfo{} |
| 41 | 71 | if player.SettleHas { |
| 42 | 72 | info.SettleHas = player.SettleHas |
| ... | ... | @@ -44,3 +74,24 @@ func GetInfo(player *Player, conf *confroomrank.ActivityConfig) models.RoomRankI |
| 44 | 74 | } |
| 45 | 75 | return info |
| 46 | 76 | } |
| 77 | + | |
| 78 | +// GetInfoFromRoom 活动信息 从房间 | |
| 79 | +func GetInfoFromRoom(player *Player, room *Room) models.RoomRankInfo { | |
| 80 | + info := models.RoomRankInfo{} | |
| 81 | + for i := 0; i < len(room.Details.Players); i++ { | |
| 82 | + p := room.Details.Players[i] | |
| 83 | + rank := i + 1 | |
| 84 | + node := models.RoomRankTopNode{ | |
| 85 | + Rank: rank, | |
| 86 | + Uid: p.Uid, | |
| 87 | + Score: p.Score, | |
| 88 | + Name: p.Name, | |
| 89 | + Icon: p.Icon, | |
| 90 | + } | |
| 91 | + if player.Uid == p.Uid { | |
| 92 | + info.RankSelf = rank | |
| 93 | + } | |
| 94 | + info.Tops = append(info.Tops, node) | |
| 95 | + } | |
| 96 | + return info | |
| 97 | +} | ... | ... |