Added logging
This commit is contained in:
+19
-2
@@ -1,9 +1,26 @@
|
|||||||
var emailExpression = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
let emailExpression = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||||
|
|
||||||
function validateEmail(email) {
|
function validateEmail(email) {
|
||||||
return emailExpression.test(email);
|
return emailExpression.test(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let excluded = [ //errors that should not be logged
|
||||||
|
'Not enough gold',
|
||||||
|
'Not enough recruits',
|
||||||
|
'Not enough soldiers',
|
||||||
|
'Not enough spies',
|
||||||
|
'Not enough scientists',
|
||||||
|
'Not enough time has passed'
|
||||||
|
];
|
||||||
|
|
||||||
|
const log = (msg, ...args) => {
|
||||||
|
if (excluded.indexOf(msg) === -1) {
|
||||||
|
console.log(`log ${Date()}: ${msg} (${args.toString()})`);
|
||||||
|
}
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validateEmail
|
validateEmail: validateEmail,
|
||||||
|
log: log
|
||||||
};
|
};
|
||||||
+27
-23
@@ -7,7 +7,7 @@ let formidable = require('formidable');
|
|||||||
let sendmail = require('sendmail')();
|
let sendmail = require('sendmail')();
|
||||||
|
|
||||||
//utilities
|
//utilities
|
||||||
let { validateEmail } = require('../common/utilities.js');
|
let { log, validateEmail } = require('../common/utilities.js');
|
||||||
let { throttle, isThrottled } = require('../common/throttle.js');
|
let { throttle, isThrottled } = require('../common/throttle.js');
|
||||||
|
|
||||||
const signup = (connection) => (req, res) => {
|
const signup = (connection) => (req, res) => {
|
||||||
@@ -20,7 +20,7 @@ const signup = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//validate email, username and password
|
//validate email, username and password
|
||||||
if (!validateEmail(fields.email) || fields.username.length < 4 || fields.username.length > 100 || fields.password.length < 8 || fields.password !== fields.retype) {
|
if (!validateEmail(fields.email) || fields.username.length < 4 || fields.username.length > 100 || fields.password.length < 8 || fields.password !== fields.retype) {
|
||||||
res.status(400).write('Invalid signup data');
|
res.status(400).write(log('Invalid signup data', fields));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -31,13 +31,13 @@ const signup = (connection) => (req, res) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results[0].email !== 0) {
|
if (results[0].email !== 0) {
|
||||||
res.status(400).write('Email already registered!');
|
res.status(400).write(log('Email already registered!', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (results[0].username !== 0) {
|
if (results[0].username !== 0) {
|
||||||
res.status(400).write('Username already registered!');
|
res.status(400).write(log('Username already registered!', fields.username));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ const signup = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//prevent too many clicks
|
//prevent too many clicks
|
||||||
if (isThrottled(fields.email)) {
|
if (isThrottled(fields.email)) {
|
||||||
res.status(400).write('signup throttled');
|
res.status(400).write(log('signup throttled', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -80,12 +80,12 @@ const signup = (connection) => (req, res) => {
|
|||||||
}, (err, reply) => {
|
}, (err, reply) => {
|
||||||
//final check
|
//final check
|
||||||
if (err) {
|
if (err) {
|
||||||
res.write(`<p>Something went wrong (did you use a valid email?)</p><p>${err}</p>`);
|
res.status(400).write(log('Something went wrong (did you use a valid email?)', err));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).write('Verification email sent!');
|
res.status(200).write(log('Verification email sent!', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -104,14 +104,14 @@ const verify = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//correct number of results
|
//correct number of results
|
||||||
if (results.length != 1) {
|
if (results.length != 1) {
|
||||||
res.write('<p>That account does not exist or this link has already been used.</p>');
|
res.status(400).write(log('That account does not exist or this link has already been used.', req.query.email, req.query.verify));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//verify the link
|
//verify the link
|
||||||
if (req.query.verify != results[0].verify) {
|
if (req.query.verify != results[0].verify) {
|
||||||
res.write('<p>Verification failed!</p>');
|
res.status(400).write(log('Verification failed!', req.query.email, req.query.verify, results[0].verify));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -126,7 +126,7 @@ const verify = (connection) => (req, res) => {
|
|||||||
connection.query(query, [results[0].email], (err) => {
|
connection.query(query, [results[0].email], (err) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
res.write('<p>Verification succeeded!</p>');
|
res.status(200).write(log('Verification succeeded!', req.query.email));
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -143,7 +143,7 @@ const login = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//validate email, username and password
|
//validate email, username and password
|
||||||
if (!validateEmail(fields.email) || fields.password.length < 8) {
|
if (!validateEmail(fields.email) || fields.password.length < 8) {
|
||||||
res.write('<p>Invalid login data</p>');
|
res.status(400).write(log('Invalid login data', fields.email)); //WARNING: NEVER LOG PASSWORDS. EVER.
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -155,7 +155,7 @@ const login = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//found this email?
|
//found this email?
|
||||||
if (results.length === 0) {
|
if (results.length === 0) {
|
||||||
res.status(400).write('Incorrect email or password');
|
res.status(400).write(log('Incorrect email or password', fields.email, 'Did not find this email'));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -166,7 +166,7 @@ const login = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//compare the passwords
|
//compare the passwords
|
||||||
if (results[0].hash !== newHash) {
|
if (results[0].hash !== newHash) {
|
||||||
res.status(400).write('Incorrect email or password');
|
res.status(400).write(log('Incorrect email or password', fields.email, 'Did not find this password'));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -185,6 +185,8 @@ const login = (connection) => (req, res) => {
|
|||||||
username: results[0].username,
|
username: results[0].username,
|
||||||
token: rand
|
token: rand
|
||||||
});
|
});
|
||||||
|
res.end();
|
||||||
|
log('Logged in', fields.email, rand);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -195,6 +197,7 @@ const logout = (connection) => (req, res) => {
|
|||||||
let query = 'DELETE FROM sessions WHERE sessions.accountId IN (SELECT accounts.id FROM accounts WHERE email = ?) AND token = ?;';
|
let query = 'DELETE FROM sessions WHERE sessions.accountId IN (SELECT accounts.id FROM accounts WHERE email = ?) AND token = ?;';
|
||||||
connection.query(query, [req.body.email, req.body.token], (err) => {
|
connection.query(query, [req.body.email, req.body.token], (err) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
log('Logged out', req.body.email, req.body.token);
|
||||||
});
|
});
|
||||||
|
|
||||||
res.end();
|
res.end();
|
||||||
@@ -210,7 +213,7 @@ const passwordChange = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//validate password, retype
|
//validate password, retype
|
||||||
if (!validateEmail(fields.email) || fields.password.length < 8 || fields.password !== fields.retype) {
|
if (!validateEmail(fields.email) || fields.password.length < 8 || fields.password !== fields.retype) {
|
||||||
res.status(400).write('Invalid password change data');
|
res.status(400).write(log('Invalid password change data', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -225,7 +228,7 @@ const passwordChange = (connection) => (req, res) => {
|
|||||||
results.map((result) => { if (result.token == fields.token) found = true; });
|
results.map((result) => { if (result.token == fields.token) found = true; });
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
res.status(400).write('Invalid password change authentication');
|
res.status(400).write(log('Invalid password change authentication', fields.email, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -256,6 +259,7 @@ const passwordChange = (connection) => (req, res) => {
|
|||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
token: rand
|
token: rand
|
||||||
});
|
});
|
||||||
|
log('Password changed', fields.email);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -275,7 +279,7 @@ const passwordRecover = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//validate email, username and password
|
//validate email, username and password
|
||||||
if (!validateEmail(fields.email)) {
|
if (!validateEmail(fields.email)) {
|
||||||
res.status(400).write('Invalid recover data');
|
res.status(400).write(log('Invalid recover data', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -286,7 +290,7 @@ const passwordRecover = (connection) => (req, res) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid recover data (did you use a registered email?)');
|
res.status(400).write(log('Invalid recover data (did you use a registered email?)', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -305,7 +309,7 @@ const passwordRecover = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//prevent too many clicks
|
//prevent too many clicks
|
||||||
if (isThrottled(fields.email)) {
|
if (isThrottled(fields.email)) {
|
||||||
res.status(400).write('recover throttled');
|
res.status(400).write(log('recover throttled', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -322,12 +326,12 @@ const passwordRecover = (connection) => (req, res) => {
|
|||||||
}, (err, reply) => {
|
}, (err, reply) => {
|
||||||
//final check
|
//final check
|
||||||
if (err) {
|
if (err) {
|
||||||
res.write(`<p>Something went wrong (did you use a valid email?)</p>${err}`)
|
res.status(400).write(log('Something went wrong (did you use a valid email?)', err));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).write('Recovery email sent!');
|
res.status(200).write(log('Recovery email sent!', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -345,7 +349,7 @@ const passwordReset = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//validate email, username and password
|
//validate email, username and password
|
||||||
if (!validateEmail(fields.email) || fields.password.length < 8 || fields.password !== fields.retype) {
|
if (!validateEmail(fields.email) || fields.password.length < 8 || fields.password !== fields.retype) {
|
||||||
res.status(400).write('Invalid reset data (invalid email/password)');
|
res.status(400).write(log('Invalid reset data (invalid email/password)', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -357,7 +361,7 @@ const passwordReset = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//results should be only 1 account
|
//results should be only 1 account
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid reset data (incorrect parameters/database state)');
|
res.status(400).write(log('Invalid reset data (incorrect parameters/database state)', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -378,7 +382,7 @@ const passwordReset = (connection) => (req, res) => {
|
|||||||
connection.query(query, [fields.email], (err) => {
|
connection.query(query, [fields.email], (err) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
res.status(200).write('Password updated!');
|
res.status(200).write(log('Password updated!', fields.email));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
|
|||||||
+6
-3
@@ -4,6 +4,9 @@ require('dotenv').config();
|
|||||||
//libraries
|
//libraries
|
||||||
let mysql = require('mysql');
|
let mysql = require('mysql');
|
||||||
|
|
||||||
|
//utilities
|
||||||
|
let { log } = require('../common/utilities.js');
|
||||||
|
|
||||||
let connection;
|
let connection;
|
||||||
|
|
||||||
function handleDisconnect() {
|
function handleDisconnect() {
|
||||||
@@ -19,16 +22,16 @@ function handleDisconnect() {
|
|||||||
//connect
|
//connect
|
||||||
connection.connect((err) => {
|
connection.connect((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('Error connecting to mysql: ', err);
|
log('Error connecting to mysql: ', err);
|
||||||
setTimeout(handleDisconnect, 2000);
|
setTimeout(handleDisconnect, 2000);
|
||||||
} else {
|
} else {
|
||||||
console.log('Connected to mysql');
|
log('Connected to mysql');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//prepare for failure
|
//prepare for failure
|
||||||
connection.on('error', (err) => {
|
connection.on('error', (err) => {
|
||||||
console.log('mysql error: ', err);
|
log('mysql error: ', err);
|
||||||
|
|
||||||
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
|
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
|
||||||
handleDisconnect();
|
handleDisconnect();
|
||||||
|
|||||||
+4
-1
@@ -8,6 +8,9 @@ let http = require('http').Server(app);
|
|||||||
let bodyParser = require('body-parser');
|
let bodyParser = require('body-parser');
|
||||||
let path = require('path');
|
let path = require('path');
|
||||||
|
|
||||||
|
//utilities
|
||||||
|
let { log } = require('../common/utilities.js');
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
//handle the news request
|
//handle the news request
|
||||||
@@ -51,5 +54,5 @@ app.get('*', (req, res) => {
|
|||||||
|
|
||||||
//startup
|
//startup
|
||||||
http.listen(4000, () => {
|
http.listen(4000, () => {
|
||||||
console.log('listening to *:4000');
|
log('listening to *:4000');
|
||||||
});
|
});
|
||||||
+30
-22
@@ -5,12 +5,15 @@ require('dotenv').config();
|
|||||||
let formidable = require('formidable');
|
let formidable = require('formidable');
|
||||||
let CronJob = require('cron').CronJob;
|
let CronJob = require('cron').CronJob;
|
||||||
|
|
||||||
|
//utilities
|
||||||
|
let { log } = require('../common/utilities.js');
|
||||||
|
|
||||||
//profile creation & requesting
|
//profile creation & requesting
|
||||||
const profileCreate = (connection) => (req, res) => {
|
const profileCreate = (connection) => (req, res) => {
|
||||||
//formidable handles forms
|
//formidable handles forms
|
||||||
let form = formidable.IncomingForm();
|
let form = formidable.IncomingForm();
|
||||||
|
|
||||||
//parse form
|
//parse form TODO: form? That was a bad idea
|
||||||
form.parse(req, (err, fields) => {
|
form.parse(req, (err, fields) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
@@ -25,7 +28,7 @@ function profileCreateInner(connection, req, res, fields) {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results.length === 1) {
|
if (results.length === 1) {
|
||||||
res.status(400).write('That profile already exists');
|
res.status(400).write(log('That profile already exists', fields.username));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -36,7 +39,7 @@ function profileCreateInner(connection, req, res, fields) {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results.length !== 1 || results[0].accountId != fields.id) {
|
if (results.length !== 1 || results[0].accountId != fields.id) {
|
||||||
res.status(400).write('Invalid profile creation credentials');
|
res.status(400).write(log('Invalid profile creation credentials', fields.username, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -45,6 +48,8 @@ function profileCreateInner(connection, req, res, fields) {
|
|||||||
connection.query(query, [fields.username], (err) => {
|
connection.query(query, [fields.username], (err) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
|
log('Profile created', fields.username, fields.id, fields.token);
|
||||||
|
|
||||||
return profileRequestInner(connection, req, res, fields);
|
return profileRequestInner(connection, req, res, fields);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -78,7 +83,7 @@ function profileRequestInner(connection, req, res, fields) {
|
|||||||
if (results.length === 1) {
|
if (results.length === 1) {
|
||||||
return profileCreateInner(connection, req, res, fields);
|
return profileCreateInner(connection, req, res, fields);
|
||||||
} else {
|
} else {
|
||||||
res.status(400).write('Profile not found');
|
res.status(400).write(log('Profile not found', fields.username, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -93,6 +98,7 @@ function profileRequestInner(connection, req, res, fields) {
|
|||||||
scientists: results[0].scientists
|
scientists: results[0].scientists
|
||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
|
// log('Profile sent', fields.username, fields.id, fields.token);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -112,7 +118,7 @@ const recruit = (connection) => (req, res) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid recruit credentials');
|
res.status(400).write(log('Invalid recruit credentials', fields.username, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -123,7 +129,7 @@ const recruit = (connection) => (req, res) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid database state'); //TODO: internal error logging
|
res.status(400).write(log('Invalid database state', fields.username, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -132,7 +138,7 @@ const recruit = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//not enough time has passed
|
//not enough time has passed
|
||||||
if (timespans < 22) {
|
if (timespans < 22) {
|
||||||
res.status(400).write('Not enough time has passed');
|
res.status(400).write(log('Not enough time has passed', fields.username, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -149,7 +155,7 @@ const recruit = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//check just in case
|
//check just in case
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid recruit credentials');
|
res.status(400).write(log('Invalid recruit credentials', fields.username, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -164,6 +170,7 @@ const recruit = (connection) => (req, res) => {
|
|||||||
scientists: results[0].scientists
|
scientists: results[0].scientists
|
||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
|
log('Recruit successful', fields.username, fields.id, fields.token);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -185,19 +192,19 @@ const train = (connection) => (req, res) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid train credentials');
|
res.status(400).write(log('Invalid train credentials', fields.username, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//verify the role argument
|
//verify the role argument
|
||||||
if (fields.role !== 'soldier' && fields.role !== 'spy' && fields.role !== 'scientist') {
|
if (fields.role !== 'soldier' && fields.role !== 'spy' && fields.role !== 'scientist') {
|
||||||
res.status(400).write('Invalid train parameters');
|
res.status(400).write(log('Invalid train parameters', fields.username, fields.role, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//determine the cost of the training
|
//determine the cost of the training TODO: make these global for the client too
|
||||||
let cost = 0;
|
let cost = 0;
|
||||||
switch(fields.role) {
|
switch(fields.role) {
|
||||||
case 'soldier':
|
case 'soldier':
|
||||||
@@ -219,13 +226,13 @@ const train = (connection) => (req, res) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results[0].recruits <= 0) {
|
if (results[0].recruits <= 0) {
|
||||||
res.status(400).write('Not enough recruits');
|
res.status(400).write(log('Not enough recruits', fields.username, results[0].recruits, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (results[0].gold < cost) {
|
if (results[0].gold < cost) {
|
||||||
res.status(400).write('Not enough gold');
|
res.status(400).write(log('Not enough gold', fields.username, results[0].gold, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -242,7 +249,7 @@ const train = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//check just in case
|
//check just in case
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid recruit credentials');
|
res.status(400).write(log('Invalid recruit credentials', fields.username, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -257,6 +264,7 @@ const train = (connection) => (req, res) => {
|
|||||||
scientists: results[0].scientists
|
scientists: results[0].scientists
|
||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
|
log('Train successful', fields.username, fields.role, fields.id, fields.token);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -278,14 +286,14 @@ const untrain = (connection) => (req, res) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid train credentials');
|
res.status(400).write(log('Invalid untrain credentials', fields.username, fields.role, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//verify the role argument
|
//verify the role argument
|
||||||
if (fields.role !== 'soldier' && fields.role !== 'spy' && fields.role !== 'scientist') {
|
if (fields.role !== 'soldier' && fields.role !== 'spy' && fields.role !== 'scientist') {
|
||||||
res.status(400).write('Invalid untrain parameters');
|
res.status(400).write(log('Invalid untrain parameters', fields.username, fields.role, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -296,19 +304,19 @@ const untrain = (connection) => (req, res) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (fields.role === 'soldier' && results[0].soldiers <= 0) {
|
if (fields.role === 'soldier' && results[0].soldiers <= 0) {
|
||||||
res.status(400).write('Not enough soldiers');
|
res.status(400).write(log('Not enough soldiers', fields.username, results[0].soldiers, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fields.role === 'spy' && results[0].spies <= 0) {
|
if (fields.role === 'spy' && results[0].spies <= 0) {
|
||||||
res.status(400).write('Not enough spies');
|
res.status(400).write(log('Not enough spies', fields.username, results[0].spies, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fields.role === 'scientist' && results[0].scientists <= 0) {
|
if (fields.role === 'scientist' && results[0].scientists <= 0) {
|
||||||
res.status(400).write('Not enough scientists');
|
res.status(400).write(log('Not enough scientists', fields.username, results[0].scientists, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -325,7 +333,7 @@ const untrain = (connection) => (req, res) => {
|
|||||||
|
|
||||||
//check just in case
|
//check just in case
|
||||||
if (results.length !== 1) {
|
if (results.length !== 1) {
|
||||||
res.status(400).write('Invalid recruit credentials');
|
res.status(400).write(log('Invalid untrain credentials', fields.username, fields.role, fields.id, fields.token));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -340,6 +348,7 @@ const untrain = (connection) => (req, res) => {
|
|||||||
scientists: results[0].scientists
|
scientists: results[0].scientists
|
||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
|
log('Untrain successful', fields.username, fields.role, fields.id, fields.token);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -353,8 +362,7 @@ const runGoldTick = (connection) => {
|
|||||||
connection.query(query, (err) => {
|
connection.query(query, (err) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
//debugging
|
log('Gold tick');
|
||||||
//console.log(Date().toString() + ' gold tick');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user