From a0bbffaf912c6d5f86e43c4800078c2333d53df5 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Sun, 18 Aug 2019 17:57:13 -0400 Subject: [PATCH] Made downvote more powerful if there are some views or votes already. --- index.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index b1d23c9..3bcba27 100644 --- a/index.js +++ b/index.js @@ -219,19 +219,27 @@ app.get('/api/voteOnSponsorTime', function (req, res) { } } - //update the votes table - if (row != undefined) { - privateDB.prepare("UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?").run(type, userID, UUID); - } else { - privateDB.prepare("INSERT INTO votes VALUES(?, ?, ?, ?)").run(UUID, userID, hashedIP, type); - } + //check if the increment amount should be multiplied (downvotes have more power if there have been many views) + db.prepare("SELECT votes, views FROM sponsorTimes WHERE UUID = ?").get(UUID, function(err, row) { + if (row != null && (row.votes > 3 || row.views > 4) && incrementAmount < 0) { + //multiply the power of this downvote + incrementAmount *= 4; + } - //update the vote count on this sponsorTime - //oldIncrementAmount will be zero is row is null - db.prepare("UPDATE sponsorTimes SET votes = votes + ? WHERE UUID = ?").run(incrementAmount - oldIncrementAmount, UUID); + //update the votes table + if (row != undefined) { + privateDB.prepare("UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?").run(type, userID, UUID); + } else { + privateDB.prepare("INSERT INTO votes VALUES(?, ?, ?, ?)").run(UUID, userID, hashedIP, type); + } - //added to db - res.sendStatus(200); + //update the vote count on this sponsorTime + //oldIncrementAmount will be zero is row is null + db.prepare("UPDATE sponsorTimes SET votes = votes + ? WHERE UUID = ?").run(incrementAmount - oldIncrementAmount, UUID); + + //added to db + res.sendStatus(200); + }); }); });