From 3917aca276cf9f004120168c1a8644394b5b637e Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 16 Jun 2019 01:47:15 +1000 Subject: [PATCH] Implemented King Of The Hill --- common/utilities.js | 1 + public/content/task_list.md | 2 +- public/news/2019-06-16-01.md | 7 ++++ server/badge_statistics.json | 2 +- server/badges.js | 61 ++++++++++++++++++++++++++----- server/utilities.js | 1 + sql/create_database_structure.sql | 24 ++++++------ 7 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 public/news/2019-06-16-01.md diff --git a/common/utilities.js b/common/utilities.js index cdfc650..47ccf3b 100644 --- a/common/utilities.js +++ b/common/utilities.js @@ -39,6 +39,7 @@ let excluded = [ //messages that should not be logged 'Sale made', 'runLadderTick completed', + 'King Of The Hill contender found', 'Cleaned database', 'outerTick' diff --git a/public/content/task_list.md b/public/content/task_list.md index 331b7b1..cc4e01d 100644 --- a/public/content/task_list.md +++ b/public/content/task_list.md @@ -77,7 +77,7 @@ Badge Ideas * ~~alpha tester~~ * capture the flag -* king of the hill +* ~~king of the hill~~ * gold horde * ~~Combat Master~~ * Beta tester diff --git a/public/news/2019-06-16-01.md b/public/news/2019-06-16-01.md new file mode 100644 index 0000000..0d3499f --- /dev/null +++ b/public/news/2019-06-16-01.md @@ -0,0 +1,7 @@ +Tonight's All Nighter +--- +_16 June 2019_ + +Things I've completed so far: + +* King Of The Hill badge is now enabled (requires 24 hours at the top of the ladder). diff --git a/server/badge_statistics.json b/server/badge_statistics.json index c872120..b3271ab 100644 --- a/server/badge_statistics.json +++ b/server/badge_statistics.json @@ -39,7 +39,7 @@ "filename": "king_of_the_hill.png", "description": "Awarded to anyone who keeps their position at the top of the game ladder for 24 hours.", "visible": true, - "unlockable": null + "unlockable": true }, "Referral Linker": { "filename": "referral_linker.png", diff --git a/server/badges.js b/server/badges.js index 0a37974..121e39e 100644 --- a/server/badges.js +++ b/server/badges.js @@ -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 = { diff --git a/server/utilities.js b/server/utilities.js index e912371..300855b 100644 --- a/server/utilities.js +++ b/server/utilities.js @@ -111,6 +111,7 @@ const isSpying = (connection, user, cb) => { const getLadderData = (connection, field, start, length, cb) => { //moved here for reusability + //TODO: implement the field parameter let query = 'SELECT accounts.id AS id, username, soldiers, recruits, gold FROM accounts JOIN profiles ON accounts.id = profiles.accountId ORDER BY -ladderRank DESC LIMIT ?, ?;'; connection.query(query, [Math.max(0, start || 0), Math.max(0, length || 0)], (err, results) => { cb(err, results); diff --git a/sql/create_database_structure.sql b/sql/create_database_structure.sql index cd9fd7e..5c3b03c 100644 --- a/sql/create_database_structure.sql +++ b/sql/create_database_structure.sql @@ -199,18 +199,18 @@ 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 +); #banning system CREATE TABLE IF NOT EXISTS bannedEmails (