feat: calculate inspection resource points
This commit is contained in:
1 parent
5809d5a595
commit
21652d0001
6 files changed
+46
-2
No files matched your search
+3
-1
@@ -8,6 +8,7 @@ from .domain import (
|
||||
InspectionReport,
|
||||
TrainingSession,
|
||||
calculate_coaching,
|
||||
calculate_inspection,
|
||||
calculate_training,
|
||||
)
|
||||
from .reports import render_coaching, render_inspection, render_training
|
||||
@@ -139,7 +140,8 @@ def calculate_payload(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
inspections=_int(payload, "inspections"),
|
||||
resources=resources,
|
||||
)
|
||||
return {"mode": mode, "message": render_inspection(report)}
|
||||
result = calculate_inspection(report)
|
||||
return {"mode": mode, "result": asdict(result), "message": render_inspection(report, result)}
|
||||
except (KeyError, IndexError, TypeError, ValueError) as exc:
|
||||
if isinstance(exc, ApiError):
|
||||
raise
|
||||
|
||||
@@ -146,3 +146,27 @@ class InspectionReport:
|
||||
for name, amount in self.resources:
|
||||
if not name.strip() or not isinstance(amount, int):
|
||||
raise ValueError("Ресурс должен иметь название и целое количество")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class InspectionResult:
|
||||
points: int
|
||||
|
||||
|
||||
def _inspection_resource_value(name: str) -> int:
|
||||
normalized = " ".join(name.casefold().replace("ё", "е").split())
|
||||
if "надпис" in normalized or "ядовит" in normalized:
|
||||
return 0
|
||||
if normalized in ("паутина", "паук"):
|
||||
return 7
|
||||
if normalized == "обычный мох":
|
||||
return 3
|
||||
if normalized == "водяной мох":
|
||||
return 2
|
||||
return 1
|
||||
|
||||
|
||||
def calculate_inspection(report: InspectionReport) -> InspectionResult:
|
||||
points = report.inspections * 2
|
||||
points += sum(amount * _inspection_resource_value(name) for name, amount in report.resources)
|
||||
return InspectionResult(points=points)
|
||||
@@ -4,8 +4,10 @@ from .domain import (
|
||||
CoachingResult,
|
||||
CoachingSession,
|
||||
InspectionReport,
|
||||
InspectionResult,
|
||||
TrainingSession,
|
||||
TrainingResult,
|
||||
calculate_inspection,
|
||||
format_clock,
|
||||
parse_clock,
|
||||
)
|
||||
@@ -38,7 +40,8 @@ def render_training(session: TrainingSession, result: TrainingResult | None = No
|
||||
)
|
||||
|
||||
|
||||
def render_inspection(report: InspectionReport) -> str:
|
||||
def render_inspection(report: InspectionReport, result: InspectionResult | None = None) -> str:
|
||||
result = result or calculate_inspection(report)
|
||||
if report.inspections == 1:
|
||||
word = "раз"
|
||||
else:
|
||||
@@ -55,4 +58,5 @@ def render_inspection(report: InspectionReport) -> str:
|
||||
)
|
||||
else:
|
||||
lines.append("0")
|
||||
lines.append(f"Баллы осмотра: {result.points} баллы.")
|
||||
return "\n".join(lines)
|
||||
Reference in new issue
Block a user