```
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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user