v01 config has gm.hotwords.[].{call,args} and impl tap to link /gm/rpc/someoneSaidAlias to button pushes
parent
440191de0f
commit
24f4b6b8f5
|
|
@ -15,6 +15,16 @@ type (
|
||||||
Players []configPlayer
|
Players []configPlayer
|
||||||
Quiet bool
|
Quiet bool
|
||||||
Broadcast configBroadcast
|
Broadcast configBroadcast
|
||||||
|
GM configGM
|
||||||
|
}
|
||||||
|
|
||||||
|
configGM struct {
|
||||||
|
Hotwords map[string]configGMHotword
|
||||||
|
}
|
||||||
|
|
||||||
|
configGMHotword struct {
|
||||||
|
Call string
|
||||||
|
Args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
configBroadcast struct {
|
configBroadcast struct {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"mayhem-party/src/device/input/button"
|
||||||
"mayhem-party/src/device/input/wrap"
|
"mayhem-party/src/device/input/wrap"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -178,7 +179,7 @@ func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) {
|
||||||
case "/gm/rpc/status":
|
case "/gm/rpc/status":
|
||||||
v01.serveGMStatus(w)
|
v01.serveGMStatus(w)
|
||||||
case "/gm/rpc/broadcastSomeoneSaidAlias":
|
case "/gm/rpc/broadcastSomeoneSaidAlias":
|
||||||
v01.serveGMSomeoneSaidAlias(w, r)
|
v01.serveGMSomeoneSaid(w, r)
|
||||||
case "/gm/rpc/fillNonPlayerAliases":
|
case "/gm/rpc/fillNonPlayerAliases":
|
||||||
v01.serveGMFillNonPlayerAliases(w, r)
|
v01.serveGMFillNonPlayerAliases(w, r)
|
||||||
case "/gm/rpc/vote":
|
case "/gm/rpc/vote":
|
||||||
|
|
@ -223,6 +224,30 @@ func (v01 *V01) serveGMStatus(w io.Writer) {
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v01 *V01) serveGMSomeoneSaid(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if gmHotword, ok := v01.cfg.GM.Hotwords[r.URL.Query().Get("message")]; ok {
|
||||||
|
v01.serveGMSomeoneSaidGMHotword(w, r, gmHotword)
|
||||||
|
}
|
||||||
|
v01.serveGMSomeoneSaidAlias(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v01 *V01) serveGMSomeoneSaidGMHotword(w http.ResponseWriter, r *http.Request, gmHotword configGMHotword) {
|
||||||
|
switch gmHotword.Call {
|
||||||
|
case "tap":
|
||||||
|
args := append([]string{}, gmHotword.Args...)
|
||||||
|
if len(args) < 1 || len(args[0]) < 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
btn := args[0][0]
|
||||||
|
go func() {
|
||||||
|
v01.alt <- []button.Button{button.Button{Down: true, Char: btn}}
|
||||||
|
v01.alt <- []button.Button{button.Button{Down: false, Char: btn}}
|
||||||
|
}()
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (v01 *V01) serveGMSomeoneSaidAlias(w http.ResponseWriter, r *http.Request) {
|
func (v01 *V01) serveGMSomeoneSaidAlias(w http.ResponseWriter, r *http.Request) {
|
||||||
v01.cfg.Quiet = true
|
v01.cfg.Quiet = true
|
||||||
for k, v := range v01.cfg.Users {
|
for k, v := range v01.cfg.Users {
|
||||||
|
|
|
||||||
|
|
@ -96,12 +96,8 @@ func TestServeGM(t *testing.T) {
|
||||||
t.Run("status", func(t *testing.T) {
|
t.Run("status", func(t *testing.T) {
|
||||||
v01 := NewV01(ctx, nil)
|
v01 := NewV01(ctx, nil)
|
||||||
var result struct {
|
var result struct {
|
||||||
Players int `yaml:"Players"`
|
Players int `yaml:"Players"`
|
||||||
Users map[string]struct {
|
Users map[string]string `yaml:"Users"`
|
||||||
Player int
|
|
||||||
Lag string
|
|
||||||
IdleFor string `yaml:"idle_for"`
|
|
||||||
} `yaml:"Users"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("empty", func(t *testing.T) {
|
t.Run("empty", func(t *testing.T) {
|
||||||
|
|
@ -157,22 +153,46 @@ func TestServeGM(t *testing.T) {
|
||||||
if len(result.Users) != 7 {
|
if len(result.Users) != 7 {
|
||||||
t.Error(result.Users)
|
t.Error(result.Users)
|
||||||
}
|
}
|
||||||
if d, err := time.ParseDuration(result.Users["bel"].Lag); err != nil {
|
if result.Users["bel"] == "" || result.Users["bel"] == "..." {
|
||||||
t.Error(err)
|
t.Error(result.Users["bel"])
|
||||||
} else if d != time.Second {
|
|
||||||
t.Error(d)
|
|
||||||
}
|
|
||||||
if d, err := time.ParseDuration(result.Users["bel"].IdleFor); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else if d < time.Minute || d > 2*time.Minute {
|
|
||||||
t.Error(d)
|
|
||||||
}
|
|
||||||
if result.Users["bel"].Player != 3 {
|
|
||||||
t.Error(result.Users["bel"].Player)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("broadcastSomeoneSaidAlias to hotword tap", func(t *testing.T) {
|
||||||
|
v01 := NewV01(ctx, nil)
|
||||||
|
v01.cfg.GM = configGM{
|
||||||
|
Hotwords: map[string]configGMHotword{
|
||||||
|
"hotword": configGMHotword{
|
||||||
|
Call: "tap",
|
||||||
|
Args: []string{"a"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
do(v01, "/gm/rpc/broadcastSomeoneSaidAlias?message=hotword", "")
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
select {
|
||||||
|
case btn := <-v01.alt:
|
||||||
|
if len(btn) != 1 {
|
||||||
|
t.Error(btn)
|
||||||
|
} else if btn[0].Down != (i == 0) {
|
||||||
|
t.Error(btn[0])
|
||||||
|
} else if btn[0].Char != 'a' {
|
||||||
|
t.Error(btn[0].Char)
|
||||||
|
}
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("nothing in alt")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
do(v01, "/gm/rpc/broadcastSomeoneSaidAlias?message=hotword", "")
|
||||||
|
time.Sleep(time.Millisecond * 150)
|
||||||
|
if got := v01.Read(); len(got) != 1 || !got[0].Down || got[0].Char != 'a' {
|
||||||
|
t.Error(got)
|
||||||
|
} else if got := v01.Read(); len(got) != 1 || got[0].Down || got[0].Char != 'a' {
|
||||||
|
t.Error(got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("broadcastSomeoneSaidAlias", func(t *testing.T) {
|
t.Run("broadcastSomeoneSaidAlias", func(t *testing.T) {
|
||||||
v01 := NewV01(ctx, nil)
|
v01 := NewV01(ctx, nil)
|
||||||
v01.cfg.Quiet = false
|
v01.cfg.Quiet = false
|
||||||
|
|
|
||||||
|
|
@ -18,3 +18,11 @@ players:
|
||||||
quiet: false
|
quiet: false
|
||||||
broadcast:
|
broadcast:
|
||||||
message: hi
|
message: hi
|
||||||
|
gm:
|
||||||
|
hotwords:
|
||||||
|
coin:
|
||||||
|
call: tap
|
||||||
|
args: ['!']
|
||||||
|
star:
|
||||||
|
call: tap
|
||||||
|
args: ['?']
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ type (
|
||||||
src raw.Raw
|
src raw.Raw
|
||||||
cfg config
|
cfg config
|
||||||
telemetryc chan message
|
telemetryc chan message
|
||||||
|
alt chan []button.Button
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -41,6 +42,7 @@ func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
||||||
src: src,
|
src: src,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
telemetryc: make(chan message),
|
telemetryc: make(chan message),
|
||||||
|
alt: make(chan []button.Button, 2),
|
||||||
}
|
}
|
||||||
go result.listen()
|
go result.listen()
|
||||||
go result.dotelemetry()
|
go result.dotelemetry()
|
||||||
|
|
@ -58,6 +60,11 @@ func (v01 *V01) Close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) Read() []button.Button {
|
func (v01 *V01) Read() []button.Button {
|
||||||
|
select {
|
||||||
|
case alt := <-v01.alt:
|
||||||
|
return alt
|
||||||
|
default:
|
||||||
|
}
|
||||||
line := v01.src.Read()
|
line := v01.src.Read()
|
||||||
var msg message
|
var msg message
|
||||||
if err := json.Unmarshal(line, &msg); err != nil {
|
if err := json.Unmarshal(line, &msg); err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue