-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
206 lines (181 loc) · 5.61 KB
/
index.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
/*jslint node, for */
/*globals Promise, Set */
// Options
var csv = 'data/clues.csv';
var output = 'public/cards.js';
// Modules
var parse = require('csv-parse/lib/sync');
var fs = require('fs');
// Get the clues from the CSV
function clues(csv) {
'use strict';
return new Promise(function (resolve) {
fs.readFile(csv, 'utf8', function (error, data) {
if (error) {
throw error;
}
var parsedData = parse(data, {columns: true, skip_empty_lines: true});
resolve(parsedData);
});
});
}
// Write the clues to public/data.js
function writeDataFile(cards) {
'use strict';
return new Promise(function (resolve, reject) {
fs.writeFile(output,
'var cards = ' + JSON.stringify(cards),
'utf8',
function (error) {
if (error) {
reject(error);
} else {
resolve('Done!');
}
});
});
}
// Create cards
function createCards(cluesByType) {
'use strict';
return new Promise(function (resolve, reject) {
var cards = [];
var outOfClues = false;
// Check number of remaining clues
function remainingClues() {
// Check that all types include at least five clues
var counter = 0;
cluesByType.forEach(function (type) {
counter += type.clues.length;
});
if (counter < 4) {
return false;
} else {
return counter;
}
}
// Get next cluesByType
var currentTypesIndex = 0;
function nextType() {
var type = cluesByType[currentTypesIndex];
currentTypesIndex = (currentTypesIndex + 1) % cluesByType.length;
// Only return a type that still contains clues
if (type.clues.length === 0) {
if (remainingClues() === false) {
console.log('Not enough clues for another card.');
outOfClues = true;
} else {
return nextType();
}
} else {
return type;
}
}
// Create a card
function createCard() {
var card = [];
function addNextClue(card) {
var clue;
var type = nextType();
// If a type has clues, select the first one
// and remove it from the array with shift()
if (type && outOfClues === false && type.clues.length > 0) {
clue = type.clues.shift();
// If the card doesn't contain that clue already,
// add it to the card. Otherwise, try again.
if (card.indexOf(clue) === -1) {
card.push(clue);
} else {
addNextClue(card);
}
} else {
addNextClue(card);
}
}
var i;
for (i = 0; i < 4; i += 1) {
addNextClue(card);
}
return card;
}
// Keep making cards while we still have clues
var numberOfCards = Math.round(remainingClues() / 4) - 1;
console.log('numberOfCards: %d', numberOfCards);
var i;
for (i = 0; i < numberOfCards; i += 1) {
cards.push(createCard());
}
if (cards.length > 0) {
console.log('Created %d cards', cards.length);
resolve(cards);
} else {
reject('Couldn\'t create cards.');
}
});
}
// Put the clues into type arrays.
// Create card sets of five clues by taking one clue from each type.
// If there are none left in a type, skip to the next type.
// (It is fine if we end up with some cards with the same type.)
function arrangeCluesByType(clues) {
'use strict';
return new Promise(function (resolve, reject) {
// Fill the array of clue types
var cluesByType = [];
function clueTypes() {
var types = [];
clues.forEach(function (clue) {
types.push(clue.Type);
});
var uniqueTypes = Array.from(new Set(types));
return uniqueTypes;
}
clueTypes().forEach(function (type) {
var object = {
type: type,
clues: []
};
clues.forEach(function (clue) {
if (clue.Type === type) {
object.clues.push(clue.Clue);
}
});
cluesByType.push(object);
});
if (cluesByType.length === clueTypes().length) {
resolve(cluesByType);
} else {
reject('Something went wrong sorting clues into types.');
}
});
}
// Randomise the list
// https://stackoverflow.com/a/12646864/1781075
function shuffle(array) {
'use strict';
return new Promise(function (resolve, reject) {
var i, j, temp;
for (i = array.length - 1; i > 0; i -= 1) {
j = Math.floor(Math.random() * (i + 1));
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
if (array.length) {
resolve(array);
} else {
reject('Dang, nuked the array.');
}
});
}
// Main process
function process() {
'use strict';
clues(csv)
.then(shuffle)
.then(arrangeCluesByType)
.then(createCards)
.then(writeDataFile);
}
// Start process
process();