From 4f8ff50bc3887a038447c4c2696bd6b9df744ccb Mon Sep 17 00:00:00 2001 From: depreciate Date: Tue, 12 Feb 2019 03:22:35 +1030 Subject: [PATCH] added rating --- handlers/rateHandler.py | 83 +++++++++++++++++++++++++++++++++++++++++ lets.py | 4 +- objects/beatmap.pyx | 2 +- 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 handlers/rateHandler.py diff --git a/handlers/rateHandler.py b/handlers/rateHandler.py new file mode 100644 index 0000000..abb7bc3 --- /dev/null +++ b/handlers/rateHandler.py @@ -0,0 +1,83 @@ +import tornado.gen +import tornado.web + +from common.ripple import userUtils +from common.sentry import sentry +from common.web import requestsManager +from common.log import logUtils as log +from constants import exceptions, rankedStatuses +from objects import glob + +MODULE_NAME = "rate" + + +class handler(requestsManager.asyncRequestHandler): + @tornado.web.asynchronous + @tornado.gen.engine + @sentry.captureTornado + def asyncGet(self): + output = "" + + try: + if not requestsManager.checkArguments(self.request.arguments, ["c", "u", "p"]): + raise exceptions.invalidArgumentsException(MODULE_NAME) + + ip = self.getRequestIP() + username = self.get_argument("u").strip() + password = self.get_argument("p").strip() + user_id = userUtils.getID(username) + checksum = self.get_argument("c").strip() + if not user_id: + raise exceptions.loginFailedException(MODULE_NAME, user_id) + if not userUtils.checkLogin(user_id, password, ip): + raise exceptions.loginFailedException(MODULE_NAME, username) + if userUtils.check2FA(user_id, ip): + raise exceptions.need2FAException(MODULE_NAME, user_id, ip) + + ranked = glob.db.fetch( + "SELECT ranked FROM beatmaps WHERE beatmap_md5 = %s LIMIT 1", + (checksum,) + ) + if ranked is None: + output = "no exist" + return + if ranked["ranked"] < rankedStatuses.RANKED: + output = "not ranked" + return + + rating = glob.db.fetch("SELECT rating FROM beatmaps WHERE beatmap_md5 = %s LIMIT 1", (checksum,)) + has_voted = glob.db.fetch( + "SELECT id FROM beatmaps_rating WHERE user_id = %s AND beatmap_md5 = %s LIMIT 1", + (user_id, checksum) + ) + if has_voted is not None: + output = f"alreadyvoted\n{rating['rating']:.2f}" + return + vote = self.get_argument("v", default=None) + if vote is None: + output = "ok" + return + try: + vote = int(vote) + except ValueError: + raise exceptions.invalidArgumentsException(MODULE_NAME) + if vote < 0 or vote > 10: + output = "out of range" + return + glob.db.execute( + "REPLACE INTO beatmaps_rating (beatmap_md5, user_id, rating) VALUES (%s, %s, %s)", + (checksum, user_id, vote) + ) + glob.db.execute( + "UPDATE beatmaps SET rating = (SELECT SUM(rating)/COUNT(rating) FROM beatmaps_rating " + "WHERE beatmap_md5 = %(md5)s) WHERE beatmap_md5 = %(md5)s LIMIT 1", + {"md5": checksum} + ) + rating = glob.db.fetch("SELECT rating FROM beatmaps WHERE beatmap_md5 = %s LIMIT 1", (checksum,)) + output = f"{rating['rating']:.2f}" + except exceptions.loginFailedException: + output = "auth failed" + except exceptions.invalidArgumentsException: + output = "no" + finally: + self.write(output) \ No newline at end of file diff --git a/lets.py b/lets.py index 66de6b7..688bd81 100644 --- a/lets.py +++ b/lets.py @@ -24,6 +24,7 @@ from handlers import checkUpdatesHandler from handlers import defaultHandler from handlers import downloadMapHandler from handlers import emptyHandler +from handlers import rateHandler from handlers import getFullReplayHandler from handlers import getReplayHandler from handlers import getScoresHandler @@ -52,6 +53,7 @@ def make_app(): (r"/web/osu-submit-modular.php", submitModularHandler.handler), (r"/web/osu-submit-modular-selector.php", submitModularHandler.handler), (r"/web/osu-getreplay.php", getReplayHandler.handler), + (r"/web/osu-rate.php", rateHandler.handler), (r"/web/osu-screenshot.php", uploadScreenshotHandler.handler), (r"/web/osu-search.php", osuSearchHandler.handler), (r"/web/osu-search-set.php", osuSearchSetHandler.handler), @@ -77,8 +79,6 @@ def make_app(): # Not done yet (r"/web/lastfm.php", emptyHandler.handler), - (r"/web/osu-rate.php", emptyHandler.handler), - (r"/web/osu-comment.php", emptyHandler.handler), (r"/web/osu-checktweets.php", emptyHandler.handler), (r"/loadTest", loadTestHandler.handler), diff --git a/objects/beatmap.pyx b/objects/beatmap.pyx index f28ae0c..f3426f8 100644 --- a/objects/beatmap.pyx +++ b/objects/beatmap.pyx @@ -25,7 +25,7 @@ class beatmap: self.beatmapID = 0 self.beatmapSetID = 0 self.offset = 0 # Won't implement - self.rating = 10.0 # Won't implement + self.rating = .0 self.starsStd = 0.0 # stars for converted self.starsTaiko = 0.0 # stars for converted