diff --git a/configs/confglobal/config.go b/configs/confglobal/config.go new file mode 100644 index 0000000..3bf47ef --- /dev/null +++ b/configs/confglobal/config.go @@ -0,0 +1,64 @@ +package confglobal + +import ( + "apigame/configs/confbase" + "apigame/service-common/svconst" + "apigame/util/util-lx/lxalilog" + "encoding/json" + "fmt" +) + +// GlobalConfig 全局配置 +type GlobalConfig struct { + Raw *Raw `json:"-"` + Names []string + Avatars []string +} + +func (c *GlobalConfig) GetUid() string { + return c.Raw.GameId +} + +func (c *GlobalConfig) CheckCurrent() bool { + return true +} + +func (c *GlobalConfig) ConfInfo(suffix string) *confbase.ConfInfo { + tableName := "s_game_global_config" + cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix) + return &confbase.ConfInfo{ + DbMysql: svconst.DbConfig, + TableName: tableName, + KeyName: "gameid", + CacheKey: cacheKey, + CacheCurrent: cacheKey + ":current", + CacheTime: 300, + } +} + +// Raw 配置原始数据 +type Raw struct { + GameId string `gorm:"column:gameid"` + NameConfig string + AvatarConfig string +} + +// Decode 解析配置原始数据 +func (c *GlobalConfig) Decode(gameId string, rawData any) { + raw := rawData.(*Raw) + c.Raw = raw + { + err := json.Unmarshal([]byte(raw.NameConfig), &c.Names) + if err != nil { + lxalilog.Errors(err, raw.NameConfig, gameId, raw.GameId) + return + } + } + { + err := json.Unmarshal([]byte(raw.AvatarConfig), &c.Avatars) + if err != nil { + lxalilog.Errors(err, raw.AvatarConfig, gameId, raw.GameId) + return + } + } +} diff --git a/configs/confglobal/get.go b/configs/confglobal/get.go new file mode 100644 index 0000000..ca4cfe1 --- /dev/null +++ b/configs/confglobal/get.go @@ -0,0 +1,19 @@ +package confglobal + +import ( + "apigame/configs/confbase" + "errors" +) + +// GetConfig 获取 api游戏配置 +func GetConfig(gameId string) (conf *GlobalConfig, err error) { + + conf = new(GlobalConfig) + has := confbase.GetConfig[*GlobalConfig, Raw](gameId, gameId, conf) + if !has { + err = errors.New("confglobal.GetConfig error") + return + } + + return +} diff --git a/configs/init.go b/configs/init.go index 5293ff6..73c3a79 100644 --- a/configs/init.go +++ b/configs/init.go @@ -3,6 +3,7 @@ package configs import ( "apigame/configs/confapi" "apigame/configs/confcardholder" + "apigame/configs/confglobal" "apigame/configs/confroomrank" "apigame/service-common/svconst" ) @@ -13,6 +14,10 @@ func Init() bool { _, _ = confapi.GetConfig(gameId) } + for _, gameId := range svconst.GameList { + _, _ = confglobal.GetConfig(gameId) + } + for _, gameId := range svconst.GameListCardHolder { _, _ = confcardholder.GetCurrent(gameId) } diff --git a/service-common/svcommon/simulate.go b/service-common/svcommon/simulate.go index 52ec0d2..8d33935 100644 --- a/service-common/svcommon/simulate.go +++ b/service-common/svcommon/simulate.go @@ -1,11 +1,30 @@ package svcommon +import ( + "apigame/configs/confglobal" + "math/rand" +) + // GetName 随机模拟玩家名字 -func GetName() string { +func GetName(gameId string) string { + config, err := confglobal.GetConfig(gameId) + if err == nil { + count := len(config.Names) + if count > 0 { + return config.Names[rand.Intn(count)] + } + } return "李四" } // GetIcon 随机模拟玩家头像 -func GetIcon() string { +func GetIcon(gameId string) string { + config, err := confglobal.GetConfig(gameId) + if err == nil { + count := len(config.Avatars) + if count > 0 { + return config.Avatars[rand.Intn(count)] + } + } return "lisi_icon" } diff --git a/service/roomrank/player.go b/service/roomrank/player.go index 4bb26a2..38dd566 100644 --- a/service/roomrank/player.go +++ b/service/roomrank/player.go @@ -38,11 +38,11 @@ func LoadPlayer(gameId string, playerUid int64) (player *Player) { } // NewRoomRobot 新建房间机器人 -func NewRoomRobot(robotConfigId int, userType int) *RoomPlayer { +func NewRoomRobot(gameId string, robotConfigId int, userType int) *RoomPlayer { d := &RoomPlayer{ Uid: rand.Int63(), - Name: svcommon.GetName(), - Icon: svcommon.GetIcon(), + Name: svcommon.GetName(gameId), + Icon: svcommon.GetIcon(gameId), Score: 0, JoinTime: lxtime.NowUninx(), UserType: userType, diff --git a/service/roomrank/room.go b/service/roomrank/room.go index ee5451b..bc5dbcd 100644 --- a/service/roomrank/room.go +++ b/service/roomrank/room.go @@ -144,7 +144,7 @@ func TryGetRoom(gameId string, player *Player, config *confroomrank.ActivityConf room = roomCreate // 配置原生机器人 - JoinInitRobot(room, roomConfig) + JoinInitRobot(gameId, room, roomConfig) PlayerJoinRoom(room, player) hasRoom = hasCreate @@ -175,7 +175,7 @@ func TryCloseRoom(gameId string, room *Room, roomConfig confroomrank.RoomConfig) canJoinCount := pair[1] - room.GetPlayerTypeCount(userType) robotConfigId := roomConfig.AutoRobot[i] for i := 0; i < canJoinCount; i++ { - roomPlayer := NewRoomRobot(robotConfigId, userType) + roomPlayer := NewRoomRobot(gameId, robotConfigId, userType) room.Details.Players = append(room.Details.Players, roomPlayer) } } @@ -197,13 +197,13 @@ func GetTypeCanJoinCount(room *Room, roomConfig confroomrank.RoomConfig, userTyp } // JoinInitRobot 配置原生机器人 -func JoinInitRobot(room *Room, roomConfig confroomrank.RoomConfig) { +func JoinInitRobot(gameId string, room *Room, roomConfig confroomrank.RoomConfig) { for i := 0; i < len(roomConfig.InitRobot); i++ { pair := roomConfig.InitRobot[i] robotConfigId := pair[0] robotCount := pair[1] for i := 0; i < robotCount; i++ { - roomPlayer := NewRoomRobot(robotConfigId, -1) + roomPlayer := NewRoomRobot(gameId, robotConfigId, -1) room.Details.Players = append(room.Details.Players, roomPlayer) } } -- libgit2 0.21.0