123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package ludo
- import (
- "crypto/rand"
- "encoding/binary"
- "fmt"
- "math/big"
- "server/msg"
- )
- func randomSz() int32 {
-
- n, err := rand.Int(rand.Reader, big.NewInt(6))
- if err != nil {
- panic(err)
- }
-
- return int32(n.Int64()) + 1
- }
- func roleIndexOf(arr []*msg.ColorData, target *msg.ColorData) int {
- for i, v := range arr {
- if v == target {
- return i
- }
- }
- return -1
- }
- func getRandomItem[T any](slice []T) T {
- if len(slice) == 0 {
- var zero T
- return zero
- }
-
- n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(slice))))
- return slice[n.Int64()]
- }
- func getRandomRobotName(existingNames []string) string {
- allNames := []string{
- "张三", "李四", "王五", "赵六",
- "钱七", "孙八", "周九", "吴十",
- }
- return getRandomName(allNames, existingNames)
- }
- func getRandomName(allNames []string, existingNames []string) string {
-
- remainingNames := make([]string, 0)
-
- existingMap := make(map[string]bool)
- for _, name := range existingNames {
- existingMap[name] = true
- }
-
- for _, name := range allNames {
- if !existingMap[name] {
- remainingNames = append(remainingNames, name)
- }
- }
-
- if len(remainingNames) == 0 {
- return ""
- }
-
- n, err := rand.Int(rand.Reader, big.NewInt(int64(len(remainingNames))))
- if err != nil {
-
- var fallbackSeed int64
- binary.Read(rand.Reader, binary.LittleEndian, &fallbackSeed)
- if fallbackSeed < 0 {
- fallbackSeed = -fallbackSeed
- }
- return remainingNames[fallbackSeed%int64(len(remainingNames))]
- }
-
- return remainingNames[n.Int64()]
- }
- func getRandomRobotHead() string {
- n, err := rand.Int(rand.Reader, big.NewInt(13))
- if err != nil {
- panic(err)
- }
- return fmt.Sprintf("3_%d", n)
- }
- func getRandomRobotCoin() int32 {
- n, err := rand.Int(rand.Reader, big.NewInt(1000))
- if err != nil {
- panic(err)
- }
- return int32(n.Int64()) + 1000
- }
- func GetNextRole(currentRole msg.RoleType) msg.RoleType {
-
- roleOrder := []msg.RoleType{
- msg.RoleType_GREEN,
- msg.RoleType_YELLOW,
- msg.RoleType_BLUE,
- msg.RoleType_RED,
- }
-
- for i, role := range roleOrder {
- if role == currentRole {
-
- if i == len(roleOrder)-1 {
- return roleOrder[0]
- }
- return roleOrder[i+1]
- }
- }
- return msg.RoleType_ROLE_TYPE_UNKNOWN
- }
|