out/cmd/server/v1.go

54 lines
1008 B
Go

package main
import (
"bytes"
"fmt"
"io"
"net/http"
"path"
"slices"
"strings"
)
func isV1(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, "/v1/")
}
func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
switch path.Join(r.Method, r.URL.Path) {
case "GET/v1/state/" + s.Session(r.Context()).ID:
return fmt.Errorf("not impl")
case "PUT/v1/state/" + s.Session(r.Context()).ID + "/party":
return s.serveV1PutParty(w, r)
default:
http.NotFound(w, r)
}
return nil
}
func (s *S) serveV1PutParty(w http.ResponseWriter, r *http.Request) error {
party, err := io.ReadAll(r.Body)
if err != nil {
return err
}
party = bytes.TrimSpace(party)
if len(party) == 0 {
return nil
}
gid, err := s.games.GameByName(r.Context(), string(party))
if err != nil {
return err
}
games, err := s.games.GamesForUser(r.Context(), gid)
if err != nil {
return err
}
if slices.Contains(games, gid) {
return nil
}
return fmt.Errorf("not impl create player join")
}