done for the night

master
Jordan Orelli 5 years ago
parent a59076aefd
commit f0c159e4fc

@ -16,6 +16,7 @@ func main() {
s := server{ s := server{
Log: log, Log: log,
join: make(chan player), join: make(chan player),
leave: make(chan *player),
inbox: make(chan message), inbox: make(chan message),
souls: make(map[string]soul), souls: make(map[string]soul),
} }

@ -26,6 +26,7 @@ func (p *player) run() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
go p.writeMessages(ctx) go p.writeMessages(ctx)
p.readMessages(cancel) p.readMessages(cancel)
p.server.leave <- p
p.outbox = nil p.outbox = nil
p.conn.Close() p.conn.Close()
} }
@ -72,7 +73,6 @@ func (p *player) writeMessages(ctx context.Context) {
select { select {
case t := <-ticker.C: case t := <-ticker.C:
n++ n++
p.Info("trying to write a tick")
w, err := p.conn.NextWriter(websocket.TextMessage) w, err := p.conn.NextWriter(websocket.TextMessage)
if err != nil { if err != nil {
p.Error("error getting writer: %v", err) p.Error("error getting writer: %v", err)

@ -41,6 +41,26 @@ type collectSoul struct {
} }
func (c *collectSoul) exec(s *server, from *player) { func (c *collectSoul) exec(s *server, from *player) {
soul, ok := s.souls[c.PlayerName]
if !ok {
return
}
delete(s.souls, c.PlayerName)
b, err := json.Marshal(soul)
if err != nil {
s.Error("unable to serialize soul: %v", err)
return
}
msg := fmt.Sprintf("soul-collected %s", string(b))
for _, player := range s.players {
select {
case player.outbox <- msg:
default:
s.Error("can't write to player %s's outbox", player.username)
}
}
} }
type login struct { type login struct {

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"strconv" "strconv"
@ -15,6 +16,7 @@ type server struct {
router *mux.Router router *mux.Router
players map[int]*player players map[int]*player
join chan player join chan player
leave chan *player
inbox chan message inbox chan message
souls map[string]soul souls map[string]soul
} }
@ -63,6 +65,17 @@ func (s *server) step(pc int) {
p.Log = s.Child("players").Child(strconv.Itoa(p.id)) p.Log = s.Child("players").Child(strconv.Itoa(p.id))
go p.run() go p.run()
s.players[p.id] = &p s.players[p.id] = &p
for _, soul := range s.souls {
b, _ := json.Marshal(soul)
msg := fmt.Sprintf("spawn-soul %s", string(b))
select {
case p.outbox <- msg:
default:
}
}
case p := <-s.leave:
delete(s.players, p.id)
case m := <-s.inbox: case m := <-s.inbox:
s.Info("received message: %v", m) s.Info("received message: %v", m)
var req request var req request

Loading…
Cancel
Save