1
0
forked from MTSR/mapserver

areas in json format

This commit is contained in:
BuckarooBanzay 2021-04-12 13:30:30 +02:00
parent 2a0e93f1ba
commit 8766d72d63
4 changed files with 357 additions and 50 deletions

View File

@ -1,8 +1,9 @@
package areasparser
import (
"bytes"
"encoding/json"
"io/ioutil"
"mapserver/luaparser"
)
type GenericPos struct {
@ -19,6 +20,26 @@ type Area struct {
Pos2 *GenericPos `json:"pos2"`
}
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
}
func ParseFile(filename string) ([]*Area, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
@ -29,39 +50,8 @@ func ParseFile(filename string) ([]*Area, error) {
}
func Parse(data []byte) ([]*Area, error) {
p := luaparser.New()
areas := make([]*Area, 0)
list, err := p.ParseList(string(data[:]))
if err != nil {
return nil, err
}
for _, entry := range list {
a := Area{}
a.Name = entry["name"].(string)
a.Owner = entry["owner"].(string)
if entry["parent"] != nil {
a.Parent = entry["parent"].(int)
}
p1 := GenericPos{}
pos1 := entry["pos1"].(map[string]interface{})
p1.X = pos1["x"].(int)
p1.Y = pos1["y"].(int)
p1.Z = pos1["z"].(int)
a.Pos1 = &p1
p2 := GenericPos{}
pos2 := entry["pos2"].(map[string]interface{})
p2.X = pos2["x"].(int)
p2.Y = pos2["y"].(int)
p2.Z = pos2["z"].(int)
a.Pos2 = &p2
areas = append(areas, &a)
}
json.NewDecoder(bytes.NewReader(data)).Decode(&areas)
return areas, nil
}

View File

@ -1,25 +1,20 @@
package areasparser
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
a, err := ParseFile("testdata/areas.json")
assert.NoError(t, err)
assert.True(t, len(a) > 1)
a, err := ParseFile("testdata/areas.dat")
if err != nil {
t.Fatal(err)
}
j, err := json.MarshalIndent(a, "", " ")
if err != nil {
t.Fatal(err)
}
fmt.Println(string(j[:]))
area := a[0]
assert.Equal(t, "ilai_house", area.Name)
assert.Equal(t, "ilai", area.Owner)
assert.NotNil(t, area.Pos1)
assert.NotNil(t, area.Pos2)
assert.Equal(t, 4970, area.Pos1.X)
}

File diff suppressed because one or more lines are too long

323
areasparser/testdata/areas.json vendored Normal file
View File

@ -0,0 +1,323 @@
[
{
"name": "ilai_house",
"owner": "ilai",
"pos1": {
"x": 4970.0,
"y": 8.0,
"z": 88.0
},
"pos2": {
"x": 4983.0,
"y": 16.0,
"z": 99.0
}
},
{
"name": "temple",
"owner": "Lukc",
"pos1": {
"x": -1911.0,
"y": 18.0,
"z": -221.0
},
"pos2": {
"x": -1718.0,
"y": 68.0,
"z": -50.0
}
},
{
"name": "temple",
"owner": "BuckarooBanzai",
"parent": 2.0,
"pos1": {
"x": -1911.0,
"y": 18.0,
"z": -221.0
},
"pos2": {
"x": -1718.0,
"y": 68.0,
"z": -50.0
}
},
{
"name": "Spaceshipyard",
"owner": "Lukc",
"pos1": {
"x": -1784.0,
"y": -49.0,
"z": -1174.0
},
"pos2": {
"x": -1688.0,
"y": 58.0,
"z": -1068.0
}
},
{
"name": "Arena",
"owner": "Lukc",
"pos1": {
"x": -1860.0,
"y": -369.0,
"z": -1162.0
},
"pos2": {
"x": -1751.0,
"y": -310.0,
"z": -1061.0
}
},
{
"name": "Spaceshipyard",
"owner": "T4im",
"parent": 4.0,
"pos1": {
"x": -1784.0,
"y": -49.0,
"z": -1174.0
},
"pos2": {
"x": -1688.0,
"y": 58.0,
"z": -1068.0
}
},
{
"name": "temple",
"owner": "T4im",
"parent": 2.0,
"pos1": {
"x": -1911.0,
"y": 18.0,
"z": -221.0
},
"pos2": {
"x": -1718.0,
"y": 68.0,
"z": -50.0
}
},
{
"name": "Spaceshipyard",
"owner": "BuckarooBanzai",
"parent": 4.0,
"pos1": {
"x": -1784.0,
"y": -49.0,
"z": -1174.0
},
"pos2": {
"x": -1688.0,
"y": 58.0,
"z": -1068.0
}
},
{
"name": "Arena",
"owner": "T4im",
"parent": 5.0,
"pos1": {
"x": -1860.0,
"y": -369.0,
"z": -1162.0
},
"pos2": {
"x": -1751.0,
"y": -310.0,
"z": -1061.0
}
},
{
"name": "Humboldt Research Station",
"owner": "T4im",
"pos1": {
"x": -624.0,
"y": -32.0,
"z": 48.0
},
"pos2": {
"x": -369.0,
"y": 127.0,
"z": 351.0
}
},
{
"name": "centrifuge cascade",
"owner": "pipo",
"pos1": {
"x": -2995.0,
"y": -40.0,
"z": -3003.0
},
"pos2": {
"x": -2958.0,
"y": -26.0,
"z": -2990.0
}
},
{
"name": "centrifuge cascade",
"owner": "barsik",
"parent": 11.0,
"pos1": {
"x": -2995.0,
"y": -40.0,
"z": -3003.0
},
"pos2": {
"x": -2958.0,
"y": -26.0,
"z": -2990.0
}
},
{
"name": "centrifuge cascade",
"owner": "BuckarooBanzai",
"parent": 11.0,
"pos1": {
"x": -2995.0,
"y": -40.0,
"z": -3003.0
},
"pos2": {
"x": -2958.0,
"y": -26.0,
"z": -2990.0
}
},
{
"name": "centrifuge cascade",
"owner": "T4im",
"parent": 11.0,
"pos1": {
"x": -2995.0,
"y": -40.0,
"z": -3003.0
},
"pos2": {
"x": -2958.0,
"y": -26.0,
"z": -2990.0
}
},
{
"name": "centrifuge cascade",
"owner": "Lukc",
"parent": 11.0,
"pos1": {
"x": -2995.0,
"y": -40.0,
"z": -3003.0
},
"pos2": {
"x": -2958.0,
"y": -26.0,
"z": -2990.0
}
},
{
"name": "Spaceshipyard",
"owner": "pipo",
"parent": 6.0,
"pos1": {
"x": -1784.0,
"y": -49.0,
"z": -1174.0
},
"pos2": {
"x": -1688.0,
"y": 58.0,
"z": -1068.0
}
},
{
"name": "Emerald extention",
"owner": "Emerald",
"pos1": {
"x": 5055.0,
"y": 8.0,
"z": 48.0
},
"pos2": {
"x": 5059.0,
"y": 10.0,
"z": 54.0
}
},
{
"name": "tower1",
"owner": "pipo",
"parent": 19.0,
"pos1": {
"x": -14.0,
"y": -126.0,
"z": 587.0
},
"pos2": {
"x": 17.0,
"y": 103.0,
"z": 617.0
}
},
{
"name": "tower1",
"owner": "barsik",
"pos1": {
"x": -14.0,
"y": -126.0,
"z": 587.0
},
"pos2": {
"x": 17.0,
"y": 103.0,
"z": 617.0
}
},
{
"name": "tower1",
"owner": "BuckarooBanzai",
"parent": 19.0,
"pos1": {
"x": -14.0,
"y": -126.0,
"z": 587.0
},
"pos2": {
"x": 17.0,
"y": 103.0,
"z": 617.0
}
},
{
"name": "tower1",
"owner": "T4im",
"parent": 19.0,
"pos1": {
"x": -14.0,
"y": -126.0,
"z": 587.0
},
"pos2": {
"x": 17.0,
"y": 103.0,
"z": 617.0
}
},
{
"name": "Arboretum",
"owner": "pipo",
"pos1": {
"x": -1734.0,
"y": 7.0,
"z": 697.0
},
"pos2": {
"x": -1604.0,
"y": 50.0,
"z": 784.0
}
}
]