From cbfbf2320bc2ef686dce2c05b754282be6c9d785 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 20 May 2019 08:31:33 +1000 Subject: [PATCH] Server is ticking ever half-hour --- server/index.js | 1 + server/profiles.js | 20 +++++++++++++++++++- src/components/pages/profile.jsx | 3 +-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/server/index.js b/server/index.js index e1c953a..43279f2 100644 --- a/server/index.js +++ b/server/index.js @@ -30,6 +30,7 @@ app.post('/profilerequest', profiles.profileRequest(connection)); app.post('/recruit', profiles.recruit(connection)); app.post('/train', profiles.train(connection)); app.post('/untrain', profiles.untrain(connection)); +profiles.runGoldTick(connection); //static directories app.use('/styles', express.static(path.resolve(__dirname + '/../public/styles')) ); diff --git a/server/profiles.js b/server/profiles.js index c2b09f7..7b2b65f 100644 --- a/server/profiles.js +++ b/server/profiles.js @@ -3,7 +3,9 @@ require('dotenv').config(); //libraries let formidable = require('formidable'); +let CronJob = require('cron').CronJob; +//profile creation & requesting const profileCreate = (connection) => (req, res) => { //formidable handles forms let form = formidable.IncomingForm(); @@ -95,6 +97,7 @@ function profileRequestInner(connection, req, res, fields) { }); } +//actual actions to be taken const recruit = (connection) => (req, res) => { //formidable handles forms let form = formidable.IncomingForm(); @@ -332,10 +335,25 @@ const untrain = (connection) => (req, res) => { }); } +const runGoldTick = (connection) => { + let goldTickJob = new CronJob('0 */30 * * * *', () => { + let query = 'UPDATE profiles SET gold = gold + recruits;'; + connection.query(query, (err) => { + if (err) throw err; + + //debugging + //console.log(Date().toString() + ' gold tick'); + }); + }); + + goldTickJob.start(); +} + module.exports = { // profileCreate: profileCreate, //NOTE: Not actually used profileRequest: profileRequest, recruit: recruit, train: train, - untrain: untrain + untrain: untrain, + runGoldTick: runGoldTick } \ No newline at end of file diff --git a/src/components/pages/profile.jsx b/src/components/pages/profile.jsx index 85d1334..5d6cfd9 100644 --- a/src/components/pages/profile.jsx +++ b/src/components/pages/profile.jsx @@ -163,8 +163,7 @@ class Profile extends React.Component {

Gold:

{this.state.gold}

-
-
+
(+1 gold for each recruit every half hour)