You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
998 B
Go
70 lines
998 B
Go
|
2 weeks ago
|
package core
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bufio"
|
||
|
|
"context"
|
||
|
|
"log/slog"
|
||
|
|
"os"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/EngoEngine/ecs"
|
||
|
|
)
|
||
|
|
|
||
|
|
type App struct {
|
||
|
|
W *ecs.World
|
||
|
|
L *slog.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func New(level string) *App {
|
||
|
|
var logLevel slog.Level
|
||
|
|
switch level {
|
||
|
|
case "INFO", "info":
|
||
|
|
logLevel = slog.LevelInfo
|
||
|
|
case "DEBUG", "debug":
|
||
|
|
logLevel = slog.LevelDebug
|
||
|
|
case "ERROR", "error":
|
||
|
|
logLevel = slog.LevelError
|
||
|
|
}
|
||
|
|
|
||
|
|
l := slog.New(slog.NewTextHandler(
|
||
|
|
os.Stderr, &slog.HandlerOptions{
|
||
|
|
Level: logLevel,
|
||
|
|
},
|
||
|
|
),
|
||
|
|
)
|
||
|
|
|
||
|
|
world := &ecs.World{}
|
||
|
|
|
||
|
|
return &App{
|
||
|
|
L: l,
|
||
|
|
W: world,
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *App) Run(ctx context.Context) {
|
||
|
|
go func() {
|
||
|
|
t := time.NewTicker(time.Millisecond * 30)
|
||
|
|
for range t.C {
|
||
|
|
a.W.Update(1)
|
||
|
|
}
|
||
|
|
}()
|
||
|
|
|
||
|
|
scanner := bufio.NewScanner(os.Stdin)
|
||
|
|
var line string
|
||
|
|
for {
|
||
|
|
select {
|
||
|
|
case <-ctx.Done():
|
||
|
|
a.L.Error("cancelled")
|
||
|
|
return
|
||
|
|
default:
|
||
|
|
if scanner.Scan() {
|
||
|
|
line = scanner.Text()
|
||
|
|
}
|
||
|
|
a.L.Info("scanning input")
|
||
|
|
a.L.With(slog.String("input", line)).Info("received command")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|