-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivia.go
154 lines (137 loc) · 3.38 KB
/
trivia.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
package main
import (
"bytes"
"math"
"net/http"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/go-joe/joe"
"github.com/lithammer/fuzzysearch/fuzzy"
)
type Trivia struct {
Question string `json:"question"`
RawAnswer string `json:"answer"`
Answers []string `json:"altanswers"`
}
const triviaStoreKey string = "edi.trivia"
var TRIVIAROOMS = []string{
"", // Terminal
"C035YQ3UG79", // testing
"CFZAU0M2M", // personal
"C04FTKUMDBN", // rdc
}
func pruneAnswer(answer string) []string {
// This just compiles a list of possible answers to assist
// the fuzzy match
var output []string = []string{answer}
// Filter out any articles
articleReg := regexp.MustCompile(`(?i)\b(a|an|the)\b\s+`)
articlePrune := articleReg.ReplaceAllString(answer, "")
if articlePrune != answer {
output = append(output, articlePrune)
}
// Filter out any punctuation or special chars
puncReg := regexp.MustCompile(`(\.|,|&|!|-|")`)
puncPrune := puncReg.ReplaceAllString(answer, "")
if puncPrune != answer {
output = append(output, puncPrune)
}
output = append(output, strings.ToLower(answer))
return output
}
func newTriviaQuestion(t *Trivia) error {
payload := []byte(`num=1&add=address&unique=true`)
body := bytes.NewReader(payload)
url := "https://www.getrandomthings.com/data/random-question-answer.php"
resp, err := http.Post(url, "application/x-www-form-urlencoded; charset=UTF-8", body)
if err != nil {
return err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return err
}
t.Question = strings.TrimSpace(doc.Find("div.name2").First().Text())
t.RawAnswer = strings.TrimSpace(doc.Find("div#result").First().Text())
t.Answers = pruneAnswer(t.RawAnswer)
Edi.Logger.Info("Trivia question: " + t.Question + " - " + t.RawAnswer)
return nil
}
func askQuestion(msg *joe.Message) error {
q := Trivia{}
err := newTriviaQuestion(&q)
if err != nil {
return err
}
Edi.Store.Set(triviaStoreKey, q)
msg.Respond(q.Question)
return nil
}
func answerQuestion(correct bool, msg *joe.Message) error {
q := Trivia{}
ok, err := Edi.Store.Get(triviaStoreKey, &q)
if err != nil {
return err
}
if !ok {
msg.Respond("Need to ask a question first")
return nil
}
correctStr := ""
if correct {
correctStr = "Correct!! "
}
msg.Respond("%s%s -- %s", correctStr, q.Question, q.RawAnswer)
return askQuestion(msg)
}
func TriviaQuestion(msg joe.Message) error {
if !correctRoom(msg, TRIVIAROOMS) {
return nil
}
err := askQuestion(&msg)
if err != nil {
return err
}
return err
}
func TriviaAnswer(msg joe.Message) error {
if !correctRoom(msg, TRIVIAROOMS) {
return nil
}
err := answerQuestion(false, &msg)
if err != nil {
return err
}
return nil
}
func TriviaGuess(msg joe.Message) error {
if !correctRoom(msg, TRIVIAROOMS) {
return nil
}
q := Trivia{}
ok, err := Edi.Store.Get(triviaStoreKey, &q)
if err != nil {
return err
}
if ok {
matches := fuzzy.RankFindFold(msg.Text, q.Answers)
// Set up an 85% rule for fuzzy matching
acceptDiff := int(math.Round(float64(len(q.RawAnswer)) * 0.85))
accept := len(q.RawAnswer) - acceptDiff
if accept < 3 {
accept = 3
}
for _, m := range matches {
if m.Distance <= accept {
return answerQuestion(true, &msg)
}
}
}
return nil
}
func TriviaGiveUp(msg joe.Message) error {
msg.Respond("Sorry :(")
return answerQuestion(false, &msg)
}