# Path: constants/exceptions.py
# class invalidArgumentsException(Exception):
# class loginFailedException(Exception):
# class userBannedException(Exception):
# class userLockedException(Exception):
# class noBanchoSessionException(Exception):
# class osuApiFailException(Exception):
# class fileNotFoundException(Exception):
# class invalidBeatmapException(Exception):
# class unsupportedGameModeException(Exception):
# class beatmapTooLongException(Exception):
# class need2FAException(Exception):
# class noAPIDataError(Exception):
# class scoreNotFoundError(Exception):
# class ppCalcException(Exception):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, handler):
# 	def __init__(self, handler, f):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, exception):
# 
# Path: constants/rankedStatuses.py
# UNKNOWN = -2
# NOT_SUBMITTED = -1
# PENDING = 0         # No leaderboard
# NEED_UPDATE = 1
# RANKED = 2          # With leaderboard.
# APPROVED = 3        # With leaderboard. Different icon. No alert.
# QUALIFIED = 4       # With leaderboard. Different icon. Alert.
# LOVED = 5           # With leaderboard. Different icon. Alert.
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# ACHIEVEMENTS_VERSION = 1
# DATADOG_PREFIX = "lets"


import tornado.gen
import tornado.web
from common.ripple import userUtils
from common.sentry import sentry
from common.web import requestsManager
from constants import exceptions, rankedStatuses
from objects import glob



class handler(requestsManager.asyncRequestHandler):
	MODULE_NAME = "rate"

	@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(self.MODULE_NAME, user_id)
			if not userUtils.checkLogin(user_id, password, ip):
				raise exceptions.loginFailedException(self.MODULE_NAME, username)
			if userUtils.check2FA(user_id, ip):
				raise exceptions.need2FAException(self.MODULE_NAME, user_id, ip)


# Path: constants/exceptions.py
# class invalidArgumentsException(Exception):
# class loginFailedException(Exception):
# class userBannedException(Exception):
# class userLockedException(Exception):
# class noBanchoSessionException(Exception):
# class osuApiFailException(Exception):
# class fileNotFoundException(Exception):
# class invalidBeatmapException(Exception):
# class unsupportedGameModeException(Exception):
# class beatmapTooLongException(Exception):
# class need2FAException(Exception):
# class noAPIDataError(Exception):
# class scoreNotFoundError(Exception):
# class ppCalcException(Exception):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, handler):
# 	def __init__(self, handler, f):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, exception):
# 
# Path: constants/rankedStatuses.py
# UNKNOWN = -2
# NOT_SUBMITTED = -1
# PENDING = 0         # No leaderboard
# NEED_UPDATE = 1
# RANKED = 2          # With leaderboard.
# APPROVED = 3        # With leaderboard. Different icon. No alert.
# QUALIFIED = 4       # With leaderboard. Different icon. Alert.
# LOVED = 5           # With leaderboard. Different icon. Alert.
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# ACHIEVEMENTS_VERSION = 1
# DATADOG_PREFIX = "lets"


# Path: handlers/rateHandler.py
import tornado.gen
import tornado.web
from common.ripple import userUtils
from common.sentry import sentry
from common.web import requestsManager
from constants import exceptions, rankedStatuses
from objects import glob




class handler(requestsManager.asyncRequestHandler):
	MODULE_NAME = "rate"

	@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(self.MODULE_NAME, user_id)
			if not userUtils.checkLogin(user_id, password, ip):
				raise exceptions.loginFailedException(self.MODULE_NAME, username)
			if userUtils.check2FA(user_id, ip):
				raise exceptions.need2FAException(self.MODULE_NAME, user_id, ip)


			ranked = glob.db.fetch(