Skip to content
Open
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
45 changes: 28 additions & 17 deletions models/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -66,6 +62,20 @@ class Match(db.Document):
"queryset_class": CustomQuerySet
}

def clean(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is only clever, if we are sure there is no invalid data in the db already, which the system needs to be able to save again. Reading shouldn't be an issue, though.

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
Expand Down Expand Up @@ -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)
2 changes: 2 additions & 0 deletions rest/matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down