feat(player): 优化玩家连接与消息发送逻辑

- 为 `Conn` 结构体新增 `Close()` 方法,并在多处替换原有的 `GetConn().Close()` 调用,
  统一通过 `MainConn` 操作以提高代码一致性。
- 在 `SendPack` 和 `GetPlayer` 等方法中增加互斥锁保护,确保对 `MainConn` 的访问是线程安全的。
- 移除冗余的 `GetConn()` 方法,直接使用 `MainConn` 提升性能并简化结构。

refactor(rpc): 临时将 RPC 客户端地址硬编码为本地地址

- 将 rpcaddr 临时设置为 "127.0.0.1",便于调试和开发环境测试。

refactor(handler): 扩展 JSON-RPC 方法过滤条件

- 增加了对 "Kick" 和 "RegisterLogic" 方法的排除,防止其被注册到 RPC 处理器中。

refactor(fight): 精简战斗服务中的判断逻辑

- 简化 `IsFirst` 函数返回表达式。
- 移除了多余的 defer 语句,提前设置 `closefight` 标志位。

refactor(pet): 改进宠物经验添加机制

- `AddPetExp` 方法新增布尔参数 `bro` 控制是否广播更新。
- 修改调用点以适配新参数,修复潜在的数据同步问题。
- 初始化宠物时禁止触发经验变更广播。

chore(build): 删除旧版二进制文件 logic1

- 清理无用的编译产物,避免混淆项目结构。
```
This commit is contained in:
2025-10-14 01:26:59 +08:00
parent 14b7b03cef
commit e84d55aec0
10 changed files with 30 additions and 28 deletions

2
.gitignore vendored
View File

@@ -31,3 +31,5 @@ frontend/
*.exe
logic/logic_linux_amd64
login/login_linux_amd64
logic/logic
.gitignore

View File

@@ -86,7 +86,7 @@ func StartClient(id, port uint16, callback any) *struct {
} {
var rpcaddr, _ = service.NewBaseSysParamService().DataByKey(context.Background(), "server_ip")
rpcaddr = "127.0.0.1"
closer1, err := jsonrpc.NewMergeClient(context.Background(),
"ws://"+rpcaddr+":"+rpcport, "", []interface{}{
&RPCClient,

View File

@@ -109,7 +109,10 @@ func (s *handler) register(namespace string, r interface{}) {
for i := 0; i < val.NumMethod(); i++ {
method := val.Type().Method(i)
//fmt.Println("method: %s", method.Name)
if method.Name != "KickPerson" && method.Name != "QuitSelf" {
if method.Name != "KickPerson" &&
method.Name != "QuitSelf" &&
method.Name != "Kick" &&
method.Name != "RegisterLogic" {
continue
}

View File

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

View File

@@ -81,12 +81,7 @@ func (f *FightC) GetRand() *rand.Rand {
// 获取随机数
func (f *FightC) IsFirst(play common.PlayerI) bool {
if f.First.Player == play {
return true
}
return false
return f.First.Player == play
}
// 加载进度
@@ -643,9 +638,7 @@ func (f *FightC) enterturn(fattack, sattack *action.SelectSkillAction) {
WinnerId: WinnerId,
})
})
defer func() {
f.closefight = true
}()
f.closefight = true
}
}

View File

@@ -33,8 +33,8 @@ func calculateExperience(level uint32, baseValue uint32) uint32 {
// 主函数实现
// 添加经验
// 超NO 加成
func (p *Player) AddPetExp(petinfo *model.PetInfo, addExp uint32) {
// 禁止发包
func (p *Player) AddPetExp(petinfo *model.PetInfo, addExp uint32, bro bool) {
originalLevel := petinfo.Level
for {
@@ -85,6 +85,9 @@ func (p *Player) AddPetExp(petinfo *model.PetInfo, addExp uint32) {
if originalLevel != petinfo.Level {
petinfo.CalculatePetPane()
}
if bro {
return
}
t1 := NewTomeeHeader(2508, p.Info.UserID)
rrr := &info.PetUpdateOutboundInfo{}
@@ -208,7 +211,7 @@ func (player *Player) GenPetInfo(id int, dv, natureId, abilityTypeEnum, shinyid,
p.SkillListLen = uint32(len(tttt))
p.CalculatePetPane()
p.Hp = p.MaxHp
player.AddPetExp(p, 0)
player.AddPetExp(p, 0, true)
return p
}

View File

@@ -58,19 +58,26 @@ func NewClientData() *ClientData {
}
var Mainplayer = &utils.SyncMap[uint32, *Player]{} //玩家数据
func (c *Conn) Close() {
c.Mu.Lock()
defer c.Mu.Unlock()
c.MainConn.Close()
}
func (c *Conn) SendPack(bytes []byte) error {
if t, ok := c.GetConn().Context().(*ClientData); ok {
c.Mu.Lock()
defer c.Mu.Unlock()
if t, ok := c.MainConn.Context().(*ClientData); ok {
if t.Wsmsg.Upgraded {
// This is the echo server
err := wsutil.WriteServerMessage(c.MainConn, ws.OpBinary, bytes)
if err != nil {
logging.Infof("conn[%v] [err=%v]", c.GetConn().RemoteAddr().String(), err.Error())
logging.Infof("conn[%v] [err=%v]", c.MainConn.RemoteAddr().String(), err.Error())
return err
}
} else {
_, err := c.GetConn().Write(bytes)
_, err := c.MainConn.Write(bytes)
if err != nil {
glog.Debug(context.Background(), err)

View File

@@ -5,8 +5,9 @@ import "blazing/common/socket/errorcode"
func GetPlayer(c *Conn, userid uint32) *Player { //TODO 这里待优化,可能存在内存泄漏问题
//检查player初始化是否为conn初始后取map防止二次连接后存在两个player
clientdata := c.GetConn().Context().(*ClientData)
c.Mu.Lock()
defer c.Mu.Unlock()
clientdata := c.MainConn.Context().(*ClientData)
if clientdata.Player == nil {
clientdata.Player = NewPlayer(
@@ -37,7 +38,7 @@ func KickPlayer(userid uint32) { //踢出玩家
//实际上这里有个问题,会造成重复保存问题
player1.SendPack(head.Pack(nil))
player1.MainConn.GetConn().Close()
player1.MainConn.Close()
// clientdata.Player = player
}

View File

@@ -17,13 +17,6 @@ type Conn struct {
Mu sync.Mutex
}
func (c *Conn) GetConn() gnet.Conn {
c.Mu.Lock()
defer c.Mu.Unlock()
return c.MainConn
}
func NewConn(c gnet.Conn) *Conn {
return &Conn{MainConn: c}
}

Binary file not shown.