1
0
forked from MTSR/mapserver
mapserver/areasparser/parser.go

68 lines
1.2 KiB
Go
Raw Normal View History

2019-02-22 15:55:11 +03:00
package areasparser
import (
2019-02-22 15:57:33 +03:00
"io/ioutil"
"mapserver/luaparser"
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
}
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
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)
2019-05-02 12:44:16 +03:00
if entry["parent"] != nil {
2019-05-02 20:10:53 +03:00
a.Parent = entry["parent"].(int)
2019-05-02 12:44:16 +03:00
}
2019-02-22 15:57:33 +03:00
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)
}
return areas, nil
2019-02-22 15:55:11 +03:00
}