Compare commits
29 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
b89ed62036 | |
|
|
67c93a9048 | |
|
|
8eae7ae9a6 | |
|
|
ce32620940 | |
|
|
24f4b6b8f5 | |
|
|
440191de0f | |
|
|
d5adc596ac | |
|
|
41a39c40d0 | |
|
|
9a38033b65 | |
|
|
6a4ad5ec36 | |
|
|
c2b8ab67f2 | |
|
|
9418cecdf5 | |
|
|
fb5da88774 | |
|
|
39f6bc8ed9 | |
|
|
f3cbfa1c48 | |
|
|
444245c0f5 | |
|
|
52ee1e5083 | |
|
|
934158b7a3 | |
|
|
87e63c27df | |
|
|
f98e417ba6 | |
|
|
d6a7ee3db0 | |
|
|
b814dabfd3 | |
|
|
0a91fc656d | |
|
|
5c3341e260 | |
|
|
0903c01b9a | |
|
|
342e2eef93 | |
|
|
b8b076450e | |
|
|
3bb7cad554 | |
|
|
44ec540db3 |
1
go.mod
1
go.mod
|
|
@ -10,6 +10,7 @@ require (
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
|
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
|
||||||
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
github.com/hajimehoshi/oto v0.7.1 // indirect
|
github.com/hajimehoshi/oto v0.7.1 // indirect
|
||||||
github.com/micmonay/keybd_event v1.1.1 // indirect
|
github.com/micmonay/keybd_event v1.1.1 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -11,6 +11,8 @@ github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38r
|
||||||
github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5zAWE=
|
github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5zAWE=
|
||||||
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
|
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
|
||||||
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
|
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
|
||||||
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
|
github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
|
||||||
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
|
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
|
||||||
github.com/hajimehoshi/oto v0.7.1 h1:I7maFPz5MBCwiutOrz++DLdbr4rTzBsbBuV2VpgU9kk=
|
github.com/hajimehoshi/oto v0.7.1 h1:I7maFPz5MBCwiutOrz++DLdbr4rTzBsbBuV2VpgU9kk=
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,12 @@
|
||||||
package button
|
package button
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type Button struct {
|
type Button struct {
|
||||||
Char byte
|
Char byte
|
||||||
Down bool
|
Down bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (button Button) String() string {
|
||||||
|
return fmt.Sprintf("%c:%v", button.Char, button.Down)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -84,11 +85,21 @@ func (v01 *V01) getUserFeedback(w http.ResponseWriter, r *http.Request) {
|
||||||
msg = v01.cfg.Broadcast.Message
|
msg = v01.cfg.Broadcast.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
if user.State.GM.Alias != "" {
|
alias := user.State.GM.Alias
|
||||||
msg = fmt.Sprintf("%s (Your secret word is '%s'. Make **someone else** say it!)", msg, user.State.GM.Alias)
|
if alias == "" {
|
||||||
|
alias = user.State.GM.LastAlias
|
||||||
|
}
|
||||||
|
if alias != "" {
|
||||||
|
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) {
|
func (v01 *V01) servePutBroadcast(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -166,9 +177,9 @@ func (v01 *V01) serveGlobalQueryRefresh(r *http.Request) {
|
||||||
func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) {
|
func (v01 *V01) serveGM(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/gm/rpc/status":
|
case "/gm/rpc/status":
|
||||||
v01.serveGMStatus(w, r)
|
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":
|
||||||
|
|
@ -188,25 +199,54 @@ 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 {
|
users := map[string]string{}
|
||||||
Lag time.Duration `yaml:"lag,omitempty"`
|
|
||||||
Player int `yaml:"player,omitempty"`
|
|
||||||
IdleFor time.Duration `yaml:"idle_for,omitempty"`
|
|
||||||
}{}
|
|
||||||
for k, v := range v01.cfg.Users {
|
for k, v := range v01.cfg.Users {
|
||||||
v2 := users[k]
|
result := ""
|
||||||
v2.Lag = time.Duration(v.Meta.LastLag) * time.Millisecond
|
|
||||||
v2.Player = v.State.Player
|
if v.State.Player > 0 {
|
||||||
if v.Meta.LastTSMS > 0 {
|
result += fmt.Sprintf("Player %v ", v.State.Player)
|
||||||
v2.IdleFor = time.Since(time.Unix(0, v.Meta.LastTSMS*int64(time.Millisecond)))
|
|
||||||
}
|
}
|
||||||
users[k] = v2
|
|
||||||
|
if ms := time.Duration(v.Meta.LastLag) * time.Millisecond; v.Meta.LastLag > 0 && ms < time.Minute {
|
||||||
|
result += fmt.Sprintf("%s ", ms.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if result == "" {
|
||||||
|
result = "..."
|
||||||
|
}
|
||||||
|
users[k] = result
|
||||||
}
|
}
|
||||||
yaml.NewEncoder(w).Encode(map[string]interface{}{
|
b, _ := yaml.Marshal(map[string]interface{}{
|
||||||
"Players": len(v01.cfg.Players),
|
"Players": len(v01.cfg.Players),
|
||||||
"Users": users,
|
"Users": users,
|
||||||
})
|
})
|
||||||
|
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}}
|
||||||
|
}()
|
||||||
|
r.URL.RawQuery = ""
|
||||||
|
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) {
|
||||||
|
|
@ -287,28 +327,36 @@ func (v01 *V01) serveGMElect(w http.ResponseWriter, r *http.Request) {
|
||||||
func (v01 *V01) serveGMVote(w http.ResponseWriter, r *http.Request) {
|
func (v01 *V01) serveGMVote(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.URL.Query().Get("payload") {
|
switch r.URL.Query().Get("payload") {
|
||||||
case "":
|
case "":
|
||||||
counts := map[string]string{}
|
v01.serveGMVoteRead(w)
|
||||||
for k, v := range v01.cfg.Users {
|
|
||||||
if v.State.GM.Vote != "" {
|
|
||||||
counts[k] = "voted"
|
|
||||||
} else {
|
|
||||||
counts[k] = "voting"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
yaml.NewEncoder(w).Encode(counts)
|
|
||||||
default:
|
default:
|
||||||
voter := r.URL.Query().Get("user")
|
v01.serveGMVoteWrite(w, r)
|
||||||
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) 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) {
|
func (v01 *V01) serveGMShuffle(r *http.Request) {
|
||||||
poolSize := len(v01.cfg.Users)
|
poolSize := len(v01.cfg.Users)
|
||||||
if altSize := len(v01.cfg.Players); altSize > poolSize {
|
if altSize := len(v01.cfg.Players); altSize > poolSize {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,28 @@
|
||||||
feedback:
|
feedback:
|
||||||
addr: :17071
|
addr: :17071
|
||||||
ttsurl: http://localhost:15002
|
ttsurl: http://localhost:15002
|
||||||
broadcast:
|
|
||||||
message: hi
|
|
||||||
users:
|
users:
|
||||||
bel:
|
bel:
|
||||||
|
meta:
|
||||||
|
lasttsms: 1681062770999
|
||||||
|
lastlag: 12
|
||||||
state:
|
state:
|
||||||
player: 0
|
player: 0
|
||||||
message: "hi"
|
message: hi
|
||||||
alias: driver
|
gm:
|
||||||
meta:
|
alias: ""
|
||||||
tsms: 1
|
lastalias: ""
|
||||||
lastlag: 2
|
vote: ""
|
||||||
players:
|
players:
|
||||||
- buttons:
|
- transformation: {}
|
||||||
up: "w"
|
|
||||||
down: "s"
|
|
||||||
left: "a"
|
|
||||||
right: "d"
|
|
||||||
l: "q"
|
|
||||||
r: "e"
|
|
||||||
a: "1"
|
|
||||||
b: "2"
|
|
||||||
x: "3"
|
|
||||||
y: "4"
|
|
||||||
quiet: false
|
quiet: false
|
||||||
|
broadcast:
|
||||||
|
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 {
|
||||||
|
|
|
||||||
|
|
@ -123,8 +123,8 @@ func TestV01Feedback(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
b, _ := io.ReadAll(resp.Body)
|
b, _ := io.ReadAll(resp.Body)
|
||||||
if string(b) != "to bel" {
|
if !strings.HasPrefix(string(b), "to bel") {
|
||||||
t.Error(b)
|
t.Error(string(b))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -135,8 +135,8 @@ func TestV01Feedback(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
b, _ := io.ReadAll(resp.Body)
|
b, _ := io.ReadAll(resp.Body)
|
||||||
if string(b) != "to everyone" {
|
if !strings.HasPrefix(string(b), "to everyone") {
|
||||||
t.Error(b)
|
t.Error(string(b))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -155,7 +155,7 @@ func TestV01Feedback(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
b, _ := io.ReadAll(resp.Body)
|
b, _ := io.ReadAll(resp.Body)
|
||||||
if string(b) != want {
|
if !strings.HasPrefix(string(b), want) {
|
||||||
t.Error(string(b))
|
t.Error(string(b))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
// a key map of allowed keys
|
||||||
|
var allowedKeys = {
|
||||||
|
37: 'left',
|
||||||
|
38: 'up',
|
||||||
|
39: 'right',
|
||||||
|
40: 'down',
|
||||||
|
65: 'a',
|
||||||
|
66: 'b'
|
||||||
|
};
|
||||||
|
|
||||||
|
// the 'official' Konami Code sequence
|
||||||
|
var konamiCode = ['up', 'up', 'down', 'down', 'left', 'right', 'left', 'right', 'b', 'a'];
|
||||||
|
|
||||||
|
// a variable to remember the 'position' the user has reached so far.
|
||||||
|
var konamiCodePosition = 0;
|
||||||
|
|
||||||
|
// add keydown event listener
|
||||||
|
document.addEventListener('keydown', function(e) {
|
||||||
|
// get the value of the key code from the key map
|
||||||
|
var key = allowedKeys[e.keyCode];
|
||||||
|
// get the value of the required key from the konami code
|
||||||
|
var requiredKey = konamiCode[konamiCodePosition];
|
||||||
|
|
||||||
|
// compare the key with the required key
|
||||||
|
if (key == requiredKey) {
|
||||||
|
|
||||||
|
// move to the next key in the konami code sequence
|
||||||
|
konamiCodePosition++;
|
||||||
|
|
||||||
|
// if the last key is reached, activate cheats
|
||||||
|
if (konamiCodePosition == konamiCode.length) {
|
||||||
|
showSecrets();
|
||||||
|
konamiCodePosition = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
konamiCodePosition = 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function showSecrets() {
|
||||||
|
var element = document.getElementById("konami")
|
||||||
|
element.style = "display:block"
|
||||||
|
var e = new Event("onKonami")
|
||||||
|
element.dispatchEvent(new Event("onKonami"))
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
function onYouTubeIframeAPIReady() {
|
||||||
|
|
||||||
|
var player;
|
||||||
|
|
||||||
|
player = new YT.Player('konami', {
|
||||||
|
videoId: 'V4oJ62xrFZo', // 👈 video id.
|
||||||
|
width: 560,
|
||||||
|
height: 316,
|
||||||
|
playerVars: {
|
||||||
|
'autoplay': 1,
|
||||||
|
'controls': 1,
|
||||||
|
'showinfo': 0,
|
||||||
|
'modestbranding': 0,
|
||||||
|
'loop': 1,
|
||||||
|
'fs': 0,
|
||||||
|
'cc_load_policty': 0,
|
||||||
|
'iv_load_policy': 3
|
||||||
|
},
|
||||||
|
events: {
|
||||||
|
'onReady': function (e) {
|
||||||
|
e.target.setVolume(33); // For max value, set value to 100.
|
||||||
|
document.getElementById("konami").addEventListener("onKonami", () => {e.target.playVideo()})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,172 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<header>
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
|
||||||
|
<script src="konami.js"></script>
|
||||||
|
<script src="lowerVolume.js"></script>
|
||||||
|
<script async src="https://www.youtube.com/iframe_api"></script>
|
||||||
|
<script>
|
||||||
|
function formsay(message) {
|
||||||
|
console.log(`say '${message}'`)
|
||||||
|
http("GET", `/proxy?user=${document.getElementById("user").value}&say=${message}`, noopcallback, null)
|
||||||
|
}
|
||||||
|
function formsend(message) {
|
||||||
|
console.log(`send '${message}'`)
|
||||||
|
http("GET", `/proxy/gm/rpc/vote?user=${document.getElementById("user").value}&payload=${message}`, noopcallback, null)
|
||||||
|
}
|
||||||
|
function http(method, remote, callback, body) {
|
||||||
|
var xmlhttp = new XMLHttpRequest();
|
||||||
|
xmlhttp.onreadystatechange = function() {
|
||||||
|
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
|
||||||
|
callback(xmlhttp.responseText, xmlhttp.status)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xmlhttp.open(method, remote, true);
|
||||||
|
if (typeof body == "undefined") {
|
||||||
|
body = null
|
||||||
|
}
|
||||||
|
xmlhttp.send(body);
|
||||||
|
}
|
||||||
|
function noopcallback(responseBody, responseStatus) {
|
||||||
|
}
|
||||||
|
setInterval(() => {
|
||||||
|
http("GET", `/proxy?user=${document.getElementById("user").value}`, (b, s) => {
|
||||||
|
if (s != 200)
|
||||||
|
return
|
||||||
|
document.getElementById("ntfy").innerHTML = `<pre>${b}</pre>`
|
||||||
|
}, null)
|
||||||
|
}, 1500)
|
||||||
|
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</header>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<form>
|
||||||
|
<h3>WHO AM I</h3>
|
||||||
|
<select id="user">
|
||||||
|
<option>bel</option>
|
||||||
|
<option>zach</option>
|
||||||
|
<option>chase</option>
|
||||||
|
<option>mason</option>
|
||||||
|
<option>nat</option>
|
||||||
|
<option>roxy</option>
|
||||||
|
<option>bill</option>
|
||||||
|
</select>
|
||||||
|
</form>
|
||||||
|
<div>
|
||||||
|
<form action="" onsubmit="formsay(this.children.say.value); return false;" style="display: inline-block;">
|
||||||
|
<h3>SAY</h3>
|
||||||
|
<input type="text" name="say">
|
||||||
|
<input type="submit" value="say">
|
||||||
|
</form>
|
||||||
|
<form action="" onsubmit="formsend(this.children.send.value); return false;" style="display: inline-block;">
|
||||||
|
<h3>SEND</h3>
|
||||||
|
<select name="send">
|
||||||
|
<option>bel</option>
|
||||||
|
<option>zach</option>
|
||||||
|
<option>chase</option>
|
||||||
|
<option>mason</option>
|
||||||
|
<option>nat</option>
|
||||||
|
<option>roxy</option>
|
||||||
|
<option>bill</option>
|
||||||
|
</select>
|
||||||
|
<input type="submit" value="send">
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<details>
|
||||||
|
<summary>CONTROLS</summary>
|
||||||
|
<form id="controls">
|
||||||
|
<div style="display: flex; flex-wrap: wrap;">
|
||||||
|
<div>
|
||||||
|
<label for="input-up">Up</label>
|
||||||
|
<input id="input-up" type="text" maxLength=1 value="w" name="w" placeholder="up" onchange="recontrol()">
|
||||||
|
<label for="input-down">Down</label>
|
||||||
|
<input id="input-down" type="text" maxLength=1 value="s" name="s" placeholder="down" onchange="recontrol()">
|
||||||
|
<label for="input-left">Left</label>
|
||||||
|
<input id="input-left" type="text" maxLength=1 value="a" name="a" placeholder="left" onchange="recontrol()">
|
||||||
|
<label for="input-right">Right</label>
|
||||||
|
<input id="input-right" type="text" maxLength=1 value="d" name="d" placeholder="right" onchange="recontrol()">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="input-start">Start</label>
|
||||||
|
<input id="input-start" type="text" maxLength=1 value="5" name="5" placeholder="start" onchange="recontrol()">
|
||||||
|
<label for="input-left-bumper">Left Bumper</label>
|
||||||
|
<input id="input-left-bumper" type="text" maxLength=1 value="q" name="q" placeholder="l" onchange="recontrol()">
|
||||||
|
<label for="input-right-bumper">Right Bumper</label>
|
||||||
|
<input id="input-right-bumper" type="text" maxLength=1 value="e" name="e" placeholder="r" onchange="recontrol()">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label for="input-a">A</label>
|
||||||
|
<input id="input-a" type="text" maxLength=1 value="1" name="1" placeholder="a" onchange="recontrol()">
|
||||||
|
<label for="input-b">B</label>
|
||||||
|
<input id="input-b" type="text" maxLength=1 value="2" name="2" placeholder="b" onchange="recontrol()">
|
||||||
|
<label for="input-x">X</label>
|
||||||
|
<input id="input-x" type="text" maxLength=1 value="3" name="3" placeholder="x" onchange="recontrol()">
|
||||||
|
<label for="input-y">Y</label>
|
||||||
|
<input id="input-y" type="text" maxLength=1 value="4" name="4" placeholder="y" onchange="recontrol()">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</details>
|
||||||
|
</div>
|
||||||
|
<div id="ntfy"></div>
|
||||||
|
<div id="ws"></div>
|
||||||
|
<div id="konami" style="display:none"></div>
|
||||||
|
</body>
|
||||||
|
<footer>
|
||||||
|
<script>
|
||||||
|
var socket = new WebSocket("ws://"+window.location.host+"/api/ws")
|
||||||
|
|
||||||
|
function nosend(data) {
|
||||||
|
}
|
||||||
|
function dosend(data) {
|
||||||
|
console.log(JSON.stringify(data))
|
||||||
|
socket.send(JSON.stringify(data))
|
||||||
|
}
|
||||||
|
send = nosend
|
||||||
|
|
||||||
|
socket.addEventListener("open", (_) => {
|
||||||
|
console.log("ws open")
|
||||||
|
send = dosend
|
||||||
|
})
|
||||||
|
socket.addEventListener("message", (event) => console.log("ws recv:", event.data))
|
||||||
|
socket.addEventListener("close", (event) => console.log("ws closed"))
|
||||||
|
|
||||||
|
keys = {}
|
||||||
|
document.addEventListener('keydown', (event) => {
|
||||||
|
var name = controls[event.key]
|
||||||
|
if (!name)
|
||||||
|
return
|
||||||
|
if (keys[name])
|
||||||
|
return
|
||||||
|
keys[name] = true
|
||||||
|
sendKeys(name, "")
|
||||||
|
})
|
||||||
|
document.addEventListener('keyup', (event) => {
|
||||||
|
var name = controls[event.key]
|
||||||
|
if (!name)
|
||||||
|
return
|
||||||
|
keys[name] = false
|
||||||
|
sendKeys("", name)
|
||||||
|
})
|
||||||
|
function sendKeys(y, n) {
|
||||||
|
send({
|
||||||
|
T: new Date().getTime(),
|
||||||
|
U: document.getElementById("user").value,
|
||||||
|
Y: y,
|
||||||
|
N: n,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var controls = {}
|
||||||
|
function recontrol() {
|
||||||
|
for (var k in controls)
|
||||||
|
controls[k] = false
|
||||||
|
for (var e of document.getElementById("controls").getElementsByTagName("input"))
|
||||||
|
controls[e.value] = e.name
|
||||||
|
}
|
||||||
|
recontrol()
|
||||||
|
</script>
|
||||||
|
</footer>
|
||||||
|
</html>
|
||||||
|
|
@ -9,6 +9,8 @@ import (
|
||||||
var (
|
var (
|
||||||
FlagRawKeyboard = os.Getenv("RAW_KEYBOARD") == "true"
|
FlagRawKeyboard = os.Getenv("RAW_KEYBOARD") == "true"
|
||||||
FlagRawUDP = os.Getenv("RAW_UDP")
|
FlagRawUDP = os.Getenv("RAW_UDP")
|
||||||
|
FlagRawWS = os.Getenv("RAW_WS")
|
||||||
|
FlagDebug = os.Getenv("DEBUG") != ""
|
||||||
FlagRawRandomWeightFile = os.Getenv("RAW_RANDOM_WEIGHT_FILE")
|
FlagRawRandomWeightFile = os.Getenv("RAW_RANDOM_WEIGHT_FILE")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -21,6 +23,9 @@ func New(ctx context.Context) Raw {
|
||||||
if FlagRawKeyboard {
|
if FlagRawKeyboard {
|
||||||
return NewKeyboard()
|
return NewKeyboard()
|
||||||
}
|
}
|
||||||
|
if port, _ := strconv.Atoi(FlagRawWS); port != 0 {
|
||||||
|
return NewWS(ctx, port)
|
||||||
|
}
|
||||||
if port, _ := strconv.Atoi(FlagRawUDP); port != 0 {
|
if port, _ := strconv.Atoi(FlagRawUDP); port != 0 {
|
||||||
return NewUDP(ctx, port)
|
return NewUDP(ctx, port)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,5 @@ func TestRaw(t *testing.T) {
|
||||||
var _ Raw = &Random{}
|
var _ Raw = &Random{}
|
||||||
var _ Raw = UDP{}
|
var _ Raw = UDP{}
|
||||||
var _ Raw = Keyboard{}
|
var _ Raw = Keyboard{}
|
||||||
|
var _ Raw = WS{}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
FlagDebug = os.Getenv("DEBUG") == "true"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UDP struct {
|
type UDP struct {
|
||||||
conn net.PacketConn
|
conn net.PacketConn
|
||||||
c chan []byte
|
c chan []byte
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,148 @@
|
||||||
|
package raw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"embed"
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
FlagWSProxy = os.Getenv("RAW_WS_PROXY_URL")
|
||||||
|
FlagWSDebug = os.Getenv("RAW_WS_DEBUG") != ""
|
||||||
|
)
|
||||||
|
|
||||||
|
type WS struct {
|
||||||
|
ctx context.Context
|
||||||
|
can context.CancelFunc
|
||||||
|
ch chan []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWS(ctx context.Context, port int) WS {
|
||||||
|
ctx, can := context.WithCancel(ctx)
|
||||||
|
ws := WS{ctx: ctx, can: can, ch: make(chan []byte, 256)}
|
||||||
|
go ws.listen(port)
|
||||||
|
return ws
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) Read() []byte {
|
||||||
|
select {
|
||||||
|
case v := <-ws.ch:
|
||||||
|
return v
|
||||||
|
case <-ws.ctx.Done():
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) Close() {
|
||||||
|
ws.can()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) listen(port int) {
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: fmt.Sprintf(":%d", port),
|
||||||
|
Handler: ws,
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
if err := server.ListenAndServe(); err != nil && ws.ctx.Err() == nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
log.Println("WS on", port)
|
||||||
|
<-ws.ctx.Done()
|
||||||
|
server.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r = r.WithContext(ws.ctx)
|
||||||
|
if err := ws.serveHTTP(w, r); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) serveHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/api/ws":
|
||||||
|
return ws.serveWS(w, r)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(r.URL.Path, "/proxy") {
|
||||||
|
return ws.serveProxy(w, r)
|
||||||
|
}
|
||||||
|
return ws.serveStaticFile(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) serveProxy(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
u, err := url.Parse(FlagWSProxy)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/proxy")
|
||||||
|
if r.URL.Path == "" {
|
||||||
|
r.URL.Path = "/"
|
||||||
|
}
|
||||||
|
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||||
|
proxy.ServeHTTP(w, r)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:embed public/*
|
||||||
|
var staticFiles embed.FS
|
||||||
|
|
||||||
|
func (ws WS) serveStaticFile(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
if FlagWSDebug {
|
||||||
|
b, _ := os.ReadFile("src/device/input/raw/public/root.html")
|
||||||
|
w.Write(b)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if r.URL.Path == "/" {
|
||||||
|
r.URL.Path = "root.html"
|
||||||
|
}
|
||||||
|
r.URL.Path = path.Join("public", r.URL.Path)
|
||||||
|
http.FileServer(http.FS(staticFiles)).ServeHTTP(w, r)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) serveWS(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
if err := ws._serveWS(w, r); err != nil {
|
||||||
|
log.Println("_serveWS:", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) _serveWS(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
for ws.ctx.Err() == nil {
|
||||||
|
msgType, p, err := conn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
if websocket.IsCloseError(err) || websocket.IsUnexpectedCloseError(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if msgType == websocket.TextMessage {
|
||||||
|
log.Println(string(p))
|
||||||
|
ws.ch <- p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -53,6 +53,8 @@ var (
|
||||||
';': PSemicolon,
|
';': PSemicolon,
|
||||||
'-': PMinus,
|
'-': PMinus,
|
||||||
'=': PEqual,
|
'=': PEqual,
|
||||||
|
'<': PageDown,
|
||||||
|
'>': PageUp,
|
||||||
}
|
}
|
||||||
keyToChar = func() map[Key]byte {
|
keyToChar = func() map[Key]byte {
|
||||||
result := map[Key]byte{}
|
result := map[Key]byte{}
|
||||||
|
|
|
||||||
|
|
@ -57,4 +57,6 @@ const (
|
||||||
PSemicolon = Key(keybd_event.VK_SEMICOLON)
|
PSemicolon = Key(keybd_event.VK_SEMICOLON)
|
||||||
PMinus = Key(keybd_event.VK_MINUS)
|
PMinus = Key(keybd_event.VK_MINUS)
|
||||||
PEqual = Key(keybd_event.VK_EQUAL)
|
PEqual = Key(keybd_event.VK_EQUAL)
|
||||||
|
PageUp = Key(keybd_event.VK_PAGEUP)
|
||||||
|
PageDown = Key(keybd_event.VK_PAGEDOWN)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,9 @@ func Main(ctx context.Context) error {
|
||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if os.Getenv("DEBUG") == "true" {
|
||||||
|
log.Printf("src.Main.writer.Press(%+v) (from %+v)", keys, delta)
|
||||||
|
}
|
||||||
writer.Press(keys...)
|
writer.Press(keys...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue