diff --git a/models/match.py b/models/match.py index 9e41420..c98fe48 100644 --- a/models/match.py +++ b/models/match.py @@ -2,11 +2,7 @@ first level class """ import enum -from email.policy import default import json -from random import choices - -from numpy import require from database.db import db, CustomQuerySet @@ -27,29 +23,29 @@ class Match(db.Document): player_dist1 = db.ListField(db.IntField()) player_dist2 = db.ListField(db.IntField()) # allies or axis - side1 = db.StringField(choices=('Axis', 'Allies')) - side2 = db.StringField(choices=('Axis', 'Allies')) + side1 = db.StringField(choices=('Axis', 'Allies')) + side2 = db.StringField(choices=('Axis', 'Allies')) # strong points hold at the end of the game - caps1 = db.IntField(required=True, choices=(0, 1, 2, 3, 4, 5)) - caps2 = db.IntField(required=True, choices=(0, 1, 2, 3, 4, 5)) + caps1 = db.IntField(required=True, choices=(0, 1, 2, 3, 4, 5)) + caps2 = db.IntField(required=True, choices=(0, 1, 2, 3, 4, 5)) # number of players on each side (assuming both teams had the same number of players) - players = db.IntField() - map = db.StringField(required=True) + players = db.IntField() + map = db.StringField(required=True) strongpoints = db.ListField(db.StringField(), max_length=5) - date = db.DateTimeField(required=True) + date = db.DateTimeField(required=True) # how long the game lasted, max is 90 min - duration = db.IntField() + duration = db.IntField() # competitive factor, see HeLO calculations - factor = db.FloatField(default=1.0) + factor = db.FloatField(default=1.0) # name of the tournament, of just a training match - event = db.StringField() + event = db.StringField() # confirmation, very important # match must be confirmed from both sides (representatives) in order to # take the match into account # user id of the user who confirmed the match for clan1 - conf1 = db.StringField() + conf1 = db.StringField() # user id of the user who confirmed the match for clan2 - conf2 = db.StringField() + conf2 = db.StringField() # flag to check whether corresponding score objects to the match exist or not score_posted = db.BooleanField() # reserved for admins, necessary to start a recalculate process for this match @@ -66,6 +62,20 @@ class Match(db.Document): "queryset_class": CustomQuerySet } + def clean(self): + if self.__playerCount() > 100: + raise ValueError("player_dist cannot exceed 100 players") + + if (self.player_dist1 is not None or self.player_dist2 is not None) and self.players != 0: + raise ValueError("players and player distributions cannot both be set") + + def __playerCount(self): + if self.players != 0: + return self.players + if self.player_dist1 is not None and self.player_dist2 is not None: + return sum(self.player_dist1, sum(self.player_dist2)) + raise ValueError("neither players, not player_dist1 and player_dist2 are set") + def needs_confirmations(self): if (self.conf1 != "" and self.conf1 is not None) and (self.conf2 != "" and self.conf2 is not None): # do the calcs then @@ -96,4 +106,5 @@ def can_be_deleted(self, user_id: str, user_clans: list[str]) -> bool: if r in self.clans2_ids: is_clan2_member = True - return not self.score_posted and (is_clan1_member or is_clan2_member or self.conf1 == user_id and self.conf2 == user_id) + return not self.score_posted and ( + is_clan1_member or is_clan2_member or self.conf1 == user_id and self.conf2 == user_id) diff --git a/rest/matches.py b/rest/matches.py index 1c25e90..4a64765 100644 --- a/rest/matches.py +++ b/rest/matches.py @@ -255,6 +255,8 @@ def post(self): return handle_error(f"match already exists in database", 400) except ValidationError as e: return handle_error(f"required field is empty: {e}") + except ValueError as e: + return handle_error(f"request is invalid: {e}") except Exception as e: return handle_error(f"error creating match in database, terminated with error: {e}", 500) else: