refactor: 优化RPC对战加入与
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
This commit is contained in:
@@ -82,8 +82,7 @@ func ListenFunc(ctx g.Ctx) {
|
||||
continue
|
||||
}
|
||||
cool.Logger.Info(ctx, "成功订阅 Redis 主题", "topic", subscribeTopic)
|
||||
_, err = conn.Do(ctx, "subscribe", "sun:join") //加入队列
|
||||
_, err = conn.Do(ctx, "subscribe", "sun:cancel") //退出队列
|
||||
_, err = conn.Do(ctx, "subscribe", "sun:join") //加入队列
|
||||
|
||||
// 4. 循环接收消息
|
||||
connError := false
|
||||
|
||||
@@ -17,42 +17,10 @@ type RPCfight struct {
|
||||
zs *zset.ZSet[uint32, *model.PVP]
|
||||
}
|
||||
|
||||
// // ExtractBetweenBrackets 提取字符串中第一个 [] 中间的文本
|
||||
// // 返回值:中间文本、是否成功、错误信息
|
||||
// func ExtractBetweenBrackets(s string) (string, bool, error) {
|
||||
// // 1. 找到第一个 [ 的索引
|
||||
// leftIdx := strings.Index(s, "[")
|
||||
// if leftIdx == -1 {
|
||||
// return "", false, errors.New("未找到左中括号 [")
|
||||
// }
|
||||
func (r *RPCfight) join(pvp info.RPCFightinfo) {
|
||||
|
||||
// // 2. 找到第一个 [ 之后的第一个 ] 的索引
|
||||
// rightIdx := strings.Index(s[leftIdx+1:], "]")
|
||||
// if rightIdx == -1 {
|
||||
// return "", false, errors.New("找到左中括号 [ 但未找到对应的右中括号 ]")
|
||||
// }
|
||||
|
||||
// // 3. 计算实际的右中括号索引(加上 leftIdx+1)
|
||||
// rightIdx += leftIdx + 1
|
||||
|
||||
// // 4. 提取中间文本(去除前后空格,可选)
|
||||
// result := strings.TrimSpace(s[leftIdx+1 : rightIdx])
|
||||
|
||||
// // 5. 检查是否为空
|
||||
// if result == "" {
|
||||
// return "", true, errors.New("中括号中间无文本")
|
||||
// }
|
||||
|
||||
// return result, true, nil
|
||||
// }
|
||||
|
||||
func (r *RPCfight) ADD(s string) {
|
||||
println("收到sun:join", s)
|
||||
var pvp info.RPCFightinfo
|
||||
json.Unmarshal([]byte(s), &pvp)
|
||||
//t, _, _ := ExtractBetweenBrackets(s)
|
||||
ret := service.NewPVPService(pvp.PlayerID).Get(pvp.PlayerID)
|
||||
score := 1000
|
||||
score := 800
|
||||
if ret != nil {
|
||||
score = int(ret.RankInfo.Score)
|
||||
}
|
||||
@@ -73,8 +41,19 @@ func (r *RPCfight) ADD(s string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RPCfight) Cancel(s string) {
|
||||
// r.zs.Remove()
|
||||
func (r *RPCfight) ADD(s string) {
|
||||
println("收到sun:join", s)
|
||||
var pvp info.RPCFightinfo
|
||||
json.Unmarshal([]byte(s), &pvp)
|
||||
if pvp.Type == 1 {
|
||||
r.join(pvp)
|
||||
} else { //==0 退出
|
||||
r.cancel(pvp)
|
||||
}
|
||||
|
||||
}
|
||||
func (r *RPCfight) cancel(pvp info.RPCFightinfo) {
|
||||
r.zs.Remove(pvp.PlayerID)
|
||||
}
|
||||
|
||||
///定义map,存储用户对战斗容器的映射,便于外部传入时候进行直接操作
|
||||
|
||||
Reference in New Issue
Block a user