package model import ( "context" "errors" "fmt" "github.com/go-redis/redis/v8" "math" "math/rand/v2" "samba/pkg/log" "samba/stub" "samba/util/rdbkey" "strconv" "strings" ) // 获取玩家所在房间 func SetUserPlayingRoom(userId int64, roomId int, isEnter bool) { if isEnter { if err := rdbBaseInfo.HSet(context.Background(), rdbkey.UserPlayingRoomKey(userId), roomId, 1).Err(); err != nil { log.Error(err.Error()) } _ = rdbBaseInfo.Expire(context.Background(), rdbkey.UserPlayingRoomKey(userId), redis30day).Err() } else { if err := rdbBaseInfo.HDel(context.Background(), rdbkey.UserPlayingRoomKey(userId), fmt.Sprintf("%v", roomId)).Err(); err != nil { log.Error(err.Error()) } _ = rdbBaseInfo.Expire(context.Background(), rdbkey.UserPlayingRoomKey(userId), redis30day).Err() } } func GetUserPlayingRoom(userId int64) map[int]int { if maps, err := rdbBaseInfo.HGetAll(context.Background(), rdbkey.UserPlayingRoomKey(userId)).Result(); err != nil && !errors.Is(err, redis.Nil) { log.Error(err.Error()) return map[int]int{} } else { rooms := map[int]int{} for k, v := range maps { roomId, _ := strconv.ParseInt(k, 10, 64) isNew, _ := strconv.ParseInt(v, 10, 64) rooms[int(roomId)] = int(isNew) } return rooms } } // AddRoomTypePlayerNum 增加房间内的在玩数量,返回增加后的结果 func AddRoomTypePlayerNum(rmInfo *stub.Room, uid int64) (int64, error) { var num int64 var err error if rmInfo.Mode == int(stub.RmClub) { if err = rdbBaseInfo.SAdd(context.Background(), rdbkey.ClubRoomPlayerNumKey(), uid).Err(); err != nil { log.Error(err.Error()) } } else { if err = rdbBaseInfo.SAdd(context.Background(), rdbkey.RoomTypePlayerNumKey(rmInfo.Id), uid).Err(); err != nil { log.Error(err.Error()) } if num, err = AddRoomPlayingNum(rmInfo.Id); err != nil { log.Error(err.Error()) } } return num, err } // DelRoomTypePlayerNum 减少房间内的在玩数量,返回改变后的结果 func DelRoomTypePlayerNum(rmInfo *stub.Room, uid int64) (int64, error) { var num int64 var err error if rmInfo.Mode == int(stub.RmClub) { if err = rdbBaseInfo.SRem(context.Background(), rdbkey.ClubRoomPlayerNumKey(), uid).Err(); err != nil { log.Error(err.Error()) } } else { if err = rdbBaseInfo.SRem(context.Background(), rdbkey.RoomTypePlayerNumKey(rmInfo.Id), uid).Err(); err != nil { log.Error(err.Error()) } if num, err = DelRoomPlayingNum(rmInfo.Id); err != nil { log.Error(err.Error()) } } return num, err } func AddRoomPlayingNum(roomType int) (int64, error) { return rdbBaseInfo.HIncrBy(context.TODO(), rdbkey.RoomPlayingNumKey(), fmt.Sprintf("%d-real", roomType), 1).Result() //if err != nil { // return err //} //err = rdbBaseInfo.HSet(context.TODO(), rdbkey.RoomPlayingNumKey(), fmt.Sprintf("%d-fake", roomType), CalculateFakeCount(num)).Err() //return err } func DelRoomPlayingNum(roomType int) (int64, error) { return rdbBaseInfo.HIncrBy(context.TODO(), rdbkey.RoomPlayingNumKey(), fmt.Sprintf("%d-real", roomType), -1).Result() //if err != nil { // return err //} //err = rdbBaseInfo.HSet(context.TODO(), rdbkey.RoomPlayingNumKey(), fmt.Sprintf("%d-fake", roomType), CalculateFakeCount(num)).Err() //return err } func GetAllRoomPlayingNum() (map[int]int, error) { res, err := rdbBaseInfo.HGetAll(context.TODO(), rdbkey.RoomPlayingNumKey()).Result() if err != nil { return nil, err } mp := make(map[int]int, len(res)) for k, v := range res { roomType, err := strconv.Atoi(strings.Split(k, "-")[0]) if err != nil { return nil, err } count, err := strconv.Atoi(v) if err != nil { return nil, err } mp[roomType] = count } return mp, nil } // CalculateFakeCount 计算虚假人数 // realCount: 实际人数 // 返回虚假人数 func CalculateFakeCount(realCount int64) int64 { if realCount < 0 { realCount = 0 } var coefficient float64 switch { case realCount <= 50: // 随机生成每天的系数范围 [5.0, 10.0] coefficient = 5.0 + rand.Float64()*(10.0-5.0) case realCount <= 200: coefficient = 3.0 case realCount <= 800: coefficient = 1.0 default: coefficient = 0 } // 计算虚假人数 fakeCount := float64(realCount) + float64(realCount)*coefficient // 返回向下取整的虚假人数 return int64(math.Floor(fakeCount)) }