added rating

This commit is contained in:
depreciate 2019-02-12 03:22:35 +10:30
parent 628fbf1916
commit 4f8ff50bc3
3 changed files with 86 additions and 3 deletions

83
handlers/rateHandler.py Normal file
View File

@ -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)

View File

@ -24,6 +24,7 @@ from handlers import checkUpdatesHandler
from handlers import defaultHandler from handlers import defaultHandler
from handlers import downloadMapHandler from handlers import downloadMapHandler
from handlers import emptyHandler from handlers import emptyHandler
from handlers import rateHandler
from handlers import getFullReplayHandler from handlers import getFullReplayHandler
from handlers import getReplayHandler from handlers import getReplayHandler
from handlers import getScoresHandler from handlers import getScoresHandler
@ -52,6 +53,7 @@ def make_app():
(r"/web/osu-submit-modular.php", submitModularHandler.handler), (r"/web/osu-submit-modular.php", submitModularHandler.handler),
(r"/web/osu-submit-modular-selector.php", submitModularHandler.handler), (r"/web/osu-submit-modular-selector.php", submitModularHandler.handler),
(r"/web/osu-getreplay.php", getReplayHandler.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-screenshot.php", uploadScreenshotHandler.handler),
(r"/web/osu-search.php", osuSearchHandler.handler), (r"/web/osu-search.php", osuSearchHandler.handler),
(r"/web/osu-search-set.php", osuSearchSetHandler.handler), (r"/web/osu-search-set.php", osuSearchSetHandler.handler),
@ -77,8 +79,6 @@ def make_app():
# Not done yet # Not done yet
(r"/web/lastfm.php", emptyHandler.handler), (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"/web/osu-checktweets.php", emptyHandler.handler),
(r"/loadTest", loadTestHandler.handler), (r"/loadTest", loadTestHandler.handler),

View File

@ -25,7 +25,7 @@ class beatmap:
self.beatmapID = 0 self.beatmapID = 0
self.beatmapSetID = 0 self.beatmapSetID = 0
self.offset = 0 # Won't implement self.offset = 0 # Won't implement
self.rating = 10.0 # Won't implement self.rating = .0
self.starsStd = 0.0 # stars for converted self.starsStd = 0.0 # stars for converted
self.starsTaiko = 0.0 # stars for converted self.starsTaiko = 0.0 # stars for converted