Implemented King Of The Hill
This commit is contained in:
+52
-9
@@ -98,7 +98,7 @@ const selectActiveBadge = (connection) => (req, res) => {
|
||||
|
||||
const rewardBadge = (connection, id, badgeName, cb) => {
|
||||
//TODO: constants as badge/equipment names?
|
||||
let query = 'INSERT INTO badges (accountId, name) SELECT ?, ? FROM DUAL WHERE NOT EXISTS(SELECT 1 FROM badges WHERE accountId = ? AND name = ?) LIMIT 1;';
|
||||
let query = 'INSERT INTO badges (accountId, name) SELECT ?, ? FROM DUAL WHERE NOT EXISTS(SELECT 1 FROM badges WHERE accountId = ? AND name = ?);';
|
||||
connection.query(query, [id, badgeName, id, badgeName], (err, packet) => {
|
||||
if (err) throw err;
|
||||
|
||||
@@ -123,20 +123,63 @@ const runBadgeTicks = (connection) => {
|
||||
});
|
||||
|
||||
combatMasterBadgeTickJob.start();
|
||||
/*
|
||||
//King Of The Hill
|
||||
let kingOfTheHillBadgeTickJob = new CronJob('0 * * * * *', () => {
|
||||
getLadderData(connection, 0, 1, (err, ladderResults) => {
|
||||
if (err) throw err;
|
||||
|
||||
//TODO: pull badge names into variables. Not good.
|
||||
let query = 'SELECT * FROM badgesTimespan WHERE qualifyTime >= DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY) AND name = "King Of The Hill";';
|
||||
//King Of The Hill
|
||||
let kingOfTheHillBadgeTickJob = new CronJob('0 * * * * *', () => { //once a minute
|
||||
//NOTE: sloppy implementation - people who have the badge may get "rewarded" twice. Thankfully rewardBadge() prevents this.
|
||||
getLadderData(connection, 'parameter not used (yet)', 0, 1, (err, ladderResults) => {
|
||||
if (err) throw err; //TODO: pull badge names into variables. Not good.
|
||||
|
||||
//only happens with 0 players, but might as well check
|
||||
if (ladderResults.length === 0) {
|
||||
log('No players in ladder');
|
||||
return;
|
||||
}
|
||||
|
||||
//get the current contender for king of the hill
|
||||
let query = 'SELECT * FROM badgesTimespan WHERE name = "King Of The Hill";';
|
||||
connection.query(query, (err, results) => {
|
||||
if (err) throw err;
|
||||
|
||||
const day = 1000*60*60*24; //milliseconds
|
||||
const now = new Date();
|
||||
const qualifyTime = results.length > 0 ? new Date(results[0].qualifyTime) : null;
|
||||
|
||||
//if someone qualifies (1 day)
|
||||
if (results.length > 0 && now - qualifyTime >= day) {
|
||||
rewardBadge(connection, results[0].accountId, results[0].name, (id, badgeName) => log("Badge rewarded", id, badgeName));
|
||||
let query = 'DELETE FROM badgesTimespan WHERE id = ?;';
|
||||
connection.query(query, [results[0].id], (err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
//if someone is still a contender for this badge
|
||||
if (results.length > 0 && ladderResults[0].id === results[0].accountId) {
|
||||
//DO NOTHING
|
||||
log('King Of The Hill contender found', ladderResults[0].id, ladderResults[0].username);
|
||||
}
|
||||
|
||||
//if the current contender is NOT in first place
|
||||
else {
|
||||
let query = 'DELETE FROM badgesTimespan WHERE name = "King Of The Hill";';
|
||||
connection.query(query, (err) => {
|
||||
if (err) throw err;
|
||||
|
||||
let query = 'INSERT INTO badgesTimespan (accountId, name) VALUES (?, "King Of The Hill")';
|
||||
connection.query(query, [ladderResults[0].id], (err) => {
|
||||
if (err) throw err;
|
||||
|
||||
log('King Of The Hill contender updated', ladderResults[0].id, ladderResults[0].username);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
kingOfTheHillBadgeTickJob.start();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user