Implemented basic recruiting
This commit is contained in:
@@ -27,6 +27,9 @@ app.post('/passwordreset', accounts.passwordReset(connection));
|
|||||||
//handle profiles
|
//handle profiles
|
||||||
let profiles = require('./profiles.js');
|
let profiles = require('./profiles.js');
|
||||||
app.post('/profilerequest', profiles.profileRequest(connection));
|
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
|
//static directories
|
||||||
app.use('/styles', express.static(path.resolve(__dirname + '/../public/styles')) );
|
app.use('/styles', express.static(path.resolve(__dirname + '/../public/styles')) );
|
||||||
|
|||||||
+82
-1
@@ -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 = {
|
module.exports = {
|
||||||
// profileCreate: profileCreate, //NOTE: Not actually used
|
// profileCreate: profileCreate, //NOTE: Not actually used
|
||||||
profileRequest: profileRequest
|
profileRequest: profileRequest,
|
||||||
|
recruit: recruit,
|
||||||
|
train: notYetImplemented,
|
||||||
|
untrain: notYetImplemented
|
||||||
}
|
}
|
||||||
@@ -51,5 +51,7 @@ CREATE TABLE IF NOT EXISTS profiles (
|
|||||||
spies INTEGER DEFAULT 0,
|
spies INTEGER DEFAULT 0,
|
||||||
scientists 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
|
CONSTRAINT FOREIGN KEY fk_accountId(accountId) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
@@ -24,7 +24,7 @@ class Profile extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentWillMount() {
|
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() {
|
render() {
|
||||||
@@ -78,11 +78,7 @@ class Profile extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//gameplay functions
|
//gameplay functions
|
||||||
requestProfileData(username) {
|
sendRequest(url, username = this.props.username, role = '') { //NOTE: merged all requests here
|
||||||
if (username === undefined || username === '') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//request this profile's info, using my credentials
|
//request this profile's info, using my credentials
|
||||||
let formData = new FormData();
|
let formData = new FormData();
|
||||||
|
|
||||||
@@ -90,6 +86,7 @@ class Profile extends React.Component {
|
|||||||
formData.append('token', this.props.token);
|
formData.append('token', this.props.token);
|
||||||
|
|
||||||
formData.append('username', username);
|
formData.append('username', username);
|
||||||
|
formData.append('role', role);
|
||||||
|
|
||||||
//build the XHR
|
//build the XHR
|
||||||
let xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
@@ -113,7 +110,7 @@ class Profile extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//send
|
//send
|
||||||
xhr.open('POST', '/profilerequest', true);
|
xhr.open('POST', url, true);
|
||||||
xhr.send(formData);
|
xhr.send(formData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,28 +170,28 @@ class Profile extends React.Component {
|
|||||||
<div className='row'>
|
<div className='row'>
|
||||||
<p className='col'>Recruits:</p>
|
<p className='col'>Recruits:</p>
|
||||||
<p className='col'>{this.state.recruits}</p>
|
<p className='col'>{this.state.recruits}</p>
|
||||||
<button className='col' style={{flex: '2 1 1.5%'}}>Get More...</button>
|
<button className='col' style={{flex: '2 1 1.5%'}} onClick={() => this.sendRequest('/recruit')}>Recruit More</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='row'>
|
<div className='row'>
|
||||||
<p className='col'>Soldiers:</p>
|
<p className='col'>Soldiers:</p>
|
||||||
<p className='col'>{this.state.soldiers}</p>
|
<p className='col'>{this.state.soldiers}</p>
|
||||||
<button className='col'>Train</button>
|
<button className='col' onClick={() => this.sendRequest('/train', this.props.username, 'soldier')}>Train (100 gold)</button>
|
||||||
<button className='col'>Untrain</button>
|
<button className='col' onClick={() => this.sendRequest('/untrain', this.props.username, 'soldier')}>Untrain</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='row'>
|
<div className='row'>
|
||||||
<p className='col'>Spies:</p>
|
<p className='col'>Spies:</p>
|
||||||
<p className='col'>{this.state.spies}</p>
|
<p className='col'>{this.state.spies}</p>
|
||||||
<button className='col'>Train</button>
|
<button className='col' onClick={() => this.sendRequest('/train', this.props.username, 'spy')}>Train (200 gold)</button>
|
||||||
<button className='col'>Untrain</button>
|
<button className='col' onClick={() => this.sendRequest('/untrain', this.props.username, 'spy')}>Untrain</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='row'>
|
<div className='row'>
|
||||||
<p className='col'>Scientists:</p>
|
<p className='col'>Scientists:</p>
|
||||||
<p className='col'>{this.state.scientists}</p>
|
<p className='col'>{this.state.scientists}</p>
|
||||||
<button className='col'>Train</button>
|
<button className='col' onClick={() => this.sendRequest('/train', this.props.username, 'scientist')}>Train (120 gold)</button>
|
||||||
<button className='col'>Untrain</button>
|
<button className='col' onClick={() => this.sendRequest('/untrain', this.props.username, 'scientist')}>Untrain</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -205,7 +202,7 @@ class Profile extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<div className='sidePanel'>
|
<div className='sidePanel'>
|
||||||
<p>Return <Link to='/'>home</Link></p>
|
<p>Return <Link to='/'>home</Link></p>
|
||||||
<p>Go to <Link to='/profile' onClick={(e) => { e.preventDefault(); this.requestProfileData(this.props.username); this.setWarning(''); this.props.history.push('/profile'); }}>your profile</Link></p>
|
<p>Go to <Link to='/profile' onClick={(e) => { e.preventDefault(); this.sendRequest('/profilerequest', this.props.username); this.setWarning(''); this.props.history.push('/profile'); }}>your profile</Link></p>
|
||||||
<Logout onClick={(e) => this.props.history.push('/')} />
|
<Logout onClick={(e) => this.props.history.push('/')} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user