area parser

This commit is contained in:
NatureFreshMilk 2019-02-22 13:55:11 +01:00
parent ddd0213629
commit 534b09649d
5 changed files with 179 additions and 23 deletions

View File

@ -0,0 +1,63 @@
package areasparser
import (
"io/ioutil"
"mapserver/luaparser"
)
type GenericPos struct {
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z"`
}
type Area struct {
Owner string `json:"owner"`
Name string `json:"name"`
Pos1 *GenericPos `json:"pos1"`
Pos2 *GenericPos `json:"pos2"`
}
func ParseFile(filename string) ([]*Area, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return Parse(content)
}
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)
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
}

View File

@ -0,0 +1,26 @@
package areasparser
import (
"testing"
"fmt"
"encoding/json"
)
func TestParse(t *testing.T){
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[:]))
}

1
server/areasparser/testdata/areas.dat vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -18,6 +18,65 @@ type LuaParser struct {
state *lua.LState
}
func parseMap(t *lua.LTable) map[string]interface{} {
result := make(map[string]interface{})
t.ForEach(func(k1, v1 lua.LValue) {
boolValue, ok := v1.(lua.LBool)
if ok {
result[k1.String()] = boolValue == lua.LTrue
}
intValue, ok := v1.(lua.LNumber)
if ok {
result[k1.String()], _ = strconv.Atoi(intValue.String())
}
strValue, ok := v1.(lua.LString)
if ok {
result[k1.String()] = strValue.String()
}
tblValue, ok := v1.(*lua.LTable)
if ok {
result[k1.String()] = parseMap(tblValue)
}
})
return result
}
func (this *LuaParser) ParseList(expr string) ([]map[string]interface{}, error){
result := make([]map[string]interface{}, 0)
err := this.state.DoString(expr)
if err != nil {
return result, err
}
lv := this.state.Get(-1)
tbl, ok := lv.(*lua.LTable)
if !ok {
return result, errors.New("parsing failed")
}
tbl.ForEach(func(k, v lua.LValue) {
key, ok := v.(*lua.LTable)
if !ok {
return
}
mapresult := parseMap(key)
result = append(result, mapresult)
})
return result, nil
}
func (this *LuaParser) ParseMap(expr string) (map[string]interface{}, error) {
result := make(map[string]interface{})
@ -33,26 +92,5 @@ func (this *LuaParser) ParseMap(expr string) (map[string]interface{}, error) {
return result, errors.New("parsing failed")
}
tbl.ForEach(func(k, v lua.LValue) {
key, ok := k.(lua.LString)
if !ok {
return
}
boolValue, ok := v.(lua.LBool)
if ok {
result[key.String()] = boolValue == lua.LTrue
}
intValue, ok := v.(lua.LNumber)
if ok {
result[key.String()], _ = strconv.Atoi(intValue.String())
}
strValue, ok := v.(lua.LString)
if ok {
result[key.String()] = strValue.String()
}
})
return result, nil
return parseMap(tbl), nil
}

View File

@ -5,7 +5,35 @@ import (
"testing"
)
func TestParse(t *testing.T) {
func TestParseList(t *testing.T) {
p := New()
m, err := p.ParseList(`return {{["x"]=1},{["y"]=2}}`)
if err != nil {
panic(err)
}
fmt.Println(m)
if len(m) != 2 {
t.Fatalf("wrong length: %d", len(m))
}
v1 := m[0]
fmt.Println(v1)
if v1["x"].(int) != 1 {
t.Fatal("[0][x] does not match")
}
v2 := m[1]
if v2["y"].(int) != 2 {
t.Fatal("[1][y] does not match")
}
}
func TestParseMap(t *testing.T) {
p := New()
m, err := p.ParseMap(`return {a=1, b=true, c="abc"}`)