refactor: 重构 PVP 匹配逻辑使用模型结构体
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

This commit is contained in:
xinian
2026-03-05 11:21:38 +08:00
committed by cnb
parent 2e7215946b
commit b1ca686e06
5 changed files with 61 additions and 39 deletions

View File

@@ -82,7 +82,8 @@ func ListenFunc(ctx g.Ctx) {
continue
}
cool.Logger.Info(ctx, "成功订阅 Redis 主题", "topic", subscribeTopic)
_, err = conn.Do(ctx, "subscribe", "sun:join:2458") //加入队列
_, err = conn.Do(ctx, "subscribe", "sun:join") //加入队列
_, err = conn.Do(ctx, "subscribe", "sun:cancel") //退出队列
// 4. 循环接收消息
connError := false
@@ -118,7 +119,7 @@ func ListenFunc(ctx g.Ctx) {
cool.Logger.Error(ctx, "执行函数失败", "payload", dataMap.Payload, "error", err)
}
}
if dataMap.Channel == "sun:join:2458" {
if dataMap.Channel == "sun:join" {
fightmap.ADD(dataMap.Payload)
//universalClient, _ := g.Redis("cool").Client().(goredis.UniversalClient)

View File

@@ -2,7 +2,10 @@ package rpc
import (
"blazing/logic/service/common"
"blazing/logic/service/fight/info"
"blazing/modules/player/model"
"blazing/modules/player/service"
"encoding/json"
"errors"
"strings"
"time"
@@ -14,7 +17,7 @@ import (
type RPCfight struct {
fightmap *csmap.CsMap[int, common.FightI]
zs *zset.ZSet[uint32, User]
zs *zset.ZSet[uint32, *model.PVP]
}
// ExtractBetweenBrackets 提取字符串中第一个 [] 中间的文本
@@ -47,44 +50,37 @@ func ExtractBetweenBrackets(s string) (string, bool, error) {
}
func (r *RPCfight) ADD(s string) {
println("收到sun:join:2458", s)
println("收到sun:join", s)
var pvp info.RPCFightinfo
json.Unmarshal([]byte(s), &pvp)
t, _, _ := ExtractBetweenBrackets(s)
ret := service.NewPVPService(gconv.Uint32(t)).Get(gconv.Uint32(t))
score := 1000
if ret == nil {
if ret != nil {
score = int(ret.RankInfo.Score)
}
r.zs.Add(gconv.Uint32(t), User{ID: gconv.Uint32(t), JoinTime: time.Now().Unix(), Score: score})
r.zs.Add(gconv.Uint32(t),
ret)
if r.zs.Length() > 2 {
r.zs.FindPrev(func(i User) bool { return i.Score > score })
//找到上一个,如果区间分数少于一定,
//直接进行匹配
u, s := r.zs.FindPrev(func(i *model.PVP) bool { return i.RankInfo.Score > score })
diff := s - score
// 等待越久,允许区间越大
wait := time.Now().Sub(u.RankInfo.LastMatchTime.Time).Seconds()
maxAllow := 100 + int(wait)*10
if diff < maxAllow {
//找到上一个,如果区间分数少于一定,
//直接进行匹配
}
}
}
type User struct {
JoinTime int64
ID uint32
Score int
}
func (u User) Key() uint32 {
return uint32(u.ID)
}
// 如果分数不对的话,就按时间排序
func (u User) Less(than User) bool {
if u.Score == than.Score {
return u.JoinTime < than.JoinTime
}
return u.Score < than.Score
}
///定义map,存储用户对战斗容器的映射,便于外部传入时候进行直接操作
var fightmap = RPCfight{
fightmap: csmap.New[int, common.FightI](),
zs: zset.New[uint32, User](func(a, b User) bool {
zs: zset.New[uint32, *model.PVP](func(a, b *model.PVP) bool {
return a.Less(b)
}),
}

View File

@@ -5,6 +5,7 @@ import (
"blazing/cool"
"blazing/logic/service/common"
"blazing/logic/service/fight"
"blazing/logic/service/fight/info"
"blazing/logic/service/player"
"context"
)
@@ -17,7 +18,10 @@ type PetTOPLEVELnboundInfo struct {
}
func (h Controller) JoINtop(data *PetTOPLEVELnboundInfo, c *player.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
cool.RedisDo(context.TODO(), "sun:join:2458", data.Head.UserID)
cool.RedisDo(context.TODO(), "sun:join", info.RPCFightinfo{
PlayerID: c.Info.UserID,
Mode: data.Mode,
})
// // 类型断言为 UniversalClient
// universalClient, _ := client.(goredis.UniversalClient)

View File

@@ -51,6 +51,15 @@ type Fightinfo struct {
Status uint32
FightType uint32
}
type RPCFightinfo struct {
PlayerID uint32
// 战斗模式 1 = 1v1 2 = 6v6 3大乱斗 0 什么都不做
Mode uint32
//Type uint32 //战斗类型
Status uint32
// FightType uint32
}
// FightPetInfo 战斗精灵信息结构体FightPetInfo类
type FightPetInfo struct {

View File

@@ -4,6 +4,7 @@ import (
"blazing/common/data"
"blazing/cool"
"github.com/gogf/gf/v2/os/gtime"
"github.com/tnnmigga/enum"
)
@@ -20,6 +21,17 @@ type PVP struct {
RankInfo PVPRankInfo `gorm:"type:jsonb;not null;comment:'本赛季排名信息'" json:"rank_info"`
}
func (u *PVP) Key() uint32 {
return uint32(u.ID)
}
// 如果分数不对的话,就按时间排序
func (u *PVP) Less(than *PVP) bool {
if u.RankInfo.Score == than.RankInfo.Score {
return u.RankInfo.LastMatchTime.Unix() < than.RankInfo.LastMatchTime.Unix()
}
return u.RankInfo.Score < than.RankInfo.Score
}
func NewPVP() *PVP {
return &PVP{
Model: cool.NewModel(),
@@ -29,16 +41,16 @@ func NewPVP() *PVP {
// PVPSeasonData PVP赛季核心统计数据
// 聚合维度:总场次、胜负、积分、胜率等
type PVPRankInfo struct {
Rank uint32 `json:"rank"` // 本赛季全服排名0=未上榜)
Score int32 `json:"score"` // 当前积分
TotalMatch uint32 `json:"total_match"` // 本赛季总场次
WinMatch uint32 `json:"win_match"` // 本赛季胜利场次
LoseMatch uint32 `json:"lose_match"` // 本赛季失败场次
DrawMatch uint32 `json:"draw_match"` // 本赛季平局场次
TotalScore int32 `json:"total_score"` // 本赛季总积分(胜加负减)
HighestScore int32 `json:"highest_score"` // 本赛季最高积分
ContinuousWin uint32 `json:"continuous_win"` // 本赛季最高连胜次数
LastMatchTime uint64 `json:"last_match_time"` // 最后一场PVP时间时间戳
Rank uint32 `json:"rank"` // 本赛季全服排名0=未上榜)
Score int `json:"score"` // 当前积分
TotalMatch uint32 `json:"total_match"` // 本赛季总场次
WinMatch uint32 `json:"win_match"` // 本赛季胜利场次
LoseMatch uint32 `json:"lose_match"` // 本赛季失败场次
DrawMatch uint32 `json:"draw_match"` // 本赛季平局场次
TotalScore int32 `json:"total_score"` // 本赛季总积分(胜加负减)
HighestScore int32 `json:"highest_score"` // 本赛季最高积分
ContinuousWin uint32 `json:"continuous_win"` // 本赛季最高连胜次数
LastMatchTime *gtime.Time `json:"last_match_time"` // 最后一场PVP时间时间戳
}
// NoteReadyToFightInfo 战斗准备就绪消息结构体NoteReadyToFightInfo