From 88a78c489fefe54cf507768b1624a74c939c7505 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Mon, 27 Mar 2023 06:50:31 -0600 Subject: [PATCH] test status --- src/device/input/parse/v01/server.go | 66 +++++++--------- src/device/input/parse/v01/server_test.go | 91 +++++++++++++++++++++++ todo.yaml | 2 + 3 files changed, 121 insertions(+), 38 deletions(-) diff --git a/src/device/input/parse/v01/server.go b/src/device/input/parse/v01/server.go index 459682a..5be5404 100644 --- a/src/device/input/parse/v01/server.go +++ b/src/device/input/parse/v01/server.go @@ -148,22 +148,8 @@ func (v01 *V01) serveGlobalQueryRefresh(r *http.Request) { func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { - case "/gm/rpc/publicStatus": - users := map[string]interface{}{} - for k, v := range v01.cfg.Users { - v2 := map[string]interface{}{ - "Last Activity": time.Since(time.Unix(0, v.LastTSMS/int64(time.Millisecond))), - "Lag": time.Duration(v.LastLag) / time.Millisecond, - } - if v.Player > 0 { - v2["Player"] = v.Player - } - users[k] = v2 - } - yaml.NewEncoder(w).Encode(map[string]interface{}{ - "Players": len(v01.cfg.Players), - "Users": users, - }) + case "/gm/rpc/status": + v01.serveGMStatus(w, r) case "/gm/rpc/markMessageRead": v01.cfg.Quiet = true for k, v := range v01.cfg.Users { @@ -172,6 +158,12 @@ func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) { v01.cfg.Users[k] = v } v01.servePutBroadcastValue(fmt.Sprintf("<>", strings.ToUpper(r.URL.Query().Get("message")))) + case "/gm/rpc/setNonPlayerAliases": + panic("TODO stash aliases") + case "/gm/rpc/vote": + panic("TODO stash user:votedForUser") + case "/gm/rpc/elect": + panic("TODO swap or shuffle") case "/gm/rpc/shuffle": v01.serveGMShuffle(w, r) v01.cfg.Quiet = false @@ -184,28 +176,26 @@ func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) { default: http.NotFound(w, r) } - // TODO: what do? could make 'em RPC endpoints that calls itself - /* - --somebody needed to assign words to players - --alias field so i can swap zach and taco without losing taco - --dont have to if i let quiet affect TTS ... - - is that gonna turn into a side effect? - .meta.alias it is - - everybody always knows their word to discourage others if in first - --what penalty if i say me? - - no play is ONE universal dis-incentive... - - what other goofy disincentive? - - shuffle the world? - - on both failed and self vote - - track last vote for cooldown and stt disable tha person - - admin ui things - --swap 2 players - - */ } -var errServeGMSwapWhoDat = errors.New("who?") -var errServeGMSwapSelf = errors.New("/spiderman pointing") +func (v01 *V01) serveGMStatus(w http.ResponseWriter, r *http.Request) { + log.Printf("status with %+v", v01.cfg) + users := map[string]interface{}{} + for k, v := range v01.cfg.Users { + v2 := map[string]interface{}{ + "Last Activity": time.Since(time.Unix(0, v.LastTSMS/int64(time.Millisecond))), + "Lag": time.Duration(v.LastLag) / time.Millisecond, + } + if v.Player > 0 { + v2["Player"] = v.Player + } + users[k] = v2 + } + yaml.NewEncoder(w).Encode(map[string]interface{}{ + "Players": len(v01.cfg.Players), + "Users": users, + }) +} func (v01 *V01) serveGMShuffle(w http.ResponseWriter, r *http.Request) { poolSize := len(v01.cfg.Users) @@ -246,10 +236,10 @@ func (v01 *V01) serveGMSwap(r *http.Request, nameA, nameB string) error { userA := getUserNameFor(nameA) userB := getUserNameFor(nameB) if userA == "" || userB == "" { - return errServeGMSwapWhoDat + return errors.New("who dat?") } if userA == userB { - return errServeGMSwapSelf + return errors.New("/spiderman-pointing") } a := v01.cfg.Users[userA] b := v01.cfg.Users[userB] diff --git a/src/device/input/parse/v01/server_test.go b/src/device/input/parse/v01/server_test.go index 63a3de4..2de15f2 100644 --- a/src/device/input/parse/v01/server_test.go +++ b/src/device/input/parse/v01/server_test.go @@ -1,6 +1,7 @@ package v01 import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -74,3 +75,93 @@ func TestPatchConfig(t *testing.T) { } } } + +func TestServeGM(t *testing.T) { + ctx, can := context.WithCancel(context.Background()) + defer can() + + do := func(v01 *V01, path, body string) *httptest.ResponseRecorder { + w := httptest.NewRecorder() + r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body)) + v01.ServeHTTP(w, r) + return w + } + + t.Run("status", func(t *testing.T) { + v01 := NewV01(ctx, nil) + var result struct { + Players int `yaml:"Players"` + Users map[string]struct { + Lag string + LastActivity string + } `yaml:"Users"` + } + + t.Run("empty", func(t *testing.T) { + resp := do(v01, "/gm/rpc/status", "") + if resp.Code != http.StatusOK { + t.Error(resp.Code) + } + t.Log(string(resp.Body.Bytes())) + if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil { + t.Fatal(err) + } + if result.Players != 0 { + t.Error(result.Players) + } + if len(result.Users) != 0 { + t.Error(result.Users) + } + }) + + t.Run("full", func(t *testing.T) { + v01.cfg.Players = []configPlayer{ + {}, + {}, + {}, + {}, + } + v01.cfg.Users = map[string]configUser{ + "bel": configUser{}, + "zach": configUser{}, + "chase": configUser{}, + "mason": configUser{}, + "nat": configUser{}, + "roxy": configUser{}, + "bill": configUser{}, + } + resp := do(v01, "/gm/rpc/status", "") + if resp.Code != http.StatusOK { + t.Error(resp.Code) + } + t.Log(string(resp.Body.Bytes())) + if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil { + t.Fatal(err) + } + if result.Players != 4 { + t.Error(result.Players) + } + if len(result.Users) != 7 { + t.Error(result.Users) + } + }) + }) + + t.Run("publicStatus", func(t *testing.T) { + }) + + t.Run("publicStatus", func(t *testing.T) { + }) + + t.Run("publicStatus", func(t *testing.T) { + }) + + t.Run("publicStatus", func(t *testing.T) { + }) + + t.Run("publicStatus", func(t *testing.T) { + }) + + t.Run("publicStatus", func(t *testing.T) { + }) +} diff --git a/todo.yaml b/todo.yaml index 10898c9..58bfce7 100755 --- a/todo.yaml +++ b/todo.yaml @@ -1,4 +1,6 @@ todo: +- clients can send STT via box +- clients can vote - single docker image to run all - game master to coordinate config change - https via home.blapointe and rproxy