only one pending request per player per game

master
Jordan Orelli 4 years ago
parent 5dcf50446e
commit bd46b57315

@ -24,3 +24,21 @@ func (v *Vec) UnmarshalJSON(b []byte) error {
}
func (v Vec) Add(v2 Vec) Vec { return Vec{v.X + v2.X, v.Y + v2.Y} }
func (v Vec) Unit() Vec {
var out Vec
switch {
case v.X < 0:
out.X = -1
case v.X > 0:
out.X = 1
}
switch {
case v.Y < 0:
out.Y = -1
case v.Y > 0:
out.Y = 1
}
return out
}

@ -86,7 +86,7 @@ func (sn *session) read() {
var req wire.Request
if err := json.Unmarshal(b, &req); err != nil {
sn.Error("unable to parse request: %v", err)
sn.outbox <- wire.ErrorResponse(0, "unable to parse request: %v", err)
sn.outbox <- wire.ErrorResponse(req.Seq, "unable to parse request: %v", err)
break
}
sn.Info("received message of type %T", req.Body)

@ -12,7 +12,7 @@ type player struct {
*room
name string
outbox chan wire.Response
pending []Request
pending *Request
avatar *entity
}
@ -74,10 +74,9 @@ func (s *SpawnPlayer) exec(r *room, p *player, seq int) result {
room: r,
name: s.Name,
outbox: s.Outbox,
pending: make([]Request, 0, 32),
avatar: avatar,
}
p.pending = append(p.pending, Request{Seq: seq, From: s.Name, Wants: s})
p.pending = &Request{Seq: seq, From: s.Name, Wants: s}
r.players[s.Name] = p
r.tiles[0].here = p.avatar
s.queued = true

@ -19,7 +19,12 @@ type room struct {
func (r *room) update(dt time.Duration) {
for _, p := range r.players {
for _, req := range p.pending {
if p.pending == nil {
continue
}
req := p.pending
p.pending = nil
res := req.Wants.exec(r, p, req.Seq)
p.outbox <- wire.Response{Re: req.Seq, Body: res.reply}
if res.announce != nil {
@ -31,8 +36,6 @@ func (r *room) update(dt time.Duration) {
}
}
}
p.pending = p.pending[0:0]
}
for _, t := range r.tiles {
if t.here != nil {

@ -78,7 +78,11 @@ func (w *World) Run(hz int) {
w.Error("received non login request of type %T from unknown player %q", req.Wants, req.From)
}
p.pending = append(p.pending, req)
if p.pending == nil {
p.pending = &req
} else {
p.outbox <- wire.ErrorResponse(req.Seq, "you already have a request for this frame")
}
case <-ticker.C:
w.tick(time.Since(lastTick))

Loading…
Cancel
Save