Initial commit: the FS25 Lua mod (live.json data export), the FS25-WebDashboard project (Node/web dashboard + PySide6 desktop variant), and FarmHUD (standalone PySide6 companion app with Discord Rich Presence, settings, and installer).
166 lines
5.1 KiB
Python
166 lines
5.1 KiB
Python
"""Formatting helpers for the small set of stats FarmHUD shows."""
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
|
||
def is_number(value: Any) -> bool:
|
||
return isinstance(value, (int, float)) and not isinstance(value, bool)
|
||
|
||
|
||
def format_euro(n: Any) -> str:
|
||
if not is_number(n):
|
||
return "–"
|
||
text = f"{round(n):,}".replace(",", ".")
|
||
return f"{text} €"
|
||
|
||
|
||
def format_speed(vehicle: dict) -> str:
|
||
if not vehicle:
|
||
return "–"
|
||
speed = vehicle.get("speedKmh")
|
||
if is_number(speed) and (isinstance(vehicle.get("motorStarted"), bool) or isinstance(vehicle.get("cruiseControlActive"), bool)):
|
||
return f"{round(max(0.0, speed))} km/h"
|
||
return "–"
|
||
|
||
|
||
def format_game_time_parts(time_data: dict) -> tuple[str, str]:
|
||
"""(clock, day) as two separate short strings - kept apart so a tight
|
||
layout can show the clock as the headline and the day as a small
|
||
subtitle instead of squeezing both onto one line."""
|
||
if not time_data:
|
||
return "--:--", ""
|
||
minutes = None
|
||
day_time_minutes = time_data.get("dayTimeMinutes")
|
||
current_hour = time_data.get("currentHour")
|
||
if is_number(day_time_minutes):
|
||
minutes = day_time_minutes / 60000 if day_time_minutes > 1440 else day_time_minutes
|
||
elif is_number(current_hour):
|
||
minutes = current_hour * 60
|
||
clock = "--:--"
|
||
if minutes is not None:
|
||
minutes = ((minutes % 1440) + 1440) % 1440
|
||
hh = int(minutes // 60)
|
||
mm = int(minutes % 60)
|
||
clock = f"{hh:02d}:{mm:02d}"
|
||
day = time_data.get("currentDay")
|
||
day_text = f"Tag {day}" if is_number(day) else ""
|
||
return clock, day_text
|
||
|
||
|
||
def format_game_time(time_data: dict) -> str:
|
||
clock, day_text = format_game_time_parts(time_data)
|
||
return f"{clock} · {day_text}" if day_text else clock
|
||
|
||
|
||
def farmland_total_value(mission: dict, farmlands: list) -> float:
|
||
if not mission:
|
||
return 0.0
|
||
farm_id = mission.get("farmId")
|
||
owned = [f for f in (farmlands or []) if f.get("isOwned") and f.get("farmId") == farm_id]
|
||
return sum(f.get("price") or 0 for f in owned)
|
||
|
||
|
||
def format_percent(n: Any) -> str:
|
||
if not is_number(n):
|
||
return "–"
|
||
return f"{round(n)}%"
|
||
|
||
|
||
def format_liters(n: Any) -> str:
|
||
if not is_number(n):
|
||
return "0 l"
|
||
text = f"{round(n):,}".replace(",", ".")
|
||
return f"{text} l"
|
||
|
||
|
||
def format_price_per_liter(n: Any) -> str:
|
||
if not is_number(n):
|
||
return "–"
|
||
return f"{n:.2f} €/l"
|
||
|
||
|
||
def format_fuel_percent(vehicle: dict) -> str:
|
||
level = vehicle.get("fuelLevel")
|
||
capacity = vehicle.get("fuelCapacity")
|
||
if not is_number(level) or not is_number(capacity) or capacity <= 0:
|
||
return "–"
|
||
return format_percent((level / capacity) * 100)
|
||
|
||
|
||
def format_cruise(vehicle: dict) -> str:
|
||
if not vehicle.get("cruiseControlActive"):
|
||
return "aus"
|
||
speed = vehicle.get("cruiseControlSpeed")
|
||
return f"an · {round(speed)} km/h" if is_number(speed) else "an"
|
||
|
||
|
||
def format_minutes_as_duration(total_minutes: Any) -> str:
|
||
if not is_number(total_minutes):
|
||
return "–"
|
||
hours = int(total_minutes // 60)
|
||
return f"{hours} h"
|
||
|
||
|
||
def yes_no(value: Any) -> str:
|
||
if value is True:
|
||
return "ja"
|
||
if value is False:
|
||
return "nein"
|
||
return "–"
|
||
|
||
|
||
# FS25's mission.type.name is an internal camelCase identifier
|
||
# ("stonePickMission", "baleWrapMission", ...), not display text - showing
|
||
# it raw (or just .capitalize()'d, which produces "Stonepickmission") reads
|
||
# as a bug, not a label. Known ones get a proper German name; anything
|
||
# unmapped still gets split into readable words instead of one mashed-together one.
|
||
MISSION_TYPE_LABELS = {
|
||
"mowmission": "Mähen",
|
||
"grassmission": "Gras mähen",
|
||
"harvestmission": "Ernten",
|
||
"cultivatemission": "Grubbern",
|
||
"plowmission": "Pflügen",
|
||
"sowmission": "Säen",
|
||
"weedmission": "Unkraut entfernen",
|
||
"herbicidemission": "Pflanzenschutz",
|
||
"fertilizemission": "Düngen",
|
||
"hoemission": "Hacken",
|
||
"rollermission": "Walzen",
|
||
"stonepickmission": "Steine sammeln",
|
||
"balewrapmission": "Ballen wickeln",
|
||
"balecollectmission": "Ballen einsammeln",
|
||
"transportmission": "Transport",
|
||
"delivermission": "Lieferung",
|
||
"treemission": "Bäume fällen",
|
||
"destructiblerockmission": "Felsen sprengen",
|
||
"levelingmission": "Planieren",
|
||
"animaldealermission": "Tierhandel",
|
||
"universalmission": "Sammelauftrag",
|
||
}
|
||
|
||
|
||
def format_mission_type(raw: Any) -> str:
|
||
if not raw:
|
||
return "Auftrag"
|
||
key = str(raw).lower()
|
||
if key in MISSION_TYPE_LABELS:
|
||
return MISSION_TYPE_LABELS[key]
|
||
|
||
# Fallback: split camelCase into words ("baleWrapMission" ->
|
||
# "bale Wrap Mission"), drop a trailing "Mission", sentence-case it.
|
||
text = str(raw)
|
||
words = []
|
||
current = text[0]
|
||
for ch in text[1:]:
|
||
if ch.isupper() and current and not current[-1].isupper():
|
||
words.append(current)
|
||
current = ch
|
||
else:
|
||
current += ch
|
||
words.append(current)
|
||
if words and words[-1].lower() == "mission":
|
||
words = words[:-1]
|
||
phrase = " ".join(w.lower() for w in words if w)
|
||
return phrase[:1].upper() + phrase[1:] if phrase else "Auftrag"
|