From 804943b5ab8bf6b8339b32f561a4d2b74f146cf8 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 3 Nov 2020 22:30:59 +0000 Subject: [PATCH] random ids i guess, this is bad --- internal/sim/player.go | 2 ++ internal/sim/world.go | 39 ++++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/internal/sim/player.go b/internal/sim/player.go index 27d90b6..7dc2689 100644 --- a/internal/sim/player.go +++ b/internal/sim/player.go @@ -3,6 +3,7 @@ package sim import ( "encoding/json" "fmt" + "math/rand" "time" "github.com/gorilla/websocket" @@ -137,6 +138,7 @@ func (s spawnPlayer) exec(r *room, p *player, seq int) result { if t.here == nil { x, y := n%r.Width, n/r.Width e := entity{ + ID: rand.Intn(9000), Position: math.Vec{x, y}, Glyph: '@', behavior: doNothing{}, diff --git a/internal/sim/world.go b/internal/sim/world.go index f4fd752..29290b2 100644 --- a/internal/sim/world.go +++ b/internal/sim/world.go @@ -68,23 +68,7 @@ func (w *world) run(hz int) { w.register(c) case req := <-w.inbox: - w.Info("read from inbox: %v", req) - - if req.From == "" { - w.Error("request has no from: %v", req) - break - } - - p, ok := w.players[req.From] - if !ok { - w.Error("received non login request of type %T from unknown player %q", req.Wants, req.From) - } - - if p.pending == nil { - p.pending = &req - } else { - p.outbox <- wire.ErrorResponse(req.Seq, "you already have a request for this frame") - } + w.handleRequest(req) case <-ticker.C: w.tick(time.Since(lastTick)) @@ -96,6 +80,27 @@ func (w *world) run(hz int) { } } +func (w *world) handleRequest(req Request) { + w.Info("read from inbox: %v", req) + + if req.From == "" { + w.Error("request has no from: %v", req) + return + } + + p, ok := w.players[req.From] + if !ok { + w.Error("received non login request of type %T from unknown player %q", req.Wants, req.From) + return + } + + if p.pending == nil { + p.pending = &req + } else { + p.outbox <- wire.ErrorResponse(req.Seq, "you already have a request for this frame") + } +} + func (w *world) register(c connect) { w.Info("register: %#v", c.login) foyer := w.rooms["foyer"]