Skip to content

Commit

Permalink
Merge branch 'json-to-yaml'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Thomas committed Feb 12, 2016
2 parents b79b7af + 342f4ff commit 9f0abdc
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 59 deletions.
6 changes: 3 additions & 3 deletions background.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

// Background represents an element providing traits to a character.
type Background struct {
Type string `json:"type"`
Name string `json:"name"`
Upgrades []string `json:"upgrades"`
Type string `yaml:"type"`
Name string `yaml:"name"`
Upgrades []string `yaml:"upgrades"`
}

// Apply changes the character's trait according to the history values
Expand Down
8 changes: 4 additions & 4 deletions characteristic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

// Characteristic is is a character's trait which holds a value.
type Characteristic struct {
Name string `json:"name"`
Aptitudes []Aptitude `json:"aptitudes"`
Tier int `json:"tier"`
Value int `json:"-"`
Name string `yaml:"name"`
Aptitudes []Aptitude `yaml:"aptitudes"`
Tier int `yaml:"tier"`
Value int `yaml:"-"`
}

// Cost returns the cost of a standard characteristic upgrade given the character's aptitudes and the characteristic current tier.
Expand Down
25 changes: 13 additions & 12 deletions cost_matrix.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main

import (
"encoding/json"
"strconv"

"gopkg.in/yaml.v2"
)

// CostMatrix is matrix of upgrade costs.
type CostMatrix map[string]map[int]map[int]int

// costMatrixJSON is the Json representation of the matrix.
type costMatrixJSON map[string]map[string]map[string]int
// costMatrixYAML is the Json representation of the matrix.
type costMatrixYAML map[string]map[string]map[string]int

// Price returns the cost in the matrix corresponding to the given type, matches and tier.
func (c CostMatrix) Price(typ string, matches int, tier int) (int, error) {
Expand Down Expand Up @@ -37,11 +38,11 @@ func (c CostMatrix) Price(typ string, matches int, tier int) (int, error) {
return cost, nil
}

// MarshalJSON return the JSON representation of the cost matrix.
// MarshalYAML return the YAML representation of the cost matrix.
// Implements the Marshaller interface.
func (c *CostMatrix) MarshalJSON() ([]byte, error) {
func (c *CostMatrix) MarshalYAML() ([]byte, error) {

jMatrix := costMatrixJSON{}
jMatrix := costMatrixYAML{}

// For each upgrade type of the matrix.
for typ, matches := range *c {
Expand All @@ -66,22 +67,22 @@ func (c *CostMatrix) MarshalJSON() ([]byte, error) {
}
}

// Marshal the transient structure to the returned JSON object.
raw, err := json.Marshal(jMatrix)
// Marshal the transient structure to the returned YAML object.
raw, err := yaml.Marshal(jMatrix)
if err != nil {
return nil, err
}

return raw, nil
}

// UnmarshalJSON parse the JSON representation of a cost matrix.
// UnmarshalYAML parse the YAML representation of a cost matrix.
// Implements the Unmarshaller interface.
func (c *CostMatrix) UnmarshalJSON(raw []byte) error {
func (c *CostMatrix) UnmarshalYAML(raw []byte) error {

// Unmarshal to the transient structure.
jMatrix := costMatrixJSON{}
err := json.Unmarshal(raw, &jMatrix)
jMatrix := costMatrixYAML{}
err := yaml.Unmarshal(raw, &jMatrix)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
// and the type of attributes that generally aren't bought by spending
// experience points.
type Gauge struct {
Name string `json:"name"`
Value int `json:"-"`
XP int `json:"xp"`
Name string `yaml:"name"`
Value int `yaml:"-"`
XP int `yaml:"xp"`
}

// Cost returns 0, a gauge has no calculated cost.
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
cli.StringFlag{
Name: "universe, u",
Usage: "The filepath to the character universe.",
Value: "universe.json",
Value: "universe.yaml",
},
}

Expand Down
6 changes: 3 additions & 3 deletions meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

// Meta is a header and a collection of associated options.
type Meta struct {
Label string `json:"label"`
Options []string `json:"options"`
Line int `json:"-"`
Label string `yaml:"label"`
Options []string `yaml:"options"`
Line int `yaml:"-"`
}

// NewMeta returns a meta with name and options given the label.
Expand Down
4 changes: 2 additions & 2 deletions rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

// Rule represent a special rule, which are generally home-made additions to the
type Rule struct {
Name string `json:"name"`
Description string `json:"description"`
Name string `yaml:"name"`
Description string `yaml:"description"`
}

// Cost returns 0, a rule has no calculated cost.
Expand Down
8 changes: 4 additions & 4 deletions skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (

// Skill is a character's trait.
type Skill struct {
Name string `json:"name"`
Aptitudes []Aptitude `json:"aptitudes"`
Tier int `json:"tier"`
Speciality string `json:"-"`
Name string `yaml:"name"`
Aptitudes []Aptitude `yaml:"aptitudes"`
Tier int `yaml:"tier"`
Speciality string `yaml:"-"`
}

// Cost returns the cost of the skill given the character's aptitudes and the current tier.
Expand Down
16 changes: 8 additions & 8 deletions spell.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"encoding/json"
import (
"gopkg.in/yaml.v2"
)

// Spell castable.
type Spell struct {
Name string `json:"name"`
Description string `json:"description"`
XP int `json:"xp"`
Name string `yaml:"name"`
Description string `yaml:"description"`
XP int `yaml:"xp"`
Attributes map[string]interface{}
}

Expand All @@ -34,9 +34,9 @@ func (s Spell) Apply(character *Character, upgrade Upgrade) error {
return nil
}

// UnmarshalJSON implements the Unmarshaler interface
func (s *Spell) UnmarshalJSON(raw []byte) error {
err := json.Unmarshal(raw, &s.Attributes)
// UnmarshalYAML implements the Unmarshaler interface
func (s *Spell) UnmarshalYAML(raw []byte) error {
err := yaml.Unmarshal(raw, &s.Attributes)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions talent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import "fmt"

// Talent is a character's trait.
type Talent struct {
Name string `json:"name"`
Description string `json:"description"`
Aptitudes []Aptitude `json:"aptitudes"`
Tier int `json:"tier"`
Requirements []Requirement `json:"requirements"`
Speciality string `json:"-"`
Value int `json:"-"`
Stackable bool `json:"stackable"`
Name string `yaml:"name"`
Description string `yaml:"description"`
Aptitudes []Aptitude `yaml:"aptitudes"`
Tier int `yaml:"tier"`
Requirements []Requirement `yaml:"requirements"`
Speciality string `yaml:"-"`
Value int `yaml:"-"`
Stackable bool `yaml:"stackable"`
}

// Cost returns the cost of the talent given the character's aptitudes and the current tier.
Expand Down
23 changes: 12 additions & 11 deletions universe.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package main

import (
"encoding/json"
"io"
"io/ioutil"
"strconv"
"strings"

"gopkg.in/yaml.v2"
)

// Universe represents a set of configuration, often refered as data or database.
type Universe struct {
Backgrounds map[string][]Background `json:"backgrounds"`
Aptitudes []Aptitude `json:"aptitudes"`
Characteristics []Characteristic `json:"characteristics"`
Gauges []Gauge `json:"gauges"`
Skills []Skill `json:"skills"`
Talents []Talent `json:"talents"`
Spells []Spell `json:"spells"`
Costs CostMatrix `json:"costs"`
Backgrounds map[string][]Background `yaml:"backgrounds"`
Aptitudes []Aptitude `yaml:"aptitudes"`
Characteristics []Characteristic `yaml:"characteristics"`
Gauges []Gauge `yaml:"gauges"`
Skills []Skill `yaml:"skills"`
Talents []Talent `yaml:"talents"`
Spells []Spell `yaml:"spells"`
Costs CostMatrix `yaml:"costs"`
}

// ParseUniverse load an from a plain JSON file.
// ParseUniverse load an from a plain YAML file.
// It returns a well-formed universe that describe all the components of a game setting.
func ParseUniverse(file io.Reader) (Universe, error) {

Expand All @@ -30,7 +31,7 @@ func ParseUniverse(file io.Reader) (Universe, error) {
return Universe{}, err
}
universe := Universe{}
err = json.Unmarshal(raw, &universe)
err = yaml.Unmarshal(raw, &universe)
if err != nil {
return Universe{}, err
}
Expand Down

0 comments on commit 9f0abdc

Please sign in to comment.