test status

master
Bel LaPointe 2023-03-27 06:50:31 -06:00
parent 8314bdc457
commit 88a78c489f
3 changed files with 121 additions and 38 deletions

View File

@ -148,7 +148,38 @@ 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":
case "/gm/rpc/status":
v01.serveGMStatus(w, r)
case "/gm/rpc/markMessageRead":
v01.cfg.Quiet = true
for k, v := range v01.cfg.Users {
v.Alias = v.Message
v.Message = ""
v01.cfg.Users[k] = v
}
v01.servePutBroadcastValue(fmt.Sprintf("<<SOMEONE SAID %q>>", 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
case "/gm/rpc/swap":
if err := v01.serveGMSwap(r, r.URL.Query().Get("a"), r.URL.Query().Get("b")); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
v01.cfg.Quiet = false
default:
http.NotFound(w, r)
}
}
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{}{
@ -164,49 +195,8 @@ func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) {
"Players": len(v01.cfg.Players),
"Users": users,
})
case "/gm/rpc/markMessageRead":
v01.cfg.Quiet = true
for k, v := range v01.cfg.Users {
v.Alias = v.Message
v.Message = ""
v01.cfg.Users[k] = v
}
v01.servePutBroadcastValue(fmt.Sprintf("<<SOMEONE SAID %q>>", strings.ToUpper(r.URL.Query().Get("message"))))
case "/gm/rpc/shuffle":
v01.serveGMShuffle(w, r)
v01.cfg.Quiet = false
case "/gm/rpc/swap":
if err := v01.serveGMSwap(r, r.URL.Query().Get("a"), r.URL.Query().Get("b")); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
v01.cfg.Quiet = false
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) serveGMShuffle(w http.ResponseWriter, r *http.Request) {
poolSize := len(v01.cfg.Users)
if altSize := len(v01.cfg.Players); altSize > poolSize {
@ -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]

View File

@ -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) {
})
}

View File

@ -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