diff --git a/server/index.js b/server/index.js index f5d091a..e1c953a 100644 --- a/server/index.js +++ b/server/index.js @@ -27,6 +27,9 @@ app.post('/passwordreset', accounts.passwordReset(connection)); //handle profiles let profiles = require('./profiles.js'); app.post('/profilerequest', profiles.profileRequest(connection)); +app.post('/recruit', profiles.recruit(connection)); +app.post('/train', profiles.train(connection)); +app.post('/untrain', profiles.untrain(connection)); //static directories app.use('/styles', express.static(path.resolve(__dirname + '/../public/styles')) ); diff --git a/server/profiles.js b/server/profiles.js index 26dcbdd..0acebdc 100644 --- a/server/profiles.js +++ b/server/profiles.js @@ -95,7 +95,88 @@ function profileRequestInner(connection, req, res, fields) { }); } +const recruit = (connection) => (req, res) => { + //formidable handles forms + let form = formidable.IncomingForm(); + + //parse form + form.parse(req, (err, fields) => { + if (err) throw err; + + //verify the credentials + 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 recruit credentials'); + res.end(); + return; + } + + //verify enough time has passed since the last successful recruit action + let query = 'SELECT TIMESTAMPDIFF(HOUR, (SELECT lastRecruitTime FROM profiles WHERE accountId = ?), CURRENT_TIMESTAMP());'; + connection.query(query, [fields.id], (err, results) => { + if (err) throw err; + + if (results.length !== 1) { + res.status(400).write('Invalid database state'); //TODO: internal error logging + res.end(); + return; + } + + let timespans = results[0][Object.keys(results[0])]; + + //not enough time has passed + if (timespans < 24) { + res.status(400).write('Not enough time has passed'); + res.end(); + return; + } + + //update the profile with the new data (gaining 1 recruit) + let query = 'UPDATE profiles SET recruits = recruits + 1, lastRecruitTime = CURRENT_TIMESTAMP() WHERE accountId = ?;'; + connection.query(query, [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) => { + res.status(400).write('Not Yet Implmented'); + res.end(); +} + module.exports = { // profileCreate: profileCreate, //NOTE: Not actually used - profileRequest: profileRequest + profileRequest: profileRequest, + recruit: recruit, + train: notYetImplemented, + untrain: notYetImplemented } \ No newline at end of file diff --git a/sql/create_database_structure.sql b/sql/create_database_structure.sql index 2b9e3c6..7901d96 100644 --- a/sql/create_database_structure.sql +++ b/sql/create_database_structure.sql @@ -51,5 +51,7 @@ CREATE TABLE IF NOT EXISTS profiles ( spies INTEGER DEFAULT 0, scientists INTEGER DEFAULT 0, + lastRecruitTime TIMESTAMP DEFAULT '2019-01-01 00:00:00', + CONSTRAINT FOREIGN KEY fk_accountId(accountId) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE ); \ No newline at end of file diff --git a/src/components/pages/profile.jsx b/src/components/pages/profile.jsx index 917cfc8..85d1334 100644 --- a/src/components/pages/profile.jsx +++ b/src/components/pages/profile.jsx @@ -24,7 +24,7 @@ class Profile extends React.Component { } componentWillMount() { - this.requestProfileData(this.state.params.username ? this.state.params.username : this.props.username); + this.sendRequest('/profilerequest', this.state.params.username ? this.state.params.username : this.props.username); } render() { @@ -78,11 +78,7 @@ class Profile extends React.Component { } //gameplay functions - requestProfileData(username) { - if (username === undefined || username === '') { - return; - } - + sendRequest(url, username = this.props.username, role = '') { //NOTE: merged all requests here //request this profile's info, using my credentials let formData = new FormData(); @@ -90,6 +86,7 @@ class Profile extends React.Component { formData.append('token', this.props.token); formData.append('username', username); + formData.append('role', role); //build the XHR let xhr = new XMLHttpRequest(); @@ -113,7 +110,7 @@ class Profile extends React.Component { } //send - xhr.open('POST', '/profilerequest', true); + xhr.open('POST', url, true); xhr.send(formData); } @@ -173,28 +170,28 @@ class Profile extends React.Component {
Recruits:
{this.state.recruits}
- +Soldiers:
{this.state.soldiers}
- - + +Spies:
{this.state.spies}
- - + +Scientists:
{this.state.scientists}
- - + +Return home
-Go to { e.preventDefault(); this.requestProfileData(this.props.username); this.setWarning(''); this.props.history.push('/profile'); }}>your profile
+Go to { e.preventDefault(); this.sendRequest('/profilerequest', this.props.username); this.setWarning(''); this.props.history.push('/profile'); }}>your profile