diff --git a/tools/convertsets/convert.go b/tools/convertsets/convert.go index 54ad5d4..b8535bb 100644 --- a/tools/convertsets/convert.go +++ b/tools/convertsets/convert.go @@ -10,93 +10,176 @@ type jsonSet struct { Cards []jsonCard } +// Some fields are in pointer type because they must not omitted +// if their value is 0, but should be omitted if empty type jsonCard struct { - ID string - Name string - Element string - Keywords []string - Cost int - Power int - Type string - Text string + 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"` } +type jsonPowerRequirement map[string]int + func convert(xmlset xmlSet) (s jsonSet) { s.Name = xmlset.Name for _, xmlcard := range xmlset.Cards { jsoncard := jsonCard{ - Name: xmlcard.Name, + Name: xmlcard.Name, + Keywords: []string{}, + Traits: []string{}, + Element: []string{}, } + + type reqUnit struct { + Element string + Count int + } + requirements := make([]reqUnit, 3) + problemreq := make([]reqUnit, 2) + for _, property := range xmlcard.Properties { switch property.Name { case "Cost": if property.Value != "" { - jsoncard.Cost, _ = strconv.Atoi(property.Value) + cost, _ := strconv.Atoi(property.Value) + jsoncard.Cost = &cost } case "Element": - if property.Value != "" { - jsoncard.Element = property.Value + if property.Value != "" && property.Value != "Multicolor" { + jsoncard.Element = append(jsoncard.Element, property.Value) } case "Keywords": if property.Value != "" { jsoncard.Keywords = strings.Split(strings.TrimRight(property.Value, "."), ", ") } - case "MultiPrimaryElement": - //todo - case "MultiSecondaryElement": - //todo case "Number": jsoncard.ID = strings.ToLower(property.Value) case "PlayRequiredElement": - //todo + if property.Value != "" { + requirements[0].Element = property.Value + } case "PlayRequiredPower": - //todo + if property.Value != "" { + requirements[0].Count, _ = strconv.Atoi(property.Value) + } case "Power": if property.Value != "" { - jsoncard.Cost, _ = strconv.Atoi(property.Value) + power, _ := strconv.Atoi(property.Value) + jsoncard.Power = &power } case "ProblemBonus": - //todo + if property.Value != "" { + bonus, _ := strconv.Atoi(property.Value) + jsoncard.ProblemBonus = &bonus + } case "ProblemOpponentPower": - //todo + if property.Value != "" { + jsoncard.ProblemOpponentPower, _ = strconv.Atoi(property.Value) + } case "ProblemPlayerElement1": - //todo + if property.Value != "" { + problemreq[0].Element = property.Value + } case "ProblemPlayerElement1Power": - //todo + if property.Value != "" { + problemreq[0].Count, _ = strconv.Atoi(property.Value) + } case "ProblemPlayerElement2": - //todo + if property.Value != "" { + problemreq[1].Element = property.Value + } case "ProblemPlayerElement2Power": - //todo + if property.Value != "" { + problemreq[1].Count, _ = strconv.Atoi(property.Value) + } case "Rarity": - //todo + if property.Value != "" { + jsoncard.Rarity = property.Value + } case "SecondaryPlayRequiredElement": - //todo + if property.Value != "" { + requirements[1].Element = property.Value + } case "SecondaryPlayRequiredPower": - //todo + if property.Value != "" { + requirements[1].Count, _ = strconv.Atoi(property.Value) + } case "Subname": - //todo + if property.Value != "" { + jsoncard.Subname = property.Value + } case "TertiaryPlayRequiredElement": - //todo + if property.Value != "" { + requirements[2].Element = property.Value + } case "TertiaryPlayRequiredPower": - //todo + if property.Value != "" { + requirements[2].Count, _ = strconv.Atoi(property.Value) + } case "Text": jsoncard.Text = property.Value case "Traits": - //todo - case "TriElement": - //todo - case "TriPrimaryElement": - //todo - case "TriSecondaryElement": - //todo + 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) + } case "Type": jsoncard.Type = property.Value } } - // Don't let empty arrays be null - if jsoncard.Keywords == nil { - jsoncard.Keywords = []string{} + + // Fill card requirements + if requirements[0].Element != "" { + jsoncard.Requirement = make(jsonPowerRequirement) } + for _, req := range requirements { + if req.Element != "" { + jsoncard.Requirement[req.Element] = req.Count + } + } + + // 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 + } + } + } + s.Cards = append(s.Cards, jsoncard) } return diff --git a/tools/convertsets/go.mod b/tools/convertsets/go.mod index 9f9d1b5..b3c8fa0 100644 --- a/tools/convertsets/go.mod +++ b/tools/convertsets/go.mod @@ -1 +1,3 @@ module git.fromouter.space/mcg/mlp-server-tools/tools/convertsets + +require golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862 diff --git a/tools/convertsets/go.sum b/tools/convertsets/go.sum new file mode 100644 index 0000000..80d86f4 --- /dev/null +++ b/tools/convertsets/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862 h1:rM0ROo5vb9AdYJi1110yjWGMej9ITfKddS89P3Fkhug= +golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=