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
|
continue
|
||||||
}
|
}
|
||||||
cool.Logger.Info(ctx, "成功订阅 Redis 主题", "topic", subscribeTopic)
|
cool.Logger.Info(ctx, "成功订阅 Redis 主题", "topic", subscribeTopic)
|
||||||
_, err = conn.Do(ctx, "subscribe", "sun:join") //加入队列
|
_, err = conn.Do(ctx, "subscribe", "sun:join") //加入队列
|
||||||
_, err = conn.Do(ctx, "subscribe", "sun:cancel") //退出队列
|
|
||||||
|
|
||||||
// 4. 循环接收消息
|
// 4. 循环接收消息
|
||||||
connError := false
|
connError := false
|
||||||
|
|||||||
@@ -17,42 +17,10 @@ type RPCfight struct {
|
|||||||
zs *zset.ZSet[uint32, *model.PVP]
|
zs *zset.ZSet[uint32, *model.PVP]
|
||||||
}
|
}
|
||||||
|
|
||||||
// // ExtractBetweenBrackets 提取字符串中第一个 [] 中间的文本
|
func (r *RPCfight) join(pvp info.RPCFightinfo) {
|
||||||
// // 返回值:中间文本、是否成功、错误信息
|
|
||||||
// func ExtractBetweenBrackets(s string) (string, bool, error) {
|
|
||||||
// // 1. 找到第一个 [ 的索引
|
|
||||||
// leftIdx := strings.Index(s, "[")
|
|
||||||
// if leftIdx == -1 {
|
|
||||||
// return "", false, errors.New("未找到左中括号 [")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 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)
|
ret := service.NewPVPService(pvp.PlayerID).Get(pvp.PlayerID)
|
||||||
score := 1000
|
score := 800
|
||||||
if ret != nil {
|
if ret != nil {
|
||||||
score = int(ret.RankInfo.Score)
|
score = int(ret.RankInfo.Score)
|
||||||
}
|
}
|
||||||
@@ -73,8 +41,19 @@ func (r *RPCfight) ADD(s string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RPCfight) Cancel(s string) {
|
func (r *RPCfight) ADD(s string) {
|
||||||
// r.zs.Remove()
|
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,存储用户对战斗容器的映射,便于外部传入时候进行直接操作
|
///定义map,存储用户对战斗容器的映射,便于外部传入时候进行直接操作
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ type Fightinfo struct {
|
|||||||
FightType uint32
|
FightType uint32
|
||||||
}
|
}
|
||||||
type RPCFightinfo struct {
|
type RPCFightinfo struct {
|
||||||
|
Type int
|
||||||
PlayerID uint32
|
PlayerID uint32
|
||||||
// 战斗模式 1 = 1v1 2 = 6v6 3大乱斗 0 什么都不做
|
// 战斗模式 1 = 1v1 2 = 6v6 3大乱斗 0 什么都不做
|
||||||
Mode uint32
|
Mode uint32
|
||||||
@@ -60,6 +61,15 @@ type RPCFightinfo struct {
|
|||||||
Status uint32
|
Status uint32
|
||||||
// FightType uint32
|
// FightType uint32
|
||||||
}
|
}
|
||||||
|
type RPCFightStartinfo struct {
|
||||||
|
Servierid int // 非0表示rpc跨服
|
||||||
|
PlayerID uint32
|
||||||
|
// 战斗模式 1 = 1v1 2 = 6v6 3大乱斗 0 什么都不做
|
||||||
|
Mode uint32
|
||||||
|
//Type uint32 //战斗类型
|
||||||
|
|
||||||
|
Status uint32
|
||||||
|
}
|
||||||
|
|
||||||
// FightPetInfo 战斗精灵信息结构体,FightPetInfo类
|
// FightPetInfo 战斗精灵信息结构体,FightPetInfo类
|
||||||
type FightPetInfo struct {
|
type FightPetInfo struct {
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ type Player struct {
|
|||||||
MainConn gnet.Conn
|
MainConn gnet.Conn
|
||||||
baseplayer
|
baseplayer
|
||||||
IsLogin bool //是否登录
|
IsLogin bool //是否登录
|
||||||
|
IsJoin bool //是否加入RPC对局 用重连函数写临时缓存join,重连后统一join
|
||||||
Done
|
Done
|
||||||
|
|
||||||
MapNPC *time.Timer
|
MapNPC *time.Timer
|
||||||
|
|||||||
Reference in New Issue
Block a user