-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
320 lines (286 loc) · 12.7 KB
/
Code.gs
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
var games = [];
var properties = PropertiesService.getDocumentProperties();
var games_optimal_numplayers = [];
var games_recommended_numplayers = [];
var games_numplayers = [];
var sss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sss.getSheetByName('Games');
var username = sheet.getRange('B2').getValue();
var includeexpansions = sheet.getRange('B3').getValue();
var spieleranzahl = parseInt(sheet.getRange("B5").getValue());
var sortparameter = sheet.getRange("B7").getValue();
var sortall = sheet.getRange("B8").getValue();
function getUserGames() {
if (includeexpansions === 'no'){
var bggCollectionUrl = "https://www.boardgamegeek.com/xmlapi2/collection?username=" + username + "&subtype=boardgame&own=1&excludesubtype=boardgameexpansion";
}else{
var bggCollectionUrl = "https://www.boardgamegeek.com/xmlapi2/collection?username=" + username + "&subtype=boardgame&own=1";
}
var collectionResponse = UrlFetchApp.fetch(bggCollectionUrl + "&stats=1");
var collectionXmlData = collectionResponse.getContentText();
var collectionDocument = XmlService.parse(collectionXmlData);
var collectionRoot = collectionDocument.getRootElement();
var collectionItems = collectionRoot.getChildren('item');
clear();
var gameIds = [];
for (var i = 0; i < collectionItems.length; i++) {
var itemId = collectionItems[i].getAttribute('objectid').getValue();
gameIds.push(itemId);
}
var bggThingUrl = "https://www.boardgamegeek.com/xmlapi2/thing?id=" + gameIds.join(',') + "&stats=1";
var thingResponse = UrlFetchApp.fetch(bggThingUrl);
var thingXmlData = thingResponse.getContentText();
var thingDocument = XmlService.parse(thingXmlData);
var thingRoot = thingDocument.getRootElement();
var thingItems = thingRoot.getChildren('item');
for (var i = 0; i < thingItems.length; i++) {
var item = thingItems[i];
var game = {};
game.id = item.getAttribute('id').getValue();
game.name = item.getChild('name').getAttribute('value').getValue();
game.minplayers = item.getChild('minplayers').getAttribute('value').getValue();
game.maxplayers = item.getChild('maxplayers').getAttribute('value').getValue();
game.numplayers = [game.minplayers, game.maxplayers];
game.numplayers_display = game.numplayers[0] + ' - ' + game.numplayers[1];
game.weight = item.getChild('statistics').getChild('ratings').getChild('averageweight').getAttribute('value').getValue();
game.weight = parseFloat(game.weight).toFixed(1);
game.rating = item.getChild('statistics').getChild('ratings').getChild('average').getAttribute('value').getValue();
game.rating = parseFloat(game.rating).toFixed(1);
game.year = item.getChild('yearpublished').getAttribute('value').getValue();
game.mintime = item.getChild('minplaytime').getAttribute('value').getValue();
game.maxtime = item.getChild('maxplaytime').getAttribute('value').getValue();
if (game.mintime === game.maxtime){
game.time = game.mintime;
}else{
game.time = game.mintime + ' - ' + game.maxtime;
}
var polls = item.getChildren('poll');
for (var j = 0; j < polls.length; j++) {
var poll = polls[j];
if (poll.getAttribute('name').getValue() === 'suggested_numplayers') {
var results = poll.getChildren('results');
for (var k = 0; k < results.length; k++) {
var result = results[k];
var numplayers = parseInt(result.getAttribute('numplayers').getValue());
var playerPollResults = result.getChildren('result');
var bestplayers = 0;
var recommendedvotes = 0;
var notrecommendedvotes = 0;
for (var l = 0; l < playerPollResults.length; l++) {
var playerPollResult = playerPollResults[l];
var numvotes = playerPollResult.getAttribute('numvotes').getValue();
var value = playerPollResult.getAttribute('value').getValue();
if (value === 'Best') {
var bestplayers = numvotes;
} else if (value === 'Recommended') {
var recommendedvotes = numvotes;
} else if (value === 'Not Recommended') {
var notrecommendedvotes = numvotes;
}
var bestplayers_perc = 0;
var recommendedvotes_perc = 0;
var notrecommendedvotes_perc = 0;
var totalvotes = bestplayers + recommendedvotes + notrecommendedvotes;
var bestplayers_perc = (bestplayers * 100 / totalvotes);
var recommendedvotes_perc = (recommendedvotes * 100 / totalvotes);
var notrecommendedvotes_perc = (notrecommendedvotes * 100 / totalvotes);
}
if (bestplayers_perc > recommendedvotes_perc && bestplayers_perc > notrecommendedvotes_perc) {
if (!game.optimal_numplayers) {
game.optimal_numplayers = [numplayers];
} else {
game.optimal_numplayers.push(numplayers);
}
}
if (bestplayers_perc > notrecommendedvotes_perc || recommendedvotes_perc > notrecommendedvotes_perc) {
if (!game.recommended_numplayers) {
game.recommended_numplayers = [numplayers];
} else {
game.recommended_numplayers.push(numplayers);
}
}
}
}
}
if (game.optimal_numplayers && game.optimal_numplayers.length > 1) {
game.optimal_numplayers_display = game.optimal_numplayers[0] + ' - ' + game.optimal_numplayers[game.optimal_numplayers.length - 1];
} else if (game.optimal_numplayers && game.optimal_numplayers.length === 1) {
game.optimal_numplayers_display = game.optimal_numplayers[0];
} else {
game.optimal_numplayers_display = '-';
}
if (game.recommended_numplayers && game.recommended_numplayers.length > 1) {
game.recommended_numplayers_display = game.recommended_numplayers[0] + ' - ' + game.recommended_numplayers[game.recommended_numplayers.length - 1];
} else if (game.recommended_numplayers && game.recommended_numplayers.length === 1) {
game.recommended_numplayers_display = game.recommended_numplayers[0];
} else {
game.recommended_numplayers_display = '-';
}
games.push(game);
properties.setProperty("loadedGames", JSON.stringify(games));
}
sortgames();
}
function sortgames() {
var arrayString = properties.getProperty("loadedGames");
var games = JSON.parse(arrayString);
clear();
for (var i = 0; i < games.length; i++) {
var game = games[i];
if (game.optimal_numplayers) {
if (spieleranzahl >= game.optimal_numplayers[0] && spieleranzahl <= game.optimal_numplayers[game.optimal_numplayers.length - 1]) {
if (spieleranzahl >= game.recommended_numplayers[0] && spieleranzahl <= game.recommended_numplayers[game.recommended_numplayers.length - 1]) {
if (spieleranzahl >= game.numplayers[0] && spieleranzahl <= game.numplayers[game.numplayers.length - 1]) {
games_optimal_numplayers.push(game);
}
}
}
}
} properties.setProperty("games_optimal_numplayers", JSON.stringify(games_optimal_numplayers));
for (var i = 0; i < games.length; i++) {
var game = games[i];
if (game.recommended_numplayers) {
if (!game.optimal_numplayers || spieleranzahl < game.optimal_numplayers[0] || spieleranzahl > game.optimal_numplayers[game.optimal_numplayers.length - 1]) {
if (spieleranzahl >= game.recommended_numplayers[0] && spieleranzahl <= game.recommended_numplayers[game.recommended_numplayers.length - 1]) {
if (spieleranzahl >= game.numplayers[0] && spieleranzahl <= game.numplayers[game.numplayers.length - 1]) {
games_recommended_numplayers.push(game);
}
}
}
}
} properties.setProperty("games_recommended_numplayers", JSON.stringify(games_recommended_numplayers));
for (var i = 0; i < games.length; i++) {
var game = games[i];
if (game.numplayers) {
if (!game.optimal_numplayers || spieleranzahl < game.optimal_numplayers[0] || spieleranzahl > game.optimal_numplayers[game.optimal_numplayers.length - 1]) {
if (!game.recommended_numplayers || spieleranzahl < game.recommended_numplayers[0] || spieleranzahl > game.recommended_numplayers[game.recommended_numplayers.length - 1]) {
if (spieleranzahl >= game.numplayers[0] && spieleranzahl <= game.numplayers[game.numplayers.length - 1]) {
games_numplayers.push(game);
}
}
}
}
} properties.setProperty("games_numplayers", JSON.stringify(games_numplayers));
sort_by();
}
function sort_by() {
var arrayString = properties.getProperty("games_optimal_numplayers");
var games_optimal_numplayers = JSON.parse(arrayString);
var arrayString = properties.getProperty("games_recommended_numplayers");
var games_recommended_numplayers = JSON.parse(arrayString);
var arrayString = properties.getProperty("games_numplayers");
var games_numplayers = JSON.parse(arrayString);
switch (sortparameter) {
case 'name':
sortparameter = "name";
break;
case 'published':
sortparameter = "year";
break;
case 'rating':
sortparameter = "rating";
break;
case 'time':
sortparameter = "mintime";
break;
case 'weight':
sortparameter = "weight";
break;
}
clear();
if (sortall == 'yes') {
var games = [];
for (var i = 0; i < Math.max(games_optimal_numplayers.length, games_recommended_numplayers.length, games_numplayers.length); i++) {
if (i < games_optimal_numplayers.length) {
games.push(games_optimal_numplayers[i]);
}
if (i < games_recommended_numplayers.length) {
games.push(games_recommended_numplayers[i]);
}
if (i < games_numplayers.length) {
games.push(games_numplayers[i]);
}
}
games.sort(function (a, b) {
return a[sortparameter] - b[sortparameter];
});
for (var i = 0; i < games.length; i++) {
var game = games[i];
addgames(game);
}
} else {
if (games_optimal_numplayers && games_optimal_numplayers.length > 0) {
games_optimal_numplayers.sort(function (a, b) {
return a[sortparameter] - b[sortparameter];
});
for (var i = 0; i < games_optimal_numplayers.length; i++) {
var game = games_optimal_numplayers[i];
addgames(game);
}
sheet.getRange("I"+sheet.getLastRow()).setValue("↑ best for Playercount")
sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).setBorder(null, null, true, null, null, null);
}
if (games_recommended_numplayers && games_recommended_numplayers.length > 0) {
games_recommended_numplayers.sort(function (a, b) {
return a[sortparameter] - b[sortparameter];
});
for (var i = 0; i < games_recommended_numplayers.length; i++) {
var game = games_recommended_numplayers[i];
addgames(game);
}
sheet.getRange("I"+sheet.getLastRow()).setValue("↑ recommended for Playercount")
sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).setBorder(null, null, true, null, null, null);
}
if (games_numplayers && games_numplayers.length > 0) {
games_numplayers.sort(function (a, b) {
return a[sortparameter] - b[sortparameter];
});
for (var i = 0; i < games_numplayers.length; i++) {
var game = games_numplayers[i];
addgames(game);
}
sheet.getRange("I"+sheet.getLastRow()).setValue("↑ possible for Playercount")
}
}
colorcodeweight();
}
function addgames(game) {
sheet.appendRow([
'=HYPERLINK("https://boardgamegeek.com/boardgame/' + game.id + '","' + game.name + '")',
game.year,
game.rating,
game.time,
game.weight,
game.numplayers_display,
game.recommended_numplayers_display,
game.optimal_numplayers_display
]);
}
function colorcodeweight() {
var rules = sheet.getConditionalFormatRules();
var rulea = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied("=AND(E7>=1, E7<=2)")
.setBackground("#93c47d")
.setRanges([sheet.getRange("E7:E")])
.build();
rules.push(rulea);
var ruleb = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied("=AND(E7>2, E7<=3)")
.setBackground("#f6b26b")
.setRanges([sheet.getRange("E7:E")])
.build();
rules.push(ruleb);
var rulec = SpreadsheetApp.newConditionalFormatRule()
.whenFormulaSatisfied("=AND(E7>3, E7<=5)")
.setBackground("#e06666")
.setRanges([sheet.getRange("E7:E")])
.build();
rules.push(rulec);
sheet.setConditionalFormatRules(rules);
}
function clear() {
var firstrowwithcontent = 11;
sheet.getRange("A"+firstrowwithcontent+":I").clear();
sheet.getRange("D"+firstrowwithcontent+":D").setNumberFormat("@").setHorizontalAlignment("center");
sheet.getRange("F"+firstrowwithcontent+":H").setNumberFormat("@").setHorizontalAlignment("center");
}