From 3e6a00caf8c2aa2b77cca5cf2e71c5d41dd6e2e9 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 10 Jun 2019 18:13:56 +1000 Subject: [PATCH] Implemented new ladder ordering algorithm --- common/utilities.js | 2 ++ public/content/task_list.md | 3 ++- server/index.js | 1 + server/profiles.js | 41 +++++++++++++++++++++++++++++-- server/utilities.js | 6 ++--- sql/create_database_structure.sql | 27 +++++++++++--------- sql/update.sql | 16 ++++++++++++ 7 files changed, 78 insertions(+), 18 deletions(-) diff --git a/common/utilities.js b/common/utilities.js index b055cb7..cdfc650 100644 --- a/common/utilities.js +++ b/common/utilities.js @@ -38,6 +38,8 @@ let excluded = [ //messages that should not be logged 'Purchase made', 'Sale made', + 'runLadderTick completed', + 'Cleaned database', 'outerTick' ]; diff --git a/public/content/task_list.md b/public/content/task_list.md index d1c8672..a718665 100644 --- a/public/content/task_list.md +++ b/public/content/task_list.md @@ -12,8 +12,8 @@ Major Minor --- +* Show the total equipment strength to the player on their own profile. * Proper changelog feed. -* Implement new game ladder sorting. * Implement banning mechanism. * Implement post scrolling for news page. * Everyone who signed up within the first month will get an exclusive badge (based on diagnostics data). @@ -55,6 +55,7 @@ Event Ideas --- * Capture the flag. +* Bosses / raids (drop exclusive badges / items). Badge Ideas --- diff --git a/server/index.js b/server/index.js index 59cfdf0..ab4b069 100644 --- a/server/index.js +++ b/server/index.js @@ -50,6 +50,7 @@ app.post('/trainrequest', profiles.trainRequest(connection)); app.post('/untrainrequest', profiles.untrainRequest(connection)); app.post('/ladderrequest', profiles.ladderRequest(connection)); profiles.runGoldTick(connection); +profiles.runLadderTick(connection); let combat = require('./combat.js'); app.post('/attackrequest', combat.attackRequest(connection)); diff --git a/server/profiles.js b/server/profiles.js index 325813e..749e5bb 100644 --- a/server/profiles.js +++ b/server/profiles.js @@ -430,7 +430,7 @@ const untrainRequest = (connection) => (req, res) => { }; const ladderRequest = (connection) => (req, res) => { - getLadderData(connection, req.body.start, req.body.length, (err, results) => { + getLadderData(connection, 'ladderRank', req.body.start, req.body.length, (err, results) => { if (err) throw err; getBadgesStatistics((err, { statistics }) => { @@ -513,6 +513,42 @@ const runGoldTick = (connection) => { outerTick.start(); }; +const runLadderTick = (connection) => { + let ladderTickJob = new CronJob('0 * * * * *', () => { + //set the ladder rank weight + let query = 'UPDATE profiles SET ladderRankWeight = ((recruits + soldiers + scientists + spies) + (SELECT COUNT(*) FROM pastCombat WHERE (attackerId = accountId AND victor = "attacker") OR (defenderId = accountId AND victor = "defender")) / 30 + gold / 10);'; + connection.query(query, (err) => { + if (err) throw err; + + //get the profiles ordered by weight descending + let query = 'SELECT id FROM profiles ORDER BY ladderRankWeight DESC;'; + connection.query(query, (err, results) => { + if (err) throw err; + + //collect the promises + let promises = []; + + //this is really inefficient + let query = 'UPDATE profiles SET ladderRank = ? WHERE id = ?;'; + for (let i = 0; i < results.length; i++) { + promises.push( + connection.query(query, [i, results[i].id], (err) => { + if (err) throw err; + }) + ); + } + + Promise.all(promises) + .then((e) => log('runLadderTick completed')) + .catch((e) => log('runLadderTick failed', e )) + ; + }); + }); + }); + + ladderTickJob.start(); +}; + module.exports = { // profileCreate: profileCreate, //NOTE: Not actually used profileRequest: profileRequest, @@ -520,5 +556,6 @@ module.exports = { trainRequest: trainRequest, untrainRequest: untrainRequest, ladderRequest: ladderRequest, - runGoldTick: runGoldTick + runGoldTick: runGoldTick, + runLadderTick: runLadderTick }; \ No newline at end of file diff --git a/server/utilities.js b/server/utilities.js index 390bac4..0f5e0f4 100644 --- a/server/utilities.js +++ b/server/utilities.js @@ -109,10 +109,10 @@ const isSpying = (connection, user, cb) => { }); }; -const getLadderData = (connection, start, length, cb) => { +const getLadderData = (connection, field, start, length, cb) => { //moved here for reusability - let query = 'SELECT accounts.id AS id, username, soldiers, recruits, gold FROM accounts JOIN profiles ON accounts.id = profiles.accountId ORDER BY soldiers DESC, recruits DESC, gold DESC LIMIT ?, ?;'; - connection.query(query, [start, length], (err, results) => { + let query = 'SELECT accounts.id AS id, username, soldiers, recruits, gold FROM accounts JOIN profiles ON accounts.id = profiles.accountId ORDER BY ? DESC LIMIT ?, ?;'; + connection.query(query, [field, start, length], (err, results) => { cb(err, results); }); }; diff --git a/sql/create_database_structure.sql b/sql/create_database_structure.sql index a71b65d..5febc60 100644 --- a/sql/create_database_structure.sql +++ b/sql/create_database_structure.sql @@ -68,6 +68,9 @@ CREATE TABLE IF NOT EXISTS profiles ( accountId INTEGER UNSIGNED UNIQUE, + ladderRank INTEGER UNSIGNED, + ladderRankWeight FLOAT UNSIGNED, + gold INTEGER DEFAULT 100, recruits INTEGER DEFAULT 0, soldiers INTEGER DEFAULT 0, @@ -194,15 +197,15 @@ CREATE TABLE IF NOT EXISTS badges ( CONSTRAINT FOREIGN KEY fk_accountId(accountId) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE ); -CREATE TABLE IF NOT EXISTS badgesTimespan ( #for recording timespan-related badges - id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY UNIQUE, - td TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), - - accountId INTEGER UNSIGNED, - - name VARCHAR(50) NOT NULL, - - qualifyTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), - - CONSTRAINT FOREIGN KEY fk_accountId(accountId) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE -); \ No newline at end of file +#CREATE TABLE IF NOT EXISTS badgesTimespan ( #for recording timespan-related badges +# id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY UNIQUE, +# td TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), +# +# accountId INTEGER UNSIGNED, +# +# name VARCHAR(50) NOT NULL, +# +# qualifyTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), +# +# CONSTRAINT FOREIGN KEY fk_accountId(accountId) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE +#); \ No newline at end of file diff --git a/sql/update.sql b/sql/update.sql index 93c9cbd..f4467ff 100644 --- a/sql/update.sql +++ b/sql/update.sql @@ -1,2 +1,18 @@ #NOTE: ALWAYS, ALWAYS, ALWAYS write a script in revert.sql that undoes these changes +ALTER TABLE + profiles +ADD COLUMN + ladderRank INTEGER UNSIGNED +AFTER + accountId +; + +ALTER TABLE + profiles +ADD COLUMN + ladderRankWeight FLOAT UNSIGNED +AFTER + ladderRank +; +