Change constructor of score. Create new function to add score value from a string.

This commit is contained in:
alwayscompiling
2022-08-06 17:32:17 -04:00
parent b80f160914
commit b5959ccc33
3 changed files with 35 additions and 8 deletions
+6 -2
View File
@@ -46,9 +46,13 @@ class Category:
category.time_record = data["time_record"]
for user, score in data["scores"].items():
if category.time_record:
category.add_score(ScoreTime(user, score))
score_obj: ScoreTime = ScoreTime(user)
score_obj.value = float(score)
category.add_score(score_obj)
else:
category.add_score(Score(user, score))
score_obj: Score = Score(user)
score_obj.value = int(score)
category.add_score(score_obj)
return category
+25 -4
View File
@@ -8,7 +8,7 @@ class Score:
# no user setter, it should never change.
def __init__(self, user: str, value: int) -> None:
def __init__(self, user: str) -> None:
"""
Default constructor for Score class.
@@ -17,7 +17,7 @@ class Score:
value (int): Score's value
"""
self._user: str = user
self._value: int = value
self._value: int = -1
def __str__(self) -> str:
"""
@@ -67,6 +67,15 @@ class Score:
"""
return str(self._value)
def set_value_str(self, value: str) -> None:
"""
Setter for score value from string.
Args:
value (str): int as a string
"""
self._value = int(value)
def to_str(self) -> str:
"""
Returns Score object as a string.
@@ -80,7 +89,7 @@ class Score:
class ScoreTime(Score):
"""Class for a time based score. Inherits from Score."""
def __init__(self, user: str, score: float) -> None: # pylint: disable=super-init-not-called
def __init__(self, user: str) -> None: # pylint: disable=super-init-not-called
"""
Default constructor for ScoreTime class.
@@ -90,7 +99,7 @@ class ScoreTime(Score):
"""
# store time score as seconds.
self._user: str = user
self._score: float = score
self._score: float = -1.0
@property
def value(self) -> float:
@@ -121,3 +130,15 @@ class ScoreTime(Score):
"""
milliseconds = round((self.value - int(self.value)) * 10)
return time.strftime(f"%-M:%S.{milliseconds}", time.gmtime(self.value))
def set_value_str(self, value: str) -> None:
"""
Setter for score value from a string.
Args:
value (str): string representation of time
"""
if value.find(":"):
self._score = int(value.split(":")[0]) * 60 + float(value.split(":")[1])
else:
self._score = float(value)
+4 -2
View File
@@ -46,9 +46,11 @@ class VerificationView(nextcord.ui.View):
submission_table = submission_guild.get_table(table)
submission_category = submission_table.get_category(category)
if submission_category.time_record:
submission_score: ScoreTime = ScoreTime(user, float(score))
submission_score: ScoreTime = ScoreTime(user)
submission_score.set_value_str(score)
else:
submission_score: Score = Score(user, int(score))
submission_score: Score = Score(user)
submission_score.set_value_str(score)
await submit_score(
interaction,