Commit 9ac6f9057b5f924f9824cf67b605cea0f2cf7706

Authored by 王家文
1 parent 44dad9cf
Exists in master and in 1 other branch dev-wjw

feat✨:机器人的ID和名字不重复算法

service-common/svcommon/simulate.go
@@ -2,6 +2,7 @@ package svcommon @@ -2,6 +2,7 @@ package svcommon
2 2
3 import ( 3 import (
4 "apigame/configs/confglobal" 4 "apigame/configs/confglobal"
  5 + "apigame/util/utmisc"
5 "math/rand" 6 "math/rand"
6 ) 7 )
7 8
@@ -17,6 +18,28 @@ func GetName(gameId string) string { @@ -17,6 +18,28 @@ func GetName(gameId string) string {
17 return "李四" 18 return "李四"
18 } 19 }
19 20
  21 +func GetIndexNames(gameId string, amount int) []int {
  22 + list := make([]int, 0)
  23 + config, err := confglobal.GetConfig(gameId)
  24 + if err == nil {
  25 + count := len(config.Names)
  26 + if count > 0 {
  27 + return utmisc.RandomMultiple(amount, 0, count)
  28 + }
  29 + }
  30 + return list
  31 +}
  32 +
  33 +func GetIndexName(gameId string, index int) string {
  34 + config, err := confglobal.GetConfig(gameId)
  35 + if err == nil {
  36 + if len(config.Names) > index {
  37 + return config.Names[index]
  38 + }
  39 + }
  40 + return "李四"
  41 +}
  42 +
20 // GetAvatar 随机模拟玩家头像 43 // GetAvatar 随机模拟玩家头像
21 func GetAvatar(gameId string) string { 44 func GetAvatar(gameId string) string {
22 config, err := confglobal.GetConfig(gameId) 45 config, err := confglobal.GetConfig(gameId)
service/roomrank/dto-room.go
@@ -48,7 +48,8 @@ type RoomPlayer struct { @@ -48,7 +48,8 @@ type RoomPlayer struct {
48 48
49 // RoomDetails 详情 49 // RoomDetails 详情
50 type RoomDetails struct { 50 type RoomDetails struct {
51 - Players []*RoomPlayer // 房间玩家列表 51 + Players []*RoomPlayer // 房间玩家列表
  52 + IndexNames []int // 房间玩家名字列表
52 } 53 }
53 54
54 // Encode 打包数据 55 // Encode 打包数据
@@ -64,7 +65,8 @@ func (d *Room) Encode() { @@ -64,7 +65,8 @@ func (d *Room) Encode() {
64 // Decode 分包数据 65 // Decode 分包数据
65 func (d *Room) Decode() { 66 func (d *Room) Decode() {
66 d.Details = &RoomDetails{ 67 d.Details = &RoomDetails{
67 - Players: make([]*RoomPlayer, 0), 68 + Players: make([]*RoomPlayer, 0),
  69 + IndexNames: make([]int, 0),
68 } 70 }
69 err := json.Unmarshal([]byte(d.DetailsText), d.Details) 71 err := json.Unmarshal([]byte(d.DetailsText), d.Details)
70 if err != nil { 72 if err != nil {
service/roomrank/player.go
@@ -8,7 +8,6 @@ import ( @@ -8,7 +8,6 @@ import (
8 "apigame/util/util-lx/lxalilog" 8 "apigame/util/util-lx/lxalilog"
9 "apigame/util/util-lx/lxtime" 9 "apigame/util/util-lx/lxtime"
10 "apigame/util/utstring" 10 "apigame/util/utstring"
11 - "apigame/util/utuuid"  
12 ) 11 )
13 12
14 func tryInitPlayer(gameId string, player *Player) { 13 func tryInitPlayer(gameId string, player *Player) {
@@ -46,17 +45,24 @@ func LoadPlayer(gameId string, playerUid int64, topType int) (player *Player) { @@ -46,17 +45,24 @@ func LoadPlayer(gameId string, playerUid int64, topType int) (player *Player) {
46 } 45 }
47 46
48 // NewRoomRobot 新建房间机器人 47 // NewRoomRobot 新建房间机器人
49 -func NewRoomRobot(gameId string, robotConfigId int, userType int) *RoomPlayer { 48 +func NewRoomRobot(gameId string, room *Room, robotConfigId int, userType int) *RoomPlayer {
50 d := &RoomPlayer{ 49 d := &RoomPlayer{
51 //Uid: rand.Int63(), 50 //Uid: rand.Int63(),
52 - Uid: int64(utuuid.GetUint32()),  
53 - Name: svcommon.GetName(gameId), 51 + //Uid: int64(utuuid.GetUint32()),
  52 + //Name: svcommon.GetName(gameId),
54 Icon: svcommon.GetAvatar(gameId), 53 Icon: svcommon.GetAvatar(gameId),
55 Score: 0, 54 Score: 0,
56 JoinTime: lxtime.NowUninx(), 55 JoinTime: lxtime.NowUninx(),
57 UserType: userType, 56 UserType: userType,
58 RobotConfigId: robotConfigId, 57 RobotConfigId: robotConfigId,
59 } 58 }
  59 + if len(room.Details.IndexNames) > 0 {
  60 + d.Name = svcommon.GetIndexName(gameId, room.Details.IndexNames[0])
  61 + room.Details.IndexNames = room.Details.IndexNames[1:]
  62 + } else {
  63 + d.Name = svcommon.GetName(gameId)
  64 + }
  65 + d.Uid = 9900_0000 + int64(len(room.Details.Players))
60 return d 66 return d
61 } 67 }
62 68
service/roomrank/room.go
@@ -2,6 +2,7 @@ package roomrank @@ -2,6 +2,7 @@ package roomrank
2 2
3 import ( 3 import (
4 "apigame/configs/confroomrank" 4 "apigame/configs/confroomrank"
  5 + "apigame/service-common/svcommon"
5 "apigame/service-common/svmysql" 6 "apigame/service-common/svmysql"
6 "apigame/util/util-lx/lxalilog" 7 "apigame/util/util-lx/lxalilog"
7 "apigame/util/util-lx/lxtime" 8 "apigame/util/util-lx/lxtime"
@@ -58,6 +59,7 @@ func CreateRoom(gameId string, topType int, activityId int64, roomConfigId int) @@ -58,6 +59,7 @@ func CreateRoom(gameId string, topType int, activityId int64, roomConfigId int)
58 room.CreateTime = lxtime.NowUninx() 59 room.CreateTime = lxtime.NowUninx()
59 details := new(RoomDetails) 60 details := new(RoomDetails)
60 details.Players = make([]*RoomPlayer, 0) 61 details.Players = make([]*RoomPlayer, 0)
  62 + details.IndexNames = make([]int, 0)
61 room.Details = details 63 room.Details = details
62 64
63 err := svmysql.Save(room, gameId) 65 err := svmysql.Save(room, gameId)
@@ -144,6 +146,8 @@ func TryGetRoom(gameId string, topType int, player *Player, config *confroomrank @@ -144,6 +146,8 @@ func TryGetRoom(gameId string, topType int, player *Player, config *confroomrank
144 if hasCreate { 146 if hasCreate {
145 room = roomCreate 147 room = roomCreate
146 148
  149 + room.Details.IndexNames = svcommon.GetIndexNames(gameId, roomConfig.TotalPlayer)
  150 +
147 // 配置原生机器人 151 // 配置原生机器人
148 JoinInitRobot(gameId, room, roomConfig) 152 JoinInitRobot(gameId, room, roomConfig)
149 153
@@ -177,7 +181,7 @@ func TryCloseRoom(gameId string, room *Room, roomConfig confroomrank.RoomConfig) @@ -177,7 +181,7 @@ func TryCloseRoom(gameId string, room *Room, roomConfig confroomrank.RoomConfig)
177 canJoinCount := pair[1] - room.GetPlayerTypeCount(userType) 181 canJoinCount := pair[1] - room.GetPlayerTypeCount(userType)
178 robotConfigId := roomConfig.AutoRobot[i] 182 robotConfigId := roomConfig.AutoRobot[i]
179 for i := 0; i < canJoinCount; i++ { 183 for i := 0; i < canJoinCount; i++ {
180 - roomPlayer := NewRoomRobot(gameId, robotConfigId, userType) 184 + roomPlayer := NewRoomRobot(gameId, room, robotConfigId, userType)
181 room.Details.Players = append(room.Details.Players, roomPlayer) 185 room.Details.Players = append(room.Details.Players, roomPlayer)
182 } 186 }
183 } 187 }
@@ -205,7 +209,7 @@ func JoinInitRobot(gameId string, room *Room, roomConfig confroomrank.RoomConfig @@ -205,7 +209,7 @@ func JoinInitRobot(gameId string, room *Room, roomConfig confroomrank.RoomConfig
205 robotConfigId := pair[0] 209 robotConfigId := pair[0]
206 robotCount := pair[1] 210 robotCount := pair[1]
207 for i := 0; i < robotCount; i++ { 211 for i := 0; i < robotCount; i++ {
208 - roomPlayer := NewRoomRobot(gameId, robotConfigId, -1) 212 + roomPlayer := NewRoomRobot(gameId, room, robotConfigId, -1)
209 room.Details.Players = append(room.Details.Players, roomPlayer) 213 room.Details.Players = append(room.Details.Players, roomPlayer)
210 } 214 }
211 } 215 }
util/utmisc/random.go 0 → 100644
@@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
  1 +package utmisc
  2 +
  3 +import "math/rand"
  4 +
  5 +func RandomMultiple(count, minNum, maxNum int) []int {
  6 + list := make([]int, 0)
  7 + if maxNum > minNum {
  8 + if count > maxNum-minNum {
  9 + count = maxNum - minNum
  10 + }
  11 + m := make(map[int]struct{})
  12 + for len(m) < count {
  13 + v := minNum + rand.Intn(maxNum-minNum)
  14 + if _, ok := m[v]; !ok {
  15 + m[v] = struct{}{}
  16 + list = append(list, v)
  17 + }
  18 + }
  19 + }
  20 + return list
  21 +}