/
main.go
155 lines (131 loc) · 3 KB
/
main.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/unixpickle/kahoot-hack/kahoot"
)
var wg sync.WaitGroup
const ConnectionDelay = time.Millisecond * 100
var StatisticsChan = make(chan int, 0)
var AnswerCount uint32
func main() {
if len(os.Args) != 3 && len(os.Args) != 4 {
fmt.Fprintln(os.Stderr, "Usage: rand <game pin> <nickname prefix> <count>")
fmt.Fprintln(os.Stderr, " rand <game pin> <name_list.txt>")
os.Exit(1)
}
gamePin := os.Args[1]
nicknames, err := readNicknames()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
botCount := 0
for nickname := range nicknames {
wg.Add(1)
go launchConnection(gamePin, nickname)
time.Sleep(ConnectionDelay)
botCount++
}
fmt.Println("Entered with", botCount, "bots.")
fmt.Println("Terminate this program to stop the automatons...")
go printStatistics(botCount)
wg.Wait()
}
func launchConnection(gamePin string, nickname string) {
defer wg.Done()
conn, err := kahoot.NewConn(gamePin)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to connect:", err)
os.Exit(1)
}
closed := make(chan bool, 1)
closed <- false
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
<-closed
closed <- true
conn.GracefulClose()
}()
if err := conn.Login(nickname); err != nil {
fmt.Fprintln(os.Stderr, "failed to login:", err)
os.Exit(1)
}
quiz := kahoot.NewQuiz(conn)
for {
action, err := quiz.Receive()
if err != nil {
if <-closed {
return
} else {
fmt.Fprintln(os.Stderr, "Could not receive question:", err)
os.Exit(1)
}
}
if action.Type == kahoot.QuestionAnswers {
atomic.StoreUint32(&AnswerCount, uint32(action.NumAnswers))
answer := rand.Intn(action.NumAnswers)
quiz.Send(action.AnswerMap[answer])
StatisticsChan <- answer
}
}
}
func printStatistics(clientCount int) {
qid := 1
for {
stats := map[int]int{}
for i := 0; i < clientCount; i++ {
stats[<-StatisticsChan]++
}
answerCount := int(atomic.LoadUint32(&AnswerCount))
fmt.Println("-----STATISTICS-----")
fmt.Println("Question ID:", qid)
for i := 0; i < answerCount; i++ {
fmt.Println("Answer "+strconv.Itoa(i)+":", stats[i])
}
qid++
}
}
func readNicknames() (<-chan string, error) {
if len(os.Args) == 3 {
contents, err := ioutil.ReadFile(os.Args[2])
if err != nil {
return nil, err
}
nameLines := strings.Split(string(contents), "\n")
res := make(chan string, len(nameLines))
for _, line := range nameLines {
nickname := strings.TrimSpace(line)
if len(nickname) != 0 {
res <- nickname
}
}
close(res)
return res, nil
}
count, err := strconv.Atoi(os.Args[3])
if err != nil {
return nil, errors.New("invalid count: " + os.Args[3])
}
baseName := os.Args[2]
res := make(chan string)
go func() {
for i := 0; i < count; i++ {
res <- baseName + strconv.Itoa(i+1)
}
close(res)
}()
return res, nil
}