refactor(socket): 重构 ClientData 结构体并优化相关逻辑

- 简化 ClientData 结构体,移除不必要的方法
- 优化 Player 结构体,调整 Conn 类型
- 更新 wscodec.go 中的 Conn 结构体
- 删除未使用的 XML 相关文件和代码
- 调整 ServerEvent 和 controller 中的相关逻辑
This commit is contained in:
2025-08-30 00:36:08 +08:00
parent 1f835c1197
commit 7b5ec208fc
31 changed files with 472 additions and 602 deletions

View File

@@ -7,5 +7,5 @@ type OgreInfo struct {
type OgrePetInfo struct {
Id uint32
Shiny uint32
Lv uint32 `struc:"skip"` //等级
}

View File

@@ -2,13 +2,16 @@ package maps
import (
"blazing/common/data/socket"
"blazing/common/data/xmlres"
"blazing/common/socket/handler"
"blazing/logic/service/space"
"blazing/modules/blazing/model"
"math/rand"
"strings"
"time"
"github.com/creasty/defaults"
"github.com/gogf/gf/v2/util/gconv"
)
type InInfo struct {
@@ -38,9 +41,9 @@ func (t *InInfo) Broadcast(mapid uint32, o OutInfo) {
func (t *InInfo) SpawnMonsters(c *socket.Player, isfrist bool) {
// 获取当前地图的怪物配置
// if c == nil || mservice.NewMonsterService().GetId(c.MapId) == 0 { //用户离线
// return
// }
if c == nil || c.Info.MapID == 0 { //用户离线
return
}
if !c.IsLogin {
defer func() {
@@ -60,27 +63,54 @@ func (t *InInfo) SpawnMonsters(c *socket.Player, isfrist bool) {
t.monsters, _, _ = replaceOneNumber(t.monsters)
}
t1 := t.genMonster(c.Info.UserID)
t1 := t.genMonster(c.Info.MapID)
if t1 != nil {
t1 := tt.Pack(t1)
c.SendPack(t1)
}
c.SendPack(tt.Pack(&t1))
}
// 2. 从 string 类型 slice 随机选一个元素
func RandomStringFromSlice(s []string) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomIdx := r.Intn(len(s))
return s[randomIdx]
}
// 应该根据怪物信息决定后端生成
func (t *InInfo) genMonster(mapid uint32) OgreInfo {
func (t *InInfo) genMonster(mapid uint32) *OgreInfo {
// 设置怪物信息
t1 := OgreInfo{}
for _, tc := range xmlres.Monster.Maps {
if tc.ID == gconv.Int(mapid) && tc.Monsters != nil {
for i, m := range tc.Monsters.Monsters { //这里是9个
id := strings.Split(m.ID, " ")
lv := strings.Split(m.Lv, " ")
ttt := OgrePetInfo{
Id: gconv.Uint32(RandomStringFromSlice(id)),
}
if ttt.Id != 0 {
ttt.Shiny = 0 //待确认是否刷新异色
ttt.Lv = gconv.Uint32(RandomStringFromSlice(lv))
}
t1.Data[i] = ttt
}
break
}
}
t2 := OgreInfo{}
for i := 0; i < 3; i++ {
ttt := OgrePetInfo{}
// ttt.Id = mservice.NewMonsterService().GetId(mapid) //待修改成xml获取
ttt.Shiny = uint32(i + 1) //异色概率,待实现自定义
//t1.Data[i] = mservice.NewMonsterService().GetId(c.MapId)
t1.Data[t.monsters[i]] = ttt
t2.Data[t.monsters[i]] = t1.Data[t.monsters[i]]
}
return t1
return &t2
}
// 计算整数的二进制1的个数Integer.bitCount
@@ -101,7 +131,7 @@ func generateThreeUniqueNumbers() [3]int {
index := 0
for index < 3 {
num := rand.Intn(10)
num := rand.Intn(9)
if !selected[num] {
selected[num] = true
result[index] = num
@@ -124,7 +154,7 @@ func replaceOneNumber(original [3]int) ([3]int, int, int) {
originalMap[num] = true
}
for i := 0; i < 10; i++ {
for i := 0; i < 8; i++ {
if !originalMap[i] {
candidates = append(candidates, i)
}