Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cardinal): upstream config, refactor NewWorld, and replace WorldID with namespace #412

Merged
merged 14 commits into from
Nov 13, 2023
17 changes: 0 additions & 17 deletions cardinal/.gitignore

This file was deleted.

208 changes: 0 additions & 208 deletions cardinal/LICENSE

This file was deleted.

22 changes: 14 additions & 8 deletions cardinal/cardinal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ type Foo struct{}
func (Foo) Name() string { return "foo" }

func TestNewWorld(t *testing.T) {
// should fail, this test should generate a compile error if the function signature changes.
_, err := cardinal.NewWorld("", "", cardinal.WithNamespace("testnamespace"))
assert.Assert(t, err != nil)
world, err := cardinal.NewWorld()
assert.NilError(t, err)
assert.Equal(t, string(world.Instance().Namespace()), cardinal.DefaultNamespace)
}

func TestNewWorldWithCustomNamespace(t *testing.T) {
t.Setenv("CARDINAL_NAMESPACE", "custom-namespace")
world, err := cardinal.NewWorld()
assert.NilError(t, err)
assert.Equal(t, string(world.Instance().Namespace()), "custom-namespace")
}

func TestCanQueryInsideSystem(t *testing.T) {
testutils.SetTestTimeout(t, 10*time.Second)

world, doTick := testutils.MakeWorldAndTicker(t, cardinal.WithCORS())
world, doTick := testutils.MakeWorldAndTicker(t)
assert.NilError(t, cardinal.RegisterComponent[Foo](world))
wantNumOfEntities := 10
wCtx := cardinal.TestingWorldToWorldContext(world)
Expand Down Expand Up @@ -59,9 +66,8 @@ func TestShutdownViaSignal(t *testing.T) {
// If this test is frozen then it failed to shut down, create a failure with panic.
var wg sync.WaitGroup
testutils.SetTestTimeout(t, 10*time.Second)
world, err := cardinal.NewMockWorld(cardinal.WithCORS())
world := testutils.NewTestWorld(t)
assert.NilError(t, cardinal.RegisterComponent[Foo](world))
assert.NilError(t, err)
wantNumOfEntities := 10
world.Init(func(worldCtx cardinal.WorldContext) error {
_, err := cardinal.CreateMany(worldCtx, wantNumOfEntities/2, Foo{})
Expand All @@ -72,7 +78,7 @@ func TestShutdownViaSignal(t *testing.T) {
})
wg.Add(1)
go func() {
err = world.StartGame()
err := world.StartGame()
assert.NilError(t, err)
wg.Done()
}()
Expand All @@ -81,7 +87,7 @@ func TestShutdownViaSignal(t *testing.T) {
time.Sleep(500 * time.Millisecond)
}
wCtx := cardinal.TestingWorldToWorldContext(world)
_, err = cardinal.CreateMany(wCtx, wantNumOfEntities/2, Foo{})
_, err := cardinal.CreateMany(wCtx, wantNumOfEntities/2, Foo{})
assert.NilError(t, err)
// test CORS with cardinal
client := &http.Client{}
Expand Down
48 changes: 48 additions & 0 deletions cardinal/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cardinal

import (
"os"

"github.com/rs/zerolog/log"
)

const (
ModeProd = "production"
ModeDev = "development"
DefaultMode = ModeDev
DefaultNamespace = "world-1"
DefaultRedisPassword = ""
)

type WorldConfig struct {
RedisAddress string
RedisPassword string
CardinalNamespace string
CardinalPort string
CardinalMode string
smsunarto marked this conversation as resolved.
Show resolved Hide resolved
}

func GetWorldConfig() WorldConfig {
return WorldConfig{
RedisAddress: getEnv("REDIS_ADDRESS", "localhost:6379"),
RedisPassword: getEnv("REDIS_PASSWORD", DefaultRedisPassword),
CardinalNamespace: getEnv("CARDINAL_NAMESPACE", DefaultNamespace),
CardinalPort: getEnv("CARDINAL_PORT", "4040"),
CardinalMode: getEnv("CARDINAL_MODE", DefaultMode),
}
}

func getEnv(key string, fallback string) string {
value, ok := os.LookupEnv(key)
if ok {
// Validate CARDINAL_DEPLOY_MODE
if key == "CARDINAL_MODE" && value != ModeProd && value != ModeDev {
log.Logger.Warn().
Msg("CARDINAL_DEPLOY_MODE is not set to [production/development]. Defaulting to development mode.")
return ModeDev
}
return value
}

return fallback
}
3 changes: 2 additions & 1 deletion cardinal/ecs/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"github.com/rs/zerolog"
"pkg.world.dev/world-engine/cardinal"
"testing"

"gotest.tools/v3/assert"
Expand All @@ -29,7 +30,7 @@ func newWorldWithRealRedis(t testing.TB) *ecs.World {

sm, err := ecb.NewManager(rs.Client)
assert.NilError(t, err)
world, err := ecs.NewWorld(&rs, sm)
world, err := ecs.NewWorld(&rs, sm, cardinal.DefaultNamespace)

assert.NilError(t, err)
return world
Expand Down
6 changes: 4 additions & 2 deletions cardinal/ecs/chain_recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package ecs_test
import (
"context"
"encoding/binary"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/ecs/message"
"pkg.world.dev/world-engine/cardinal/testutils"
"sort"
"testing"

Expand Down Expand Up @@ -91,7 +93,7 @@ func TestWorld_RecoverFromChain(t *testing.T) {
// setup world and transactions
ctx := context.Background()
adapter := &DummyAdapter{txs: make(map[uint64][]*types.Transaction, 0)}
w := ecs.NewTestWorld(t, ecs.WithAdapter(adapter))
w := testutils.NewTestWorld(t, cardinal.WithAdapter(adapter)).Instance()
sendEnergyTx := ecs.NewMessageType[SendEnergyMsg, SendEnergyResult]("send_energy")
err := w.RegisterMessages(sendEnergyTx)
assert.NilError(t, err)
Expand Down Expand Up @@ -146,7 +148,7 @@ func generateRandomTransaction(t *testing.T, ns string, msg message.Message) *si
func TestWorld_RecoverShouldErrorIfTickExists(t *testing.T) {
ctx := context.Background()
adapter := &DummyAdapter{}
w := ecs.NewTestWorld(t, ecs.WithAdapter(adapter))
w := testutils.NewTestWorld(t, cardinal.WithAdapter(adapter)).Instance()
assert.NilError(t, w.LoadGameState())
assert.NilError(t, w.Tick(ctx))

Expand Down
Loading