<commit_before>import tornado.gen
import tornado.web

from objects import beatmap
from objects import scoreboard
from common.constants import privileges
from common.log import logUtils as log
from common.ripple import userUtils, scoreUtils
from common.web import requestsManager
from constants import exceptions
from objects import glob
from common.constants import mods
from common.sentry import sentry

class handler(requestsManager.asyncRequestHandler):
	"""
	Handler for /web/osu-osz2-getscores.php
	"""
	MODULE_NAME = "get_scores"

	@tornado.web.asynchronous
	@tornado.gen.engine
	@sentry.captureTornado
	def asyncGet(self):
		try:
			# Get request ip
			ip = self.getRequestIP()

			# Print arguments
			if glob.conf["DEBUG"]:
				requestsManager.printArguments(self)

			# TODO: Maintenance check

			# Check required arguments
			if not requestsManager.checkArguments(
				self.request.arguments,
				["c", "f", "i", "m", "us", "v", "vv", "mods"]
			):
				raise exceptions.invalidArgumentsException(self.MODULE_NAME)

			# GET parameters
			md5 = self.get_argument("c")
			fileName = self.get_argument("f")
			beatmapSetID = self.get_argument("i")
			gameMode = self.get_argument("m")
			username = self.get_argument("us")
			password = self.get_argument("ha")
			scoreboardType = int(self.get_argument("v"))
			scoreboardVersion = int(self.get_argument("vv"))

			# Login and ban check
			userID = userUtils.getID(username)
			if userID == 0:
				raise exceptions.loginFailedException(self.MODULE_NAME, userID)
			if not userUtils.checkLogin(userID, password, ip):
				raise exceptions.loginFailedException(self.MODULE_NAME, username)
			if userUtils.check2FA(userID, ip):
				raise exceptions.need2FAException(self.MODULE_NAME, username, ip)
			# Ban check is pointless here, since there's no message on the client
			#if userHelper.isBanned(userID) == True:
			#	raise exceptions.userBannedException(self.MODULE_NAME, username)

			# Hax check
			if "a" in self.request.arguments:
				if int(self.get_argument("a")) == 1 and not userUtils.getAqn(userID):
					log.cm("Found AQN folder on user {} ({})".format(username, userID))
					userUtils.setAqn(userID)

			# Scoreboard type
			isDonor = userUtils.getPrivileges(userID) & privileges.USER_DONOR > 0
			country = False
			friends = False
			modsFilter = -1
			isRelax = userUtils.isRelaxLeaderboard(userID)
			if scoreboardType == 4:
				# Country leaderboard
				country = True
			elif scoreboardType == 2:
				# Mods leaderboard, replace mods (-1, every mod) with "mods" GET parameters
				modsFilter = int(self.get_argument("mods"))

				# Disable automod (pp sort) if we are not donors
				# if not isDonor:
				#	modsFilter = modsFilter & ~mods.AUTOPLAY
			elif scoreboardType == 3 and isDonor:
				# Friends leaderboard
				friends = True

			# Console output
			fileNameShort = fileName[:32]+"..." if len(fileName) > 32 else fileName[:-4]
			log.info("Requested beatmap {} ({})".format(fileNameShort, md5))

			# Create beatmap object and set its data
			bmap = beatmap.beatmap(md5, beatmapSetID, gameMode, fileName=fileName)
			bmap.saveFileName(fileName)

			# Create leaderboard object, link it to bmap and get all scores
			displayMode = userUtils.getDisplayMode(userID, isRelax)
			sboard = scoreboard.scoreboard(
				username, gameMode, bmap, setScores=True, country=country,
				mods=modsFilter, friends=friends, relax=isRelax,
				display=displayMode
			)

			# Data to return
			data = ""
			data += bmap.getData(sboard.totalScores, scoreboardVersion)
			data += sboard.getScoresData()
			self.write(data)

			glob.stats["served_leaderboards"].labels(
				game_mode=scoreUtils.readableGameMode(gameMode),
				relax="1" if isRelax else "0",
			).inc()

			# Send bancho notification if needed
			"""
			if modsFilter > -1:<commit_msg>Fix wrong game mode label in prometheus served_leaderboards<commit_after>import tornado.gen
import tornado.web

from objects import beatmap
from objects import scoreboard
from common.constants import privileges
from common.log import logUtils as log
from common.ripple import userUtils, scoreUtils
from common.web import requestsManager
from constants import exceptions
from objects import glob
from common.constants import mods
from common.sentry import sentry

class handler(requestsManager.asyncRequestHandler):
	"""
	Handler for /web/osu-osz2-getscores.php
	"""
	MODULE_NAME = "get_scores"

	@tornado.web.asynchronous
	@tornado.gen.engine
	@sentry.captureTornado
	def asyncGet(self):
		try:
			# Get request ip
			ip = self.getRequestIP()

			# Print arguments
			if glob.conf["DEBUG"]:
				requestsManager.printArguments(self)

			# TODO: Maintenance check

			# Check required arguments
			if not requestsManager.checkArguments(
				self.request.arguments,
				["c", "f", "i", "m", "us", "v", "vv", "mods"]
			):
				raise exceptions.invalidArgumentsException(self.MODULE_NAME)

			# GET parameters
			md5 = self.get_argument("c")
			fileName = self.get_argument("f")
			beatmapSetID = self.get_argument("i")
			gameMode = int(self.get_argument("m"))
			if gameMode < 0 or gameMode > 3:
				raise exceptions.invalidArgumentsException(self.MODULE_NAME)
			username = self.get_argument("us")
			password = <PASSWORD>.get_argument("ha")
			scoreboardType = int(self.get_argument("v"))
			scoreboardVersion = int(self.get_argument("vv"))

			# Login and ban check
			userID = userUtils.getID(username)
			if userID == 0:
				raise exceptions.loginFailedException(self.MODULE_NAME, userID)
			if not userUtils.checkLogin(userID, password, ip):
				raise exceptions.loginFailedException(self.MODULE_NAME, username)
			if userUtils.check2FA(userID, ip):
				raise exceptions.need2FAException(self.MODULE_NAME, username, ip)
			# Ban check is pointless here, since there's no message on the client
			#if userHelper.isBanned(userID) == True:
			#	raise exceptions.userBannedException(self.MODULE_NAME, username)

			# Hax check
			if "a" in self.request.arguments:
				if int(self.get_argument("a")) == 1 and not userUtils.getAqn(userID):
					log.cm("Found AQN folder on user {} ({})".format(username, userID))
					userUtils.setAqn(userID)

			# Scoreboard type
			isDonor = userUtils.getPrivileges(userID) & privileges.USER_DONOR > 0
			country = False
			friends = False
			modsFilter = -1
			isRelax = userUtils.isRelaxLeaderboard(userID)
			if scoreboardType == 4:
				# Country leaderboard
				country = True
			elif scoreboardType == 2:
				# Mods leaderboard, replace mods (-1, every mod) with "mods" GET parameters
				modsFilter = int(self.get_argument("mods"))

				# Disable automod (pp sort) if we are not donors
				# if not isDonor:
				#	modsFilter = modsFilter & ~mods.AUTOPLAY
			elif scoreboardType == 3 and isDonor:
				# Friends leaderboard
				friends = True

			# Console output
			fileNameShort = fileName[:32]+"..." if len(fileName) > 32 else fileName[:-4]
			log.info("Requested beatmap {} ({})".format(fileNameShort, md5))

			# Create beatmap object and set its data
			bmap = beatmap.beatmap(md5, beatmapSetID, gameMode, fileName=fileName)
			bmap.saveFileName(fileName)

			# Create leaderboard object, link it to bmap and get all scores
			displayMode = userUtils.getDisplayMode(userID, isRelax)
			sboard = scoreboard.scoreboard(
				username, gameMode, bmap, setScores=True, country=country,
				mods=modsFilter, friends=friends, relax=isRelax,
				display=displayMode
			)

			# Data to return
			data = ""
			data += bmap.getData(sboard.totalScores, scoreboardVersion)
			data += sboard.getScoresData()
			self.write(data)

			glob.stats["served_leaderboards"].labels(
				game_mode=scoreUtils.readableGameMode(int(gameMode)),
				relax="1" if isRelax else "0",
			).inc()

			# Send bancho notification if needed
			"""
			if modsFilter > -1:
{'fraction_non_alphanumeric': 0.1069182389937107, 'fraction_numerical': 0.00590424849184957, 'mean_word_length': 4.102815979043877, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5423240', 'n_tokens_mistral': 2899, 'n_tokens_neox': 2449, 'n_words': 636}