Skip to content

Commit 3b7eccc

Browse files
feat: Add support for Push Back game highscores
1 parent f208c98 commit 3b7eccc

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

highscores/lib.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,26 @@ def send_world_record_webhook(new_score: Score, previous_record: Score = None) -
3030
if not DISCORD_WEBHOOK_URL:
3131
logging.error("Discord webhook URL not configured")
3232
return
33-
33+
3434
try:
3535
# Calculate duration and get previous record holder info
3636
if previous_record is None:
3737
# Get the current world record (which will become the previous one)
3838
previous_record = Score.objects.filter(
39-
leaderboard=new_score.leaderboard,
39+
leaderboard=new_score.leaderboard,
4040
approved=True
4141
).order_by('-score', 'time_set').first()
42-
42+
4343
if previous_record and previous_record.score < new_score.score:
4444
# Calculate how long the previous record stood
4545
duration_diff = new_score.time_set - previous_record.time_set
46-
46+
4747
# Calculate duration in a readable format
4848
total_seconds = int(duration_diff.total_seconds())
4949
days = total_seconds // 86400
5050
hours = (total_seconds % 86400) // 3600
5151
minutes = (total_seconds % 3600) // 60
52-
52+
5353
if days > 0:
5454
duration_text = f"{days} day{'s' if days != 1 else ''}, {hours} hour{'s' if hours != 1 else ''}"
5555
elif hours > 0:
@@ -58,7 +58,7 @@ def send_world_record_webhook(new_score: Score, previous_record: Score = None) -
5858
duration_text = f"{minutes} minute{'s' if minutes != 1 else ''}"
5959
else:
6060
duration_text = "less than a minute"
61-
61+
6262
previous_record_info = f"**{previous_record.player.username}**'s record ({previous_record.score:,} points) stood for **{duration_text}**"
6363
else:
6464
previous_record_info = "**First record set for this category!**"
@@ -113,11 +113,12 @@ def send_world_record_webhook(new_score: Score, previous_record: Score = None) -
113113
'Content-Type': 'application/json',
114114
'User-Agent': USER_AGENT
115115
})
116-
116+
117117
response = urlopen(req)
118118
if response.status != 204:
119-
logging.error(f"Discord webhook failed with status: {response.status}")
120-
119+
logging.error(
120+
f"Discord webhook failed with status: {response.status}")
121+
121122
except Exception as e:
122123
logging.error(f"Failed to send Discord webhook: {e}")
123124

@@ -127,7 +128,7 @@ def test_world_record_webhook(player_name: str, score: int, game: str, robot: st
127128
if not DISCORD_WEBHOOK_URL:
128129
logging.error("Discord webhook URL not configured")
129130
return False
130-
131+
131132
try:
132133
embed = {
133134
"title": "🧪 TEST WORLD RECORD NOTIFICATION 🧪",
@@ -177,10 +178,10 @@ def test_world_record_webhook(player_name: str, score: int, game: str, robot: st
177178
'Content-Type': 'application/json',
178179
'User-Agent': USER_AGENT
179180
})
180-
181+
181182
response = urlopen(req)
182183
return response.status == 204
183-
184+
184185
except Exception as e:
185186
logging.error(f"Failed to send test Discord webhook: {e}")
186187
return False
@@ -276,6 +277,10 @@ def submit_reefscape(score_obj: Score) -> Union[str, None]:
276277
return submit_score(score_obj, reefscape_clean_code_check)
277278

278279

280+
def submit_push_back(score_obj: Score) -> Union[str, None]:
281+
return submit_score(score_obj, push_back_clean_code_check)
282+
283+
279284
def decode_time_data(in_string: str) -> str:
280285
out_bytes = ""
281286

@@ -318,12 +323,12 @@ def extract_form_data(form: ScoreForm, request: HttpRequest) -> Score:
318323
def approve_score(score_obj: Score, prev_submissions):
319324
# Check if this is a new world record before deleting previous submissions
320325
current_world_record = Score.objects.filter(
321-
leaderboard=score_obj.leaderboard,
326+
leaderboard=score_obj.leaderboard,
322327
approved=True
323328
).order_by('-score', 'time_set').first()
324-
325-
is_world_record = (current_world_record is None or
326-
score_obj.score > current_world_record.score)
329+
330+
is_world_record = (current_world_record is None or
331+
score_obj.score > current_world_record.score)
327332

328333
# Delete previous submissions with lower or equal scores in the category
329334
prev_submissions.filter(score__lte=score_obj.score).delete()
@@ -340,7 +345,8 @@ def approve_score(score_obj: Score, prev_submissions):
340345
except Exception as e:
341346
logging.error(f"Failed to send world record webhook: {e}")
342347
else:
343-
logging.info(f"DEBUG: World record detected for {score_obj.player.username} - {score_obj.score} on {score_obj.leaderboard.name} (webhook disabled in debug mode)")
348+
logging.info(
349+
f"DEBUG: World record detected for {score_obj.player.username} - {score_obj.score} on {score_obj.leaderboard.name} (webhook disabled in debug mode)")
344350

345351
code_obj = CleanCodeSubmission()
346352
code_obj.clean_code = score_obj.clean_code
@@ -509,6 +515,10 @@ def reefscape_clean_code_check(score_obj: Score) -> Union[str, None]:
509515
return clean_code_check(score_obj, check_reefscape_game_settings, check_subtraction_score)
510516

511517

518+
def push_back_clean_code_check(score_obj: Score) -> Union[str, None]:
519+
return clean_code_check(score_obj, check_push_back_game_settings, check_skills_challenge_score)
520+
521+
512522
def extract_clean_code_info(score_obj: Score) -> tuple[str, list[str], str, str, str, str, str, str]:
513523
""" Extracts the relevant information from the clean code.
514524
:param score_obj: Score object to extract from
@@ -778,6 +788,18 @@ def check_reefscape_game_settings(game_options: list, restart_option: str, game_
778788
return None # No error
779789

780790

791+
def check_push_back_game_settings(game_options: list, restart_option: str, game_index: str) -> Union[str, None]:
792+
""" Checks if the Push Back game settings are valid.
793+
:return: None if the settings are valid, or a response with an error message if they are not.
794+
"""
795+
if (game_index != '20'):
796+
return 'Wrong game! This form is for Push Back.'
797+
if (restart_option != '2'):
798+
return 'You must use restart option 2 (skills challenge) for Push Back high score submissions.'
799+
800+
return None # No error
801+
802+
781803
def check_robot_type(score_obj: Score, robot_model: str) -> Union[str, None]:
782804
""" Checks if the robot model is valid.
783805
:return: None if the robot model is valid, or a response with an error message if it is not.

0 commit comments

Comments
 (0)