Commit 4611a69d64f9df0d6a30c3bcd4250423b645a40b
1 parent
5e54fb0e
Exists in
master
and in
1 other branch
feat✨:房间排行活动:机器人名字和头像处理
Showing
6 changed files
with
116 additions
and
9 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,64 @@ |
| 1 | +package confglobal | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "apigame/configs/confbase" | |
| 5 | + "apigame/service-common/svconst" | |
| 6 | + "apigame/util/util-lx/lxalilog" | |
| 7 | + "encoding/json" | |
| 8 | + "fmt" | |
| 9 | +) | |
| 10 | + | |
| 11 | +// GlobalConfig 全局配置 | |
| 12 | +type GlobalConfig struct { | |
| 13 | + Raw *Raw `json:"-"` | |
| 14 | + Names []string | |
| 15 | + Avatars []string | |
| 16 | +} | |
| 17 | + | |
| 18 | +func (c *GlobalConfig) GetUid() string { | |
| 19 | + return c.Raw.GameId | |
| 20 | +} | |
| 21 | + | |
| 22 | +func (c *GlobalConfig) CheckCurrent() bool { | |
| 23 | + return true | |
| 24 | +} | |
| 25 | + | |
| 26 | +func (c *GlobalConfig) ConfInfo(suffix string) *confbase.ConfInfo { | |
| 27 | + tableName := "s_game_global_config" | |
| 28 | + cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix) | |
| 29 | + return &confbase.ConfInfo{ | |
| 30 | + DbMysql: svconst.DbConfig, | |
| 31 | + TableName: tableName, | |
| 32 | + KeyName: "gameid", | |
| 33 | + CacheKey: cacheKey, | |
| 34 | + CacheCurrent: cacheKey + ":current", | |
| 35 | + CacheTime: 300, | |
| 36 | + } | |
| 37 | +} | |
| 38 | + | |
| 39 | +// Raw 配置原始数据 | |
| 40 | +type Raw struct { | |
| 41 | + GameId string `gorm:"column:gameid"` | |
| 42 | + NameConfig string | |
| 43 | + AvatarConfig string | |
| 44 | +} | |
| 45 | + | |
| 46 | +// Decode 解析配置原始数据 | |
| 47 | +func (c *GlobalConfig) Decode(gameId string, rawData any) { | |
| 48 | + raw := rawData.(*Raw) | |
| 49 | + c.Raw = raw | |
| 50 | + { | |
| 51 | + err := json.Unmarshal([]byte(raw.NameConfig), &c.Names) | |
| 52 | + if err != nil { | |
| 53 | + lxalilog.Errors(err, raw.NameConfig, gameId, raw.GameId) | |
| 54 | + return | |
| 55 | + } | |
| 56 | + } | |
| 57 | + { | |
| 58 | + err := json.Unmarshal([]byte(raw.AvatarConfig), &c.Avatars) | |
| 59 | + if err != nil { | |
| 60 | + lxalilog.Errors(err, raw.AvatarConfig, gameId, raw.GameId) | |
| 61 | + return | |
| 62 | + } | |
| 63 | + } | |
| 64 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,19 @@ |
| 1 | +package confglobal | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "apigame/configs/confbase" | |
| 5 | + "errors" | |
| 6 | +) | |
| 7 | + | |
| 8 | +// GetConfig 获取 api游戏配置 | |
| 9 | +func GetConfig(gameId string) (conf *GlobalConfig, err error) { | |
| 10 | + | |
| 11 | + conf = new(GlobalConfig) | |
| 12 | + has := confbase.GetConfig[*GlobalConfig, Raw](gameId, gameId, conf) | |
| 13 | + if !has { | |
| 14 | + err = errors.New("confglobal.GetConfig error") | |
| 15 | + return | |
| 16 | + } | |
| 17 | + | |
| 18 | + return | |
| 19 | +} | ... | ... |
configs/init.go
| ... | ... | @@ -3,6 +3,7 @@ package configs |
| 3 | 3 | import ( |
| 4 | 4 | "apigame/configs/confapi" |
| 5 | 5 | "apigame/configs/confcardholder" |
| 6 | + "apigame/configs/confglobal" | |
| 6 | 7 | "apigame/configs/confroomrank" |
| 7 | 8 | "apigame/service-common/svconst" |
| 8 | 9 | ) |
| ... | ... | @@ -13,6 +14,10 @@ func Init() bool { |
| 13 | 14 | _, _ = confapi.GetConfig(gameId) |
| 14 | 15 | } |
| 15 | 16 | |
| 17 | + for _, gameId := range svconst.GameList { | |
| 18 | + _, _ = confglobal.GetConfig(gameId) | |
| 19 | + } | |
| 20 | + | |
| 16 | 21 | for _, gameId := range svconst.GameListCardHolder { |
| 17 | 22 | _, _ = confcardholder.GetCurrent(gameId) |
| 18 | 23 | } | ... | ... |
service-common/svcommon/simulate.go
| 1 | 1 | package svcommon |
| 2 | 2 | |
| 3 | +import ( | |
| 4 | + "apigame/configs/confglobal" | |
| 5 | + "math/rand" | |
| 6 | +) | |
| 7 | + | |
| 3 | 8 | // GetName 随机模拟玩家名字 |
| 4 | -func GetName() string { | |
| 9 | +func GetName(gameId string) string { | |
| 10 | + config, err := confglobal.GetConfig(gameId) | |
| 11 | + if err == nil { | |
| 12 | + count := len(config.Names) | |
| 13 | + if count > 0 { | |
| 14 | + return config.Names[rand.Intn(count)] | |
| 15 | + } | |
| 16 | + } | |
| 5 | 17 | return "李四" |
| 6 | 18 | } |
| 7 | 19 | |
| 8 | 20 | // GetIcon 随机模拟玩家头像 |
| 9 | -func GetIcon() string { | |
| 21 | +func GetIcon(gameId string) string { | |
| 22 | + config, err := confglobal.GetConfig(gameId) | |
| 23 | + if err == nil { | |
| 24 | + count := len(config.Avatars) | |
| 25 | + if count > 0 { | |
| 26 | + return config.Avatars[rand.Intn(count)] | |
| 27 | + } | |
| 28 | + } | |
| 10 | 29 | return "lisi_icon" |
| 11 | 30 | } | ... | ... |
service/roomrank/player.go
| ... | ... | @@ -38,11 +38,11 @@ func LoadPlayer(gameId string, playerUid int64) (player *Player) { |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | // NewRoomRobot 新建房间机器人 |
| 41 | -func NewRoomRobot(robotConfigId int, userType int) *RoomPlayer { | |
| 41 | +func NewRoomRobot(gameId string, robotConfigId int, userType int) *RoomPlayer { | |
| 42 | 42 | d := &RoomPlayer{ |
| 43 | 43 | Uid: rand.Int63(), |
| 44 | - Name: svcommon.GetName(), | |
| 45 | - Icon: svcommon.GetIcon(), | |
| 44 | + Name: svcommon.GetName(gameId), | |
| 45 | + Icon: svcommon.GetIcon(gameId), | |
| 46 | 46 | Score: 0, |
| 47 | 47 | JoinTime: lxtime.NowUninx(), |
| 48 | 48 | UserType: userType, | ... | ... |
service/roomrank/room.go
| ... | ... | @@ -144,7 +144,7 @@ func TryGetRoom(gameId string, player *Player, config *confroomrank.ActivityConf |
| 144 | 144 | room = roomCreate |
| 145 | 145 | |
| 146 | 146 | // 配置原生机器人 |
| 147 | - JoinInitRobot(room, roomConfig) | |
| 147 | + JoinInitRobot(gameId, room, roomConfig) | |
| 148 | 148 | |
| 149 | 149 | PlayerJoinRoom(room, player) |
| 150 | 150 | hasRoom = hasCreate |
| ... | ... | @@ -175,7 +175,7 @@ func TryCloseRoom(gameId string, room *Room, roomConfig confroomrank.RoomConfig) |
| 175 | 175 | canJoinCount := pair[1] - room.GetPlayerTypeCount(userType) |
| 176 | 176 | robotConfigId := roomConfig.AutoRobot[i] |
| 177 | 177 | for i := 0; i < canJoinCount; i++ { |
| 178 | - roomPlayer := NewRoomRobot(robotConfigId, userType) | |
| 178 | + roomPlayer := NewRoomRobot(gameId, robotConfigId, userType) | |
| 179 | 179 | room.Details.Players = append(room.Details.Players, roomPlayer) |
| 180 | 180 | } |
| 181 | 181 | } |
| ... | ... | @@ -197,13 +197,13 @@ func GetTypeCanJoinCount(room *Room, roomConfig confroomrank.RoomConfig, userTyp |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | // JoinInitRobot 配置原生机器人 |
| 200 | -func JoinInitRobot(room *Room, roomConfig confroomrank.RoomConfig) { | |
| 200 | +func JoinInitRobot(gameId string, room *Room, roomConfig confroomrank.RoomConfig) { | |
| 201 | 201 | for i := 0; i < len(roomConfig.InitRobot); i++ { |
| 202 | 202 | pair := roomConfig.InitRobot[i] |
| 203 | 203 | robotConfigId := pair[0] |
| 204 | 204 | robotCount := pair[1] |
| 205 | 205 | for i := 0; i < robotCount; i++ { |
| 206 | - roomPlayer := NewRoomRobot(robotConfigId, -1) | |
| 206 | + roomPlayer := NewRoomRobot(gameId, robotConfigId, -1) | |
| 207 | 207 | room.Details.Players = append(room.Details.Players, roomPlayer) |
| 208 | 208 | } |
| 209 | 209 | } | ... | ... |