2026-03-04 23:38:21 +08:00
|
|
|
|
package rpc
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"blazing/logic/service/common"
|
2026-03-05 11:21:38 +08:00
|
|
|
|
"blazing/logic/service/fight/info"
|
|
|
|
|
|
"blazing/modules/player/model"
|
2026-03-04 23:38:21 +08:00
|
|
|
|
"blazing/modules/player/service"
|
2026-03-05 11:21:38 +08:00
|
|
|
|
"encoding/json"
|
2026-03-04 23:38:21 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/liwnn/zset"
|
|
|
|
|
|
csmap "github.com/mhmtszr/concurrent-swiss-map"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type RPCfight struct {
|
|
|
|
|
|
fightmap *csmap.CsMap[int, common.FightI]
|
2026-03-05 11:21:38 +08:00
|
|
|
|
zs *zset.ZSet[uint32, *model.PVP]
|
2026-03-04 23:38:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-05 13:21:58 +08:00
|
|
|
|
// // ExtractBetweenBrackets 提取字符串中第一个 [] 中间的文本
|
|
|
|
|
|
// // 返回值:中间文本、是否成功、错误信息
|
|
|
|
|
|
// func ExtractBetweenBrackets(s string) (string, bool, error) {
|
|
|
|
|
|
// // 1. 找到第一个 [ 的索引
|
|
|
|
|
|
// leftIdx := strings.Index(s, "[")
|
|
|
|
|
|
// if leftIdx == -1 {
|
|
|
|
|
|
// return "", false, errors.New("未找到左中括号 [")
|
|
|
|
|
|
// }
|
2026-03-04 23:38:21 +08:00
|
|
|
|
|
2026-03-05 13:21:58 +08:00
|
|
|
|
// // 2. 找到第一个 [ 之后的第一个 ] 的索引
|
|
|
|
|
|
// rightIdx := strings.Index(s[leftIdx+1:], "]")
|
|
|
|
|
|
// if rightIdx == -1 {
|
|
|
|
|
|
// return "", false, errors.New("找到左中括号 [ 但未找到对应的右中括号 ]")
|
|
|
|
|
|
// }
|
2026-03-04 23:38:21 +08:00
|
|
|
|
|
2026-03-05 13:21:58 +08:00
|
|
|
|
// // 3. 计算实际的右中括号索引(加上 leftIdx+1)
|
|
|
|
|
|
// rightIdx += leftIdx + 1
|
2026-03-04 23:38:21 +08:00
|
|
|
|
|
2026-03-05 13:21:58 +08:00
|
|
|
|
// // 4. 提取中间文本(去除前后空格,可选)
|
|
|
|
|
|
// result := strings.TrimSpace(s[leftIdx+1 : rightIdx])
|
2026-03-04 23:38:21 +08:00
|
|
|
|
|
2026-03-05 13:21:58 +08:00
|
|
|
|
// // 5. 检查是否为空
|
|
|
|
|
|
// if result == "" {
|
|
|
|
|
|
// return "", true, errors.New("中括号中间无文本")
|
|
|
|
|
|
// }
|
2026-03-04 23:38:21 +08:00
|
|
|
|
|
2026-03-05 13:21:58 +08:00
|
|
|
|
// return result, true, nil
|
|
|
|
|
|
// }
|
2026-03-04 23:38:21 +08:00
|
|
|
|
|
|
|
|
|
|
func (r *RPCfight) ADD(s string) {
|
2026-03-05 11:21:38 +08:00
|
|
|
|
println("收到sun:join", s)
|
|
|
|
|
|
var pvp info.RPCFightinfo
|
|
|
|
|
|
json.Unmarshal([]byte(s), &pvp)
|
2026-03-05 13:21:58 +08:00
|
|
|
|
//t, _, _ := ExtractBetweenBrackets(s)
|
|
|
|
|
|
ret := service.NewPVPService(pvp.PlayerID).Get(pvp.PlayerID)
|
2026-03-04 23:38:21 +08:00
|
|
|
|
score := 1000
|
2026-03-05 11:21:38 +08:00
|
|
|
|
if ret != nil {
|
2026-03-04 23:38:21 +08:00
|
|
|
|
score = int(ret.RankInfo.Score)
|
|
|
|
|
|
}
|
2026-03-05 13:21:58 +08:00
|
|
|
|
r.zs.Add(pvp.PlayerID,
|
2026-03-05 11:21:38 +08:00
|
|
|
|
ret)
|
2026-03-04 23:38:21 +08:00
|
|
|
|
if r.zs.Length() > 2 {
|
2026-03-05 11:21:38 +08:00
|
|
|
|
u, s := r.zs.FindPrev(func(i *model.PVP) bool { return i.RankInfo.Score > score })
|
2026-03-04 23:38:21 +08:00
|
|
|
|
|
2026-03-05 11:21:38 +08:00
|
|
|
|
diff := s - score
|
|
|
|
|
|
// 等待越久,允许区间越大
|
|
|
|
|
|
wait := time.Now().Sub(u.RankInfo.LastMatchTime.Time).Seconds()
|
|
|
|
|
|
maxAllow := 100 + int(wait)*10
|
|
|
|
|
|
if diff < maxAllow {
|
|
|
|
|
|
//找到上一个,如果区间分数少于一定,
|
|
|
|
|
|
//直接进行匹配
|
|
|
|
|
|
}
|
2026-03-04 23:38:21 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-05 13:21:58 +08:00
|
|
|
|
func (r *RPCfight) Cancel(s string) {
|
|
|
|
|
|
r.zs.Remove()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 23:38:21 +08:00
|
|
|
|
///定义map,存储用户对战斗容器的映射,便于外部传入时候进行直接操作
|
|
|
|
|
|
|
|
|
|
|
|
var fightmap = RPCfight{
|
|
|
|
|
|
fightmap: csmap.New[int, common.FightI](),
|
2026-03-05 11:21:38 +08:00
|
|
|
|
zs: zset.New[uint32, *model.PVP](func(a, b *model.PVP) bool {
|
2026-03-04 23:38:21 +08:00
|
|
|
|
return a.Less(b)
|
|
|
|
|
|
}),
|
|
|
|
|
|
}
|