2019-02-22 15:55:11 +03:00
|
|
|
package areasparser
|
|
|
|
|
|
|
|
import (
|
2021-04-12 14:30:30 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2019-02-22 15:57:33 +03:00
|
|
|
"io/ioutil"
|
2019-02-22 15:55:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type GenericPos struct {
|
|
|
|
X int `json:"x"`
|
|
|
|
Y int `json:"y"`
|
|
|
|
Z int `json:"z"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Area struct {
|
2019-05-02 12:44:16 +03:00
|
|
|
Owner string `json:"owner"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Parent int `json:"parent"`
|
|
|
|
Pos1 *GenericPos `json:"pos1"`
|
|
|
|
Pos2 *GenericPos `json:"pos2"`
|
2019-02-22 15:55:11 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 14:30:30 +03:00
|
|
|
func getInt(o interface{}) int {
|
|
|
|
v, _ := o.(float64)
|
|
|
|
return int(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pos *GenericPos) UnmarshalJSON(data []byte) error {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
err := json.Unmarshal(data, &m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// float-like to int workaround
|
|
|
|
pos.X = getInt(m["x"])
|
|
|
|
pos.Y = getInt(m["y"])
|
|
|
|
pos.Z = getInt(m["z"])
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:55:11 +03:00
|
|
|
func ParseFile(filename string) ([]*Area, error) {
|
2019-02-22 15:57:33 +03:00
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-22 15:55:11 +03:00
|
|
|
|
2019-02-22 15:57:33 +03:00
|
|
|
return Parse(content)
|
2019-02-22 15:55:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(data []byte) ([]*Area, error) {
|
2019-02-22 15:57:33 +03:00
|
|
|
areas := make([]*Area, 0)
|
2021-04-12 14:30:30 +03:00
|
|
|
json.NewDecoder(bytes.NewReader(data)).Decode(&areas)
|
2019-02-22 15:57:33 +03:00
|
|
|
|
|
|
|
return areas, nil
|
2019-02-22 15:55:11 +03:00
|
|
|
}
|