Wire the world through to session creation

main
Nick Dumas 1 week ago
parent a75e6d7eec
commit 08e4039518

@ -19,6 +19,8 @@ type AppConfig struct {
Port string Port string
TickInterval time.Duration TickInterval time.Duration
World *ecs.World
} }
type App struct { type App struct {
@ -46,6 +48,7 @@ func New(ac AppConfig) *App {
) )
world := &ecs.World{} world := &ecs.World{}
ac.World = world
return &App{ return &App{
L: l, L: l,
@ -63,7 +66,7 @@ func (a *App) Run(ctx context.Context) {
} }
}() }()
s := NewServer(ctx, a.L, a.Config.Port) s := NewServer(ctx, a.L, a.Config.Port, world)
go s.Start() go s.Start()
for { for {

@ -5,6 +5,8 @@ import (
"log/slog" "log/slog"
"net" "net"
"time" "time"
"github.com/EngoEngine/ecs"
) )
type Server struct { type Server struct {
@ -13,9 +15,10 @@ type Server struct {
ctx context.Context ctx context.Context
Sessions chan *Session Sessions chan *Session
ln net.Listener ln net.Listener
w *ecs.World
} }
func NewServer(ctx context.Context, l *slog.Logger, port string) *Server { func NewServer(ctx context.Context, l *slog.Logger, port string, w *ecs.World) *Server {
var s Server var s Server
c := make(chan *Session) c := make(chan *Session)
@ -41,6 +44,7 @@ func NewServer(ctx context.Context, l *slog.Logger, port string) *Server {
s.ln = ln s.ln = ln
s.l = l s.l = l
s.port = port s.port = port
s.w = w
return &s return &s
} }

@ -7,6 +7,7 @@ import (
"log/slog" "log/slog"
"net" "net"
"github.com/EngoEngine/ecs"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -16,15 +17,23 @@ type Session struct {
p Printer p Printer
l *slog.Logger l *slog.Logger
userID uint64 w *ecs.World
sessionID uuid.UUID
userID uint64
} }
func NewSession(ctx context.Context, c net.Conn, p Printer, l *slog.Logger) *Session { func NewSession(ctx context.Context, c net.Conn, p Printer, l *slog.Logger, w *ecs.World) *Session {
sid := uuid.New() sid := uuid.New()
return &Session{ return &Session{
ctx: ctx, ctx: ctx,
l: l.With(slog.String("sid", sid.String())),
c: c, l: l.With(slog.String("sid", sid.String())),
c: c,
w: w,
p: p,
sessionID: sid,
} }
} }

Loading…
Cancel
Save