split out message, config
parent
e1e2ce3eec
commit
44cb05487e
|
|
@ -0,0 +1,14 @@
|
||||||
|
package v01
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
Feedback struct {
|
||||||
|
Addr string
|
||||||
|
}
|
||||||
|
Users map[string]struct {
|
||||||
|
Player int
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
Players []struct {
|
||||||
|
Transformation transformation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package v01
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"mayhem-party/src/device/input/button"
|
||||||
|
)
|
||||||
|
|
||||||
|
type message struct {
|
||||||
|
T int64
|
||||||
|
U string
|
||||||
|
Y string
|
||||||
|
N string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg message) buttons() []button.Button {
|
||||||
|
buttons := make([]button.Button, len(msg.Y)+len(msg.N))
|
||||||
|
for i := range msg.Y {
|
||||||
|
buttons[i] = button.Button{Char: msg.Y[i], Down: true}
|
||||||
|
}
|
||||||
|
for i := range msg.N {
|
||||||
|
buttons[len(msg.Y)+i] = button.Button{Char: msg.N[i], Down: false}
|
||||||
|
}
|
||||||
|
if FlagDebug {
|
||||||
|
log.Printf("%+v", msg)
|
||||||
|
}
|
||||||
|
return buttons
|
||||||
|
}
|
||||||
|
|
@ -23,30 +23,12 @@ type (
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
can context.CancelFunc
|
can context.CancelFunc
|
||||||
src raw.Raw
|
src raw.Raw
|
||||||
cfg v01Cfg
|
cfg config
|
||||||
}
|
|
||||||
v01Msg struct {
|
|
||||||
T int64
|
|
||||||
U string
|
|
||||||
Y string
|
|
||||||
N string
|
|
||||||
}
|
|
||||||
v01Cfg struct {
|
|
||||||
Feedback struct {
|
|
||||||
Addr string
|
|
||||||
}
|
|
||||||
Users map[string]struct {
|
|
||||||
Player int
|
|
||||||
Message string
|
|
||||||
}
|
|
||||||
Players []struct {
|
|
||||||
Transformation transformation
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
||||||
var cfg v01Cfg
|
var cfg config
|
||||||
b, _ := ioutil.ReadFile(FlagParseV01Config)
|
b, _ := ioutil.ReadFile(FlagParseV01Config)
|
||||||
yaml.Unmarshal(b, &cfg)
|
yaml.Unmarshal(b, &cfg)
|
||||||
ctx, can := context.WithCancel(ctx)
|
ctx, can := context.WithCancel(ctx)
|
||||||
|
|
@ -72,47 +54,33 @@ func (v01 *V01) Close() {
|
||||||
|
|
||||||
func (v01 *V01) Read() []button.Button {
|
func (v01 *V01) Read() []button.Button {
|
||||||
line := v01.src.Read()
|
line := v01.src.Read()
|
||||||
var msg v01Msg
|
var msg message
|
||||||
if err := json.Unmarshal(line, &msg); err != nil {
|
if err := json.Unmarshal(line, &msg); err != nil {
|
||||||
log.Printf("%v: %s", err, line)
|
log.Printf("%v: %s", err, line)
|
||||||
}
|
}
|
||||||
v01.telemetry(msg)
|
v01.telemetry(msg)
|
||||||
|
|
||||||
return v01.cfg.transform(msg).buttons()
|
return v01.transform(msg).buttons()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg v01Cfg) transform(msg v01Msg) v01Msg {
|
func (v01 *V01) telemetry(msg message) {
|
||||||
if len(cfg.Players) == 0 {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
user := cfg.Users[msg.U]
|
|
||||||
if user.Player < 1 {
|
|
||||||
msg.Y = ""
|
|
||||||
msg.N = ""
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
player := cfg.Players[user.Player-1]
|
|
||||||
msg.Y = player.Transformation.pipe(msg.Y)
|
|
||||||
msg.N = player.Transformation.pipe(msg.N)
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v01 *V01) telemetry(msg v01Msg) {
|
|
||||||
if FlagDebug {
|
if FlagDebug {
|
||||||
log.Printf("%s|%dms", msg.U, time.Now().UnixNano()/int64(time.Millisecond)-msg.T)
|
log.Printf("%s|%dms", msg.U, time.Now().UnixNano()/int64(time.Millisecond)-msg.T)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg v01Msg) buttons() []button.Button {
|
func (v01 *V01) transform(msg message) message {
|
||||||
buttons := make([]button.Button, len(msg.Y)+len(msg.N))
|
if len(v01.cfg.Players) == 0 {
|
||||||
for i := range msg.Y {
|
return msg
|
||||||
buttons[i] = button.Button{Char: msg.Y[i], Down: true}
|
|
||||||
}
|
}
|
||||||
for i := range msg.N {
|
user := v01.cfg.Users[msg.U]
|
||||||
buttons[len(msg.Y)+i] = button.Button{Char: msg.N[i], Down: false}
|
if user.Player < 1 {
|
||||||
|
msg.Y = ""
|
||||||
|
msg.N = ""
|
||||||
|
return msg
|
||||||
}
|
}
|
||||||
if FlagDebug {
|
player := v01.cfg.Players[user.Player-1]
|
||||||
log.Printf("%+v", msg)
|
msg.Y = player.Transformation.pipe(msg.Y)
|
||||||
}
|
msg.N = player.Transformation.pipe(msg.N)
|
||||||
return buttons
|
return msg
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue