Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions highscores/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.core.mail import send_mail
from django.utils import timezone
import logging
import re

from .models import Score, CleanCodeSubmission, ExemptedIP
from .forms import ScoreForm
Expand All @@ -15,6 +16,8 @@

USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'

MINIMUM_CLIENT_VERSION = '21.3e'

WRONG_ROBOT_MESSAGE = 'Double-check the robot type that you selected!'
HIGHER_SCORE_MESSAGE = 'You already have a submission with an equal or higher score than this!'
ERROR_WRONG_GAME_MESSAGE = 'There is something wrong with your clean code! Are you submitting for the right game?'
Expand Down Expand Up @@ -597,13 +600,30 @@ def clean_code_decryption(score_obj: Score) -> None:
score_obj.decrypted_code = cipher.decrypt(data).decode("utf-8")


def _client_version_key(version: str) -> tuple[int, int, str]:
match = re.fullmatch(r"v?([0-9]+)\.([0-9]+)([a-z])", version)
if match is None:
raise ValueError("Invalid client version")
major, minor, revision = match.groups()
return int(major), int(minor), revision


def check_generic_game_settings(score_obj: Score, auto_or_teleop: str) -> Union[str, None]:
""" Checks if the universal game settings are valid.
:return: None if the settings are valid, or a response with an error message if they are not.
"""
if (not score_obj.client_version or float(score_obj.client_version[1:5]) < 20.0):
if not score_obj.client_version:
return WRONG_VERSION_MESSAGE

version, prerelease_marker, _ = score_obj.client_version.partition("_p")
try:
version_key = _client_version_key(version)
except ValueError:
return ERROR_CORRUPT_CODE_MESSAGE

if version_key < _client_version_key(MINIMUM_CLIENT_VERSION):
return WRONG_VERSION_MESSAGE
if "_p" in score_obj.client_version:
if prerelease_marker:
return PRERELEASE_MESSAGE
if score_obj.leaderboard.auto_or_teleop != auto_or_teleop:
return WRONG_AUTO_OR_TELEOP_MESSAGE
Expand Down
36 changes: 34 additions & 2 deletions highscores/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
from django.test import TestCase
import pytest

# Create your tests here.
from highscores import lib
from highscores.models import Leaderboard, Score


@pytest.mark.parametrize(
("version", "expected"),
[
(None, lib.WRONG_VERSION_MESSAGE),
("v21.2z", lib.WRONG_VERSION_MESSAGE),
("v21.3d", lib.WRONG_VERSION_MESSAGE),
("v21.3e", None),
("21.3e", None),
("v21.3f", None),
("v21.10a", None),
("v22.0a", None),
("v21.3e_p1", lib.PRERELEASE_MESSAGE),
("invalid", lib.ERROR_CORRUPT_CODE_MESSAGE),
],
)
def test_high_score_client_version(version, expected):
score = Score(client_version=version, leaderboard=Leaderboard(auto_or_teleop="TELE"))

assert lib.check_generic_game_settings(score, "TELE") == expected


def test_changing_minimum_client_version(monkeypatch):
monkeypatch.setattr(lib, "MINIMUM_CLIENT_VERSION", "21.10b")
score = Score(client_version="v21.10a", leaderboard=Leaderboard(auto_or_teleop="TELE"))

assert lib.check_generic_game_settings(score, "TELE") == lib.WRONG_VERSION_MESSAGE

score.client_version = "v21.10b"
assert lib.check_generic_game_settings(score, "TELE") is None
Loading