mlp-server-tools/tools/convertsets/convert.go

187 lines
5.3 KiB
Go
Raw Permalink Normal View History

2019-05-21 22:34:21 +00:00
package main
import (
"strconv"
"strings"
)
type jsonSet struct {
Name string
Cards []jsonCard
}
2019-05-25 09:32:56 +00:00
// Some fields are in pointer type because they must not omitted
// if their value is 0, but should be omitted if empty
2019-05-21 22:34:21 +00:00
type jsonCard struct {
2019-05-25 09:32:56 +00:00
ID string
Name string
Subname string
Element []string
Keywords []string
Traits []string
Requirement jsonPowerRequirement `json:",omitempty"`
Cost *int `json:",omitempty"`
Power *int `json:",omitempty"`
Type string
Text string
Rarity string
ProblemBonus *int `json:",omitempty"`
ProblemOpponentPower int `json:",omitempty"`
ProblemRequirement jsonPowerRequirement `json:",omitempty"`
2019-05-21 22:34:21 +00:00
}
2019-05-25 09:32:56 +00:00
type jsonPowerRequirement map[string]int
2019-05-21 22:34:21 +00:00
func convert(xmlset xmlSet) (s jsonSet) {
s.Name = xmlset.Name
for _, xmlcard := range xmlset.Cards {
jsoncard := jsonCard{
2019-05-25 09:32:56 +00:00
Name: xmlcard.Name,
Keywords: []string{},
Traits: []string{},
Element: []string{},
}
type reqUnit struct {
Element string
Count int
2019-05-21 22:34:21 +00:00
}
2019-05-25 09:32:56 +00:00
requirements := make([]reqUnit, 3)
problemreq := make([]reqUnit, 2)
2019-05-21 22:34:21 +00:00
for _, property := range xmlcard.Properties {
switch property.Name {
case "Cost":
if property.Value != "" {
2019-05-25 09:32:56 +00:00
cost, _ := strconv.Atoi(property.Value)
jsoncard.Cost = &cost
2019-05-21 22:34:21 +00:00
}
case "Element":
2019-05-25 09:32:56 +00:00
if property.Value != "" && property.Value != "Multicolor" {
jsoncard.Element = append(jsoncard.Element, property.Value)
2019-05-21 22:34:21 +00:00
}
case "Keywords":
if property.Value != "" {
jsoncard.Keywords = strings.Split(strings.TrimRight(property.Value, "."), ", ")
}
case "Number":
jsoncard.ID = strings.ToLower(property.Value)
case "PlayRequiredElement":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
requirements[0].Element = property.Value
}
2019-05-21 22:34:21 +00:00
case "PlayRequiredPower":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
requirements[0].Count, _ = strconv.Atoi(property.Value)
}
2019-05-21 22:34:21 +00:00
case "Power":
if property.Value != "" {
2019-05-25 09:32:56 +00:00
power, _ := strconv.Atoi(property.Value)
jsoncard.Power = &power
2019-05-21 22:34:21 +00:00
}
case "ProblemBonus":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
bonus, _ := strconv.Atoi(property.Value)
jsoncard.ProblemBonus = &bonus
}
2019-05-21 22:34:21 +00:00
case "ProblemOpponentPower":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
jsoncard.ProblemOpponentPower, _ = strconv.Atoi(property.Value)
}
2019-05-21 22:34:21 +00:00
case "ProblemPlayerElement1":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
problemreq[0].Element = property.Value
}
2019-05-21 22:34:21 +00:00
case "ProblemPlayerElement1Power":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
problemreq[0].Count, _ = strconv.Atoi(property.Value)
}
2019-05-21 22:34:21 +00:00
case "ProblemPlayerElement2":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
problemreq[1].Element = property.Value
}
2019-05-21 22:34:21 +00:00
case "ProblemPlayerElement2Power":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
problemreq[1].Count, _ = strconv.Atoi(property.Value)
}
2019-05-21 22:34:21 +00:00
case "Rarity":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
jsoncard.Rarity = property.Value
}
2019-05-21 22:34:21 +00:00
case "SecondaryPlayRequiredElement":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
requirements[1].Element = property.Value
}
2019-05-21 22:34:21 +00:00
case "SecondaryPlayRequiredPower":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
requirements[1].Count, _ = strconv.Atoi(property.Value)
}
2019-05-21 22:34:21 +00:00
case "Subname":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
jsoncard.Subname = property.Value
}
2019-05-21 22:34:21 +00:00
case "TertiaryPlayRequiredElement":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
requirements[2].Element = property.Value
}
2019-05-21 22:34:21 +00:00
case "TertiaryPlayRequiredPower":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
requirements[2].Count, _ = strconv.Atoi(property.Value)
}
2019-05-21 22:34:21 +00:00
case "Text":
jsoncard.Text = property.Value
case "Traits":
2019-05-25 09:32:56 +00:00
if property.Value != "" {
jsoncard.Traits = strings.Split(property.Value, ", ")
}
case "TriElement",
"TriPrimaryElement",
"TriSecondaryElement",
"MultiPrimaryElement",
"MultiSecondaryElement":
if property.Value != "" {
jsoncard.Element = append(jsoncard.Element, property.Value)
}
2019-05-21 22:34:21 +00:00
case "Type":
jsoncard.Type = property.Value
}
}
2019-05-25 09:32:56 +00:00
// Fill card requirements
if requirements[0].Element != "" {
jsoncard.Requirement = make(jsonPowerRequirement)
}
for _, req := range requirements {
if req.Element != "" {
jsoncard.Requirement[req.Element] = req.Count
}
2019-05-21 22:34:21 +00:00
}
2019-05-25 09:32:56 +00:00
// Fill problem requirements (this is a bit more tricky)
if problemreq[0].Element != "" {
jsoncard.ProblemRequirement = make(jsonPowerRequirement)
jsoncard.ProblemRequirement[problemreq[0].Element] = problemreq[0].Count
/*
The way OCTGN problems are managed is like this (I think):
- If there are two problem requirements, then both are specified
- If there is one problem requirement and it's "WILD", then
there is just one problem requirement specified (which is colorless)
- If there are two problem requirements, one of which is colorless,
then there is JUST the colored requirement specified, and a
colorless extra requirement is assumed to exist with the same
non-wild requirement amount
*/
if problemreq[0].Element != "Wild" {
if problemreq[1].Element != "" {
jsoncard.ProblemRequirement[problemreq[1].Element] = problemreq[1].Count
} else {
jsoncard.ProblemRequirement["Wild"] = problemreq[0].Count
}
}
}
2019-05-21 22:34:21 +00:00
s.Cards = append(s.Cards, jsoncard)
}
return
}