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
|
||||
can context.CancelFunc
|
||||
src raw.Raw
|
||||
cfg v01Cfg
|
||||
}
|
||||
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
|
||||
}
|
||||
cfg config
|
||||
}
|
||||
)
|
||||
|
||||
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
||||
var cfg v01Cfg
|
||||
var cfg config
|
||||
b, _ := ioutil.ReadFile(FlagParseV01Config)
|
||||
yaml.Unmarshal(b, &cfg)
|
||||
ctx, can := context.WithCancel(ctx)
|
||||
|
|
@ -72,47 +54,33 @@ func (v01 *V01) Close() {
|
|||
|
||||
func (v01 *V01) Read() []button.Button {
|
||||
line := v01.src.Read()
|
||||
var msg v01Msg
|
||||
var msg message
|
||||
if err := json.Unmarshal(line, &msg); err != nil {
|
||||
log.Printf("%v: %s", err, line)
|
||||
}
|
||||
v01.telemetry(msg)
|
||||
|
||||
return v01.cfg.transform(msg).buttons()
|
||||
return v01.transform(msg).buttons()
|
||||
}
|
||||
|
||||
func (cfg v01Cfg) transform(msg v01Msg) v01Msg {
|
||||
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) {
|
||||
func (v01 *V01) telemetry(msg message) {
|
||||
if FlagDebug {
|
||||
log.Printf("%s|%dms", msg.U, time.Now().UnixNano()/int64(time.Millisecond)-msg.T)
|
||||
}
|
||||
}
|
||||
|
||||
func (msg v01Msg) 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}
|
||||
func (v01 *V01) transform(msg message) message {
|
||||
if len(v01.cfg.Players) == 0 {
|
||||
return msg
|
||||
}
|
||||
for i := range msg.N {
|
||||
buttons[len(msg.Y)+i] = button.Button{Char: msg.N[i], Down: false}
|
||||
user := v01.cfg.Users[msg.U]
|
||||
if user.Player < 1 {
|
||||
msg.Y = ""
|
||||
msg.N = ""
|
||||
return msg
|
||||
}
|
||||
if FlagDebug {
|
||||
log.Printf("%+v", msg)
|
||||
}
|
||||
return buttons
|
||||
player := v01.cfg.Players[user.Player-1]
|
||||
msg.Y = player.Transformation.pipe(msg.Y)
|
||||
msg.N = player.Transformation.pipe(msg.N)
|
||||
return msg
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue