refactor(socket): 重构 ClientData 结构体并优化相关逻辑
- 简化 ClientData 结构体,移除不必要的方法 - 优化 Player 结构体,调整 Conn 类型 - 更新 wscodec.go 中的 Conn 结构体 - 删除未使用的 XML 相关文件和代码 - 调整 ServerEvent 和 controller 中的相关逻辑
This commit is contained in:
117
common/data/xmlres/skill.go
Normal file
117
common/data/xmlres/skill.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package xmlres
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MovesTbl 定义 XML 根元素
|
||||
type MovesTbl struct {
|
||||
XMLName xml.Name `xml:"MovesTbl"`
|
||||
Moves []Move `xml:"Moves>Move"`
|
||||
EFF []SideEffect `xml:"SideEffects>SideEffect"`
|
||||
}
|
||||
type MovesMap struct {
|
||||
XMLName xml.Name `xml:"MovesTbl"`
|
||||
Moves map[int]Move
|
||||
EFF []SideEffect `xml:"SideEffects>SideEffect"`
|
||||
}
|
||||
|
||||
// Move 定义单个技能的结构
|
||||
type Move struct {
|
||||
ID int `xml:"ID,attr"`
|
||||
Name string `xml:"Name,attr"`
|
||||
Category int `xml:"Category,attr"`
|
||||
Type int `xml:"Type,attr"`
|
||||
Power int `xml:"Power,attr"`
|
||||
MaxPP int `xml:"MaxPP,attr"`
|
||||
Accuracy int `xml:"Accuracy,attr"`
|
||||
CritRate int `xml:"CritRate,attr,omitempty"`
|
||||
Priority int `xml:"Priority,attr,omitempty"`
|
||||
MustHit int `xml:"MustHit,attr,omitempty"`
|
||||
SwapElemType int `xml:"SwapElemType,attr,omitempty"`
|
||||
CopyElemType int `xml:"CopyElemType,attr,omitempty"`
|
||||
CritAtkFirst int `xml:"CritAtkFirst,attr,omitempty"`
|
||||
CritAtkSecond int `xml:"CritAtkSecond,attr,omitempty"`
|
||||
CritSelfHalfHp int `xml:"CritSelfHalfHp,attr,omitempty"`
|
||||
CritFoeHalfHp int `xml:"CritFoeHalfHp,attr,omitempty"`
|
||||
DmgBindLv int `xml:"DmgBindLv,attr,omitempty"`
|
||||
PwrBindDv int `xml:"PwrBindDv,attr,omitempty"`
|
||||
PwrDouble int `xml:"PwrDouble,attr,omitempty"`
|
||||
|
||||
SideEffect string `xml:"SideEffect,attr,omitempty"`
|
||||
SideEffectArg string `xml:"SideEffectArg,attr,omitempty"`
|
||||
AtkNum int `xml:"AtkNum,attr,omitempty"`
|
||||
Url string `xml:"Url,attr,omitempty"`
|
||||
|
||||
Info string `xml:"info,attr,omitempty"`
|
||||
|
||||
CD int `xml:"CD,attr"`
|
||||
}
|
||||
type SideEffect struct {
|
||||
ID int `xml:"ID,attr"`
|
||||
Help string `xml:"help,attr"`
|
||||
Des string `xml:"des,attr"`
|
||||
}
|
||||
|
||||
// ReadHTTPFile 通过HTTP GET请求获取远程文件内容
|
||||
// url: 远程文件的URL地址
|
||||
// 返回文件内容字节流和可能的错误
|
||||
func ReadHTTPFile(url string) ([]byte, error) {
|
||||
// 创建HTTP客户端并设置超时时间(避免无限等待)
|
||||
client := &http.Client{
|
||||
Timeout: 30 * time.Second, // 30秒超时
|
||||
}
|
||||
|
||||
// 发送GET请求
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close() // 确保响应体被关闭
|
||||
|
||||
// 检查响应状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("请求返回非成功状态码: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// 读取响应体内容
|
||||
content, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取内容失败: %w", err)
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
func getxml() ([]byte, error) {
|
||||
|
||||
// 读取整个文件内容,返回字节切片和错误
|
||||
content, err := ReadHTTPFile("http://127.0.0.1:8080/assets/227.xml")
|
||||
if err != nil {
|
||||
// 处理错误(文件不存在、权限问题等)
|
||||
log.Fatalf("无法读取文件: %v", err)
|
||||
}
|
||||
return content, nil
|
||||
|
||||
}
|
||||
func getMoves() MovesMap {
|
||||
|
||||
// 解析XML到结构体
|
||||
var maps MovesTbl
|
||||
t1, _ := getxml()
|
||||
xml.Unmarshal(t1, &maps)
|
||||
var mapss MovesMap
|
||||
mapss.Moves = make(map[int]Move, 0)
|
||||
for _, v := range maps.Moves {
|
||||
mapss.Moves[int(v.ID)] = v
|
||||
}
|
||||
|
||||
return mapss
|
||||
}
|
||||
|
||||
// 全局函数配置
|
||||
var MovesConfig = getMoves()
|
||||
Reference in New Issue
Block a user