Implemented new ladder ordering algorithm

This commit is contained in:
2019-06-10 18:13:56 +10:00
parent a9017da72c
commit 3e6a00caf8
7 changed files with 78 additions and 18 deletions
+2
View File
@@ -38,6 +38,8 @@ let excluded = [ //messages that should not be logged
'Purchase made',
'Sale made',
'runLadderTick completed',
'Cleaned database',
'outerTick'
];
+2 -1
View File
@@ -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
---
+1
View File
@@ -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));
+39 -2
View File
@@ -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
};
+3 -3
View File
@@ -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);
});
};
+15 -12
View File
@@ -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
);
#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
#);
+16
View File
@@ -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
;