```
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

refactor(common): 统一Redis连接方式并优化代码结构

- 将 g.Redis("cool").Conn(ctx) 统一改为 Redis.Conn(ctx) 的调用方式
- 在coolconfig中添加ServerList.GetID()方法用于生成服务器唯一标识
- 引入gconv包用于类型转换操作

feat(rpc): 完善ListenFight函数实现集群消息监听

- 新增ListenFight函数,完全对齐ListenFunc
This commit is contained in:
昔念
2026-03-20 04:58:23 +08:00
parent 5657f1e673
commit 90b62b44e4
13 changed files with 119 additions and 60 deletions

View File

@@ -1,13 +1,18 @@
package rpc
import (
"blazing/common/data/share"
"blazing/cool"
"blazing/logic/service/common"
"blazing/logic/service/fight/info"
"blazing/modules/player/model"
"blazing/modules/player/service"
"context"
"encoding/json"
"time"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/liwnn/zset"
csmap "github.com/mhmtszr/concurrent-swiss-map"
)
@@ -20,22 +25,26 @@ type RPCfight struct {
func (r *RPCfight) join(pvp info.RPCFightinfo) {
ret := service.NewPVPService(pvp.PlayerID).Get(pvp.PlayerID)
score := 800
if ret != nil {
score = int(ret.RankInfo[len(ret.RankInfo)-1].Score)
}
r.zs.Add(pvp.PlayerID,
ret)
if r.zs.Length() > 2 {
u, s := r.zs.FindPrev(func(i *model.PVP) bool { return i.RankInfo[len(ret.RankInfo)-1].Score > score })
ret.RankInfo.LastMatchTime = gtime.Now()
diff := s - score
r.zs.Add(pvp.PlayerID, ret)
if r.zs.Length() > 1 {
u, _ := r.zs.FindNext(func(i *model.PVP) bool { return i.RankInfo.Score >= ret.RankInfo.Score })
diff := u.RankInfo.Score - ret.RankInfo.Score
// 等待越久,允许区间越大
wait := time.Now().Sub(u.RankInfo[len(ret.RankInfo)-1].LastMatchTime.Time).Seconds()
wait := time.Now().Sub(u.RankInfo.LastMatchTime.Time).Seconds()
maxAllow := 100 + int(wait)*10
if diff < maxAllow {
//找到上一个,如果区间分数少于一定,
//直接进行匹配
useid1, _ := share.ShareManager.GetUserOnline(u.PlayerID)
cool.RedisDo(context.TODO(), "sun:start:"+gconv.String(useid1), info.RPCFightStartinfo{
Serverid: int(useid1),
PlayerID: u.PlayerID,
Mode: pvp.Mode,
Status: pvp.Status,
})
}
}
@@ -43,12 +52,13 @@ func (r *RPCfight) join(pvp info.RPCFightinfo) {
func (r *RPCfight) ADD(s string) {
println("收到sun:join", s)
var pvp info.RPCFightinfo
var pvp []info.RPCFightinfo
json.Unmarshal([]byte(s), &pvp)
if pvp.Type == 1 {
r.join(pvp)
if pvp[0].Type == 1 {
r.join(pvp[0])
} else { //==0 退出
r.cancel(pvp)
r.cancel(pvp[0])
}
}