Skip to content

Commit

Permalink
chore: clean up code/comments (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallyty committed Aug 9, 2023
1 parent 4d17c6e commit 5c9ca2d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 152 deletions.
45 changes: 20 additions & 25 deletions cardinal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
33 changes: 23 additions & 10 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand All @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions cardinal/utils/common.go

This file was deleted.

69 changes: 0 additions & 69 deletions cardinal/utils/decimal.go

This file was deleted.

19 changes: 0 additions & 19 deletions cardinal/utils/loop.go

This file was deleted.

17 changes: 0 additions & 17 deletions cardinal/utils/math.go

This file was deleted.

6 changes: 3 additions & 3 deletions cardinal/utils/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5c9ca2d

Please sign in to comment.