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
+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);
});
};