diff --git a/core/schemaclient.go b/core/schemaclient.go index 3109f5a9..f9521110 100644 --- a/core/schemaclient.go +++ b/core/schemaclient.go @@ -33,13 +33,15 @@ type SchemaClient struct { stats *SchemaClientStats } -func NewSchemaClient(opts *SchemaClientOpts) *SchemaClient { - origin := mt.NewPos(opts.Pull.PosX, opts.Pull.PosY, opts.Pull.PosZ) +func NewSchemaClient(opts *SchemaClientOpts) (*SchemaClient, error) { + origin, err := types.ParsePos(opts.Pull.Origin) + if err != nil { + return nil, fmt.Errorf("errors parsing origin: %v", err) + } + size := mt.NewPos(opts.Schema.SizeX, opts.Schema.SizeY, opts.Schema.SizeZ) pos2 := origin.Add(size.Add(mt.NewPos(-1, -1, -1))) - fmt.Printf("Origin: %v, pos2: %v, size: %v\n", origin, pos2, size) - return &SchemaClient{ opts: opts, origin: origin, @@ -51,7 +53,7 @@ func NewSchemaClient(opts *SchemaClientOpts) *SchemaClient { CONTENT_UNKNOWN: "unknown", }, stats: &SchemaClientStats{}, - } + }, nil } func (sc *SchemaClient) Stats() *SchemaClientStats { diff --git a/core/schemaclient_test.go b/core/schemaclient_test.go index c662d656..b99cc85c 100644 --- a/core/schemaclient_test.go +++ b/core/schemaclient_test.go @@ -21,9 +21,7 @@ func TestSchemaClient(t *testing.T) { Pull: &types.SchematicPull{ Hostname: "127.0.0.1", Port: 30000, - PosX: 0, - PosY: 100, - PosZ: 0, + Origin: "0,100,0", }, PullClient: &types.SchematicPullClient{ Username: "test", @@ -46,9 +44,12 @@ func TestSchemaClient(t *testing.T) { }, } - sc := core.NewSchemaClient(opts) - err := sc.Run(time.Second * 5) + sc, err := core.NewSchemaClient(opts) assert.NoError(t, err) + + err = sc.Run(time.Second * 5) + assert.NoError(t, err) + fmt.Printf("Stats: %v\n", sc.Stats()) t.FailNow() diff --git a/db/migrations/18_schema_pull.up.sql b/db/migrations/18_schema_pull.up.sql index 3a446211..c40f5204 100644 --- a/db/migrations/18_schema_pull.up.sql +++ b/db/migrations/18_schema_pull.up.sql @@ -2,8 +2,7 @@ create table schema_pull( schema_uid uuid primary key not null references public.schema(uid) on delete cascade, enabled boolean not null, - pos1 varchar(32) not null, - pos2 varchar(32) not null, + origin varchar(32) not null, interval int not null, next_run bigint not null default 0, hostname varchar(128) not null, diff --git a/types/pos.go b/types/pos.go new file mode 100644 index 00000000..31b284e2 --- /dev/null +++ b/types/pos.go @@ -0,0 +1,34 @@ +package types + +import ( + "fmt" + "strconv" + "strings" + + mt "github.com/minetest-go/types" +) + +// parses a comma separated string to a pos object +func ParsePos(pos string) (*mt.Pos, error) { + parts := strings.Split(pos, ",") + if len(parts) != 3 { + return nil, fmt.Errorf("invalid part-count: %d, should be 3", len(parts)) + } + + x, err := strconv.ParseInt(strings.TrimSpace(parts[0]), 10, 32) + if err != nil { + return nil, fmt.Errorf("error parsing x part: %v", err) + } + + y, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 32) + if err != nil { + return nil, fmt.Errorf("error parsing y part: %v", err) + } + + z, err := strconv.ParseInt(strings.TrimSpace(parts[2]), 10, 32) + if err != nil { + return nil, fmt.Errorf("error parsing z part: %v", err) + } + + return mt.NewPos(int(x), int(y), int(z)), nil +} diff --git a/types/pos_test.go b/types/pos_test.go new file mode 100644 index 00000000..bfbcef8e --- /dev/null +++ b/types/pos_test.go @@ -0,0 +1,48 @@ +package types_test + +import ( + "blockexchange/types" + "testing" + + mt "github.com/minetest-go/types" + + "github.com/stretchr/testify/assert" +) + +func TestParsePos(t *testing.T) { + pos, err := types.ParsePos("1,2,3") + assert.NoError(t, err) + assert.Equal(t, mt.NewPos(1, 2, 3), pos) + + pos, err = types.ParsePos("-1,-2,-3") + assert.NoError(t, err) + assert.Equal(t, mt.NewPos(-1, -2, -3), pos) + + pos, err = types.ParsePos("0,0,0") + assert.NoError(t, err) + assert.Equal(t, mt.NewPos(0, 0, 0), pos) + + pos, err = types.ParsePos("1 , 2 , 03") + assert.NoError(t, err) + assert.Equal(t, mt.NewPos(1, 2, 3), pos) + + pos, err = types.ParsePos("1,2,3,4") + assert.Error(t, err) + assert.Nil(t, pos) + + pos, err = types.ParsePos("") + assert.Error(t, err) + assert.Nil(t, pos) + + pos, err = types.ParsePos("1,2,") + assert.Error(t, err) + assert.Nil(t, pos) + + pos, err = types.ParsePos("garbage") + assert.Error(t, err) + assert.Nil(t, pos) + + pos, err = types.ParsePos("garbage,x,y") + assert.Error(t, err) + assert.Nil(t, pos) +} diff --git a/types/schemapull.go b/types/schemapull.go index 2ac33547..2f2072d3 100644 --- a/types/schemapull.go +++ b/types/schemapull.go @@ -4,8 +4,7 @@ package types type SchematicPull struct { SchemaUID string `json:"schema_uid" gorm:"primarykey;column:schema_uid"` Enabled bool `json:"enabled" gorm:"column:enabled"` - Pos1 string `json:"pos1" gorm:"column:pos1"` - Pos2 string `json:"pos2" gorm:"column:pos2"` + Origin string `json:"origin" gorm:"column:origin"` Interval int64 `json:"interval" gorm:"column:interval"` // interval in seconds NextRun int64 `json:"next_run" gorm:"column:next_run"` // time.Now().UnixMilli() Hostname string `json:"hostname" gorm:"column:hostname"`