Training works

This commit is contained in:
2019-05-20 07:28:10 +10:00
parent 9d9c735938
commit cbce586d70
2 changed files with 95 additions and 2 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ function isThrottled(email) {
return false; return false;
} }
if ( Math.abs(emails[email] - new Date()) / 1000 > 3) { //3 seconds if ( Math.abs(emails[email] - new Date()) / 1000 > 10) { //10 seconds
return false; return false;
} }
+94 -1
View File
@@ -168,6 +168,99 @@ const recruit = (connection) => (req, res) => {
}); });
} }
const train = (connection) => (req, res) => {
//formidable handles forms
let form = formidable.IncomingForm();
//parse form
form.parse(req, (err, fields) => {
if (err) throw err;
//verify the credentials (NOTE: duplication)
let query = 'SELECT accountId FROM sessions WHERE accountId = ? AND token = ?;';
connection.query(query, [fields.id, fields.token], (err, results) => {
if (err) throw err;
if (results.length !== 1) {
res.status(400).write('Invalid train credentials');
res.end();
return;
}
//verify the role argument
if (fields.role !== 'soldier' && fields.role !== 'spy' && fields.role !== 'scientist') {
res.status(400).write('Invalid train parameters');
res.end();
return;
}
//determine the cost of the training
let cost = 0;
switch(fields.role) {
case 'soldier':
cost = 100;
break;
case 'spy':
cost = 200;
break;
case 'scientist':
cost = 120;
break;
}
//verify that the user has a high enough gold and recruit balance
let query = 'SELECT recruits, gold FROM profiles WHERE accountId = ?;';
connection.query(query, [fields.id], (err, results) => {
if (err) throw err;
if (results[0].recruits <= 0) {
res.status(400).write('Not enough recruits');
res.end();
return;
}
if (results[0].gold < cost) {
res.status(400).write('Not enough gold');
res.end();
return;
}
//update the profile with new values
let query = 'UPDATE profiles SET gold = gold - ?, recruits = recruits - 1, soldiers = soldiers + ?, spies = spies + ?, scientists = scientists + ? WHERE accountId = ?;';
connection.query(query, [cost, fields.role === 'soldier' ? 1 : 0, fields.role === 'spy' ? 1 : 0, fields.role === 'scientist' ? 1 : 0, fields.id], (err) => {
if (err) throw err;
//send the new profile data as JSON (NOTE: possible duplication)
let query = 'SELECT * FROM profiles WHERE accountId = ?;';
connection.query(query, [fields.id], (err, results) => {
if (err) throw err;
//check just in case
if (results.length !== 1) {
res.status(400).write('Invalid recruit credentials');
res.end();
return;
}
//results.length === 1
res.status(200).json({
username: fields.username, //TODO: join here
gold: results[0].gold,
recruits: results[0].recruits,
soldiers: results[0].soldiers,
spies: results[0].spies,
scientists: results[0].scientists
});
res.end();
});
});
});
});
});
}
const notYetImplemented = (connection) => (req, res) => { const notYetImplemented = (connection) => (req, res) => {
res.status(400).write('Not Yet Implmented'); res.status(400).write('Not Yet Implmented');
res.end(); res.end();
@@ -177,6 +270,6 @@ module.exports = {
// profileCreate: profileCreate, //NOTE: Not actually used // profileCreate: profileCreate, //NOTE: Not actually used
profileRequest: profileRequest, profileRequest: profileRequest,
recruit: recruit, recruit: recruit,
train: notYetImplemented, train: train,
untrain: notYetImplemented untrain: notYetImplemented
} }