unit tests are good and v01cfg transforms input if players and user in players

This commit is contained in:
bel
2023-03-25 09:06:43 -06:00
parent 0ee3a8b6e8
commit db69f76aa0
3 changed files with 103 additions and 36 deletions

View File

@@ -30,20 +30,10 @@ type (
Message string
}
Players []struct {
Buttons struct {
Up string
Down string
Left string
Right string
L string
R string
A string
B string
X string
Y string
}
Transformation v01Transformation
}
}
v01Transformation map[string]string
)
func NewV01(src raw.Raw) V01 {
@@ -67,7 +57,33 @@ func (v01 V01) Read() []Button {
log.Printf("%v: %s", err, line)
}
v01.telemetry(msg)
return msg.buttons()
return v01.cfg.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 (t v01Transformation) pipe(s string) string {
for i := range s {
if v := t[s[i:i+1]]; v != "" {
s = s[:i] + v[:1] + s[i+1:]
}
}
return s
}
func (v01 V01) telemetry(msg v01Msg) {