From 7baaec5581ab510667f776e8b5b412e2c8d7f8ef Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Wed, 31 Jul 2019 11:36:30 +0200 Subject: [PATCH] blockaccessor.GetBlock(x,y,z) and simple test --- blockaccessor/blockaccessor.go | 43 +++++++++++++++++++++ blockaccessor/blockaccessor_test.go | 59 +++++++++++++++++++++++++++++ coords/mapblockcoords.go | 12 ++++++ coords/mapblockcoords_test.go | 26 +++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 blockaccessor/blockaccessor.go create mode 100644 blockaccessor/blockaccessor_test.go create mode 100644 coords/mapblockcoords_test.go diff --git a/blockaccessor/blockaccessor.go b/blockaccessor/blockaccessor.go new file mode 100644 index 0000000..52575d1 --- /dev/null +++ b/blockaccessor/blockaccessor.go @@ -0,0 +1,43 @@ +package blockaccessor + +import ( + "mapserver/coords" + "mapserver/mapblockaccessor" +) + +func New(mba *mapblockaccessor.MapBlockAccessor) *BlockAccessor { + return &BlockAccessor{mba: mba} +} + +type BlockAccessor struct { + mba *mapblockaccessor.MapBlockAccessor +} + +type Block struct { + Name string + //TODO: param1, param2 +} + +func (this *BlockAccessor) GetBlock(x, y, z int) (*Block, error) { + + mbc := coords.NewMapBlockCoordsFromBlock(x, y, z) + mapblock, err := this.mba.GetMapBlock(mbc) + + if err != nil { + return nil, err + } + + if mapblock == nil { + return nil, nil + } + + relx := x % 16 + rely := y % 16 + relz := z % 16 + + block := Block{ + Name: mapblock.GetNodeName(relx, rely, relz), + } + + return &block, nil +} diff --git a/blockaccessor/blockaccessor_test.go b/blockaccessor/blockaccessor_test.go new file mode 100644 index 0000000..28557f0 --- /dev/null +++ b/blockaccessor/blockaccessor_test.go @@ -0,0 +1,59 @@ +package blockaccessor + +import ( + "fmt" + "io/ioutil" + "mapserver/db/sqlite" + "mapserver/mapblockaccessor" + "mapserver/testutils" + "os" + "testing" + "time" + + "github.com/sirupsen/logrus" +) + +func TestSimpleAccess(t *testing.T) { + logrus.SetLevel(logrus.DebugLevel) + + tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite") + if err != nil { + panic(err) + } + defer os.Remove(tmpfile.Name()) + testutils.CreateTestDatabase(tmpfile.Name()) + + a, err := sqlite.New(tmpfile.Name()) + if err != nil { + panic(err) + } + + err = a.Migrate() + if err != nil { + panic(err) + } + + mba := mapblockaccessor.NewMapBlockAccessor(a, 500*time.Millisecond, 1000*time.Millisecond, 1000) + + if mba == nil { + t.Fatal("Mapblockaccessor is nil") + } + + ba := New(mba) + + if ba == nil { + t.Fatal("blockaccessor is nil") + } + + block, err := ba.GetBlock(0, 2, 0) + + if err != nil { + panic(err) + } + + if block == nil { + t.Fatal("block is nil") + } + + fmt.Println(block.Name) +} diff --git a/coords/mapblockcoords.go b/coords/mapblockcoords.go index e83b50e..85c2b88 100644 --- a/coords/mapblockcoords.go +++ b/coords/mapblockcoords.go @@ -1,5 +1,9 @@ package coords +import ( + "math" +) + type MapBlockCoords struct { X int `json:"x"` Y int `json:"y"` @@ -10,6 +14,14 @@ func NewMapBlockCoords(x, y, z int) *MapBlockCoords { return &MapBlockCoords{X: x, Y: y, Z: z} } +func NewMapBlockCoordsFromBlock(x, y, z int) *MapBlockCoords { + return &MapBlockCoords{ + X: int(math.Floor(float64(x) / 16)), + Y: int(math.Floor(float64(y) / 16)), + Z: int(math.Floor(float64(z) / 16)), + } +} + type MapBlockRange struct { Pos1, Pos2 *MapBlockCoords } diff --git a/coords/mapblockcoords_test.go b/coords/mapblockcoords_test.go new file mode 100644 index 0000000..e2c8933 --- /dev/null +++ b/coords/mapblockcoords_test.go @@ -0,0 +1,26 @@ +package coords + +import ( + "testing" +) + +func TestNewMapBlockCoordsFromBlock(t *testing.T) { + c := NewMapBlockCoordsFromBlock(1, 1, 1) + + if c.X != 0 || c.Y != 0 || c.Z != 0 { + t.Fatal("mismatch", c) + } + + c = NewMapBlockCoordsFromBlock(16, 1, 1) + + if c.X != 1 || c.Y != 0 || c.Z != 0 { + t.Fatal("mismatch", c) + } + + c = NewMapBlockCoordsFromBlock(16, 1, -1) + + if c.X != 1 || c.Y != 0 || c.Z != -1 { + t.Fatal("mismatch", c) + } + +}