107 lines
3.8 KiB
Go
107 lines
3.8 KiB
Go
package xml
|
||
|
||
import (
|
||
"fmt"
|
||
"io"
|
||
"log"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/ECUST-XX/xml" // 保持与之前一致的XML库
|
||
)
|
||
|
||
// Items 根节点,对应<Items>标签
|
||
type Items struct {
|
||
XMLName xml.Name `xml:"Items"`
|
||
Items []Item `xml:"Item"` // 包含所有物品配置
|
||
}
|
||
|
||
// Item 单个物品配置,对应<Item>标签
|
||
type Item struct {
|
||
ID int `xml:"ID,attr"` // 物品ID(与items.xml一致)
|
||
Name string `xml:"Name,attr"` // 物品名称
|
||
Rarity int `xml:"Rarity,attr,omitempty"` // 稀有度(可选)
|
||
Price int `xml:"Price,attr"` // 价格
|
||
Tradability int `xml:"Tradability,attr"` // 可交易性(0/1)
|
||
VipTradability int `xml:"VipTradability,attr"` // VIP可交易性(0/1)
|
||
DailyKey int `xml:"DailyKey,attr,omitempty"` // 每日限制键值(可选)
|
||
DailyOutMax int `xml:"DailyOutMax,attr,omitempty"` // 每日最大产出(可选)
|
||
Wd int `xml:"wd,attr"` // 未知属性(根据原XML保留)
|
||
UseMax int `xml:"UseMax,attr"` // 最大使用次数
|
||
LifeTime int `xml:"LifeTime,attr"` // 生命周期(0为永久)
|
||
Purpose int `xml:"purpose,attr"` // 用途标识
|
||
Bean int `xml:"Bean,attr,omitempty"` // 豆子数量(可选)
|
||
Hide int `xml:"Hide,attr"` // 是否隐藏(0/1)
|
||
Sort int `xml:"Sort,attr,omitempty"` // 排序序号(可选)
|
||
Des string `xml:"des,attr"` // 物品用途(根据说明补充)
|
||
Color string `xml:"color,attr,omitempty"` // 装备名字颜色(可选)
|
||
Level int `xml:"level,attr,omitempty"` // 装备等级(星星,可选)
|
||
|
||
Pet *Pet `xml:"pet,omitempty"` // 精灵属性子节点(可选)
|
||
TeamPK *TeamPK `xml:"teamPK,omitempty"` // 要塞保卫战子节点(可选)
|
||
}
|
||
|
||
// Pet 精灵属性子节点,对应<pet>标签
|
||
// 注:根据实际需求补充字段,这里以常见属性为例
|
||
type Pet struct {
|
||
Attack int `xml:"attack,attr,omitempty"` // 攻击加成
|
||
Defense int `xml:"defense,attr,omitempty"` // 防御加成
|
||
HP int `xml:"hp,attr,omitempty"` // 生命值加成
|
||
Speed int `xml:"speed,attr,omitempty"` // 速度加成
|
||
}
|
||
|
||
// TeamPK 要塞保卫战子节点,对应<teamPK>标签
|
||
// 注:根据实际需求补充字段,这里以常见属性为例
|
||
type TeamPK struct {
|
||
FortBonus int `xml:"fortBonus,attr,omitempty"` // 要塞加成
|
||
DefenseBonus int `xml:"defenseBonus,attr,omitempty"` // 防御加成
|
||
AttackBonus int `xml:"attackBonus,attr,omitempty"` // 攻击加成
|
||
}
|
||
|
||
// 复用HTTP文件读取函数
|
||
func ReadHTTPFile(url string) ([]byte, error) {
|
||
client := &http.Client{
|
||
Timeout: 30 * time.Second,
|
||
}
|
||
|
||
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
|
||
}
|
||
|
||
// 获取物品XML内容
|
||
func getItemsXML() ([]byte, error) {
|
||
// 替换为实际的Items XML文件URL
|
||
content, err := ReadHTTPFile("http://127.0.0.1:8080/assets/43.xml")
|
||
if err != nil {
|
||
log.Fatalf("无法读取物品文件: %v", err)
|
||
}
|
||
return content, nil
|
||
}
|
||
|
||
// 解析XML到结构体
|
||
func getItems() Items {
|
||
var items Items
|
||
content, _ := getItemsXML()
|
||
if err := xml.Unmarshal(content, &items); err != nil {
|
||
log.Fatalf("物品XML解析失败: %v", err)
|
||
}
|
||
return items
|
||
}
|
||
|
||
// 全局物品配置变量
|
||
var ItemsConfig = getItems()
|