GameByName doesnt take uid
parent
37291e68aa
commit
f7a303168a
|
|
@ -97,20 +97,19 @@ func (s PlayerState) Points() int {
|
||||||
return points
|
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
|
var result string
|
||||||
err := games.db.Query(ctx, func(rows *sql.Rows) error {
|
err := games.db.Query(ctx, func(rows *sql.Rows) error {
|
||||||
return rows.Scan(&result)
|
return rows.Scan(&result)
|
||||||
}, `
|
}, `
|
||||||
SELECT
|
SELECT
|
||||||
players.game_uuid
|
games.uuid
|
||||||
FROM
|
FROM
|
||||||
players
|
games
|
||||||
JOIN games ON players.game_uuid=games.uuid
|
WHERE games.name=?
|
||||||
WHERE players.user_uuid=? AND games.name=?
|
|
||||||
ORDER BY games.updated DESC
|
ORDER BY games.updated DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`, uid, name)
|
`, name)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ func TestGames(t *testing.T) {
|
||||||
t.Error(v)
|
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)
|
t.Error("err getting game by empty name for empty user:", err)
|
||||||
} else if len(v) > 0 {
|
} else if len(v) > 0 {
|
||||||
t.Error(v)
|
t.Error(v)
|
||||||
|
|
@ -112,7 +112,7 @@ func TestGames(t *testing.T) {
|
||||||
t.Error("wrong game found for user:", v)
|
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)
|
t.Error("err getting game by name for user:", err)
|
||||||
} else if v != id {
|
} else if v != id {
|
||||||
t.Error("wrong game by name for user:", v)
|
t.Error("wrong game by name for user:", v)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -13,6 +16,8 @@ func isV1(r *http.Request) bool {
|
||||||
|
|
||||||
func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
|
func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
|
||||||
switch path.Join(r.Method, r.URL.Path) {
|
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":
|
case "PUT/v1/state/" + s.Session(r.Context()).ID + "/party":
|
||||||
return s.serveV1PutParty(w, r)
|
return s.serveV1PutParty(w, r)
|
||||||
default:
|
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 {
|
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