feat: 实现跨服PVP匹配和战斗功能

This commit is contained in:
xinian
2026-04-05 05:04:04 +08:00
committed by cnb
parent 102d87da3e
commit 7d054bbe91
8 changed files with 1167 additions and 29 deletions

View File

@@ -2,6 +2,8 @@ package rpc
import (
"blazing/cool"
"blazing/logic/service/fight/pvp"
"blazing/logic/service/fight/pvpwire"
"context"
"fmt"
"time"
@@ -9,6 +11,7 @@ import (
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
// ListenFunc 监听函数
@@ -150,6 +153,8 @@ func ListenFight(ctx g.Ctx) {
serverID := cool.Config.ServerInfo.GetID()
startTopic := "sun:start:" + serverID
sendPackTopic := "sendpack:" + serverID
pvpServerTopic := pvpwire.ServerTopic(gconv.Uint32(serverID))
pvpCoordinatorTopic := pvpwire.CoordinatorTopicPrefix
// 外层循环:负责连接断开后的整体重连
for {
@@ -199,16 +204,26 @@ func ListenFight(ctx g.Ctx) {
}()
// 3. 订阅主题(对齐 ListenFunc 的错误处理,替换 panic 为优雅重连)
// 订阅 sun:start:服务器ID
_, err = conn.Do(ctx, "subscribe", startTopic)
if err != nil {
cool.Logger.Error(ctx, "订阅 Redis 主题失败", "topic", startTopic, "error", err)
heartbeatCancel() // 关闭心跳协程
_ = conn.Close(ctx)
time.Sleep(retryDelay)
subscribeTopics := []string{startTopic, pvpServerTopic}
if cool.Config.GameOnlineID == pvp.CoordinatorOnlineID {
subscribeTopics = append(subscribeTopics, pvpCoordinatorTopic)
}
subscribeFailed := false
for _, topic := range subscribeTopics {
_, err = conn.Do(ctx, "subscribe", topic)
if err != nil {
cool.Logger.Error(ctx, "订阅 Redis 主题失败", "topic", topic, "error", err)
heartbeatCancel()
_ = conn.Close(ctx)
time.Sleep(retryDelay)
subscribeFailed = true
break
}
cool.Logger.Info(ctx, "成功订阅 Redis 主题", "topic", topic)
}
if subscribeFailed {
continue
}
cool.Logger.Info(ctx, "成功订阅 Redis 主题", "topic", startTopic)
// // 订阅 sun:sendpack:服务器ID
// _, err = conn.Do(ctx, "subscribe", sendPackTopic)
@@ -255,6 +270,10 @@ func ListenFight(ctx g.Ctx) {
// universalClient, _ := g.Redis("cool").Client().(goredis.UniversalClient)
}
if dataMap.Channel == pvpServerTopic || dataMap.Channel == pvpCoordinatorTopic {
pvp.HandleRedisMessage(dataMap.Channel, dataMap.Payload)
}
// 【可选】处理 sun:sendpack:服务器ID 消息(如果需要)
if dataMap.Channel == sendPackTopic {
fmt.Println("收到战斗包", dataMap.Payload)