diff --git a/src/device/input/parse/v01/server.go b/src/device/input/parse/v01/server.go index ec7ce8c..c7c0730 100644 --- a/src/device/input/parse/v01/server.go +++ b/src/device/input/parse/v01/server.go @@ -92,7 +92,13 @@ func (v01 *V01) getUserFeedback(w http.ResponseWriter, r *http.Request) { msg = fmt.Sprintf("%s (Your secret word is '%s'. Make **someone else** say it!)", msg, alias) } - w.Write([]byte(msg)) + w.Write([]byte(msg + "\n\n")) + v01.serveGMStatus(w) + + if v01.cfg.Quiet { + w.Write([]byte("\n\n")) + v01.serveGMVoteRead(w) + } } func (v01 *V01) servePutBroadcast(w http.ResponseWriter, r *http.Request) { @@ -170,7 +176,7 @@ func (v01 *V01) serveGlobalQueryRefresh(r *http.Request) { func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/gm/rpc/status": - v01.serveGMStatus(w, r) + v01.serveGMStatus(w) case "/gm/rpc/broadcastSomeoneSaidAlias": v01.serveGMSomeoneSaidAlias(w, r) case "/gm/rpc/fillNonPlayerAliases": @@ -192,7 +198,7 @@ func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) { } } -func (v01 *V01) serveGMStatus(w http.ResponseWriter, r *http.Request) { +func (v01 *V01) serveGMStatus(w io.Writer) { users := map[string]struct { Lag time.Duration `yaml:"lag,omitempty"` Player int `yaml:"player,omitempty"` @@ -291,28 +297,36 @@ func (v01 *V01) serveGMElect(w http.ResponseWriter, r *http.Request) { func (v01 *V01) serveGMVote(w http.ResponseWriter, r *http.Request) { switch r.URL.Query().Get("payload") { case "": - counts := map[string]string{} - for k, v := range v01.cfg.Users { - if v.State.GM.Vote != "" { - counts[k] = "voted" - } else { - counts[k] = "voting" - } - } - yaml.NewEncoder(w).Encode(counts) + v01.serveGMVoteRead(w) default: - voter := r.URL.Query().Get("user") - candidate := r.URL.Query().Get("payload") - v, ok := v01.cfg.Users[voter] - if _, ok2 := v01.cfg.Users[candidate]; !ok || !ok2 { - http.Error(w, "bad voter/candidate", http.StatusBadRequest) - return - } - v.State.GM.Vote = candidate - v01.cfg.Users[voter] = v + v01.serveGMVoteWrite(w, r) } } +func (v01 *V01) serveGMVoteRead(w io.Writer) { + counts := map[string]string{} + for k, v := range v01.cfg.Users { + if v.State.GM.Vote != "" { + counts[k] = "voted" + } else { + counts[k] = "voting" + } + } + yaml.NewEncoder(w).Encode(counts) +} + +func (v01 *V01) serveGMVoteWrite(w http.ResponseWriter, r *http.Request) { + voter := r.URL.Query().Get("user") + candidate := r.URL.Query().Get("payload") + v, ok := v01.cfg.Users[voter] + if _, ok2 := v01.cfg.Users[candidate]; !ok || !ok2 { + http.Error(w, "bad voter/candidate", http.StatusBadRequest) + return + } + v.State.GM.Vote = candidate + v01.cfg.Users[voter] = v +} + func (v01 *V01) serveGMShuffle(r *http.Request) { poolSize := len(v01.cfg.Users) if altSize := len(v01.cfg.Players); altSize > poolSize { diff --git a/src/device/input/parse/v01/v01_exported_test.go b/src/device/input/parse/v01/v01_exported_test.go index 127c696..9463aac 100644 --- a/src/device/input/parse/v01/v01_exported_test.go +++ b/src/device/input/parse/v01/v01_exported_test.go @@ -123,8 +123,8 @@ func TestV01Feedback(t *testing.T) { } defer resp.Body.Close() b, _ := io.ReadAll(resp.Body) - if string(b) != "to bel" { - t.Error(b) + if !strings.HasPrefix(string(b), "to bel") { + t.Error(string(b)) } }) @@ -135,8 +135,8 @@ func TestV01Feedback(t *testing.T) { } defer resp.Body.Close() b, _ := io.ReadAll(resp.Body) - if string(b) != "to everyone" { - t.Error(b) + if !strings.HasPrefix(string(b), "to everyone") { + t.Error(string(b)) } }) @@ -155,7 +155,7 @@ func TestV01Feedback(t *testing.T) { } defer resp.Body.Close() b, _ := io.ReadAll(resp.Body) - if string(b) != want { + if !strings.HasPrefix(string(b), want) { t.Error(string(b)) } })