-
Notifications
You must be signed in to change notification settings - Fork 1
/
schoolcpu.js
448 lines (382 loc) · 16.3 KB
/
schoolcpu.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
var Random = require( 'rng' );
var clone = require('clone');
var util = require('util');
var Handlebars = require("handlebars");
var fs = require('fs');
var phantom = require('phantom');
var arrayShuffle = require("array-shuffle");
var ArgumentParser = require('argparse').ArgumentParser;
var parser = new ArgumentParser({
version: '0.0.1',
addHelp:true,
description: 'Educational exercise about the Fetch->Decode->Execute cycle'
});
parser.addArgument(
[ '-n', '--numbers' ],
{
help: "How many of each level C,B,A,A*",
defaultValue: "2,2,2,2",
dest:"numbers"
}
);
parser.addArgument(
[ '-o', '--output' ],
{
help: "Output directory.",
defaultValue: "output",
dest:"outputdir"
}
);
parser.addArgument(
[ '-g', '--group' ],
{
help: "Group Name",
defaultValue: "default",
dest:"groupname"
}
);
parser.addArgument(
[ '-l', '--levels' ],
{
help: "Level Config Name",
defaultValue: "default",
dest:"levelconfig"
}
);
parser.addArgument(
[ '-r', '--rooms' ],
{
help: "Rooms Config Name",
defaultValue: "default",
dest:"roomconfig"
}
);
var options = parser.parseArgs();
console.log("options =", options);
if(!fs.existsSync(options.outputdir)) {
fs.mkdirSync(options.outputdir);
}
var aInstructionNames = ["A", "B", "C", "D", "E", "F", "2", "3"];
var aRoomGroups = JSON.parse(fs.readFileSync("roomconfig/"+options.roomconfig+".json").toString());
var aNumbers = options.numbers.split(",");//[6,6,5,3];
//TODO : make A* hard rather than possibly hard
var sClass = options.groupname;//"Y11 Option";
var aPosters = [];
for(var i = 0; i<aInstructionNames.length; i++) {
for(var j = 0; j<aRoomGroups[i].length; j++) {
aPosters.push({
"data":aInstructionNames[i],
"name":aRoomGroups[i][j]
});
}
}
page = fs.readFileSync("templates/Posters.handlebars", "utf8");
var oPostersTemplate = Handlebars.compile(page);
html = oPostersTemplate({"aPosters":aPosters});
fs.writeFile(options.outputdir+"/posters.html", html, function (err) {
if (err){ return console.log(err);}
console.log('html > posters.html');
});
phantom.create(function (ph) {
ph.createPage(function (page) {
page.set('paperSize', {
format: 'A4'
}, function() {
page.open(options.outputdir+"/posters.html", function (status) {
page.render(options.outputdir+"/posters.pdf", function(){
console.log("Posters sheets rendered ", status);
ph.exit();
});
});
});
});
});
//process.exit()
var iClass = sClass.split("").reduce(function(previousValue, currentValue, index, array) {
return index + previousValue + currentValue.charCodeAt(0);
},0);
var mt = new Random.MT( iClass );
// TODO : Refactor Instuction Set into Levels
// TODO : Add sorting on hardeness of the actuall maths (possibly by size of final figure)
// TODO : limit the numer per level replacing the 500 / 1000
// TODO : Node /jquery style options
// TODO : Change variable names to Program, instruction set .....
// TODO : Findway to render to tablet / phone interface
var aLevels = JSON.parse(fs.readFileSync("levelconfig/"+options.levelconfig+".json").toString());
//Map the Room names, Data entries and the instructions avaiable to each level togther
var aLevels = aLevels.map(function(oLevel, iKey){
oLevel.aRoomInstructions = [];
for(var i = 0; i<oLevel.aInstructions.length; i++) {
if(typeof oLevel.aInstructions[i].limit === "undefined") {
oLevel.aInstructions[i].limit = aRoomGroups[i].length;
}
for(var j = 0; j<oLevel.aInstructions[i].limit; j++) {
var oRoomInstructions = clone(oLevel.aInstructions[i]);
//console.log("oRoomInstructions =", oRoomInstructions);
oRoomInstructions.sData = aInstructionNames[i];
oRoomInstructions.sRoomName = aRoomGroups[i][j];
oLevel.aRoomInstructions.push(oRoomInstructions);
}
}
//oLevel.aRoomInstructions = arrayShuffle(oLevel.aRoomInstructions);
//console.log("oLevel.aRoomInstructions =", oLevel.aRoomInstructions);
return oLevel;
});
var Worksheet = {
sGroupName:"",
iInstructionSet: 0,
iSheetNumber: 1,
iSteps:6,
iPosition:1,
aResults:[],
aRandomNumbers:[],
sCurrentOperation:"",
sStartType:"both",
aInstructions:[],
aAvailableInstructions:[],
oLevel:{},
aValidList:[],
aChosen:[],
init: function init(iSheetNumber, iInstructionSet, sGroupName) {
this.sGroupName = sGroupName;
this.iInstructionSet = iInstructionSet;
this.oLevel = clone(aLevels[this.iInstructionSet]);
this.aAvailableInstructions = clone(this.oLevel.aRoomInstructions);
this.aInstructions = clone(this.oLevel.aInstructions);
for(var i = 0; i<this.aInstructions.length; i++) {
this.aInstructions[i].sData = aInstructionNames[i];
}
this.sStartType = this.oLevel.sStartType;
for( var i = 0; i <= this.iSteps; i++ )
{
this.aRandomNumbers[i] = mt.next();
}
this.iSheetNumber = iSheetNumber;
this.aResults[0] = this.iSheetNumber;
},
getChosen:function() {
var aFinalList = [];
if(this.aValid.length) {
aFinalList = this.getLevel(this.aValid, 1, aFinalList);
this.aChosen = aFinalList;
} else {
console.log("No Valids to be chosen");
}
return aFinalList;
},
getLevel:function(aList, iLevel, aSelected) {
// TODO : hreuristic to choose best
// TODO : or just filter the ones with duplicates out
var iRandom = this.aRandomNumbers[iLevel];
var iListLength = aList.length;
//var iChosen = iRandom % iListLength;
var iChosen = iRandom % iListLength;
var oChosen = clone(aList[iChosen]);
oChosen.aChildren = false;
aSelected.push(oChosen);
if(aList[iChosen].aChildren.length>0)
{
aSelected = this.getLevel(aList[iChosen].aChildren, iLevel+1, aSelected);
}
return aSelected;
},
createValidList:function() {
this.aValid = this.genValidList(1, this.sStartType, this.iSheetNumber, "", 0, "", this.aAvailableInstructions);
return this.aValid;
},
//the guts stips out invalid options
genValidList:function(iStep, sType, iCurrVal, sCurrentOperation, iStepsMapped, sCurrentRoom, aCurrInstrSet) {
//console.log("iStep =", iStep); console.log("sType =", sType); console.log("iCurrVal =", iCurrVal);console.log("sCurrentOperation =", sCurrentOperation); console.log("iStepsMapped =", iStepsMapped); console.log("sCurrentRoom =", sCurrentRoom); console.log("aCurrInstrSet =", aCurrInstrSet.length);
var aNextValid = aCurrInstrSet.filter(function(aInstruction){
if (aInstruction.sRoomName === sCurrentRoom)
{
return false;
}
return true;
});
//Filter down to just the room we can work with this time
var aValidList = aNextValid.filter(function(aInstruction){
// we want an operand and there isn't one (or if we don't and there is get shot of it)
if((sType === "operand" ) && (aInstruction.operand === null ))
{
return false;
}
if((sType === "operation" ) && (aInstruction.operation === null ))
{
return false;
}
return true;
});
// Do the calculations and operations for the Valid rooms this time
var aProcessed = aValidList.map(function(line){
var iResult = iCurrVal;
var iNextStep = iStep + 1;
var sNextType = ((sType === "both")?"both":(sType === "operation")?"operand":"operation");
var aNewLine = clone(line);
// TODO : could try putting the score tracking in here
aNewLine.sCurrentOperation = sCurrentOperation;
//For display purposes
aNewLine.bListOperation = true;
aNewLine.bListOperand = true;
if(sType === "operation")
{
aNewLine.bListOperand = false;
} else {
aNewLine.bListOperation = false;
}
//If the room is an operation
if(sType === "operation")
{
aNewLine.sCurrentOperation = line.operation;
iResult = iCurrVal;
//The operations which have np Operand
switch(aNewLine.sCurrentOperation)
{
case "INC":
iResult = iCurrVal + 1;
sNextType = sType;
break;
case "DEC":
iResult = iCurrVal - 1;
sNextType = sType;
break;
case "NOP":
sNextType = sType;
break;
}
aNewLine.iStep = iStep;
aNewLine.iCurrVal = iCurrVal;
aNewLine.iResult = iResult;
aNewLine.iNextStep = iNextStep;
aNewLine.sNextType = sNextType;
aNewLine.sDisplay = aNewLine.sCurrentOperation;
aNewLine.aChildren = [];
return aNewLine;
}
//The C grade ones have Operators and operands
if(sType === "both")
{
aNewLine.sCurrentOperation = aNewLine.operation;
aNewLine.sDisplay = aNewLine.operation+aNewLine.operand;
}
else
{
aNewLine.sDisplay = aNewLine.operand;
}
//The calculations
switch(aNewLine.sCurrentOperation)
{
case "+":
iResult = iCurrVal + aNewLine.operand;
break;
case "-":
iResult = iCurrVal - aNewLine.operand;
break;
case "x":
iResult = iCurrVal * aNewLine.operand;
break;
case "/":
iResult = iCurrVal / aNewLine.operand;
break;
case "JMP":
iNextStep = aNewLine.operand;
break;
}
aNewLine.iStep = iStep;
aNewLine.iCurrVal = iCurrVal;
aNewLine.iResult = iResult;
aNewLine.iNextStep = iNextStep;
aNewLine.sNextType = sNextType;
aNewLine.aChildren = [];
return aNewLine;
});
//Get rid of things that don't give sane answers
var aIntList = aProcessed.filter(function(val, index, org){
if (Math.round(val.iResult) !== val.iResult)
{
return false;
}
if (val.iResult <= 0)
{
return false;
}
// TODO : make this limit part of the level
//if (val.iResult > this.oLevel.iMaxNumber) {
if (val.iResult > 500) {
return false;
}
//Would a JMP take us out of the program?
if(val.iNextStep >= this.iSteps)
{
return false;
}
return true;
});
aIntList = aIntList.slice(0,5);
//have we stil got steps to work out
if(iStepsMapped < this.iSteps && iStep < this.iSteps )
{
var aListChildren = aIntList.map(function(aLine, ind, cur){
var aLineWChild = clone(aLine);
aLineWChild.aChildren = this.genValidList(aLine.iNextStep, aLine.sNextType, aLine.iResult, aLine.sCurrentOperation, iStepsMapped +1, aLine.sRoomName, aNextValid);
return aLineWChild;
}, this);
var aListChildrenTrimmed = aListChildren.filter(function(aChildLine, ind, cur){
return aChildLine.aChildren.length > 0;
});
return aListChildrenTrimmed;
}
else
{
return aIntList;
}
}
};
var aWorksheets = [];
for (var iCurrLevel = 0; iCurrLevel < aNumbers.length; iCurrLevel++) {
for(var j = 0; j < aNumbers[iCurrLevel]; j++) {
var oWorksheet = Object.create(Worksheet);
oWorksheet.init(j+1, iCurrLevel, options.groupname);
var aValid = oWorksheet.createValidList();
//console.log("aValid.length =", aValid.length);
var aChosen = oWorksheet.getChosen();
aWorksheets.push(oWorksheet);
console.log("Worksheet calculation complete", iCurrLevel, j);
}
}
var page = fs.readFileSync("templates/teachers.handlebars", "utf8");
var oTeachersTemplate = Handlebars.compile(page);
var html = oTeachersTemplate({"aWorksheets":aWorksheets, "sGroupName":options.groupname});
fs.writeFile(options.outputdir+"/teachers.html", html, function (err) {
if (err){ return console.log(err);}
console.log('html > teachers.html');
});
page = fs.readFileSync("templates/worksheets.handlebars", "utf8");
var oWorksheetsTemplate = Handlebars.compile(page);
html = oWorksheetsTemplate({"aWorksheets":aWorksheets});
fs.writeFile(options.outputdir+"/worksheets.html", html, function (err) {
if (err){ return console.log(err);}
console.log('html > worksheets.html');
});
phantom.create(function (ph) {
//console.log("creating phantom for "+sPersonID);
ph.createPage(function (page) {
//console.log("creating page for "+sPersonID);
page.set('paperSize', {
format: 'A4'
}, function() {
// continue with page setup
page.open(options.outputdir+"/teachers.html", function (status) {
page.render(options.outputdir+"/teachers.pdf", function(){
console.log("Teachers sheets rendered ", status);
page.open(options.outputdir+"/worksheets.html", function (status) {
page.render(options.outputdir+"/worksheets.pdf", function(){
console.log("Worksheets sheets rendered ", status);
ph.exit();
});
});
});
});
});
});
});