serve GET /config
parent
e968ce17ce
commit
163bf2b405
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"mayhem-party/src/device/input/wrap"
|
"mayhem-party/src/device/input/wrap"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
|
@ -50,17 +51,19 @@ func (v01 *V01) _listen() {
|
||||||
func (v01 *V01) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (v01 *V01) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
r = r.WithContext(v01.ctx)
|
r = r.WithContext(v01.ctx)
|
||||||
v01.serveHTTP(w, r)
|
v01.serveHTTP(w, r)
|
||||||
v01.globalQueries(r)
|
v01.serveGlobalQueries(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
func (v01 *V01) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch strings.Split(r.URL.Path[1:], "/")[0] {
|
||||||
case "/":
|
case "":
|
||||||
v01.getUserFeedback(w, r)
|
v01.getUserFeedback(w, r)
|
||||||
case "/broadcast":
|
case "broadcast":
|
||||||
v01.putBroadcast(w, r)
|
v01.servePutBroadcast(w, r)
|
||||||
case "/config":
|
case "config":
|
||||||
v01.patchConfig(w, r)
|
v01.serveConfig(w, r)
|
||||||
|
case "gm":
|
||||||
|
v01.serveGM(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,14 +75,27 @@ func (v01 *V01) getUserFeedback(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte(user.Message))
|
w.Write([]byte(user.Message))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) putBroadcast(w http.ResponseWriter, r *http.Request) {
|
func (v01 *V01) servePutBroadcast(w http.ResponseWriter, r *http.Request) {
|
||||||
b, _ := io.ReadAll(r.Body)
|
b, _ := io.ReadAll(r.Body)
|
||||||
v := v01.cfg.Users["broadcast"]
|
v := v01.cfg.Users["broadcast"]
|
||||||
v.Message = string(b)
|
v.Message = string(b)
|
||||||
v01.cfg.Users["broadcast"] = v
|
v01.cfg.Users["broadcast"] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) patchConfig(w http.ResponseWriter, r *http.Request) {
|
func (v01 *V01) serveConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == http.MethodGet {
|
||||||
|
v01.serveGetConfig(w, r)
|
||||||
|
} else {
|
||||||
|
v01.servePatchConfig(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v01 *V01) serveGetConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
|
b, _ := json.Marshal(v01.cfg)
|
||||||
|
w.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v01 *V01) servePatchConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
b, _ := io.ReadAll(r.Body)
|
b, _ := io.ReadAll(r.Body)
|
||||||
var v []interface{}
|
var v []interface{}
|
||||||
if err := json.Unmarshal(b, &v); err != nil {
|
if err := json.Unmarshal(b, &v); err != nil {
|
||||||
|
|
@ -94,12 +110,12 @@ func (v01 *V01) patchConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) globalQueries(r *http.Request) {
|
func (v01 *V01) serveGlobalQueries(r *http.Request) {
|
||||||
v01.globalQuerySay(r)
|
v01.serveGlobalQuerySay(r)
|
||||||
v01.globalQueryRefresh(r)
|
v01.serveGlobalQueryRefresh(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) globalQuerySay(r *http.Request) {
|
func (v01 *V01) serveGlobalQuerySay(r *http.Request) {
|
||||||
text := r.URL.Query().Get("say")
|
text := r.URL.Query().Get("say")
|
||||||
if text == "" {
|
if text == "" {
|
||||||
text = r.Header.Get("say")
|
text = r.Header.Get("say")
|
||||||
|
|
@ -110,7 +126,7 @@ func (v01 *V01) globalQuerySay(r *http.Request) {
|
||||||
go v01.tts(text)
|
go v01.tts(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) globalQueryRefresh(r *http.Request) {
|
func (v01 *V01) serveGlobalQueryRefresh(r *http.Request) {
|
||||||
if _, ok := r.URL.Query()["refresh"]; !ok {
|
if _, ok := r.URL.Query()["refresh"]; !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -119,3 +135,26 @@ func (v01 *V01) globalQueryRefresh(r *http.Request) {
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// 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?
|
||||||
|
**note for server side meta, because thats flexible and supports other stuff like heartbeat
|
||||||
|
.meta.messagelog actually kinda solves it but how to stash... cant hook into PATCH
|
||||||
|
.meta.lastseen is still #worth
|
||||||
|
.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
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ func TestPatchConfig(t *testing.T) {
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r := httptest.NewRequest(http.MethodPatch, "/config", strings.NewReader(c.patch))
|
r := httptest.NewRequest(http.MethodPatch, "/config", strings.NewReader(c.patch))
|
||||||
v01.patchConfig(w, r)
|
v01.servePatchConfig(w, r)
|
||||||
if fmt.Sprintf("%+v", c.want) != fmt.Sprintf("%+v", v01.cfg) {
|
if fmt.Sprintf("%+v", c.want) != fmt.Sprintf("%+v", v01.cfg) {
|
||||||
t.Errorf("want \n\t%+v, got \n\t%+v", c.want, v01.cfg)
|
t.Errorf("want \n\t%+v, got \n\t%+v", c.want, v01.cfg)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue