simplify input package but also build secretly new config file option
This commit is contained in:
@@ -5,5 +5,5 @@ type Input interface {
|
||||
}
|
||||
|
||||
func New() Input {
|
||||
return NewRandom(RandomCharFromRange('a', 'g'))
|
||||
return NewRandom(randomCharFromRange('a', 'g'))
|
||||
}
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-yaml/yaml"
|
||||
)
|
||||
|
||||
type Button struct {
|
||||
@@ -35,13 +41,36 @@ func (r *Random) Read() []Button {
|
||||
}
|
||||
}
|
||||
|
||||
func RandomCharFromRange(start, stop byte) func() byte {
|
||||
func randomCharFromRange(start, stop byte) func() byte {
|
||||
return func() byte {
|
||||
return start + byte(rand.Int()%int(1+stop-start))
|
||||
}
|
||||
}
|
||||
|
||||
func RandomCharFromWeights(m map[byte]int) func() byte {
|
||||
func randomCharFromWeightFile(p string) func() byte {
|
||||
b, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return randomCharFromWeightReader(bytes.NewReader(b))
|
||||
}
|
||||
|
||||
func randomCharFromWeightReader(r io.Reader) func() byte {
|
||||
var m map[string]int
|
||||
if err := yaml.NewDecoder(r).Decode(&m); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
byted := map[byte]int{}
|
||||
for k, v := range m {
|
||||
if len(k) != 1 {
|
||||
panic(fmt.Sprintf("keys must be 1 character but got %q", k))
|
||||
}
|
||||
byted[k[0]] = v
|
||||
}
|
||||
return randomCharFromWeights(byted)
|
||||
}
|
||||
|
||||
func randomCharFromWeights(m map[byte]int) func() byte {
|
||||
type pair struct {
|
||||
b byte
|
||||
i int
|
||||
|
||||
16
src/device/input/random_exported_test.go
Normal file
16
src/device/input/random_exported_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package input_test
|
||||
|
||||
import (
|
||||
"mayhem-party/src/device/input"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
r := input.New()
|
||||
for i := 0; i < 15; i++ {
|
||||
batch := r.Read()
|
||||
for _, got := range batch {
|
||||
t.Logf("[%d] %c:%v", i, got.Char, got.Down)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,45 @@
|
||||
package input_test
|
||||
package input
|
||||
|
||||
import (
|
||||
"mayhem-party/src/device/input"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRandom(t *testing.T) {
|
||||
r := input.NewRandom(input.RandomCharFromRange('a', 'z'))
|
||||
for i := 0; i < 100; i++ {
|
||||
batch := r.Read()
|
||||
for _, got := range batch {
|
||||
t.Logf("[%d] %c:%v", i, got.Char, got.Down)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomCharFromWeights(t *testing.T) {
|
||||
weights := map[byte]int{
|
||||
'a': 1,
|
||||
'b': 99,
|
||||
}
|
||||
foo := input.RandomCharFromWeights(weights)
|
||||
foo := randomCharFromWeights(weights)
|
||||
for {
|
||||
got := foo()
|
||||
t.Logf("%c", got)
|
||||
if got == 'a' {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomCharFromWeightsReader(t *testing.T) {
|
||||
t.Run("json", func(t *testing.T) {
|
||||
b := `{"a": 1}`
|
||||
r := strings.NewReader(b)
|
||||
foo := randomCharFromWeightReader(r)
|
||||
for i := 0; i < 10; i++ {
|
||||
if got := foo(); got != 'a' {
|
||||
t.Errorf("%c", got)
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("yaml", func(t *testing.T) {
|
||||
b := `
|
||||
a: 2000
|
||||
`
|
||||
r := strings.NewReader(b)
|
||||
foo := randomCharFromWeightReader(r)
|
||||
for i := 0; i < 10; i++ {
|
||||
if got := foo(); got != 'a' {
|
||||
t.Errorf("%c", got)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user