mlp-server-tools/tools/convertsets/convert.go
Hamcha a3db545be3
Some checks failed
continuous-integration/drone/push Build is failing
Update convertset to add boosted manes
2019-08-27 11:59:07 +02:00

198 lines
5.5 KiB
Go

package main
import (
"strconv"
"strings"
)
type jsonSet struct {
Name string
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
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"`
Boosted *jsonCard `json:",omitempty"`
}
type jsonPowerRequirement map[string]int
func convert(xmlset xmlSet) (s jsonSet) {
s.Name = xmlset.Name
for _, xmlcard := range xmlset.Cards {
jsoncard := decodeCard(xmlcard)
if jsoncard.Type == "Mane Character" && xmlcard.Alternate != nil {
boosted := decodeCard(*xmlcard.Alternate)
jsoncard.Boosted = &boosted
}
s.Cards = append(s.Cards, jsoncard)
}
return
}
func decodeCard(xmlcard xmlCard) jsonCard {
jsoncard := jsonCard{
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 != "" {
cost, _ := strconv.Atoi(property.Value)
jsoncard.Cost = &cost
}
case "Element":
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 "Number":
jsoncard.ID = strings.ToLower(property.Value)
case "PlayRequiredElement":
if property.Value != "" {
requirements[0].Element = property.Value
}
case "PlayRequiredPower":
if property.Value != "" {
requirements[0].Count, _ = strconv.Atoi(property.Value)
}
case "Power":
if property.Value != "" {
power, _ := strconv.Atoi(property.Value)
jsoncard.Power = &power
}
case "ProblemBonus":
if property.Value != "" {
bonus, _ := strconv.Atoi(property.Value)
jsoncard.ProblemBonus = &bonus
}
case "ProblemOpponentPower":
if property.Value != "" {
jsoncard.ProblemOpponentPower, _ = strconv.Atoi(property.Value)
}
case "ProblemPlayerElement1":
if property.Value != "" {
problemreq[0].Element = property.Value
}
case "ProblemPlayerElement1Power":
if property.Value != "" {
problemreq[0].Count, _ = strconv.Atoi(property.Value)
}
case "ProblemPlayerElement2":
if property.Value != "" {
problemreq[1].Element = property.Value
}
case "ProblemPlayerElement2Power":
if property.Value != "" {
problemreq[1].Count, _ = strconv.Atoi(property.Value)
}
case "Rarity":
if property.Value != "" {
jsoncard.Rarity = property.Value
}
case "SecondaryPlayRequiredElement":
if property.Value != "" {
requirements[1].Element = property.Value
}
case "SecondaryPlayRequiredPower":
if property.Value != "" {
requirements[1].Count, _ = strconv.Atoi(property.Value)
}
case "Subname":
if property.Value != "" {
jsoncard.Subname = property.Value
}
case "TertiaryPlayRequiredElement":
if property.Value != "" {
requirements[2].Element = property.Value
}
case "TertiaryPlayRequiredPower":
if property.Value != "" {
requirements[2].Count, _ = strconv.Atoi(property.Value)
}
case "Text":
jsoncard.Text = property.Value
case "Traits":
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
}
}
// 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
}
}
}
return jsoncard
}