blockaccessor.GetBlock(x,y,z) and simple test

This commit is contained in:
NatureFreshMilk 2019-07-31 11:36:30 +02:00
parent c608e058d5
commit 7baaec5581
4 changed files with 140 additions and 0 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -1,5 +1,9 @@
package coords package coords
import (
"math"
)
type MapBlockCoords struct { type MapBlockCoords struct {
X int `json:"x"` X int `json:"x"`
Y int `json:"y"` Y int `json:"y"`
@ -10,6 +14,14 @@ func NewMapBlockCoords(x, y, z int) *MapBlockCoords {
return &MapBlockCoords{X: x, Y: y, Z: z} 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 { type MapBlockRange struct {
Pos1, Pos2 *MapBlockCoords Pos1, Pos2 *MapBlockCoords
} }

View File

@ -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)
}
}