-
Notifications
You must be signed in to change notification settings - Fork 23
/
game_test.go
69 lines (60 loc) · 1.54 KB
/
game_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package fam100
import (
"math/rand"
"testing"
"github.com/yulrizka/fam100/model"
"github.com/yulrizka/fam100/qna"
"github.com/yulrizka/fam100/repo"
)
func Test_Game(t *testing.T) {
repo.RedisPrefix = "test_fam100"
if err := repo.DefaultDB.Init(); err != nil {
t.Fatalf("failed to load database: %v", err)
}
if err := repo.DefaultDB.Reset(); err != nil {
t.Fatalf("failed to reset database: %v", err)
}
questionDB, err := qna.NewText("qna/famili100.txt")
if err != nil {
t.Fatalf("failed to load questions: %v", err)
}
t.Run("test questionString", func(t *testing.T) {
var seed, totalRoundPlayed = 7, 0
players := map[model.PlayerID]model.Player{
"1": {ID: "1", Name: "foo"},
"2": {ID: "2", Name: "bar"},
"3": {ID: "3", Name: "baz"},
}
q, err := questionDB.NextQuestion(int64(seed), totalRoundPlayed, 10)
if err != nil {
t.Fatalf("failed to get next questions: %v", err)
}
r, err := newRound(q, players)
if err != nil {
t.Error(err)
}
r.state = Started
rand.Seed(7)
idx := rand.Perm(len(r.q.Answers))
for i := 0; i < len(players); i++ {
answerText := r.q.Answers[idx[i]].Text[0]
// get random player
var player model.Player
for _, value := range players {
player = value
break
}
r.answer(player, answerText)
}
// no checking, just to debug output
/*
showUnAnswered := false
fmt.Print(r.questionText(showUnAnswered))
fmt.Println()
for pID, score := range r.scores() {
p := r.players[pID]
fmt.Printf("p.name = %s, score = %d\n", p.Name, score)
}
*/
})
}