fix(rpc): 修复日志输出格式并修正logic服务器注册逻辑

修复了rpc模块中日志输出时参数拼接错误的问题,同时修正了RegisterLogic函数中端口映射的逻辑错误。

feat(socket): 替换错误处理方式为panic

在ServerEvent.go中将网络启动失败的返回错误改为panic处理,提高错误可见性。

feat(fight): 增加战斗加载进度控制

在LoadPercent函数中增加对FightC非
This commit is contained in:
2025-11-01 00:40:19 +08:00
parent 06c30c6113
commit eac1d41043
16 changed files with 92 additions and 76 deletions

View File

@@ -29,8 +29,8 @@ type ServerHandler struct{}
// 实现踢人
func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
cool.Loger.Info(context.TODO(),"服务器收到踢人" )
cool.Loger.Info(context.TODO(), "服务器收到踢人")
useid1, err := share.ShareManager.GetUserOnline(userid)
if err != nil {
@@ -48,13 +48,14 @@ func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
// 注册logic服务器
func (h *ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) error {
cool.Loger.Debug(context.Background(), "注册logic服务器", id, port)
//TODO 待修复滚动更新可能导致的玩家可以同时在旧服务器和新服务器同时在线的bug
revClient, ok := jsonrpc.ExtractReverseClient[ClientHandler](ctx)
if !ok {
return fmt.Errorf("no reverse client")
}
blservice.NewLoginServiceService().GetServerID(id)
aa, ok := clientmap[port]
t, _ := blservice.NewLoginServiceService().GetServerID(id)
aa, ok := clientmap[t]
if ok { //如果已经存在且这个端口已经被存过
aa.QuitSelf(0)
}

View File

@@ -24,7 +24,7 @@ func (s *Server) Boot() error {
// gnet.WithReuseAddr(true),
gnet.WithSocketRecvBuffer(s.bufferSize))
if err != nil {
return err
panic(err)
}
// err := gnet.Run(s, s.network+"://"+s.addr, gnet.WithMulticore(s.multicore))

View File

@@ -184,7 +184,10 @@ func (h Controller) Capture(data *fight.CatchMonsterInboundInfo, c *player.Playe
// 加载进度
func (h Controller) LoadPercent(data *fight.LoadPercentInboundInfo, c *player.Player) (result *info.LoadPercentOutboundInfo, err errorcode.ErrorCode) {
if c.FightC != nil {
c.FightC.LoadPercent(c, int32(data.Percent))
}
c.FightC.LoadPercent(c, int32(data.Percent))
return nil, -1
}

View File

@@ -126,9 +126,17 @@ func (h Controller) TalkCate(data *item.TalkCateInboundInfo, c *player.Player) (
// 2. 生成 1-10 的随机数rand.Intn(10) → 0-9+1 后范围变为 1-10
randomNum := rand.Intn(10) + 1
c.Service.Talk(func(t map[uint32]uint32) bool {
if t == nil {
t = make(map[uint32]uint32)
}
_, ok := t[data.ID]
if ok {
t[data.ID] += 1
if !ok {
t[data.ID] = 0
}
t[data.ID] += 1
if t[data.ID] < uint32(te.CollectCnt) {
result.OutList = append(result.OutList, item.CateInfo{ID: uint32(talkcacche[te.Name]), Count: uint32(randomNum)})
c.ItemAdd(model.SingleItemInfo{ItemId: uint32(talkcacche[te.Name]), ItemCnt: uint32(randomNum)})
}

View File

@@ -166,7 +166,7 @@ func (h Controller) SetPetExp(data *pet.PetSetExpInboundInfo, c *player.Player)
})
if ok {
c.AddPetExp(onpet, data.Exp, false)
c.AddPetExp(onpet, data.Exp)
}
return &pet.PetSetExpOutboundInfo{

Binary file not shown.

View File

@@ -44,7 +44,7 @@ func (e *EffectStat) OnSkill(ctx input.Ctx) bool {
if !e.Hit() {
return true
}
t, _, _ := e.Input.Player.Roll(e.EffectNode.SideEffectArgs[1], 100)
t, _, _ := e.Input.Player.Roll(e.SideEffectArgs[1], 100)
if !t { //没触发
return true
}
@@ -53,10 +53,10 @@ func (e *EffectStat) OnSkill(ctx input.Ctx) bool {
ptype = info.AbilityOpType.SUB
}
if !e.Etype { //自身
e.Input.SetProp(e.Input, int8(e.EffectNode.SideEffectArgs[0]), int8(e.EffectNode.SideEffectArgs[2]), ptype)
e.Input.SetProp(e.Input, int8(e.SideEffectArgs[0]), int8(e.SideEffectArgs[2]), ptype)
} else { //对方
ctx.SetProp(e.Input, int8(e.EffectNode.SideEffectArgs[0]), int8(e.EffectNode.SideEffectArgs[2]), ptype)
ctx.SetProp(e.Input, int8(e.SideEffectArgs[0]), int8(e.SideEffectArgs[2]), ptype)
}
return true
}

View File

@@ -166,8 +166,6 @@ func (f *FightC) initplayer(c common.PlayerI, opp bool) bool {
func NewFight(mode, status info.EnumBattleMode, p1 common.PlayerI, p2 common.PlayerI) *FightC {
f := &FightC{}
f.ownerID = p1.GetInfo().UserID
f.Info.Status = status //房主
f.Info.Mode = mode
f.StartTime = time.Now()
seed := f.StartTime.UnixNano() ^ int64(p1.GetInfo().UserID) ^ int64(p2.GetInfo().UserID) // ^ int64(f.Round) // 用异或运算混合多维度信息
@@ -176,6 +174,8 @@ func NewFight(mode, status info.EnumBattleMode, p1 common.PlayerI, p2 common.Pla
Status: status,
}
f.Info.Status = status //房主
f.Info.Mode = mode
ok := f.initplayer(p1, false)
if !ok {
return nil
@@ -710,11 +710,12 @@ func (f *FightC) enterturn(fattack, sattack *action.SelectSkillAction) {
for i := 0; i < 20; i++ { //堆叠状态剩余回合
t := f.First.GetEffect(input.EffectType.Status, i)
if t[0].ID() != 0 { //状态都是叠层类的
if len(t) > 0 && t[0].ID() != 0 { //状态都是叠层类的
ret.FAttack.Status[i] = int8(t[0].Duration())
}
t = f.Second.GetEffect(input.EffectType.Status, i)
if t[0].ID() != 0 {
if len(t) > 0 && t[0].ID() != 0 {
ret.SAttack.Status[i] = int8(t[0].Duration())
}

View File

@@ -112,8 +112,9 @@ func (c *Input) AddEffect(e Effect) {
e.Stack(v.Stack() + e.Stack()) //获取到当前叠层数然后叠加
//v.Duration(e.Duration()) //回合数覆盖
}
c.Effects = append(c.Effects, e)
}
c.Effects = append(c.Effects, e)
return
}
@@ -131,13 +132,13 @@ func (c *Input) Exec(fn func(Effect) bool) bool {
result := true
for _, value := range c.Effects {
if value.Alive() {
result1 := fn(value)
if !result1 {
if !fn(value) { //存在false,但是仍然要向下执行
result = false //如果是false,说明存在阻止向下执行的effect比如免疫能力提升效果
}
}
return true
}
return result

View File

@@ -1,6 +1,9 @@
package node
import "blazing/logic/service/fight/input"
import (
"blazing/logic/service/fight/info"
"blazing/logic/service/fight/input"
)
// 切精灵返回false重写change方法来实现切换效果
// 精灵切换相关触发
@@ -14,6 +17,7 @@ func (e *EffectNode) OnSwitchOut(ctx input.Ctx) bool {
if e.GetInput().UserID == ctx.Player.GetInfo().UserID { //清除对方的我方施加uff
e.NotALive()
}
return true
}
@@ -21,11 +25,8 @@ func (e *EffectNode) OnOwnerSwitchIn(ctx input.Ctx) bool {
return true
}
//自身下场,清除掉技能效果
// 自身下场,清除掉技能效果
func (e *EffectNode) OnOwnerSwitchOut(ctx input.Ctx) bool {
e.Input.AttackValue = nil
//自身下场清除掉自身的回合效果
//this.GetBattle().Effects[this.GetInput().UserID].RemoveEffect(this)
e.NotALive()
e.Input.AttackValue = info.NewAttackValue(e.Input.UserID)
return true
}

View File

@@ -51,7 +51,7 @@ func (f *FightC) ChangePet(c common.PlayerI, id uint32) {
BaseAction: action.NewBaseAction(c.GetInfo().UserID),
}
f.Switch = append(f.Switch, ret)
f.GetInputByPlayer(c, false).InitAttackValue() //切换精灵消除能力提升
f.GetInputByPlayer(c, false).Exec(func(t input.Effect) bool {
t.OnOwnerSwitchOut(input.Ctx{})

View File

@@ -12,9 +12,12 @@ import (
"fmt"
"reflect"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/gogf/gf/v2/os/glog"
"github.com/lunixbochs/struc"
"github.com/panjf2000/gnet/v2"
"github.com/panjf2000/gnet/v2/pkg/logging"
)
// TomeeHeader 结构体字段定义
@@ -177,7 +180,7 @@ func (h *ClientData) Recv(data TomeeHeader) {
return
}
t := h.Conn.Context().(*ClientData).Player
t := h.Conn.Context().(*ClientData)
if ok && aa != 0 { //这里实现回复错误包
@@ -245,3 +248,30 @@ func (h *ClientData) OnEvent(v []byte) {
h.Recv(header)
}
func (p *ClientData) SendPack(b []byte) error {
// if _, ok := p.MainConn.Context().(*ClientData); !ok {
// return fmt.Errorf("链接错误,取消发包")
// }
p.Conn.Context().(*ClientData).Mu.Lock()
defer p.Conn.Context().(*ClientData).Mu.Unlock()
if p.Conn.Context().(*ClientData).Wsmsg.Upgraded {
// This is the echo server
err := wsutil.WriteServerMessage(p.Conn, ws.OpBinary, b)
if err != nil {
logging.Infof("conn[%v] [err=%v]", p.Conn.RemoteAddr().String(), err.Error())
return err
}
} else {
_, err := p.Conn.Write(b)
if err != nil {
glog.Debug(context.Background(), err)
}
}
return nil
}

View File

@@ -13,7 +13,7 @@ import (
// 主函数实现
// 添加经验
// 禁止发包
func (p *Player) AddPetExp(petinfo *model.PetInfo, addExp uint32, bro bool) {
func (p *Player) AddPetExp(petinfo *model.PetInfo, addExp uint32) {
addExp = utils.Min(addExp, p.Info.ExpPool)
originalLevel := petinfo.Level
Exp := petinfo.Exp + addExp
@@ -38,19 +38,6 @@ func (p *Player) AddPetExp(petinfo *model.PetInfo, addExp uint32, bro bool) {
petinfo.CalculatePetPane()
petinfo.Cure()
}
if bro {
return
}
t1 := NewTomeeHeader(2508, p.Info.UserID)
rrr := &info.PetUpdateOutboundInfo{}
var petinfwo info.UpdatePropInfo
copier.Copy(&petinfwo, petinfo)
rrr.Data = append(rrr.Data, petinfwo)
p.SendPack(t1.Pack(rrr)) //准备包由各自发,因为协议不一样
// 发送经验更新消息
//player.SendMessage(generatePetUpdateInfo(petEntity, originalExp+addExp-exp, addition))
// 处理技能学习
canLearnSkillList := LastFourElements(petinfo.GetLevelRangeCanLearningSkills(originalLevel, petinfo.Level)) //获取最后四个技能,如果不足,那就取全部技能
@@ -70,7 +57,18 @@ func (p *Player) AddPetExp(petinfo *model.PetInfo, addExp uint32, bro bool) {
}
petinfo.SkillList = petinfo.SkillList[:4] //归正到4
//todo 待实现
t1 := NewTomeeHeader(2508, p.Info.UserID)
rrr := &info.PetUpdateOutboundInfo{}
var petinfwo info.UpdatePropInfo
copier.Copy(&petinfwo, petinfo)
rrr.Data = append(rrr.Data, petinfwo)
p.SendPack(t1.Pack(rrr)) //准备包由各自发,因为协议不一样
// 发送经验更新消息
//player.SendMessage(generatePetUpdateInfo(petEntity, originalExp+addExp-exp, addition))
// // 发送技能更新消息
// updateSkillInfo := UpdateSkillInfo{
// PetCatchTime: petEntity.captureTime,

View File

@@ -6,7 +6,6 @@ import (
"blazing/common/socket/errorcode"
"blazing/common/utils"
"blazing/cool"
"fmt"
"math/rand"
"strings"
@@ -21,12 +20,8 @@ import (
"time"
"github.com/antlabs/timer"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/util/gconv"
"github.com/panjf2000/gnet/pkg/logging"
"github.com/panjf2000/gnet/v2"
)
@@ -109,6 +104,10 @@ func (p *Player) SpawnMonsters() {
p.SendPack(tt.Pack(&p.OgreInfo))
}
func (p *Player) SendPack(b []byte) error {
return p.MainConn.Context().(*ClientData).SendPack(b)
}
// 2. 从 string 类型 slice 随机选一个元素
@@ -282,33 +281,6 @@ func (player1 *Player) Kick() {
<-player1.MainConn.Context().(*ClientData).CloseChan
}
}
func (p *Player) SendPack(b []byte) error {
if _, ok := p.MainConn.Context().(*ClientData); !ok {
return fmt.Errorf("链接错误,取消发包")
}
p.MainConn.Context().(*ClientData).Mu.Lock()
defer p.MainConn.Context().(*ClientData).Mu.Unlock()
if p.MainConn.Context().(*ClientData).Wsmsg.Upgraded {
// This is the echo server
err := wsutil.WriteServerMessage(p.MainConn, ws.OpBinary, b)
if err != nil {
logging.Infof("conn[%v] [err=%v]", p.MainConn.RemoteAddr().String(), err.Error())
return err
}
} else {
err := p.MainConn.AsyncWrite(b, nil)
if err != nil {
glog.Debug(context.Background(), err)
}
}
return nil
}
func (p *Player) Cheak(b error) {
if b != nil {

View File

@@ -3,7 +3,7 @@ server:
address: ":8080" #前台服务器地址
port: 53388 #后台服务器端口
rpc: 56409 #rpc服务端口
game: [27777]
game: [27777, 27778]
openapiPath: "/api.json"
swaggerPath: "/swagger"
clientMaxBodySize:

View File

@@ -36,4 +36,5 @@
<energy type="35" mapID="702" name="甲烷燃气" collectType="采集" collectCnt="2" unit="罐" dir="7"/>
<energy type="36" mapID="29" name="甲烷燃气" collectType="采集" collectCnt="2" unit="罐" dir="7"/>
<energy type="37" mapID="688" name="甲烷燃气" collectType="采集" collectCnt="2" unit="罐" dir="6"/>
</root>