diff --git a/app/modules/flappybird/endpoints_flappybird.py b/app/modules/flappybird/endpoints_flappybird.py index 074116a6e..ecaff2ab8 100644 --- a/app/modules/flappybird/endpoints_flappybird.py +++ b/app/modules/flappybird/endpoints_flappybird.py @@ -1,3 +1,4 @@ +import hashlib import uuid from datetime import UTC, datetime @@ -14,6 +15,17 @@ ) from app.types.module import Module + +def genKey(score: int) -> int: + """Generates a key based on the score of the player.""" + data = f"{score}" + + hash_bytes = hashlib.sha256(data.encode()).digest() + key = int.from_bytes(hash_bytes[:4], byteorder="big") + + return key + + module = Module( root="flappybird", tag="Flappy Bird", @@ -91,6 +103,12 @@ async def create_flappybird_score( # And get the current date and time creation_time = datetime.now(UTC) + if genKey(flappybird_score.value) != flappybird_score.key: + raise HTTPException( + status_code=400, + detail="Invalid key for the provided score", + ) + db_flappybird_score = models_flappybird.FlappyBirdScore( id=score_id, user_id=user.id, diff --git a/app/modules/flappybird/schemas_flappybird.py b/app/modules/flappybird/schemas_flappybird.py index 37f752cff..f30bd18b6 100644 --- a/app/modules/flappybird/schemas_flappybird.py +++ b/app/modules/flappybird/schemas_flappybird.py @@ -8,6 +8,7 @@ class FlappyBirdScoreBase(BaseModel): value: int + key: int class FlappyBirdScore(FlappyBirdScoreBase):