87 lines
1.5 KiB
Go
87 lines
1.5 KiB
Go
package v01
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"mayhem-party/src/device/input/button"
|
|
"mayhem-party/src/device/input/raw"
|
|
"os"
|
|
"time"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
var (
|
|
FlagDebug = os.Getenv("DEBUG") == "true"
|
|
FlagParseV01Config = os.Getenv("V01_CONFIG")
|
|
)
|
|
|
|
type (
|
|
V01 struct {
|
|
ctx context.Context
|
|
can context.CancelFunc
|
|
src raw.Raw
|
|
cfg config
|
|
}
|
|
)
|
|
|
|
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
|
var cfg config
|
|
b, _ := ioutil.ReadFile(FlagParseV01Config)
|
|
yaml.Unmarshal(b, &cfg)
|
|
ctx, can := context.WithCancel(ctx)
|
|
result := &V01{
|
|
ctx: ctx,
|
|
can: can,
|
|
src: src,
|
|
cfg: cfg,
|
|
}
|
|
go result.listen()
|
|
return result
|
|
}
|
|
|
|
func (v01 *V01) CloseWrap() raw.Raw {
|
|
v01.can()
|
|
return v01.src
|
|
}
|
|
|
|
func (v01 *V01) Close() {
|
|
v01.can()
|
|
v01.src.Close()
|
|
}
|
|
|
|
func (v01 *V01) Read() []button.Button {
|
|
line := v01.src.Read()
|
|
var msg message
|
|
if err := json.Unmarshal(line, &msg); err != nil {
|
|
log.Printf("%v: %s", err, line)
|
|
}
|
|
v01.telemetry(msg)
|
|
|
|
return v01.transform(msg).buttons()
|
|
}
|
|
|
|
func (v01 *V01) telemetry(msg message) {
|
|
if FlagDebug {
|
|
log.Printf("%s|%dms", msg.U, time.Now().UnixNano()/int64(time.Millisecond)-msg.T)
|
|
}
|
|
}
|
|
|
|
func (v01 *V01) transform(msg message) message {
|
|
if len(v01.cfg.Players) == 0 {
|
|
return msg
|
|
}
|
|
user := v01.cfg.Users[msg.U]
|
|
if user.Player < 1 {
|
|
msg.Y = ""
|
|
msg.N = ""
|
|
return msg
|
|
}
|
|
player := v01.cfg.Players[user.Player-1]
|
|
msg.Y = player.Transformation.pipe(msg.Y)
|
|
msg.N = player.Transformation.pipe(msg.N)
|
|
return msg
|
|
}
|