Skip to content

Commit

Permalink
feat(starter-game-template) added pretty logging when run under 'mage…
Browse files Browse the repository at this point in the history
… dev' (#8)

* added pretty logging when run under 'mage dev'

* user external pretty log option.

* user external pretty log option.
  • Loading branch information
pyrofolium authored Sep 6, 2023
1 parent 0512367 commit 798474d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
7 changes: 5 additions & 2 deletions cardinal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@ type Config struct {
Mode string
RedisAddr string
RedisPass string
DeployMode string
}

func GetConfig() Config {
mode := os.Getenv("REDIS_MODE")
redisAddr := os.Getenv("REDIS_ADDR")
redisPassword := os.Getenv("REDIS_PASSWORD")
cardinalPort := os.Getenv("CARDINAL_PORT")
deployMode := os.Getenv("DEPLOY_MODE")

return Config{
CardinalPort: cardinalPort,
Mode: mode,
RedisAddr: redisAddr,
RedisPass: redisPassword,
DeployMode: deployMode,
}
}

func NewWorld(cfg Config) *ecs.World {
if cfg.Mode == "normal" {
return utils.NewWorld(cfg.RedisAddr, cfg.RedisPass)
return utils.NewWorld(cfg.RedisAddr, cfg.RedisPass, cfg.DeployMode)
}
return utils.NewEmbeddedWorld()
return utils.NewEmbeddedWorld(cfg.DeployMode)
}
2 changes: 1 addition & 1 deletion cardinal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ require (
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
pkg.world.dev/world-engine/cardinal v0.1.32-alpha
pkg.world.dev/world-engine/cardinal v0.1.33-alpha
pkg.world.dev/world-engine/chain v0.1.11-alpha // indirect
pkg.world.dev/world-engine/sign v0.1.7-alpha // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions cardinal/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1903,8 +1903,8 @@ nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
pkg.world.dev/world-engine/cardinal v0.1.32-alpha h1:wAYvdz/Z/Jp2vNZnjkDBD9KFKrMkGKvatOGZmwQP+6k=
pkg.world.dev/world-engine/cardinal v0.1.32-alpha/go.mod h1:cGVvoACq1bWWy13te3BRJW7+SE5MK4hUYa+0K/69imU=
pkg.world.dev/world-engine/cardinal v0.1.33-alpha h1:lSqsYIz+JsHCiZozqvlmEc+XOjB5ZIY5vH93JWne6wg=
pkg.world.dev/world-engine/cardinal v0.1.33-alpha/go.mod h1:cGVvoACq1bWWy13te3BRJW7+SE5MK4hUYa+0K/69imU=
pkg.world.dev/world-engine/chain v0.1.11-alpha h1:a+a+eZUIG2XuO+PL5WBr+IgQZoGRnm8plk5bP1KKoR4=
pkg.world.dev/world-engine/chain v0.1.11-alpha/go.mod h1:qpm1QXHj2RyXIiwkEolaZMMqeNVcMX+hH4OQ9nE0/5M=
pkg.world.dev/world-engine/sign v0.1.7-alpha h1:7Oaoc0OUBP0fAPGna+/fw5fbrNw88sIVzr0SujY0wgQ=
Expand Down
17 changes: 13 additions & 4 deletions cardinal/utils/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package utils

import (
"github.com/rs/zerolog/log"

"pkg.world.dev/world-engine/cardinal/ecs"
"pkg.world.dev/world-engine/cardinal/ecs/inmem"
"pkg.world.dev/world-engine/cardinal/ecs/storage"
)

// NewWorld is the recommended way to run the game
func NewWorld(addr string, password string) *ecs.World {
func NewWorld(addr string, password string, deployMode string) *ecs.World {
options := make([]ecs.Option, 0)
log.Log().Msg("Running in normal mode, using external Redis")
if addr == "" {
log.Log().Msg("Redis address is not set, using fallback - localhost:6379")
Expand All @@ -18,14 +20,17 @@ func NewWorld(addr string, password string) *ecs.World {
log.Log().Msg("Redis password is not set, make sure to set up redis with password in prod")
password = ""
}
if deployMode == "development" {
options = append(options, ecs.WithPrettyLog())
}

rs := storage.NewRedisStorage(storage.Options{
Addr: addr,
Password: password, // make sure to set this in prod
DB: 0, // use default DB
}, "world")
worldStorage := storage.NewWorldStorage(&rs)
world, err := ecs.NewWorld(worldStorage)
world, err := ecs.NewWorld(worldStorage, options...)
if err != nil {
panic(err)
}
Expand All @@ -37,7 +42,11 @@ func NewWorld(addr string, password string) *ecs.World {
// 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 {
func NewEmbeddedWorld(deployMode string) *ecs.World {
log.Log().Msg("Running in embedded mode, using embedded miniredis")
return inmem.NewECSWorld()
options := make([]ecs.Option, 0)
if deployMode == "development" {
options = append(options, ecs.WithPrettyLog())
}
return inmem.NewECSWorld(options...)
}
5 changes: 3 additions & 2 deletions magefiles/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package main

import (
"fmt"
"os"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"os"
)

// Check verifies that various prerequisites are installed or configured on your machine
Expand All @@ -22,7 +23,6 @@ func Clear() error {
return nil
}


// Test runs the test suite
func Test() error {
mg.Deps(exitMagefilesDir)
Expand Down Expand Up @@ -125,6 +125,7 @@ func Dev() error {
os.Setenv("REDIS_MODE", "normal")
os.Setenv("CARDINAL_PORT", "3333")
os.Setenv("REDIS_ADDR", "localhost:6379")
os.Setenv("DEPLOY_MODE", "development")

// Run redis in a docker container because Miniredis doesn't work with Retool
// NOTE: this is because it doesn't implement CLIENT
Expand Down

0 comments on commit 798474d

Please sign in to comment.