Files
bl/common/data/xml/skill/skill.go
昔念 038a5f13da refactor(fight): 重构战斗模块
- 优化了数据结构和类型定义,提高了代码的可读性和可维护性
- 移除了未使用的代码和冗余的结构体字段
- 重新组织了代码文件,提高了模块化程度
- 为后续的战斗逻辑实现和优化奠定了坚实的基础
2025-08-25 16:26:56 +08:00

118 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package skill
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()