-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.go
138 lines (122 loc) · 3.83 KB
/
global.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
package quiz
import (
"encoding/json"
"errors"
"os"
"strings"
"sync"
"time"
"github.com/kesuaheli/twitchgo"
"github.com/spf13/viper"
)
var (
lastFetch time.Time
TwitchIRC *twitchgo.Session
joinedChannels map[string]*Connection = make(map[string]*Connection)
channelMapMu sync.RWMutex
)
func FetchQuestions() (err error) {
const timeout = 30 * time.Second
if time.Now().Add(timeout).Before(lastFetch) {
return nil
}
lastFetch = time.Now()
log.Println("Getting Quiz from Google Spreadsheet...")
Categories, err = ParseFromGoogleSheets(viper.GetString("google.spreadsheetID"))
if err != nil {
return err
}
var categoryCount, questionCount, answerCountCorrect, answerCountWrong int
for _, group := range Categories {
categoryCount += len(group.Categories)
for _, cat := range group.Categories {
questionCount += len(cat.Pool)
for _, q := range cat.Pool {
answerCountCorrect += len(q.Correct)
answerCountWrong += len(q.Wrong)
}
var data []byte
data, err = json.MarshalIndent(cat, "", " ")
if err != nil {
log.Printf("Error marshaling category '%s': %v", cat.ID, err)
continue
}
err = os.Mkdir("sheets/"+group.Title, os.ModeDir)
if err != nil {
if !errors.Is(err, os.ErrExist) {
log.Printf("Error creting json file of category '%s/%s': %v", group.Title, cat.ID, err)
continue
}
}
err = os.WriteFile("sheets/"+group.Title+"/"+cat.ID+".json", data, 0644)
if err != nil {
log.Printf("Error writing json file of category '%s/%s': %v", group.Title, cat.ID, err)
}
}
}
log.Printf("Got %d quiz categories in %d groups with a total of %d questions and %d correct and %d wrong answers (%d total) (%.3f%% correct)", categoryCount, len(Categories), questionCount, answerCountCorrect, answerCountWrong, answerCountCorrect+answerCountWrong, float64(answerCountCorrect)/float64(answerCountCorrect+answerCountWrong)*100)
return nil
}
// MsgToVote checks if msg is a valid vote for an answer. It returns the number of the voted answer
// (indexed 1). If msg isn't valid for a vote MsgToVote returns 0.
//
// If g is nil or the current round g is pointing to is nil all possible votes are valid. With g
// containing a non-nil round MsgToVote will get the maximum vote and invalidates all votes above,
// e.g., the current question has 2 answers but msg is a valid vote for answer 3, MsgToVote will
// return 0, because 3 is not a valid choice at this point.
func MsgToVote(msg string, g *Game) int {
var vote int
switch msg {
case "1", "a", "A":
vote = 1
case "2", "b", "B":
vote = 2
case "3", "c", "C":
vote = 3
case "4", "d", "D":
vote = 4
default:
return 0
}
if g == nil {
return vote
}
if g.Current == 0 || g.Current > len(g.Rounds) {
return 0
}
if g.Rounds[g.Current-1] == nil {
return vote
}
if vote > len(g.Rounds[g.Current-1].Answers) {
return 0
}
return vote
}
func (c *Connection) JoinTwitchChannel(channel string) {
channelMapMu.Lock()
defer channelMapMu.Unlock()
if old, ok := joinedChannels[channel]; ok {
log.Printf("Tried to join a connection (uID: %s) with a already connected Twitch channel (%s). Overriding it with uID: %s", old.userID, channel, c.userID)
}
TwitchIRC.JoinChannel(channel)
joinedChannels[channel] = c
}
// LeaveTwitchChannel leaves the twitch channel for the corresponding connection.
func (c *Connection) LeaveTwitchChannel() {
for channel, connection := range joinedChannels {
if connection == c {
delete(joinedChannels, channel)
}
}
}
func OnTwitchChannelMessage(t *twitchgo.Session, channel string, source *twitchgo.IRCUser, msg, msgID string, tags twitchgo.IRCMessageTags) {
channelMapMu.RLock()
defer channelMapMu.RUnlock()
channel, _ = strings.CutPrefix(channel, "#")
c, ok := joinedChannels[channel]
if !ok {
TwitchIRC.LeaveChannel(channel)
return
}
c.OnTwitchChannelMessage(t, source, msg, msgID, tags)
}