diff --git a/service-common/svcommon/simulate.go b/service-common/svcommon/simulate.go index 1731ea6..e7f60bd 100644 --- a/service-common/svcommon/simulate.go +++ b/service-common/svcommon/simulate.go @@ -2,6 +2,7 @@ package svcommon import ( "apigame/configs/confglobal" + "apigame/util/utmisc" "math/rand" ) @@ -17,6 +18,28 @@ func GetName(gameId string) string { return "李四" } +func GetIndexNames(gameId string, amount int) []int { + list := make([]int, 0) + config, err := confglobal.GetConfig(gameId) + if err == nil { + count := len(config.Names) + if count > 0 { + return utmisc.RandomMultiple(amount, 0, count) + } + } + return list +} + +func GetIndexName(gameId string, index int) string { + config, err := confglobal.GetConfig(gameId) + if err == nil { + if len(config.Names) > index { + return config.Names[index] + } + } + return "李四" +} + // GetAvatar 随机模拟玩家头像 func GetAvatar(gameId string) string { config, err := confglobal.GetConfig(gameId) diff --git a/service/roomrank/dto-room.go b/service/roomrank/dto-room.go index 4803611..bd92bfb 100644 --- a/service/roomrank/dto-room.go +++ b/service/roomrank/dto-room.go @@ -48,7 +48,8 @@ type RoomPlayer struct { // RoomDetails 详情 type RoomDetails struct { - Players []*RoomPlayer // 房间玩家列表 + Players []*RoomPlayer // 房间玩家列表 + IndexNames []int // 房间玩家名字列表 } // Encode 打包数据 @@ -64,7 +65,8 @@ func (d *Room) Encode() { // Decode 分包数据 func (d *Room) Decode() { d.Details = &RoomDetails{ - Players: make([]*RoomPlayer, 0), + Players: make([]*RoomPlayer, 0), + IndexNames: make([]int, 0), } err := json.Unmarshal([]byte(d.DetailsText), d.Details) if err != nil { diff --git a/service/roomrank/player.go b/service/roomrank/player.go index a210342..be88fd7 100644 --- a/service/roomrank/player.go +++ b/service/roomrank/player.go @@ -8,7 +8,6 @@ import ( "apigame/util/util-lx/lxalilog" "apigame/util/util-lx/lxtime" "apigame/util/utstring" - "apigame/util/utuuid" ) func tryInitPlayer(gameId string, player *Player) { @@ -46,17 +45,24 @@ func LoadPlayer(gameId string, playerUid int64, topType int) (player *Player) { } // NewRoomRobot 新建房间机器人 -func NewRoomRobot(gameId string, robotConfigId int, userType int) *RoomPlayer { +func NewRoomRobot(gameId string, room *Room, robotConfigId int, userType int) *RoomPlayer { d := &RoomPlayer{ //Uid: rand.Int63(), - Uid: int64(utuuid.GetUint32()), - Name: svcommon.GetName(gameId), + //Uid: int64(utuuid.GetUint32()), + //Name: svcommon.GetName(gameId), Icon: svcommon.GetAvatar(gameId), Score: 0, JoinTime: lxtime.NowUninx(), UserType: userType, RobotConfigId: robotConfigId, } + if len(room.Details.IndexNames) > 0 { + d.Name = svcommon.GetIndexName(gameId, room.Details.IndexNames[0]) + room.Details.IndexNames = room.Details.IndexNames[1:] + } else { + d.Name = svcommon.GetName(gameId) + } + d.Uid = 9900_0000 + int64(len(room.Details.Players)) return d } diff --git a/service/roomrank/room.go b/service/roomrank/room.go index 9aeef71..0e9be0c 100644 --- a/service/roomrank/room.go +++ b/service/roomrank/room.go @@ -2,6 +2,7 @@ package roomrank import ( "apigame/configs/confroomrank" + "apigame/service-common/svcommon" "apigame/service-common/svmysql" "apigame/util/util-lx/lxalilog" "apigame/util/util-lx/lxtime" @@ -58,6 +59,7 @@ func CreateRoom(gameId string, topType int, activityId int64, roomConfigId int) room.CreateTime = lxtime.NowUninx() details := new(RoomDetails) details.Players = make([]*RoomPlayer, 0) + details.IndexNames = make([]int, 0) room.Details = details err := svmysql.Save(room, gameId) @@ -144,6 +146,8 @@ func TryGetRoom(gameId string, topType int, player *Player, config *confroomrank if hasCreate { room = roomCreate + room.Details.IndexNames = svcommon.GetIndexNames(gameId, roomConfig.TotalPlayer) + // 配置原生机器人 JoinInitRobot(gameId, room, roomConfig) @@ -177,7 +181,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(gameId, robotConfigId, userType) + roomPlayer := NewRoomRobot(gameId, room, robotConfigId, userType) room.Details.Players = append(room.Details.Players, roomPlayer) } } @@ -205,7 +209,7 @@ func JoinInitRobot(gameId string, room *Room, roomConfig confroomrank.RoomConfig robotConfigId := pair[0] robotCount := pair[1] for i := 0; i < robotCount; i++ { - roomPlayer := NewRoomRobot(gameId, robotConfigId, -1) + roomPlayer := NewRoomRobot(gameId, room, robotConfigId, -1) room.Details.Players = append(room.Details.Players, roomPlayer) } } diff --git a/util/utmisc/random.go b/util/utmisc/random.go new file mode 100644 index 0000000..958433d --- /dev/null +++ b/util/utmisc/random.go @@ -0,0 +1,21 @@ +package utmisc + +import "math/rand" + +func RandomMultiple(count, minNum, maxNum int) []int { + list := make([]int, 0) + if maxNum > minNum { + if count > maxNum-minNum { + count = maxNum - minNum + } + m := make(map[int]struct{}) + for len(m) < count { + v := minNum + rand.Intn(maxNum-minNum) + if _, ok := m[v]; !ok { + m[v] = struct{}{} + list = append(list, v) + } + } + } + return list +} -- libgit2 0.21.0