clean up relax-reg

This commit is contained in:
cmyui 2019-02-16 13:16:22 -05:00
parent 2e1ce5a3de
commit 26c274badb
3 changed files with 26 additions and 106 deletions

View File

@ -15,10 +15,8 @@ class handler(requestsManager.asyncRequestHandler):
@tornado.gen.engine
@sentry.captureTornado
def asyncGet(self, replayID):
try:
fullReplay = replayHelper.buildFullReplay(scoreID=replayID)
except (exceptions.fileNotFoundException, exceptions.scoreNotFoundError):
fullReplay = replayHelper.rxbuildFullReplay(scoreID=replayID)
fullReplay = replayHelper.buildFullReplay(scoreID=replayID, relax=0 if replayID > 500000000 else 1)
self.write(fullReplay)
self.add_header("Content-type", "application/octet-stream")

View File

@ -117,7 +117,6 @@ class handler(requestsManager.asyncRequestHandler):
isRelaxing = used_mods & 128
# Create score object and set its data
log.info("[{}] {} has submitted a score on {}...".format("RELAX" if isRelaxing else "VANILLA", username, scoreData[0]))
s = rxscore.score() if isRelaxing else score.score()
s.setDataFromScoreData(scoreData)
@ -138,7 +137,7 @@ class handler(requestsManager.asyncRequestHandler):
log.debug("Beatmap is not submitted/outdated/unknown. Score submission aborted.")
return
# increment user playtime
# Increment user playtime
length = 0
if s.passed:
try:
@ -284,23 +283,6 @@ class handler(requestsManager.asyncRequestHandler):
# Send to cono ALL passed replays, even non high-scores
if glob.conf.config["cono"]["enable"]:
if isRelaxing:
threading.Thread(target=lambda: glob.redis.publish(
"cono:analyze", json.dumps({
"score_id": s.scoreID,
"beatmap_id": beatmapInfo.beatmapID,
"user_id": s.playerUserID,
"game_mode": s.gameMode,
"pp": s.pp,
"replay_data": base64.b64encode(
replayHelper.rxbuildFullReplay(
s.scoreID,
rawReplay=self.request.files["score"][0]["body"]
)
).decode(),
})
)).start()
else:
# We run this in a separate thread to avoid slowing down scores submission,
# as cono needs a full replay
threading.Thread(target=lambda: glob.redis.publish(
@ -313,7 +295,8 @@ class handler(requestsManager.asyncRequestHandler):
"replay_data": base64.b64encode(
replayHelper.buildFullReplay(
s.scoreID,
rawReplay=self.request.files["score"][0]["body"]
rawReplay=self.request.files["score"][0]["body"],
relax=1 if isRelaxing else 0
)
).decode(),
})
@ -330,6 +313,9 @@ class handler(requestsManager.asyncRequestHandler):
# Update beatmap playcount (and passcount)
beatmap.incrementPlaycount(s.fileMd5, s.passed)
# Print out score submission
log.info("[{}] {} has submitted a score on {}...".format("RELAX" if isRelaxing else "VANILLA", username, beatmapInfo.songName.encode().decode("ASCII", "ignore")))
# Let the api know of this score
if s.scoreID:
glob.redis.publish("api:score_submission", s.scoreID)

View File

@ -5,80 +5,16 @@ from constants import exceptions, dataTypes
from helpers import binaryHelper
from objects import glob
def rxbuildFullReplay(scoreID=None, scoreData=None, rawReplay=None):
def buildFullReplay(scoreID=None, scoreData=None, rawReplay=None, relax):
if all(v is None for v in (scoreID, scoreData)) or all(v is not None for v in (scoreID, scoreData)):
raise AttributeError("Either scoreID or scoreData must be provided, not neither or both")
if relax is None:
raise AttributeError("Not specified whether the replay is relax or regular. WILL NOT BUILD REPLAY!")
if scoreData is None:
scoreData = glob.db.fetch(
"SELECT scores_relax.*, users.username FROM scores_relax LEFT JOIN users ON scores_relax.userid = users.id "
"WHERE scores_relax.id = %s",
[scoreID]
)
else:
scoreID = scoreData["id"]
if scoreData is None or scoreID is None:
raise exceptions.scoreNotFoundError()
if rawReplay is None:
# Make sure raw replay exists
fileName = ".data/replays/replay_{}.osr".format(scoreID)
if not os.path.isfile(fileName):
raise FileNotFoundError()
# Read raw replay
with open(fileName, "rb") as f:
rawReplay = f.read()
# Calculate missing replay data
rank = generalUtils.getRank(int(scoreData["play_mode"]), int(scoreData["mods"]), int(scoreData["accuracy"]),
int(scoreData["300_count"]), int(scoreData["100_count"]), int(scoreData["50_count"]),
int(scoreData["misses_count"]))
magicHash = generalUtils.stringMd5(
"{}p{}o{}o{}t{}a{}r{}e{}y{}o{}u{}{}{}".format(int(scoreData["100_count"]) + int(scoreData["300_count"]),
scoreData["50_count"], scoreData["gekis_count"],
scoreData["katus_count"], scoreData["misses_count"],
scoreData["beatmap_md5"], scoreData["max_combo"],
"True" if int(scoreData["full_combo"]) == 1 else "False",
scoreData["username"], scoreData["score"], rank,
scoreData["mods"], "True"))
# Add headers (convert to full replay)
fullReplay = binaryHelper.binaryWrite([
[scoreData["play_mode"], dataTypes.byte],
[20150414, dataTypes.uInt32],
[scoreData["beatmap_md5"], dataTypes.string],
[scoreData["username"], dataTypes.string],
[magicHash, dataTypes.string],
[scoreData["300_count"], dataTypes.uInt16],
[scoreData["100_count"], dataTypes.uInt16],
[scoreData["50_count"], dataTypes.uInt16],
[scoreData["gekis_count"], dataTypes.uInt16],
[scoreData["katus_count"], dataTypes.uInt16],
[scoreData["misses_count"], dataTypes.uInt16],
[scoreData["score"], dataTypes.uInt32],
[scoreData["max_combo"], dataTypes.uInt16],
[scoreData["full_combo"], dataTypes.byte],
[scoreData["mods"], dataTypes.uInt32],
[0, dataTypes.byte],
[0, dataTypes.uInt64],
[rawReplay, dataTypes.rawReplay],
[0, dataTypes.uInt32],
[0, dataTypes.uInt32],
])
# Return full replay
return fullReplay
def buildFullReplay(scoreID=None, scoreData=None, rawReplay=None):
if all(v is None for v in (scoreID, scoreData)) or all(v is not None for v in (scoreID, scoreData)):
raise AttributeError("Either scoreID or scoreData must be provided, not neither or both")
if scoreData is None:
scoreData = glob.db.fetch(
"SELECT scores.*, users.username FROM scores LEFT JOIN users ON scores.userid = users.id "
"WHERE scores.id = %s",
[scoreID]
)
"SELECT scores{relax}.*, users.username FROM scores LEFT JOIN users ON scores{relax}.userid = users.id WHERE scores{relax}.id = {scoreID}".format(scoreID=scoreID, relax="_relax" if relax == 1 else ""))
else:
scoreID = scoreData["id"]
if scoreData is None or scoreID is None: