battle_manager.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package ludo
  2. import (
  3. "crypto/rand"
  4. "encoding/binary"
  5. "fmt"
  6. "math/big"
  7. "server/msg"
  8. )
  9. func randomSz() int32 {
  10. // 生成0-5的随机数
  11. n, err := rand.Int(rand.Reader, big.NewInt(6))
  12. if err != nil {
  13. panic(err) // 在实际应用中应该更优雅地处理错误
  14. }
  15. // 转换为int32并加1得到1-6的范围
  16. return int32(n.Int64()) + 1
  17. }
  18. func roleIndexOf(arr []*msg.ColorData, target *msg.ColorData) int {
  19. for i, v := range arr {
  20. if v == target { // 比较指针地址
  21. return i
  22. }
  23. }
  24. return -1 // 未找到
  25. }
  26. func getRandomItem[T any](slice []T) T {
  27. if len(slice) == 0 {
  28. var zero T
  29. return zero
  30. }
  31. // 生成加密安全的随机数
  32. n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(slice))))
  33. return slice[n.Int64()]
  34. }
  35. // 随机获取一个人机名字
  36. func getRandomRobotName(existingNames []string) string {
  37. allNames := []string{
  38. "张三", "李四", "王五", "赵六",
  39. "钱七", "孙八", "周九", "吴十",
  40. }
  41. return getRandomName(allNames, existingNames)
  42. }
  43. func getRandomName(allNames []string, existingNames []string) string {
  44. // 1. 创建剩余名字的切片
  45. remainingNames := make([]string, 0)
  46. // 2. 构建已存在名字的映射(提高查找效率)
  47. existingMap := make(map[string]bool)
  48. for _, name := range existingNames {
  49. existingMap[name] = true
  50. }
  51. // 3. 筛选出未使用的名字
  52. for _, name := range allNames {
  53. if !existingMap[name] {
  54. remainingNames = append(remainingNames, name)
  55. }
  56. }
  57. // 4. 如果没有剩余名字,返回空字符串
  58. if len(remainingNames) == 0 {
  59. return ""
  60. }
  61. // 5. 使用 crypto/rand 生成安全的随机索引
  62. n, err := rand.Int(rand.Reader, big.NewInt(int64(len(remainingNames))))
  63. if err != nil {
  64. // 如果出现错误,作为后备方案使用不太安全但更可靠的方案
  65. var fallbackSeed int64
  66. binary.Read(rand.Reader, binary.LittleEndian, &fallbackSeed)
  67. if fallbackSeed < 0 {
  68. fallbackSeed = -fallbackSeed
  69. }
  70. return remainingNames[fallbackSeed%int64(len(remainingNames))]
  71. }
  72. // 6. 返回随机选择的名字
  73. return remainingNames[n.Int64()]
  74. }
  75. // 随机获取一个人机头像
  76. func getRandomRobotHead() string {
  77. n, err := rand.Int(rand.Reader, big.NewInt(13))
  78. if err != nil {
  79. panic(err) // 在实际应用中应该更优雅地处理错误
  80. }
  81. return fmt.Sprintf("3_%d", n)
  82. }
  83. // 随机获取一个人机金币
  84. func getRandomRobotCoin() int32 {
  85. n, err := rand.Int(rand.Reader, big.NewInt(1000))
  86. if err != nil {
  87. panic(err) // 在实际应用中应该更优雅地处理错误
  88. }
  89. return int32(n.Int64()) + 1000
  90. }
  91. func GetNextRole(currentRole msg.RoleType) msg.RoleType {
  92. // 定义角色顺序
  93. roleOrder := []msg.RoleType{
  94. msg.RoleType_GREEN,
  95. msg.RoleType_YELLOW,
  96. msg.RoleType_BLUE,
  97. msg.RoleType_RED,
  98. }
  99. // 查找当前角色在顺序中的位置
  100. for i, role := range roleOrder {
  101. if role == currentRole {
  102. // 如果是最后一个角色,返回第一个角色(循环)
  103. if i == len(roleOrder)-1 {
  104. return roleOrder[0]
  105. }
  106. return roleOrder[i+1]
  107. }
  108. }
  109. return msg.RoleType_ROLE_TYPE_UNKNOWN
  110. }