-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheets.go
377 lines (332 loc) · 9.7 KB
/
sheets.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/sheets/v4"
)
const (
spreadsheetATID = "1xvR1BOLcFEL42wtplSbnTRVh-y2FuOkjp-1bDVFJZJo"
spreadsheetWPID = "1DTquUKV-ayLsKU64P9w9Lcs2oddDrpiGjcOfsKsPiYk"
playerRangeAT = "Trap Trial!B2:D"
readRangeAT = "Trap Trial!E%d:%d"
playerRangeWP = "Ark1!B2:B51"
readRangeWP = "Ark1!E%d:%d"
)
var (
// redRanges = []string{"Trap Trial!D%d:LB", "Trap Trial!LF%d:LJ", "Trap Trial!LN%d:LP", "Trap Trial!LT%d:LW"}
// blueRanges = []string{readRange}
redMaps = []int{311, 312, 313, 319, 320, 321, 325, 326, 327, 331, 332, 333, 334}
blueMaps = []int{314, 315, 316, 322, 323, 324, 328, 329, 330, 335, 336, 337, 338}
srv *sheets.Service
spaceRgx = regexp.MustCompile(`\s\s+`)
)
const (
TeamNone = iota
TeamRed
TeamBlue
)
type sheetPlayer struct {
row int
name string
team int
}
func extractSheetPlayers(vrange *sheets.ValueRange) []sheetPlayer {
r := make([]sheetPlayer, len(vrange.Values))
for i, row := range vrange.Values {
if len(row) > 0 && len(row[0].(string)) > 0 {
team := TeamNone
if len(row) > 2 {
switch row[2] {
case "R":
team = TeamRed
case "B":
team = TeamBlue
}
}
sp := sheetPlayer{
row: i,
name: row[0].(string),
team: team,
}
r[i] = sp
}
}
return r
}
func matchName(expr string, possible []sheetPlayer) (sheetPlayer, bool) {
aliases := make([][]string, len(possible))
for i, p := range possible {
aliases[i] = strings.Split(p.name, "|")
}
// simple alias search
for i, a := range aliases {
for _, alias := range a {
if strings.EqualFold(alias, expr) {
return possible[i], true
}
}
}
// fuzzy search
// playerNames := make([]string, len(possible))
// for i := range possible {
// playerNames[i] = aliases[i][0] // only check the first alias
// }
// var best float32 = math.MaxFloat32
// var bestIdx int
// for i, pname := range playerNames {
// dist := float32(levenshtein.ComputeDistance(pname, expr))
// threshold := 0.2 * float32(len(expr))
// if dist <= threshold && dist < best {
// best = dist
// if dist == 0 {
// return possible[i], true
// }
// bestIdx = i
// }
// }
// if best < math.MaxFloat32 {
// return possible[bestIdx], true
// }
// no match found :(
return sheetPlayer{}, false
}
func findUnbeatenAT(players []string) ([]string, error) {
playersResp, err := srv.Spreadsheets.Values.Get(spreadsheetATID, playerRangeAT).Do()
if err != nil {
return nil, fmt.Errorf("unable to retrieve players from sheet: %v", err)
}
sheetPlayers := extractSheetPlayers(playersResp)
wantRanges := []string{fmt.Sprintf(readRangeAT, 1, 1)}
found := []string{}
var hasRed, hasBlue bool
for _, player := range players {
// matched := strings.EqualFold(player, sheetPlayer.name)
match, matched := matchName(player, sheetPlayers)
fmt.Printf("matched %s with %s\n", player, match.name)
if matched {
found = append(found, player)
wantRow := match.row + 2 // +2 to skip name and team columns
wantRanges = append(wantRanges, fmt.Sprintf(readRangeAT, wantRow, wantRow))
switch match.team {
case TeamRed:
hasRed = true
case TeamBlue:
hasBlue = true
}
}
}
searchFailed := len(found) != len(players)
if searchFailed {
// construct an informative error message
notfound := []string{}
for _, p := range players {
missing := true
for _, f := range found {
if strings.EqualFold(p, f) {
missing = false
}
}
if missing {
notfound = append(notfound, p)
}
}
return nil, fmt.Errorf("the following names were not found on the spreadsheet: %s.\n<https://docs.google.com/spreadsheets/d/1xvR1BOLcFEL42wtplSbnTRVh-y2FuOkjp-1bDVFJZJo/edit?usp=sharing>", strings.Join(notfound, ", "))
}
resp, err := srv.Spreadsheets.Values.BatchGet(spreadsheetATID).Ranges(wantRanges...).Do()
if err != nil {
return nil, err
}
unbeaten := []string{}
titles, ranges := resp.ValueRanges[0].Values[0], resp.ValueRanges[1:]
// fmt.Println("teams:", ranges[0].Values[0][0])
fmt.Println("titles:", len(titles))
colLoop:
for col := 0; col < len(titles); col++ {
// -4 because ABCD columns are not levels
if hasBlue && !hasRed {
for _, s := range redMaps {
if s-4 == col {
continue colLoop
}
}
} else if hasRed && !hasBlue {
for _, s := range blueMaps {
if s-4 == col {
continue colLoop
}
}
} else {
for _, s := range append(redMaps, blueMaps...) {
if s-4 == col {
continue colLoop
}
}
}
beat := false
for _, valueRange := range ranges {
// fmt.Println("checking",i,"for ")
// if col < len(valueRange.Values[0]) {
// v := valueRange.Values[0][col]
// fmt.Printf("(%d) lvl: %s\n", i, v)
// }
if len(valueRange.Values) > 0 && col < len(valueRange.Values[0]) && valueRange.Values[0][col].(string) != "" {
beat = true
break
}
}
if !beat {
title := titles[col].(string)
// spaces are used in between words to force them on the next line so fix that
title = spaceRgx.ReplaceAllString(title, " ")
for _, exclusion := range unbeatenExclusionsAT {
if title == exclusion {
continue colLoop
}
}
unbeaten = append(unbeaten, title)
}
}
return unbeaten, nil
}
func findUnbeatenWP(players []string) ([]string, error) {
playersResp, err := srv.Spreadsheets.Values.Get(spreadsheetWPID, playerRangeWP).Do()
if err != nil {
return nil, fmt.Errorf("unable to retrieve players from sheet: %v", err)
}
sheetPlayers := extractSheetPlayers(playersResp)
wantRanges := []string{fmt.Sprintf(readRangeWP, 1, 1)}
found := []string{}
for _, player := range players {
match, matched := matchName(player, sheetPlayers)
fmt.Printf("matched %s with %s\n", player, match.name)
if matched {
found = append(found, player)
wantRow := match.row + 2 // +2 to skip name and team columns
wantRanges = append(wantRanges, fmt.Sprintf(readRangeWP, wantRow, wantRow))
}
}
if len(found) != len(players) {
notfound := []string{}
for _, p := range players {
missing := true
for _, f := range found {
if strings.EqualFold(p, f) {
missing = false
}
}
if missing {
notfound = append(notfound, p)
}
}
return nil, fmt.Errorf("the following names were not found on the spreadsheet: %s.\n<https://docs.google.com/spreadsheets/d/1DTquUKV-ayLsKU64P9w9Lcs2oddDrpiGjcOfsKsPiYk/edit#gid=0>", strings.Join(notfound, ", "))
}
resp, err := srv.Spreadsheets.Values.BatchGet(spreadsheetWPID).Ranges(wantRanges...).Do()
if err != nil {
return nil, err
}
unbeaten := []string{}
titles, ranges := resp.ValueRanges[0].Values[0], resp.ValueRanges[1:]
// fmt.Println("teams:", ranges[0].Values[0][0])
fmt.Println("titles:", len(titles))
colLoop:
for col := 0; col < len(titles); col++ {
beat := false
for _, valueRange := range ranges {
// fmt.Println("checking",i,"for ")
// if col < len(valueRange.Values[0]) {
// v := valueRange.Values[0][col]
// fmt.Printf("(%d) lvl: %s\n", i, v)
// }
if len(valueRange.Values) > 0 && col < len(valueRange.Values[0]) && valueRange.Values[0][col].(string) != "" {
beat = true
break
}
}
if !beat {
title := titles[col].(string)
// spaces are used in between words to force them on the next line so fix that
title = spaceRgx.ReplaceAllString(title, " ")
for _, exclusion := range unbeatenExclusionsWP {
if title == exclusion {
continue colLoop
}
}
unbeaten = append(unbeaten, title)
}
}
return unbeaten, nil
}
func initSheets() {
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets.readonly")
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
srv, err = sheets.New(client)
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
}
}
// Retrieve a token, saves the token, then returns the generated client.
func getClient(config *oauth2.Config) *http.Client {
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
tokFile := "token.json"
tok, err := tokenFromFile(tokFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(tokFile, tok)
}
return config.Client(context.Background(), tok)
}
// Request a token from the web, then returns the retrieved token.
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\n", authURL)
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
log.Fatalf("Unable to read authorization code: %v", err)
}
tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
log.Fatalf("Unable to retrieve token from web: %v", err)
}
return tok
}
// Retrieves a token from a local file.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
// Saves a token to a file path.
func saveToken(path string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}