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) 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 var req wire.Request
if err := json.Unmarshal(b, &req); err != nil { if err := json.Unmarshal(b, &req); err != nil {
sn.Error("unable to parse request: %v", err) 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 break
} }
sn.Info("received message of type %T", req.Body) sn.Info("received message of type %T", req.Body)

@ -12,7 +12,7 @@ type player struct {
*room *room
name string name string
outbox chan wire.Response outbox chan wire.Response
pending []Request pending *Request
avatar *entity avatar *entity
} }
@ -70,14 +70,13 @@ func (s *SpawnPlayer) exec(r *room, p *player, seq int) result {
behavior: doNothing{}, behavior: doNothing{},
} }
p := &player{ p := &player{
Log: r.Log.Child("players").Child(s.Name), Log: r.Log.Child("players").Child(s.Name),
room: r, room: r,
name: s.Name, name: s.Name,
outbox: s.Outbox, outbox: s.Outbox,
pending: make([]Request, 0, 32), avatar: avatar,
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.players[s.Name] = p
r.tiles[0].here = p.avatar r.tiles[0].here = p.avatar
s.queued = true s.queued = true

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

@ -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) 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: case <-ticker.C:
w.tick(time.Since(lastTick)) w.tick(time.Since(lastTick))

Loading…
Cancel
Save