-
Notifications
You must be signed in to change notification settings - Fork 0
/
sojdle.js
413 lines (391 loc) · 13 KB
/
sojdle.js
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const gameListSize = 1178;
if (context.params.event.channel_id == '944567098502438932') {
let sheet = "games_list";
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let sojID;
//Processing the entry
let guess = context.params.event.data.options[0].value;
// Normalize and remove anything that's not a letter, a space or a digit
let normalizedGuess = guess.normalize("NFD").replace(/[^a-zA-Z0-9 ]/g, "");
// Switch everything to lowercase
let lowercaseGuess = normalizedGuess.toLowerCase();
// Change the guess into emojis
let text = [];
text.push('> ');
for (i = 0; i < lowercaseGuess.length; i++) {
// Spaces
if (lowercaseGuess.charAt(i) === ' ') {
text.push(':blue_square:');
}
// Letters
else if (lowercaseGuess.charAt(i).match(/[a-z]/g) !== null) {
text.push(':regional_indicator_' + lowercaseGuess.charAt(i) + ':');
}
// Digits : no test needed - if not a space or a letter, it must be a digit. If you want to add a test, use : if (lowercaseGuess.charAt(i).match(/[0-9]/g) !== null)
else {
switch (lowercaseGuess.charAt(i)) {
case '0':
text.push(':zero:');
break;
case '1':
text.push(':one:');
break;
case '2':
text.push(':two:');
break;
case '3':
text.push(':three:');
break;
case '4':
text.push(':four:');
break;
case '5':
text.push(':five:');
break;
case '6':
text.push(':six:');
break;
case '7':
text.push(':seven:');
break;
case '8':
text.push(':eight:');
break;
case '9':
text.push(':nine:');
break;
}
}
}
text.push('\n');
//Check if we look for a new guess or for the same
let check = await lib.googlesheets.query['@0.3.0'].select({
range: `${sheet}!E:H`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'check': 'sojID'
}
],
limit: {
'count': 0,
'offset': 0
},
});
let intentsNbr = parseInt(check.rows[0].fields.intents) + 1;
let gamesNbr = parseInt(check.rows[0].fields.game) + 1;
if (lowercaseGuess === 'new') {
sojID = getRandomInt(1,gameListSize); ////////////////// NEW SOJ GAME
await lib.googlesheets.query['@0.3.0'].update({
range: `${sheet}!E:H`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'check__is': 'sojID'
}
],
fields: {
'activeround': `${sojID}`,
'intents' : 0,
'game' : gamesNbr
},
});
}
else if (check.rows[0].fields.activeround != 'empty') {
sojID = check.rows[0].fields.activeround;
await lib.googlesheets.query['@0.3.0'].update({
range: `${sheet}!E:H`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'check__is': 'sojID'
}
],
fields: {
'intents': intentsNbr
},
});
}
else {
sojID = getRandomInt(1,gameListSize); ////////////////// NEW SOJ GAME
await lib.googlesheets.query['@0.3.0'].update({
range: `${sheet}!E:H`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'check__is': 'sojID'
}
],
fields: {
'activeround': `${sojID}`,
'intents': 0,
'game' : gamesNbr
},
});
}
//Get the word to be guessed
let result = await lib.googlesheets.query['@0.3.0'].select({
range: `${sheet}!A:C`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'id': `${sojID}`
}
],
limit: {
'count': 0,
'offset': 0
},
});
//Prepare the answer
text.push('> ');
let orangeFlag;
let stringLength = result.rows[0].fields.games.length;
let answer = result.rows[0].fields.games;
//Les lettres bien placees sont enlevees de answer, pour ne pas les compter comme Orange par la suite
for (i=0; i<stringLength; i++) {
if (lowercaseGuess.charAt(i) === result.rows[0].fields.games.charAt(i)) {
answer = answer.replace(`${lowercaseGuess.charAt(i)}`,'');
}
}
// Construction de la reponse
for (i=0; i<stringLength; i++) {
if(result.rows[0].fields.games.charAt(i) === ' ') {
text.push(':blue_square:');
}
else {
if (lowercaseGuess.charAt(i) === ' ') {
text.push(':x:');
}
else {
if (lowercaseGuess.charAt(i) === result.rows[0].fields.games.charAt(i)) {
text.push(':green_square:');
}
else {
orangeFlag = 0;
for (j=0;j<stringLength;j++) {
if (lowercaseGuess.charAt(i) === result.rows[0].fields.games.charAt(j)) {
if (answer.includes(lowercaseGuess.charAt(i))) {
text.push(':orange_square:');
answer = answer.replace(`${lowercaseGuess.charAt(i)}`,'');
j = stringLength;
orangeFlag = 1;
}
else {
text.push(':x:');
j = stringLength;
orangeFlag = 1;
}
}
}
if (orangeFlag == 0) {
if (lowercaseGuess.charAt(i) === '') {
text.push(':grey_question:');
}
else {
text.push(':x:');
}
}
else {}
}
}
}
}
let reward = ' ';
if (lowercaseGuess === result.rows[0].fields.games) {
let playedNbr = parseInt(result.rows[0].fields.played) + 1;
await lib.googlesheets.query['@0.3.0'].update({
range: `${sheet}!A:C`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'id__is': `${sojID}`
}
],
fields: {
'played': playedNbr
},
});
let currentDate = new Date();
let cDay = currentDate.toISOString();
// console.log(cDay);
cDay = cDay.substring(0,10);
if (intentsNbr == 1) {
reward = ` SOJDLE #${check.rows[0].fields.game} résolu au ${intentsNbr}er essai!! Wow!`;
await lib.googlesheets.query['@0.3.0'].insert({
range: `users_scoring!A:D`,
fieldsets: [
{
'players': `${context.params.event.member.user.username}`,
'date': `${cDay}`,
'points' : 10,
'games' : `SOJDLE #${check.rows[0].fields.game}`
}
]
});
}
else {
reward = ` SOJDLE #${check.rows[0].fields.game} résolu au ${intentsNbr}e essai. Bravo!`;
let points = 10;
if(intentsNbr > 9) {
points = 0;
}
else {
points -= intentsNbr;
}
await lib.googlesheets.query['@0.3.0'].insert({
range: `users_scoring!A:D`,
fieldsets: [
{
'players': `${context.params.event.member.user.username}`,
'date': `${cDay}`,
'points' : points,
'games' : `SOJDLE #${check.rows[0].fields.game}`
}
]
});
}
sojID = getRandomInt(1,gameListSize); ////////////////// NEW SOJ GAME
var newResult = await lib.googlesheets.query['@0.3.0'].select({
range: `${sheet}!A:C`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'id': `${sojID}`
}
],
limit: {
'count': 0,
'offset': 0
},
});
if (newResult.rows[0].fields.played != 0) {
sojID = getRandomInt(1,gameListSize);
newResult = await lib.googlesheets.query['@0.3.0'].select({
range: `${sheet}!A:C`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'id': `${sojID}`
}
],
limit: {
'count': 0,
'offset': 0
},
});
}
if (newResult.rows[0].fields.played != 0) {
sojID = getRandomInt(1,gameListSize);
newResult = await lib.googlesheets.query['@0.3.0'].select({
range: `${sheet}!A:C`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'id': `${sojID}`
}
],
limit: {
'count': 0,
'offset': 0
},
});
}
}
else {}
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `<@!${context.params.event.member.user.id}> :` + reward + '\n' + text.join(' ')
});
//Print the new word to guess
if (lowercaseGuess === result.rows[0].fields.games) {
await lib.googlesheets.query['@0.3.0'].update({
range: `${sheet}!E:H`,
bounds: 'FIRST_EMPTY_ROW',
where: [
{
'check__is': 'sojID'
}
],
fields: {
'activeround': `${sojID}`,
'intents' : 0,
'game' : gamesNbr
},
});
let newText = [];
//New text begins with:
// Spaces
if (newResult.rows[0].fields.games.charAt(0) === ' ') {
newText.push(':blue_square:');
}
// Letters
else if (newResult.rows[0].fields.games.charAt(0).match(/[a-z]/g) !== null) {
newText.push(':regional_indicator_' + newResult.rows[0].fields.games.charAt(0) + ':');
}
// Digits : no test needed - if not a space or a letter, it must be a digit. If you want to add a test, use : if (lowercaseGuess.charAt(i).match(/[0-9]/g) !== null)
else {
switch (newResult.rows[0].fields.games.charAt(0)) {
case '0':
newText.push(':zero:');
break;
case '1':
newText.push(':one:');
break;
case '2':
newText.push(':two:');
break;
case '3':
newText.push(':three:');
break;
case '4':
newText.push(':four:');
break;
case '5':
newText.push(':five:');
break;
case '6':
newText.push(':six:');
break;
case '7':
newText.push(':seven:');
break;
case '8':
newText.push(':eight:');
break;
case '9':
newText.push(':nine:');
break;
}
}
for (i=1; i<newResult.rows[0].fields.games.length; i++) {
if(newResult.rows[0].fields.games.charAt(i) === ' ') {
newText.push(':blue_square:');
}
else {
newText.push(':grey_question:');
}
}
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: [
`SOJDLE #${gamesNbr}`,
newText.join(' '),
' '
].join('\n')
});
}
}
else {
let result = await lib.discord.channels['@0.3.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `<@!${context.params.event.member.user.id}> : Cette commande n'est permise que dans le fil #SOJDLE : https://discord.com/channels/342731229315072000/944567098502438932`
});
}