fix(goal): bound public calculator inputs
This commit is contained in:
1 parent
041d717439
commit
5d6e5987d1
8 files changed
+58
-8
No files matched your search
+13
-5
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from difflib import SequenceMatcher
|
||||
from math import ceil
|
||||
from math import ceil, isfinite
|
||||
import re
|
||||
from typing import Literal
|
||||
|
||||
@@ -19,6 +19,11 @@ Place = Literal["дупло", "расщелина"]
|
||||
|
||||
_CLOCK_RE = re.compile(r"^(?:[01]\d|2[0-3]):[0-5]\d$")
|
||||
|
||||
# The goal calculator is public. These limits keep accidental and hostile
|
||||
# values from turning a simple estimate into an unbounded exponentiation.
|
||||
MAX_GOAL_MINUTES_PER_SESSION = 24 * 60
|
||||
MAX_GOAL_KITTENS = 50
|
||||
|
||||
|
||||
def parse_clock(value: str) -> int:
|
||||
"""Преобразует строку 'HH:MM' в количество минут от начала суток."""
|
||||
@@ -134,16 +139,19 @@ def calculate_goal(
|
||||
kitten_count: int = 1,
|
||||
) -> GoalResult:
|
||||
"""Рассчитывает, сколько времени нужно до целевого числа баллов."""
|
||||
for value, label in ((current_points, "Текущие баллы"), (target_points, "Целевые баллы")):
|
||||
if isinstance(value, bool) or not isinstance(value, (int, float)) or not isfinite(value):
|
||||
raise ValueError(f"{label} должны быть конечным числом")
|
||||
if skill not in ("ЛУ", "ЗУ"):
|
||||
raise ValueError("Навык должен быть ЛУ или ЗУ")
|
||||
if kitten_independence not in ("самостоятельно", "несамостоятельно"):
|
||||
raise ValueError("Некорректный статус самостоятельности")
|
||||
if current_points < 0 or target_points < 0:
|
||||
raise ValueError("Баллы не могут быть отрицательными")
|
||||
if minutes_per_session <= 0:
|
||||
raise ValueError("Продолжительность захода должна быть больше нуля")
|
||||
if kitten_count <= 0:
|
||||
raise ValueError("Количество котят должно быть положительным")
|
||||
if isinstance(minutes_per_session, bool) or not isinstance(minutes_per_session, int) or not 0 < minutes_per_session <= MAX_GOAL_MINUTES_PER_SESSION:
|
||||
raise ValueError(f"Продолжительность захода должна быть целым числом от 1 до {MAX_GOAL_MINUTES_PER_SESSION} минут")
|
||||
if isinstance(kitten_count, bool) or not isinstance(kitten_count, int) or not 0 < kitten_count <= MAX_GOAL_KITTENS:
|
||||
raise ValueError(f"Количество котят должно быть целым числом от 1 до {MAX_GOAL_KITTENS}")
|
||||
if skill == "ЗУ" and kitten_independence != "самостоятельно":
|
||||
raise ValueError("Для ЗУ котёнок должен быть самостоятельным")
|
||||
|
||||
|
||||
Reference in new issue
Block a user