Skip to content

Commit

Permalink
pos parse util, origin only
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Dec 20, 2024
1 parent 8353bd7 commit 2dcb953
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
12 changes: 7 additions & 5 deletions core/schemaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -51,7 +53,7 @@ func NewSchemaClient(opts *SchemaClientOpts) *SchemaClient {
CONTENT_UNKNOWN: "unknown",
},
stats: &SchemaClientStats{},
}
}, nil
}

func (sc *SchemaClient) Stats() *SchemaClientStats {
Expand Down
11 changes: 6 additions & 5 deletions core/schemaclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()

Expand Down
3 changes: 1 addition & 2 deletions db/migrations/18_schema_pull.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 34 additions & 0 deletions types/pos.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 48 additions & 0 deletions types/pos_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions types/schemapull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 2dcb953

Please sign in to comment.