-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
58 lines (47 loc) · 1.57 KB
/
util.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
const assert = require('assert');
const axios = require('axios');
function shuffle(deck) {
// implementation of Fisher-Yates shuffle, stolen from a previous project: https://github.com/kuff/discordify
if (!Array.isArray(deck)) throw new Error("Parameter Must Be An Array");
let randomizedDeck = [];
let array = deck.slice();
while (array.length !== 0) {
let rIndex = Math.floor(array.length * Math.random());
randomizedDeck.push(array[rIndex]);
array.splice(rIndex, 1);
}
return randomizedDeck;
}
async function getMock() {
// database mock used for testing
return {
games: {
"ip1": {
createdTimestamp: Date.now(),
playerCardsTurned: [0, 1, 4],
opponentCardsTurned: [5, 3, 6],
board: [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7],
}
},
points: {
"ip1": 10
}
}
}
async function getDogs(board) {
// retreive random dog pictures from dog.ceo
assert(board.length % 2 === 0, 'uneven number of cards!');
const numberOfDogs = board.length / 2;
const dogRequest = await axios.get('https://dog.ceo/api/breeds/image/random/' + numberOfDogs, { timeout: 1000 });
return dogRequest.data.message;
}
module.exports = {
shuffle: shuffle,
generateBoard: () => {
// "generate" the board by shuffling an ordered array
sortedBoard = [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7]
return shuffle(sortedBoard);
},
getDogs: getDogs,
getDatabaseMock: getMock
}