done for the night

master
Jordan Orelli 5 years ago
parent a59076aefd
commit f0c159e4fc

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

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

@ -41,6 +41,26 @@ type collectSoul struct {
}
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 {

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
@ -15,6 +16,7 @@ type server struct {
router *mux.Router
players map[int]*player
join chan player
leave chan *player
inbox chan message
souls map[string]soul
}
@ -63,6 +65,17 @@ func (s *server) step(pc int) {
p.Log = s.Child("players").Child(strconv.Itoa(p.id))
go p.run()
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:
s.Info("received message: %v", m)
var req request

Loading…
Cancel
Save