diff --git a/cardinal/config.go b/cardinal/config.go index 685ecf6..0f78c3d 100644 --- a/cardinal/config.go +++ b/cardinal/config.go @@ -3,38 +3,33 @@ package main import ( "github.com/argus-labs/starter-game-template/cardinal/utils" "github.com/argus-labs/world-engine/cardinal/ecs" - "github.com/rs/zerolog/log" "os" ) type Config struct { CardinalPort string - World *ecs.World + Mode string + RedisAddr string + RedisPass string } -var ( - EnvRedisMode = os.Getenv("REDIS_MODE") - EnvRedisAddr = os.Getenv("REDIS_ADDR") - EnvRedisPassword = os.Getenv("REDIS_PASSWORD") - EnvCardinalPort = os.Getenv("CARDINAL_PORT") -) - func GetConfig() Config { - if EnvRedisMode == "normal" { - return Config{ - CardinalPort: EnvCardinalPort, - World: utils.NewWorld(EnvRedisAddr, EnvRedisPassword), - } - } else if EnvRedisMode == "embedded" { - return Config{ - CardinalPort: EnvCardinalPort, - World: utils.NewEmbeddedWorld(), - } - } else { - log.Log().Msg("REDIS_MODE is not set, using fallback - embedded") - return Config{ - CardinalPort: EnvCardinalPort, - World: utils.NewEmbeddedWorld(), - } + mode := os.Getenv("REDIS_MODE") + redisAddr := os.Getenv("REDIS_ADDR") + redisPassword := os.Getenv("REDIS_PASSWORD") + cardinalPort := os.Getenv("CARDINAL_PORT") + + return Config{ + CardinalPort: cardinalPort, + Mode: mode, + RedisAddr: redisAddr, + RedisPass: redisPassword, + } +} + +func NewWorld(cfg Config) *ecs.World { + if cfg.Mode == "normal" { + utils.NewWorld(cfg.RedisAddr, cfg.RedisPass) } + return utils.NewEmbeddedWorld() } diff --git a/cardinal/main.go b/cardinal/main.go index f6def9a..b0c7647 100644 --- a/cardinal/main.go +++ b/cardinal/main.go @@ -6,9 +6,9 @@ import ( "github.com/argus-labs/starter-game-template/cardinal/read" "github.com/argus-labs/starter-game-template/cardinal/system" "github.com/argus-labs/starter-game-template/cardinal/tx" - "github.com/argus-labs/starter-game-template/cardinal/utils" "github.com/argus-labs/world-engine/cardinal/server" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "time" ) @@ -19,32 +19,41 @@ func main() { // TODO: In production, you should set DEPLOY_MODE=production // and set REDIS_ADDR and REDIS_PASSWORD to use a real Redis instance. - // Otherwise, by default cardinal will run using an in-memory "miniredis" - world := cfg.World + // Otherwise, by default cardinal will run using an in-memory redis. + world := NewWorld(cfg) // Register components // NOTE: You must register your components here, // otherwise it will show an error when you try to use them in a system. - utils.Must(world.RegisterComponents( + err := world.RegisterComponents( component.Player, component.Health, - )) + ) + if err != nil { + log.Fatal().Err(err) + } // Register transactions // NOTE: You must register your transactions here, // otherwise it will show an error when you try to use them in a system. - utils.Must(world.RegisterTransactions( + err = world.RegisterTransactions( tx.CreatePlayer, tx.AttackPlayer, - )) + ) + if err != nil { + log.Fatal().Err(err) + } // Register read endpoints // NOTE: You must register your read endpoints here, // otherwise it will not be accessible. - utils.Must(world.RegisterReads( + err = world.RegisterReads( read.Archetype, read.Constant, - )) + ) + if err != nil { + log.Fatal().Err(err) + } // Each system executes deterministically in the order they are added. // This is a neat feature that can be strategically used for systems that depends on the order of execution. @@ -55,7 +64,11 @@ func main() { world.AddSystem(system.PlayerSpawnerSystem) // Load game state - utils.Must(world.LoadGameState()) + err = world.LoadGameState() + if err != nil { + log.Fatal().Err(err) + } + world.StartGameLoop(context.Background(), time.Second) // TODO: When launching to production, you should enable signature verification. diff --git a/cardinal/utils/common.go b/cardinal/utils/common.go deleted file mode 100644 index cc2087f..0000000 --- a/cardinal/utils/common.go +++ /dev/null @@ -1,9 +0,0 @@ -package utils - -import "github.com/rs/zerolog/log" - -func Must(err error) { - if err != nil { - log.Fatal().Err(err) - } -} diff --git a/cardinal/utils/decimal.go b/cardinal/utils/decimal.go deleted file mode 100644 index 3a5c498..0000000 --- a/cardinal/utils/decimal.go +++ /dev/null @@ -1,69 +0,0 @@ -package utils - -import ( - "math/big" - - "github.com/ericlagergren/decimal" -) - -var ( - DecCtx = decimal.Context64 -) - -func IntToDec(i int) *decimal.Big { - return decimal.New(int64(i), 0) -} - -func DecToInt(dec *decimal.Big) int { - bigInt := new(big.Int) - dec.Int(bigInt) - return int(bigInt.Int64()) -} - -func StrToDec(str string) *decimal.Big { - dec := new(decimal.Big) - DecCtx.SetString(dec, str) - return dec -} - -func Uint16ToDec(i uint16) *decimal.Big { - return decimal.New(int64(i), 0) -} - -func DecToStr(dec *decimal.Big) string { - return dec.String() -} - -func LessThan(a, b *decimal.Big) bool { - return a.Cmp(b) == -1 -} - -func LessThanOrEqual(a, b *decimal.Big) bool { - return a.Cmp(b) <= 0 -} - -func GreaterThan(a, b *decimal.Big) bool { - return a.Cmp(b) == 1 -} - -func GreaterThanOrEqual(a, b *decimal.Big) bool { - return a.Cmp(b) >= 0 -} - -func Equal(a, b *decimal.Big) bool { - return a.Cmp(b) == 0 -} - -func DecMax(a, b *decimal.Big) *decimal.Big { - if GreaterThan(a, b) { - return a - } - return b -} - -func DecMin(a, b *decimal.Big) *decimal.Big { - if LessThan(a, b) { - return a - } - return b -} diff --git a/cardinal/utils/loop.go b/cardinal/utils/loop.go deleted file mode 100644 index e2f6af5..0000000 --- a/cardinal/utils/loop.go +++ /dev/null @@ -1,19 +0,0 @@ -package utils - -import ( - "context" - "time" - - "github.com/rs/zerolog/log" - - "github.com/argus-labs/world-engine/cardinal/ecs" -) - -func GameLoop(world *ecs.World) { - log.Info().Msg("Game loop started") - for range time.Tick(time.Second) { - if err := world.Tick(context.Background()); err != nil { - panic(err) - } - } -} diff --git a/cardinal/utils/math.go b/cardinal/utils/math.go deleted file mode 100644 index 6dceec5..0000000 --- a/cardinal/utils/math.go +++ /dev/null @@ -1,17 +0,0 @@ -package utils - -import "golang.org/x/exp/constraints" - -func Min[T constraints.Ordered](a, b T) T { - if a < b { - return a - } - return b -} - -func Max[T constraints.Ordered](a, b T) T { - if a > b { - return a - } - return b -} diff --git a/cardinal/utils/world.go b/cardinal/utils/world.go index 8cd2e35..5fff09c 100644 --- a/cardinal/utils/world.go +++ b/cardinal/utils/world.go @@ -34,9 +34,9 @@ func NewWorld(addr string, password string) *ecs.World { } // NewEmbeddedWorld is the most convenient way to run the game -// because it doesn't require spinning up Redis in a container -// it runs a Redis server as a part of the Go process -// However, it will not work with Cardinal Editor. +// because it doesn't require spinning up Redis in a container. +// It runs a Redis server as a part of the Go process. +// NOTE: worlds with embedded redis are incompatible with Cardinal Editor. func NewEmbeddedWorld() *ecs.World { log.Log().Msg("Running in embedded mode, using embedded miniredis") return inmem.NewECSWorld()