shotgun/server.go

110 lines
2.6 KiB
Go
Raw Normal View History

2024-01-22 20:16:10 -06:00
package main
import (
"context"
"errors"
2024-01-27 13:51:20 -06:00
"log/slog"
2024-01-22 20:16:10 -06:00
"net/http"
"sync"
"time"
"nhooyr.io/websocket"
2024-01-27 13:24:42 -06:00
"nhooyr.io/websocket/wsjson"
2024-01-22 20:16:10 -06:00
2024-01-27 13:24:42 -06:00
"git.sunturtle.xyz/studio/shotgun/game"
2024-01-22 20:16:10 -06:00
"git.sunturtle.xyz/studio/shotgun/lobby"
"git.sunturtle.xyz/studio/shotgun/player"
"git.sunturtle.xyz/studio/shotgun/serve"
)
type Server struct {
2024-01-27 13:24:42 -06:00
l *lobby.Lobby
2024-01-22 20:16:10 -06:00
mu sync.Mutex
2024-01-27 13:24:42 -06:00
pp map[player.ID]*websocket.Conn
2024-01-22 20:16:10 -06:00
}
type person struct {
conn *websocket.Conn
id player.ID
}
2024-01-27 13:24:42 -06:00
func (s *Server) person(p player.ID) person {
2024-01-22 20:16:10 -06:00
s.mu.Lock()
defer s.mu.Unlock()
2024-01-27 13:24:42 -06:00
return person{conn: s.pp[p], id: p}
2024-01-22 20:16:10 -06:00
}
2024-01-27 13:24:42 -06:00
func (s *Server) accept(w http.ResponseWriter, r *http.Request, p player.ID) (person, error) {
conn, err := websocket.Accept(w, r, nil)
if err != nil {
return person{}, err
}
2024-01-27 13:51:20 -06:00
slog.Debug("upgraded", "player", p)
2024-01-22 20:16:10 -06:00
s.mu.Lock()
defer s.mu.Unlock()
2024-01-27 13:24:42 -06:00
s.pp[p] = conn
return person{conn: conn, id: p}, nil
2024-01-22 20:16:10 -06:00
}
2024-01-27 13:24:42 -06:00
func (s *Server) left(p player.ID) {
2024-01-22 20:16:10 -06:00
s.mu.Lock()
2024-01-27 13:24:42 -06:00
// NOTE(zeph): neither map index nor map delete can panic, so this critical
// section does not need a defer
c := s.pp[p]
delete(s.pp, p)
s.mu.Unlock()
if c != nil {
// We don't care about the error here since the connection is leaving.
// It's probably already closed anyway.
2024-01-27 13:51:20 -06:00
slog.Debug("leaving", "player", p)
2024-01-27 13:24:42 -06:00
c.Close(websocket.StatusNormalClosure, "bye")
}
2024-01-22 20:16:10 -06:00
}
// Queue connects players to games. The connection immediately upgrades.
// This handler MUST be wrapped in [serve.WithPlayerID].
func (s *Server) Queue(w http.ResponseWriter, r *http.Request) {
p := serve.PlayerID(r.Context())
if p == (player.ID{}) {
panic("missing player ID")
}
2024-01-27 13:24:42 -06:00
person, err := s.accept(w, r, p)
2024-01-22 20:16:10 -06:00
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
2024-01-27 13:24:42 -06:00
s.joinAndServe(person)
2024-01-22 20:16:10 -06:00
}
func (s *Server) joinAndServe(p person) {
2024-01-27 13:51:20 -06:00
slog.Debug("joining", "player", p.id)
2024-01-22 20:16:10 -06:00
ctx, stop := context.WithTimeoutCause(context.Background(), 10*time.Minute, errQueueEmpty)
2024-01-27 13:24:42 -06:00
id, chall, deal := s.l.Queue(ctx, p.id)
2024-01-22 20:16:10 -06:00
stop()
2024-01-27 13:24:42 -06:00
if id == (lobby.GameID{}) {
2024-01-22 20:16:10 -06:00
// Context canceled.
2024-01-27 13:24:42 -06:00
p.conn.Close(websocket.StatusTryAgainLater, "sorry, queue is empty...")
return
}
if deal {
g := game.New(p.id, chall)
other := s.person(chall)
// TODO(zeph): save the game state s.t. we can provide a join channel
go gameActor(ctx, g, p, other, nil)
}
// Reply with the game ID so they can share.
r := struct {
Game lobby.GameID `json:"game"`
}{
Game: id,
}
if err := wsjson.Write(context.TODO(), p.conn, r); err != nil {
2024-01-27 13:51:20 -06:00
slog.WarnContext(ctx, "got a game but player dropped", "game", id, "player", p.id)
2024-01-27 13:24:42 -06:00
s.left(p.id)
2024-01-22 20:16:10 -06:00
return
}
}
var errQueueEmpty = errors.New("sorry, queue is empty")