Files
bl/common/rpc/user.go

77 lines
1.9 KiB
Go
Raw Permalink Normal View History

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"
)
type RPCfight struct {
fightmap *csmap.CsMap[int, common.FightI]
zs *zset.ZSet[uint32, *model.PVP]
}
2026-03-12 14:35:27 +08:00
func (r *RPCfight) join(pvp info.RPCFightinfo) {
ret := service.NewPVPService(pvp.PlayerID).Get(pvp.PlayerID)
ret.RankInfo.LastMatchTime = gtime.Now()
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.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,
})
}
}
}
2026-03-12 14:35:27 +08:00
func (r *RPCfight) ADD(s string) {
println("收到sun:join", s)
var pvp []info.RPCFightinfo
2026-03-12 14:35:27 +08:00
json.Unmarshal([]byte(s), &pvp)
if pvp[0].Type == 1 {
r.join(pvp[0])
2026-03-12 14:35:27 +08:00
} else { //==0 退出
r.cancel(pvp[0])
2026-03-12 14:35:27 +08:00
}
}
func (r *RPCfight) cancel(pvp info.RPCFightinfo) {
r.zs.Remove(pvp.PlayerID)
}
///定义map,存储用户对战斗容器的映射,便于外部传入时候进行直接操作
var fightmap = RPCfight{
fightmap: csmap.New[int, common.FightI](),
zs: zset.New[uint32, *model.PVP](func(a, b *model.PVP) bool {
return a.Less(b)
}),
}