锦标赛机器人优化

This commit is contained in:
hahwu 2026-03-09 19:30:18 +08:00
parent fc830c47d9
commit 6547a4a9db
4 changed files with 183 additions and 31 deletions

View File

@ -219,6 +219,20 @@ func RedisZRevRangeWithScores(key string, start, stop int64) ([]redis.Z, error)
return val, nil
}
func RedisZGetByIndexWithScore(key string, index int64) (*redis.Z, error) {
if RdbRead == nil {
return nil, nil
}
val, err := RdbRead.ZRangeWithScores(ctx, key, index, index).Result()
if err != nil {
return nil, err
}
if len(val) == 0 {
return nil, nil
}
return &val[0], nil
}
func RedisZRankWithScores(key, member string) (int64, float64, error) {
if RdbRead == nil {
return 0, 0, nil
@ -234,6 +248,17 @@ func RedisZRankWithScores(key, member string) (int64, float64, error) {
return val, score, nil
}
func RedisZCard(key string) (int64, error) {
if RdbRead == nil {
return 0, nil
}
val, err := RdbRead.ZCard(ctx, key).Result()
if err != nil {
return 0, err
}
return val, nil
}
func RedisDel(key string) {
if RdbWrite == nil {
log.Debug("redis write client is nil")

View File

@ -3,11 +3,14 @@ package game
import (
"fmt"
"math"
"math/rand"
avatarCfg "server/conf/avatar"
champshipCfg "server/conf/champship"
faceCfg "server/conf/face"
randnameCfg "server/conf/randname"
"server/db"
"server/game/mod/friend"
"server/game/mod/msg"
GoUtil "server/game_util"
proto "server/msg"
@ -57,6 +60,11 @@ type ChampshipRobot struct {
Time int64
Score float64
PerScore float64
Playroom map[int]int
DressSet map[int]int
FurSet int
ActLog *friend.ActLogInfo
PetName string
}
type GroupInfo struct {
@ -278,6 +286,14 @@ func (c *ChampshipMgr) GetPreRankMsg(Uid int) *proto.ResChampshipPreRank {
if Robot == nil {
continue
}
last := &proto.ActLog{}
if Robot.ActLog != nil {
last = &proto.ActLog{
Type: int32(Robot.ActLog.Type),
Time: Robot.ActLog.Time,
Param: Robot.ActLog.Param,
}
}
RL[int32(k+1)] = &proto.ResPlayerRank{
Uid: int64(v.Uid),
Score: float32(v.Score),
@ -286,12 +302,25 @@ func (c *ChampshipMgr) GetPreRankMsg(Uid int) *proto.ResChampshipPreRank {
Face: int32(Robot.Face),
Level: int32(Robot.Level),
Type: int32(v.Type),
PlayroomSet: GoUtil.MapIntToInt32(Robot.Playroom),
DressSet: GoUtil.MapIntToInt32(Robot.DressSet),
FurSet: int32(Robot.FurSet),
PetName: Robot.PetName,
Last: last,
}
} else {
SimplePlayer := G_GameLogicPtr.GetResSimplePlayerByUid(v.Uid)
SimplePlayer := G_GameLogicPtr.GetSimplePlayerByUid(v.Uid)
if SimplePlayer == nil {
continue
}
last := &proto.ActLog{}
if SimplePlayer.ActLog != nil {
last = &proto.ActLog{
Type: int32(SimplePlayer.ActLog.Type),
Time: SimplePlayer.ActLog.Time,
Param: SimplePlayer.ActLog.Param,
}
}
RL[int32(k+1)] = &proto.ResPlayerRank{
Uid: int64(v.Uid),
Score: float32(v.Score),
@ -300,6 +329,11 @@ func (c *ChampshipMgr) GetPreRankMsg(Uid int) *proto.ResChampshipPreRank {
Face: int32(SimplePlayer.Face),
Level: int32(SimplePlayer.Level),
Type: int32(v.Type),
PlayroomSet: GoUtil.MapIntToInt32(SimplePlayer.Playroom),
DressSet: GoUtil.MapIntToInt32(SimplePlayer.DressSet),
FurSet: int32(SimplePlayer.PetFur),
PetName: SimplePlayer.PetName,
Last: last,
}
}
}
@ -337,6 +371,14 @@ func (c *ChampshipMgr) GetRankMsg(Uid int) *proto.ResChampshipRank {
if Robot == nil {
continue
}
last := &proto.ActLog{}
if Robot.ActLog != nil {
last = &proto.ActLog{
Type: int32(Robot.ActLog.Type),
Time: Robot.ActLog.Time,
Param: Robot.ActLog.Param,
}
}
RL[int32(k+1)] = &proto.ResPlayerRank{
Uid: int64(v.Uid),
Score: float32(v.Score),
@ -345,12 +387,25 @@ func (c *ChampshipMgr) GetRankMsg(Uid int) *proto.ResChampshipRank {
Face: int32(Robot.Face),
Level: int32(Robot.Level),
Type: int32(v.Type),
PlayroomSet: GoUtil.MapIntToInt32(Robot.Playroom),
DressSet: GoUtil.MapIntToInt32(Robot.DressSet),
FurSet: int32(Robot.FurSet),
PetName: Robot.PetName,
Last: last,
}
} else {
SimplePlayer := G_GameLogicPtr.GetResSimplePlayerByUid(v.Uid)
SimplePlayer := G_GameLogicPtr.GetSimplePlayerByUid(v.Uid)
if SimplePlayer == nil {
continue
}
last := &proto.ActLog{}
if SimplePlayer.ActLog != nil {
last = &proto.ActLog{
Type: int32(SimplePlayer.ActLog.Type),
Time: SimplePlayer.ActLog.Time,
Param: SimplePlayer.ActLog.Param,
}
}
RL[int32(k+1)] = &proto.ResPlayerRank{
Uid: int64(v.Uid),
Score: float32(v.Score),
@ -359,6 +414,11 @@ func (c *ChampshipMgr) GetRankMsg(Uid int) *proto.ResChampshipRank {
Face: int32(SimplePlayer.Face),
Level: int32(SimplePlayer.Level),
Type: int32(v.Type),
PlayroomSet: GoUtil.MapIntToInt32(SimplePlayer.Playroom),
DressSet: GoUtil.MapIntToInt32(SimplePlayer.DressSet),
FurSet: int32(SimplePlayer.PetFur),
PetName: SimplePlayer.PetName,
Last: last,
}
}
}
@ -436,6 +496,9 @@ func (c *ChampshipMgr) group(iszero bool) (interface{}, error) {
for j := StartId; j <= ChampshipData.AutoId; j++ { // 填充机器人
RobotNum := 30 - len(ChampshipData.Rank[j])
RobotList := CreateRobotList(i, RobotNum, j)
for i := 0; i < RobotNum; i++ {
FormatRobotInfo(RobotList[i], i+1)
}
for _, v := range RobotList {
ChampshipData.Robot[ChampshipData.RobotId] = v
ChampshipData.Rank[j] = append(ChampshipData.Rank[j], &ChampshipRank{
@ -839,7 +902,13 @@ func CreateRobot(M float64, GroupId int) *ChampshipRobot {
PerScore = math.Round((M/10*0.34)/(float64(GoUtil.NextZeroTimestampDuration())/1800)*100) / 100
Score = 2
}
num, err := db.RedisZCard(RANK_USER)
if err != nil {
num = 0
}
id := rand.Intn(int(num)) + 1
rinfo, err := db.RedisZGetByIndexWithScore(RANK_USER, int64(id))
playerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(GoUtil.Int(rinfo.Member))
return &ChampshipRobot{
Max: M / 10,
Type: Type,
@ -851,9 +920,50 @@ func CreateRobot(M float64, GroupId int) *ChampshipRobot {
Time: GoUtil.Now(),
Score: Score,
PerScore: PerScore,
Playroom: playerSimpleData.Playroom,
DressSet: playerSimpleData.DressSet,
FurSet: playerSimpleData.PetFur,
}
}
func FormatRobotInfo(Robot *ChampshipRobot, index int) {
num, err := db.RedisZCard(RANK_USER)
if err != nil {
num = 0
}
x := int(num) / 30
if index > int(x) {
index = int(x)
}
start := int64((index - 1) * x)
end := int64(index*x - 1)
rinfo, err := db.RedisZRevRangeWithScores(RANK_USER, start, end)
if err != nil {
log.Error("FormatRobotInfo RedisZRevRangeWithScores error: %v, start %d, end %d", err, start, end)
return
}
if len(rinfo) == 0 {
log.Error("FormatRobotInfo RedisZRevRangeWithScores no data, start %d, end %d", start, end)
return
}
id := rand.Intn(len(rinfo))
if id >= len(rinfo) {
id = len(rinfo) - 1
}
playerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(GoUtil.Int(rinfo[id].Member))
if playerSimpleData == nil {
return
}
Robot.Level = playerSimpleData.Level
Robot.Avatar = playerSimpleData.Avatar
Robot.Face = playerSimpleData.Face
Robot.Playroom = playerSimpleData.Playroom
Robot.DressSet = playerSimpleData.DressSet
Robot.FurSet = playerSimpleData.PetFur
Robot.ActLog = playerSimpleData.ActLog
Robot.PetName = playerSimpleData.PetName
}
func (c *ChampshipMgr) SetRankCache(Uid int) {
PreRank := c.unsafe_getLastMyRank(Uid)
Rank := c.unsafe_getMyRank(Uid)

View File

@ -1,6 +1,7 @@
package test
import (
"fmt"
champshipCfg "server/conf/champship"
"server/game"
"testing"
@ -35,3 +36,14 @@ func TestReqChampshipReward(t *testing.T) {
ChampshipMod.Score = 1000
game.ReqChampshipReward(p1, nil)
}
func TestChampionshipCreateRobot(t *testing.T) {
RobotNum := 30
i := 5
j := 1
RobotList := game.CreateRobotList(i, RobotNum, j)
for i := 0; i < RobotNum; i++ {
game.FormatRobotInfo(RobotList[i], i+1)
}
fmt.Print("success")
}

View File

@ -264,3 +264,8 @@ func TestRandFace(t *testing.T) {
avatar := avatarCfg.GetRandInitId()
fmt.Printf("Random Face ID: %d, Random Avatar ID: %d\n", face, avatar)
}
func TestChampionShipCreateRobot(t *testing.T) {
robot := game.CreateRobot(1.0, 1)
fmt.Printf("Created Robot: %v\n", robot)
}