GameByName doesnt take uid
parent
37291e68aa
commit
f7a303168a
|
|
@ -97,20 +97,19 @@ func (s PlayerState) Points() int {
|
|||
return points
|
||||
}
|
||||
|
||||
func (games Games) GameByName(ctx context.Context, uid, name string) (string, error) {
|
||||
func (games Games) GameByName(ctx context.Context, name string) (string, error) {
|
||||
var result string
|
||||
err := games.db.Query(ctx, func(rows *sql.Rows) error {
|
||||
return rows.Scan(&result)
|
||||
}, `
|
||||
SELECT
|
||||
players.game_uuid
|
||||
games.uuid
|
||||
FROM
|
||||
players
|
||||
JOIN games ON players.game_uuid=games.uuid
|
||||
WHERE players.user_uuid=? AND games.name=?
|
||||
games
|
||||
WHERE games.name=?
|
||||
ORDER BY games.updated DESC
|
||||
LIMIT 1
|
||||
`, uid, name)
|
||||
`, name)
|
||||
return result, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func TestGames(t *testing.T) {
|
|||
t.Error(v)
|
||||
}
|
||||
|
||||
if v, err := games.GameByName(ctx, "", ""); err != nil {
|
||||
if v, err := games.GameByName(ctx, ""); err != nil {
|
||||
t.Error("err getting game by empty name for empty user:", err)
|
||||
} else if len(v) > 0 {
|
||||
t.Error(v)
|
||||
|
|
@ -112,7 +112,7 @@ func TestGames(t *testing.T) {
|
|||
t.Error("wrong game found for user:", v)
|
||||
}
|
||||
|
||||
if v, err := games.GameByName(ctx, "p1", "g1"); err != nil {
|
||||
if v, err := games.GameByName(ctx, "g1"); err != nil {
|
||||
t.Error("err getting game by name for user:", err)
|
||||
} else if v != id {
|
||||
t.Error("wrong game by name for user:", v)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -13,6 +16,8 @@ func isV1(r *http.Request) bool {
|
|||
|
||||
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:
|
||||
|
|
@ -22,5 +27,27 @@ func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
|
|||
}
|
||||
|
||||
func (s *S) serveV1PutParty(w http.ResponseWriter, r *http.Request) error {
|
||||
return fmt.Errorf("not impl")
|
||||
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")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue