Converter should be done!

This commit is contained in:
Hamcha 2019-05-25 11:32:56 +02:00
parent 9bc98676fa
commit 361c1dd59e
Signed by: hamcha
GPG key ID: A40413D21021EAEE
3 changed files with 128 additions and 41 deletions

View file

@ -10,93 +10,176 @@ type jsonSet struct {
Cards []jsonCard 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 { type jsonCard struct {
ID string ID string
Name string Name string
Element string Subname string
Element []string
Keywords []string Keywords []string
Cost int Traits []string
Power int Requirement jsonPowerRequirement `json:",omitempty"`
Cost *int `json:",omitempty"`
Power *int `json:",omitempty"`
Type string Type string
Text 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) { func convert(xmlset xmlSet) (s jsonSet) {
s.Name = xmlset.Name s.Name = xmlset.Name
for _, xmlcard := range xmlset.Cards { for _, xmlcard := range xmlset.Cards {
jsoncard := jsonCard{ 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 { for _, property := range xmlcard.Properties {
switch property.Name { switch property.Name {
case "Cost": case "Cost":
if property.Value != "" { if property.Value != "" {
jsoncard.Cost, _ = strconv.Atoi(property.Value) cost, _ := strconv.Atoi(property.Value)
jsoncard.Cost = &cost
} }
case "Element": case "Element":
if property.Value != "" { if property.Value != "" && property.Value != "Multicolor" {
jsoncard.Element = property.Value jsoncard.Element = append(jsoncard.Element, property.Value)
} }
case "Keywords": case "Keywords":
if property.Value != "" { if property.Value != "" {
jsoncard.Keywords = strings.Split(strings.TrimRight(property.Value, "."), ", ") jsoncard.Keywords = strings.Split(strings.TrimRight(property.Value, "."), ", ")
} }
case "MultiPrimaryElement":
//todo
case "MultiSecondaryElement":
//todo
case "Number": case "Number":
jsoncard.ID = strings.ToLower(property.Value) jsoncard.ID = strings.ToLower(property.Value)
case "PlayRequiredElement": case "PlayRequiredElement":
//todo if property.Value != "" {
requirements[0].Element = property.Value
}
case "PlayRequiredPower": case "PlayRequiredPower":
//todo if property.Value != "" {
requirements[0].Count, _ = strconv.Atoi(property.Value)
}
case "Power": case "Power":
if property.Value != "" { if property.Value != "" {
jsoncard.Cost, _ = strconv.Atoi(property.Value) power, _ := strconv.Atoi(property.Value)
jsoncard.Power = &power
} }
case "ProblemBonus": case "ProblemBonus":
//todo if property.Value != "" {
bonus, _ := strconv.Atoi(property.Value)
jsoncard.ProblemBonus = &bonus
}
case "ProblemOpponentPower": case "ProblemOpponentPower":
//todo if property.Value != "" {
jsoncard.ProblemOpponentPower, _ = strconv.Atoi(property.Value)
}
case "ProblemPlayerElement1": case "ProblemPlayerElement1":
//todo if property.Value != "" {
problemreq[0].Element = property.Value
}
case "ProblemPlayerElement1Power": case "ProblemPlayerElement1Power":
//todo if property.Value != "" {
problemreq[0].Count, _ = strconv.Atoi(property.Value)
}
case "ProblemPlayerElement2": case "ProblemPlayerElement2":
//todo if property.Value != "" {
problemreq[1].Element = property.Value
}
case "ProblemPlayerElement2Power": case "ProblemPlayerElement2Power":
//todo if property.Value != "" {
problemreq[1].Count, _ = strconv.Atoi(property.Value)
}
case "Rarity": case "Rarity":
//todo if property.Value != "" {
jsoncard.Rarity = property.Value
}
case "SecondaryPlayRequiredElement": case "SecondaryPlayRequiredElement":
//todo if property.Value != "" {
requirements[1].Element = property.Value
}
case "SecondaryPlayRequiredPower": case "SecondaryPlayRequiredPower":
//todo if property.Value != "" {
requirements[1].Count, _ = strconv.Atoi(property.Value)
}
case "Subname": case "Subname":
//todo if property.Value != "" {
jsoncard.Subname = property.Value
}
case "TertiaryPlayRequiredElement": case "TertiaryPlayRequiredElement":
//todo if property.Value != "" {
requirements[2].Element = property.Value
}
case "TertiaryPlayRequiredPower": case "TertiaryPlayRequiredPower":
//todo if property.Value != "" {
requirements[2].Count, _ = strconv.Atoi(property.Value)
}
case "Text": case "Text":
jsoncard.Text = property.Value jsoncard.Text = property.Value
case "Traits": case "Traits":
//todo if property.Value != "" {
case "TriElement": jsoncard.Traits = strings.Split(property.Value, ", ")
//todo }
case "TriPrimaryElement": case "TriElement",
//todo "TriPrimaryElement",
case "TriSecondaryElement": "TriSecondaryElement",
//todo "MultiPrimaryElement",
"MultiSecondaryElement":
if property.Value != "" {
jsoncard.Element = append(jsoncard.Element, property.Value)
}
case "Type": case "Type":
jsoncard.Type = property.Value jsoncard.Type = property.Value
} }
} }
// Don't let empty arrays be null
if jsoncard.Keywords == nil { // Fill card requirements
jsoncard.Keywords = []string{} 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) s.Cards = append(s.Cards, jsoncard)
} }
return return

View file

@ -1 +1,3 @@
module git.fromouter.space/mcg/mlp-server-tools/tools/convertsets module git.fromouter.space/mcg/mlp-server-tools/tools/convertsets
require golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862

2
tools/convertsets/go.sum Normal file
View file

@ -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=