feat(database): add gearTxt field to parsed results and update related functions

This commit is contained in:
kever
2026-02-17 22:42:59 +08:00
parent 5dba3d9930
commit 8c4c4e77d7
13 changed files with 658 additions and 226 deletions

View File

@@ -26,6 +26,7 @@ type ParserService struct {
heroBase map[string]heroBaseStats
heroOnce sync.Once
heroErr error
fallbackMainStatCount int
}
func NewParserService(cfg *config.Config, logger *utils.Logger) *ParserService {
@@ -37,24 +38,43 @@ func NewParserService(cfg *config.Config, logger *utils.Logger) *ParserService {
}
}
func (ps *ParserService) writeParseSnapshot(kind string, data []byte) {
if len(data) == 0 {
return
}
homeDir, err := os.UserHomeDir()
if err != nil {
return
}
dir := filepath.Join(homeDir, ".equipment-analyzer", "remote_parse_snapshots")
if err := os.MkdirAll(dir, 0755); err != nil {
return
}
stamp := time.Now().Format("20060102_150405.000")
filename := filepath.Join(dir, stamp+"_"+kind+".json")
_ = os.WriteFile(filename, data, 0644)
}
// ParseHexData 解析十六进制数据
func (ps *ParserService) ParseHexData(hexDataList []string) (*model.ParsedResult, string, error) {
if len(hexDataList) == 0 {
ps.logger.Warn("没有数据需要解析")
ps.logger.Warn("no data to parse")
return &model.ParsedResult{
Items: make([]interface{}, 0),
Heroes: make([]interface{}, 0),
}, "", nil
}
ps.logger.Info("开始远程解析数据", "count", len(hexDataList))
ps.logger.Info("remote parse start", "count", len(hexDataList))
url := "https://krivpfvxi0.execute-api.us-west-2.amazonaws.com/dev/getItems"
reqBody := map[string]interface{}{
"data": hexDataList,
}
jsonBytes, _ := json.Marshal(reqBody)
client := &http.Client{Timeout: 15 * time.Second}
ps.writeParseSnapshot("request", jsonBytes)
client := &http.Client{Timeout: 60 * time.Second}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBytes))
if err == nil {
req.Header.Set("Content-Type", "application/json")
@@ -62,23 +82,21 @@ func (ps *ParserService) ParseHexData(hexDataList []string) (*model.ParsedResult
if err == nil && resp.StatusCode == 200 {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
ps.writeParseSnapshot("response", body)
// 新校验逻辑校验data和units字段
var raw map[string]interface{}
if err := json.Unmarshal(body, &raw); err != nil {
ps.logger.Error("远程json解析失败", "error", err)
return nil, "", fmt.Errorf("远程json解析失败: %v", err)
ps.logger.Error("remote json unmarshal failed", "error", err)
return nil, "", fmt.Errorf("remote json unmarshal failed: %v", err)
}
// 校验data字段
dataArr, dataOk := raw["data"].([]interface{})
if !dataOk || len(dataArr) == 0 {
ps.logger.Error("远程json校验失败data字段缺失或为空")
return nil, "", fmt.Errorf("远程json校验失败data字段缺失或为空")
ps.logger.Error("remote json validate failed: data missing or empty")
return nil, "", fmt.Errorf("remote json validate failed: data missing or empty")
}
// 校验通过,直接解析数据
ps.logger.Info("远程原始数据校验通过,开始解析")
ps.logger.Info("remote json validate ok, start parse")
parsedResult, err := ps.ReadRawJsonFile(string(body))
if err != nil {
return nil, "", err
@@ -86,15 +104,15 @@ func (ps *ParserService) ParseHexData(hexDataList []string) (*model.ParsedResult
return parsedResult, "", nil
} else if err != nil {
ps.logger.Error("远程解析请求失败", "error", err)
return nil, "", fmt.Errorf("远程解析请求失败: %v", err)
ps.logger.Error("remote parse request failed", "error", err)
return nil, "", fmt.Errorf("remote parse request failed: %v", err)
} else {
ps.logger.Error("远程解析响应码异常", "status", resp.StatusCode)
return nil, "", fmt.Errorf("远程解析响应码异常: %d", resp.StatusCode)
ps.logger.Error("remote parse http status", "status", resp.StatusCode)
return nil, "", fmt.Errorf("remote parse http status %d", resp.StatusCode)
}
} else {
ps.logger.Error("远程解析请求构建失败", "error", err)
return nil, "", fmt.Errorf("远程解析请求构建失败: %v", err)
ps.logger.Error("remote parse request build failed", "error", err)
return nil, "", fmt.Errorf("remote parse request build failed: %v", err)
}
}
@@ -106,7 +124,62 @@ func (ps *ParserService) ReadRawJsonFile(rawJson string) (*model.ParsedResult, e
return nil, err
}
// 提取装备和英雄数据
// If input is already parsed (gear.txt export), use items/heroes directly.
if itemsRaw, ok := rawData["items"].([]interface{}); ok {
heroesRaw, _ := rawData["heroes"].([]interface{})
parsedItems := ps.normalizeParsedItems(itemsRaw)
result := &model.ParsedResult{
Items: parsedItems,
Heroes: heroesRaw,
GearTxt: rawJson,
}
return result, nil
}
// If input is a mixed "data" array (remote parse response), split items/heroes.
if dataRaw, ok := rawData["data"].([]interface{}); ok && len(dataRaw) > 0 {
dataItems, dataHeroes := ps.splitDataArray(dataRaw)
if len(dataItems) > 0 || len(dataHeroes) > 0 {
ps.logger.Info("raw json format: mixed data array", "items", len(dataItems), "heroes", len(dataHeroes))
// If heroes are not present in data array, fall back to units.
var rawUnits []interface{}
if unitsRaw, ok := rawData["units"].([]interface{}); ok && len(unitsRaw) > 0 {
maxLen := 0
for _, u := range unitsRaw {
if arr, ok := u.([]interface{}); ok && len(arr) > maxLen {
maxLen = len(arr)
rawUnits = arr
}
}
}
parsedHeroes := dataHeroes
if len(rawUnits) > 0 {
parsedHeroes = rawUnits
}
ps.fallbackMainStatCount = 0
convertedItems := ps.convertItemsAllWithLog(dataItems)
ps.logger.Info("main stat fallback count", "count", ps.fallbackMainStatCount)
convertedHeroes := ps.convertUnits(parsedHeroes)
result := &model.ParsedResult{
Items: make([]interface{}, len(convertedItems)),
Heroes: make([]interface{}, len(convertedHeroes)),
}
for i, v := range convertedItems {
result.Items[i] = v
}
for i, v := range convertedHeroes {
result.Heroes[i] = v
}
result.GearTxt = rawJson
return result, nil
}
}
// 提取装备和英雄数?
equips, _ := rawData["data"].([]interface{})
// 修正units 取最大长度的那组
var rawUnits []interface{}
@@ -131,8 +204,10 @@ func (ps *ParserService) ReadRawJsonFile(rawJson string) (*model.ParsedResult, e
}
// 转换装备数据
ps.fallbackMainStatCount = 0
convertedItems := ps.convertItemsAllWithLog(validEquips)
// 转换英雄数据(只对最大组)
ps.logger.Info("main stat fallback count", "count", ps.fallbackMainStatCount)
// 转换英雄数据(只对最大组?
convertedHeroes := ps.convertUnits(rawUnits)
result := &model.ParsedResult{
@@ -145,10 +220,115 @@ func (ps *ParserService) ReadRawJsonFile(rawJson string) (*model.ParsedResult, e
for i, v := range convertedHeroes {
result.Heroes[i] = v
}
result.GearTxt = rawJson
return result, nil
}
func (ps *ParserService) isGearRecord(itemMap map[string]interface{}) bool {
// Require set field if present in this raw format.
if f, ok := itemMap["f"].(string); ok && f != "" {
return true
}
// Allow already-normalized gear fields from other sources.
if _, ok := itemMap["set"]; ok {
return true
}
if _, ok := itemMap["gear"]; ok {
return true
}
if _, ok := itemMap["type"]; ok {
return true
}
if _, ok := itemMap["mainStatType"]; ok {
return true
}
// Otherwise treat as non-gear to avoid including material-like records.
return false
}
func (ps *ParserService) isHeroRecord(itemMap map[string]interface{}) bool {
if name, ok := itemMap["name"].(string); ok && name != "" && name != "Unknown" {
return true
}
if code, ok := itemMap["code"].(string); ok && strings.HasPrefix(code, "c") {
return true
}
// heroes commonly have opt/exp fields
if _, ok := itemMap["opt"]; ok {
return true
}
if _, ok := itemMap["exp"]; ok {
return true
}
return false
}
func (ps *ParserService) splitDataArray(data []interface{}) (items []interface{}, heroes []interface{}) {
for _, entry := range data {
itemMap, ok := entry.(map[string]interface{})
if !ok || itemMap == nil {
continue
}
if ps.isGearRecord(itemMap) {
items = append(items, entry)
continue
}
if ps.isHeroRecord(itemMap) {
heroes = append(heroes, entry)
continue
}
}
return items, heroes
}
func (ps *ParserService) normalizeParsedItems(items []interface{}) []interface{} {
for i, item := range items {
itemMap, ok := item.(map[string]interface{})
if !ok || itemMap == nil {
continue
}
if _, exists := itemMap["main"]; exists {
continue
}
op, opExists := itemMap["op"].([]interface{})
if !opExists || len(op) == 0 {
continue
}
mainOp, ok := op[0].([]interface{})
if !ok || len(mainOp) < 2 {
continue
}
mainOpType, _ := mainOp[0].(string)
mainOpValue, _ := mainOp[1].(float64)
if mainOpType == "" {
continue
}
mainType := statByIngameStat[mainOpType]
if mainType == "" {
continue
}
var mainValue float64
if ps.isFlat(mainOpType) {
mainValue = mainOpValue
} else {
mainValue = ps.round10ths(mainOpValue * 100)
}
if mainValue == 0 || mainValue != mainValue {
mainValue = 0
}
itemMap["main"] = map[string]interface{}{
"type": mainType,
"value": mainValue,
}
items[i] = itemMap
}
return items
}
// convertItems 转换装备数据
func (ps *ParserService) convertItems(rawItems []interface{}) []map[string]interface{} {
var convertedItems []map[string]interface{}
@@ -197,10 +377,10 @@ func (ps *ParserService) convertSingleItem(item map[string]interface{}) map[stri
// 转换增强
ps.convertEnhance(converted)
// 转换主属
// 转换主属?
ps.convertMainStat(converted)
// 转换副属
// 转换副属?
ps.convertSubStats(converted)
// 转换ID
@@ -227,7 +407,7 @@ func (ps *ParserService) convertUnits(rawUnits []interface{}) []map[string]inter
convertedUnit[key] = value
}
// 转换星星和觉
// 转换星星和觉?
if g, exists := unitMap["g"]; exists {
convertedUnit["stars"] = g
}
@@ -335,6 +515,7 @@ func (ps *ParserService) convertEnhance(item map[string]interface{}) {
func (ps *ParserService) convertMainStat(item map[string]interface{}) {
op, opExists := item["op"].([]interface{})
mainStatValue, mainStatExists := item["mainStatValue"].(float64)
fallbackUsed := false
if opExists && len(op) > 0 && mainStatExists {
if mainOp, ok := op[0].([]interface{}); ok && len(mainOp) > 0 {
@@ -359,6 +540,39 @@ func (ps *ParserService) convertMainStat(item map[string]interface{}) {
}
}
}
// Fallback: if main is missing, derive from op[0] value directly.
if _, exists := item["main"]; !exists {
if opExists && len(op) > 0 {
if mainOp, ok := op[0].([]interface{}); ok && len(mainOp) >= 2 {
mainOpType, _ := mainOp[0].(string)
mainOpValue, _ := mainOp[1].(float64)
if mainOpType != "" {
mainType := statByIngameStat[mainOpType]
if mainType != "" {
var mainValue float64
if ps.isFlat(mainOpType) {
mainValue = mainOpValue
} else {
mainValue = ps.round10ths(mainOpValue * 100)
}
if mainValue == 0 || mainValue != mainValue {
mainValue = 0
}
item["main"] = map[string]interface{}{
"type": mainType,
"value": mainValue,
}
fallbackUsed = true
}
}
}
}
}
if fallbackUsed {
ps.fallbackMainStatCount++
}
}
func (ps *ParserService) convertSubStats(item map[string]interface{}) {
@@ -371,7 +585,7 @@ func (ps *ParserService) convertSubStats(item map[string]interface{}) {
statAcc := make(map[string]map[string]interface{})
// 处理副属(从索引1开始)
// 处理副属?(从索?开?
for i := 1; i < len(op); i++ {
if opItem, ok := op[i].([]interface{}); ok && len(opItem) >= 2 {
opType, _ := opItem[0].(string)
@@ -417,7 +631,7 @@ func (ps *ParserService) convertSubStats(item map[string]interface{}) {
}
}
// 转换为最终格
// 转换为最终格?
var substats []interface{}
for statType, statData := range statAcc {
substat := map[string]interface{}{
@@ -765,3 +979,4 @@ func (ps *ParserService) applyHeroBase(unit map[string]interface{}, base heroBas
unit["res"] = base.Res
}
}