-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
121 lines (100 loc) · 3.04 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express');
var app = module.exports = express.createServer();
var routes = require("./routes");
var models = require("./models");
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.enable('jsonp callback');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'hushu' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.logger("dev"));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.helpers({
imageOrText: function(question) {
var imgPattern = new RegExp("^(.+)\.(jpg|png|gif|jpeg)$", "i");
var match = imgPattern.exec(question);
if(match) {
return "<img src='/content/cards/"+question+"' />";
} else {
return question;
}
},
// Returns a map with the question and its answer, or
// null if question is not a math one.
generateMathQA: function(question) {
var MAX_ARG = 20;
var mathPattern = new RegExp("^\\$([+*/\-])\\$$", "i");
var match = mathPattern.exec(question);
if(match) {
var opCode = " " + match[1] + " ";
var arg1 = 1 + Math.floor(Math.random()*MAX_ARG);
var arg2 = 1 + Math.floor(Math.random()*MAX_ARG);
// prevent less-than-one fraction for division (UX consideration)
if (arg1 < arg2 && match[1] === "/") {
var temp = arg1;
arg1 = arg2;
arg2 = temp;
}
var questionText = arg1 + opCode + arg2;
var answerText = "" + eval(questionText);
return {q: questionText, a: answerText};
} else {
return null;
}
},
generateAlphaMathQA: function(question) {
var ALPHABET = [
"A","B","C","D",
"E","F","G","H",
"I","J","K","L",
"M","N","O","P",
"Q","R","S","T",
"U","V","W","X",
"Y","Z"
];
var MAX_ARG = ALPHABET.length;
var alphaMathPattern = new RegExp("^@([+\-])@$", "i");
var match = alphaMathPattern.exec(question);
if(match) {
var opCode = " " + match[1] + " ";
var arg1 = Math.floor(Math.random()*MAX_ARG);
var arg2 = Math.floor(Math.random()*MAX_ARG);
var question = arg1 + opCode + arg2;
var answer = eval(question);
answer = (answer + (MAX_ARG)) % (MAX_ARG); // the addition is to fix negative results
var answerText = ALPHABET[answer];
var questionText = ALPHABET[arg1] + opCode + ALPHABET[arg2];
return {q: questionText, a: answerText};
} else {
return null;
}
}
});
app.dynamicHelpers({
session: function (req, res) {
return req.session;
},
messages: require('express-messages')
});
models.startClient("kushu_test", "root", "hushu");
app.models = models;
// Routes
routes(app);
var port = (process.argv.length > 2) ? process.argv[2] : 3000;
app.listen(port);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);