refactor: Regelsatz als eigenes Objekt (RulesContext)

Etappe 5 der Controller-Zerlegung. Der Regelsatz liegt jetzt in
`src/core/rules/rules_context.gd`: Er lädt die Bibliothek, merkt sich die
Voreinstellung, wendet eine Saison an und hält die drei Dokumente
(catalog, levels, adult).

- Der Controller besitzt genau eine Instanz (`_ruleset`) statt sechs
  Feldern; alle Zugriffe laufen über sie.
- `_load_default_rules` und `_apply_rules_for_season` delegieren nur noch.
- `RulesContext` ist ohne Szene testbar (Saison laden, wechseln, leeren);
  eigener Test mit 10 Assertions.

Bewusst offen: die drei Statusleisten-Widgets (Saison, Programm, Kategorie)
und ihre Sync-Funktionen. Sie sind reine UI und bekommen einen eigenen
Node-Schnitt (Etappe 6).

Controller: 8.529 -> 8.513 Zeilen (überwiegend Struktur, nicht Zeilen).
Suite grün.
This commit is contained in:
torsten 2026-11-13 13:30:36 +01:00
parent 7ee5e36af4
commit ef25c380c9
6 changed files with 212 additions and 116 deletions

View file

@ -0,0 +1,71 @@
class_name RulesContext
extends RefCounted
## Owns the season rulebooks of the whiteboard.
##
## The context discovers the season packages, keeps the one that is currently
## applied and exposes its three documents (`catalog`, `levels`, `adult`). The
## controller holds one instance and asks it to switch seasons; nothing else
## needs to know about [RulesLibrary] or [RulesPackage].
var library: RulesLibrary
var package: RulesPackage
## Season new routines start with (newest complete season by `valid_from`).
var default_season: String = ""
var catalog: ElementCatalog
var levels: ElementLevelRules
var adult: AdultCategoryRules
## Scans the season folders and applies the default season. Returns the
## package, or null when no usable season was found.
func load_default() -> RulesPackage:
library = RulesLibrary.scan()
default_season = library.get_default_season() if library != null else ""
return apply(default_season)
## Applies one season. An empty or unknown season clears the documents on
## purpose: the app then says that it cannot check instead of silently using
## the wrong rulebook. Returns the applied package or null.
func apply(season: String) -> RulesPackage:
package = null
catalog = null
levels = null
adult = null
if library == null:
return null
var cleaned := season.strip_edges()
if cleaned.is_empty():
return null
package = library.get_package(cleaned)
if package == null:
return null
catalog = package.catalog
levels = package.levels
adult = package.adult
return package
func has_season() -> bool:
return package != null
func is_complete() -> bool:
return package != null and package.is_complete()
func season_label() -> String:
if package != null and not package.season.is_empty():
return package.season
return ""
func get_errors() -> PackedStringArray:
var errors := PackedStringArray()
if library != null:
errors.append_array(library.get_errors())
if package != null:
errors.append_array(package.get_errors())
return errors

View file

@ -0,0 +1 @@
uid://vppy828isg22

View file

@ -337,12 +337,7 @@ var _direction: PathSegment.Direction = PathSegment.Direction.FORWARD
var _edge: PathSegment.Edge = PathSegment.Edge.OUTSIDE
var _element_type: PathSegment.ElementType = PathSegment.ElementType.JUMP
var _element_level: int = 0
var _element_catalog: ElementCatalog
var _level_rules: ElementLevelRules
var _adult_rules: AdultCategoryRules
var _rules_library: RulesLibrary
var _rules_package: RulesPackage
var _default_season: String = ""
var _ruleset: RulesContext = RulesContext.new()
var _dialog_features: PackedStringArray = PackedStringArray()
var _dialog_feature_feet: Dictionary[String, String] = {}
var _dialog_jump_kind: PathSegment.JumpKind = PathSegment.JumpKind.SOLO
@ -604,7 +599,7 @@ func new_routine() -> void:
var routine := Routine.new()
routine.routine_id = "draft-routine"
routine.title = "Neue Kür"
routine.season = _default_season
routine.season = _ruleset.default_season
routine.rink = RinkDimensions.new()
var section := RoutineSection.new()
@ -805,31 +800,32 @@ func get_element_level() -> int:
func get_element_catalog() -> ElementCatalog:
return _element_catalog
return _ruleset.catalog
func get_level_rules() -> ElementLevelRules:
return _level_rules
return _ruleset.levels
func get_rules_package() -> RulesPackage:
return _rules_package
return _ruleset.package
func get_rules_library() -> RulesLibrary:
return _rules_library
return _ruleset.library
## Loads the season package new routines start with. Seasons are discovered on
## disk; there is no hardcoded file name here.
func _load_default_rules() -> void:
_rules_library = RulesLibrary.scan()
if not _rules_library.get_errors().is_empty():
push_warning("Rules: %s" % ", ".join(_rules_library.get_errors()))
_default_season = _rules_library.get_default_season()
if _default_season.is_empty():
var package := _ruleset.load_default()
if _ruleset.library != null and not _ruleset.library.get_errors().is_empty():
push_warning("Rules: %s" % ", ".join(_ruleset.library.get_errors()))
if package == null:
push_warning("Rules: keine nutzbare Saison gefunden")
_apply_rules_for_season(_default_season)
return
for error: String in package.get_errors():
push_warning("Rules (%s): %s" % [package.season, error])
## Resolves the rule package of one season and points the inspector, the
@ -837,28 +833,16 @@ func _load_default_rules() -> void:
## rules unset on purpose: the app then says that it cannot check instead of
## silently validating against the wrong rulebook.
func _apply_rules_for_season(season: String) -> void:
_rules_package = null
_element_catalog = null
_level_rules = null
_adult_rules = null
if _rules_library == null:
var package := _ruleset.apply(season)
if package == null:
return
var cleaned := season.strip_edges()
if cleaned.is_empty():
return
_rules_package = _rules_library.get_package(cleaned)
if _rules_package == null:
return
for error: String in _rules_package.get_errors():
push_warning("Rules (%s): %s" % [_rules_package.season, error])
_element_catalog = _rules_package.catalog
_level_rules = _rules_package.levels
_adult_rules = _rules_package.adult
for error: String in package.get_errors():
push_warning("Rules (%s): %s" % [package.season, error])
func _catalog_season_label() -> String:
if _rules_package != null and not _rules_package.season.is_empty():
return _rules_package.season
if _ruleset.package != null and not _ruleset.package.season.is_empty():
return _ruleset.package.season
if _routine != null and not _routine.season.is_empty():
return _routine.season
return I18n.t("nicht festgelegt")
@ -868,9 +852,9 @@ func _catalog_season_label() -> String:
## Paths and section bookkeeping are validated by the model itself.
func validate_routine() -> Array[ValidationIssue]:
var validator: RoutineValidator = ROUTINE_VALIDATOR_SCRIPT.new(
_element_catalog,
_level_rules,
_adult_rules
_ruleset.catalog,
_ruleset.levels,
_ruleset.adult
)
return validator.validate(_routine)
@ -887,18 +871,18 @@ func open_validation_dialog() -> void:
func _render_validation_dialog() -> void:
var validator: RoutineValidator = ROUTINE_VALIDATOR_SCRIPT.new(
_element_catalog,
_level_rules,
_adult_rules
_ruleset.catalog,
_ruleset.levels,
_ruleset.adult
)
var report := validator.validate_report(_routine)
var category: AdultCategoryRules.Category = null
if (
_routine != null
and not _routine.adult_category.is_empty()
and _adult_rules != null
and _ruleset.adult != null
):
category = _adult_rules.get_category(
category = _ruleset.adult.get_category(
_routine.adult_category,
_routine.program_type
)
@ -927,9 +911,9 @@ func _render_validation_dialog() -> void:
validation_dialog.popup_centered(target)
func open_upgrade_dialog() -> void:
if _routine == null or _rules_package == null or _rules_library == null:
if _routine == null or _ruleset.package == null or _ruleset.library == null:
return
if _rules_library.get_seasons().size() < 2:
if _ruleset.library.get_seasons().size() < 2:
return
_fill_upgrade_seasons()
_run_upgrade_check()
@ -945,7 +929,7 @@ func open_upgrade_dialog() -> void:
func _fill_upgrade_seasons() -> void:
upgrade_season_option.clear()
var current := _routine.season.strip_edges()
for season: String in _rules_library.get_seasons():
for season: String in _ruleset.library.get_seasons():
if season == current:
continue
var index := upgrade_season_option.item_count
@ -963,17 +947,17 @@ func _selected_upgrade_season() -> String:
func _run_upgrade_check() -> void:
if _routine == null or _rules_package == null or _rules_library == null:
if _routine == null or _ruleset.package == null or _ruleset.library == null:
return
var season := _selected_upgrade_season()
_upgrade_target_season = season
var target := _rules_library.get_package(season)
var target := _ruleset.library.get_package(season)
if target == null:
upgrade_text.text = I18n.t("Ziel-Saison nicht verfügbar.")
upgrade_apply_button.disabled = true
upgrade_pdf_button.disabled = true
return
_upgrade_report = SeasonUpgradeCheck.run(_routine, _rules_package, target)
_upgrade_report = SeasonUpgradeCheck.run(_routine, _ruleset.package, target)
upgrade_text.text = I18n.join_lines(
SeasonComparisonText.build(_routine, _upgrade_report)
)
@ -1049,9 +1033,9 @@ func _on_validation_meta_clicked(meta: Variant) -> void:
func _can_compare_seasons() -> bool:
return (
_rules_package != null
and _rules_library != null
and _rules_library.get_seasons().size() > 1
_ruleset.package != null
and _ruleset.library != null
and _ruleset.library.get_seasons().size() > 1
)
@ -1168,17 +1152,17 @@ func update_element_feature_hint() -> void:
if type == PathSegment.ElementType.JUMP:
element_feature_hint.text = "Sprünge haben keine Level-Features."
return
if _level_rules == null:
if _ruleset.levels == null:
element_feature_hint.text = ""
return
if code.is_empty():
element_feature_hint.text = "Kürzel wählen, um Level-Features zu erfassen."
return
if _element_catalog == null or not _element_catalog.has_code(code):
if _ruleset.catalog == null or not _ruleset.catalog.has_code(code):
element_feature_hint.text = "Kürzel nicht im Katalog."
return
var result := _level_rules.calculate_level(
var result := _ruleset.levels.calculate_level(
type,
code,
features,
@ -1244,10 +1228,10 @@ func _open_feature_menu() -> void:
## Fills the feature menu from the pending dialog state. Split from the popup
## call so tests can inspect the items without opening a window.
func _populate_feature_menu() -> bool:
if _level_rules == null or not element_dialog.visible:
if _ruleset.levels == null or not element_dialog.visible:
return false
var type := _dialog_element_type()
var features := _level_rules.get_features(type)
var features := _ruleset.levels.get_features(type)
if features.is_empty():
return false
@ -1322,22 +1306,22 @@ func _update_feature_foot_panel() -> void:
child.free()
var per_foot: Array[ElementFeature] = []
if _level_rules != null and _element_catalog != null:
var definition := _element_catalog.find(_dialog_element_code())
if _ruleset.levels != null and _ruleset.catalog != null:
var definition := _ruleset.catalog.find(_dialog_element_code())
if (
definition != null
and definition.element_type == PathSegment.ElementType.SPIN
and definition.change_of_foot
):
for feature: ElementFeature in _level_rules.get_features(
for feature: ElementFeature in _ruleset.levels.get_features(
PathSegment.ElementType.SPIN
):
if feature.per_foot and _dialog_features.has(feature.code):
per_foot.append(feature)
var maximum := 0
if _level_rules != null:
maximum = _level_rules.get_max_features_per_foot(
if _ruleset.levels != null:
maximum = _ruleset.levels.get_max_features_per_foot(
PathSegment.ElementType.SPIN
)
var needed := maximum > 0 and per_foot.size() > maximum
@ -1407,7 +1391,7 @@ func _refresh_foot_buttons() -> void:
func update_element_value_hint() -> void:
if element_value_label == null:
return
if _element_catalog == null:
if _ruleset.catalog == null:
element_value_label.text = ""
return
@ -1419,7 +1403,7 @@ func update_element_value_hint() -> void:
if element_level_option.selected >= 0:
level = element_level_option.get_item_id(element_level_option.selected)
var code := _dialog_element_code()
var source := "%s · %s" % [_element_catalog.season, _element_catalog.source]
var source := "%s · %s" % [_ruleset.catalog.season, _ruleset.catalog.source]
element_value_label.tooltip_text = source
if type == PathSegment.ElementType.NONE or code.is_empty():
@ -1441,7 +1425,7 @@ func update_element_value_hint() -> void:
if not parts.is_empty():
value = 0.0
for part: String in parts:
var part_value := _element_catalog.get_base_value(
var part_value := _ruleset.catalog.get_base_value(
part,
0,
PathSegment.ElementType.JUMP
@ -1451,10 +1435,10 @@ func update_element_value_hint() -> void:
break
value += part_value
else:
value = _element_catalog.get_base_value(code, level, type)
value = _ruleset.catalog.get_base_value(code, level, type)
if value < 0.0:
element_value_label.text = (
I18n.t("Base Value: – (Kürzel nicht im Katalog %s)") % _element_catalog.season
I18n.t("Base Value: – (Kürzel nicht im Katalog %s)") % _ruleset.catalog.season
)
return
element_value_label.text = (
@ -1503,16 +1487,16 @@ func _is_derived_level(
return true
if (
type == PathSegment.ElementType.NONE
or _element_catalog == null
or _level_rules == null
or _ruleset.catalog == null
or _ruleset.levels == null
):
return false
var definition := _element_catalog.find(code)
var definition := _ruleset.catalog.find(code)
if definition == null or definition.element_type != type:
return false
if _level_rules.get_fixed_level(type, code) >= 0:
if _ruleset.levels.get_fixed_level(type, code) >= 0:
return true
return not _level_rules.get_features(type).is_empty()
return not _ruleset.levels.get_features(type).is_empty()
func _derived_level(
@ -1523,9 +1507,9 @@ func _derived_level(
) -> int:
if type == PathSegment.ElementType.JUMP:
return 0
if _level_rules == null:
if _ruleset.levels == null:
return 0
return _level_rules.calculate_level(type, code, features, feet).level
return _ruleset.levels.calculate_level(type, code, features, feet).level
## Keeps only the feet of selected features and normalises them to L/R.
@ -1570,9 +1554,9 @@ func _populate_code_options(type: PathSegment.ElementType) -> void:
"— Kürzel wählen —",
CODE_OPTION_PLACEHOLDER_ID
)
if _element_catalog != null and type != PathSegment.ElementType.NONE:
for family: String in _element_catalog.get_codes(type):
var definition := _element_catalog.find(family)
if _ruleset.catalog != null and type != PathSegment.ElementType.NONE:
for family: String in _ruleset.catalog.get_codes(type):
var definition := _ruleset.catalog.find(family)
var label := family
if definition != null and not definition.display_name.is_empty():
label = "%s — %s" % [family, definition.display_name]
@ -1604,8 +1588,8 @@ func _select_code_in_option(
return
var definition: ElementDefinition = null
if _element_catalog != null:
definition = _element_catalog.find(cleaned)
if _ruleset.catalog != null:
definition = _ruleset.catalog.find(cleaned)
if definition != null and definition.element_type == type:
var family := definition.code
var index := _find_code_option_index(family)
@ -1613,11 +1597,11 @@ func _select_code_in_option(
element_code_option.select(index)
element_code_edit.text = family
_set_code_free_text(false)
var suffix := _element_catalog.get_level_suffix(cleaned)
var suffix := _ruleset.catalog.get_level_suffix(cleaned)
if suffix >= 0:
_select_level_option(suffix)
elif _level_rules != null:
var fixed := _level_rules.get_fixed_level(type, family)
elif _ruleset.levels != null:
var fixed := _ruleset.levels.get_fixed_level(type, family)
if fixed >= 0:
_select_level_option(fixed)
return
@ -1667,8 +1651,8 @@ func _on_element_code_option_selected(index: int) -> void:
var type: PathSegment.ElementType = element_dialog_type_option.get_item_id(
element_dialog_type_option.selected
)
if _level_rules != null:
var fixed := _level_rules.get_fixed_level(type, family)
if _ruleset.levels != null:
var fixed := _ruleset.levels.get_fixed_level(type, family)
if fixed >= 0:
_select_level_option(fixed)
_update_level_control_state()
@ -1681,12 +1665,12 @@ func _populate_jump_extra_options() -> void:
option.clear()
option.add_item("— kein weiterer Sprung —", 0)
option.set_item_metadata(0, "")
if _element_catalog == null:
if _ruleset.catalog == null:
continue
for family: String in _element_catalog.get_codes(
for family: String in _ruleset.catalog.get_codes(
PathSegment.ElementType.JUMP
):
var definition := _element_catalog.find(family)
var definition := _ruleset.catalog.find(family)
var label := family
if definition != null and not definition.display_name.is_empty():
label = "%s — %s" % [family, definition.display_name]
@ -1827,18 +1811,18 @@ func _normalize_element_input(
var cleaned := code.strip_edges().left(PathSegment.ELEMENT_CODE_MAX_LENGTH)
var clamped := clampi(level, 0, PathSegment.ELEMENT_MAX_LEVEL)
if (
_element_catalog == null
_ruleset.catalog == null
or type == PathSegment.ElementType.NONE
or cleaned.length() < 2
):
return {"code": cleaned, "level": clamped}
var suffix := _element_catalog.get_level_suffix(cleaned)
var suffix := _ruleset.catalog.get_level_suffix(cleaned)
if suffix < 0:
return {"code": cleaned, "level": clamped}
var family := cleaned.left(cleaned.length() - 1)
var definition := _element_catalog.find(family)
var definition := _ruleset.catalog.find(family)
if definition == null or definition.element_type != type:
return {"code": cleaned, "level": clamped}
if not definition.has_level(suffix):
@ -2477,8 +2461,8 @@ func _setup_adult_category_controls() -> void:
adult_category_option.clear()
adult_category_option.add_item("Ohne Kategorie", 0)
adult_category_option.set_item_metadata(0, "")
if _adult_rules != null:
for category: AdultCategoryRules.Category in _adult_rules.get_categories(
if _ruleset.adult != null:
for category: AdultCategoryRules.Category in _ruleset.adult.get_categories(
_program_type()
):
var index := adult_category_option.item_count
@ -2562,8 +2546,8 @@ func set_adult_category(code: String) -> bool:
if (
not cleaned.is_empty()
and (
_adult_rules == null
or not _adult_rules.has_category(cleaned, _program_type())
_ruleset.adult == null
or not _ruleset.adult.has_category(cleaned, _program_type())
)
):
return false
@ -2582,7 +2566,7 @@ func get_adult_category() -> String:
func get_adult_rules() -> AdultCategoryRules:
return _adult_rules
return _ruleset.adult
func _sync_adult_category_option() -> void:
@ -2645,12 +2629,12 @@ func _sync_season_option() -> void:
season_option.set_item_metadata(0, "")
selected = 0
if _rules_library != null:
for season: String in _rules_library.get_seasons():
if _ruleset.library != null:
for season: String in _ruleset.library.get_seasons():
var index := season_option.item_count
var label := I18n.t("Saison %s") % season
var status := str(
_rules_library.get_manifest(season).get("status", "complete")
_ruleset.library.get_manifest(season).get("status", "complete")
).strip_edges()
if status != "complete":
label += " " + I18n.t("(unvollständig)")
@ -2686,7 +2670,7 @@ func _season_tooltip(current: String) -> String:
## Best guess for a routine that predates the season field: the season that was
## valid when its file was last written. Only used as a hint, never applied.
func _infer_season_from_current_path() -> String:
if _current_path.is_empty() or _rules_library == null:
if _current_path.is_empty() or _ruleset.library == null:
return ""
if not FileAccess.file_exists(_current_path):
return ""
@ -2694,7 +2678,7 @@ func _infer_season_from_current_path() -> String:
if unix <= 0:
return ""
var date := Time.get_date_string_from_unix_time(unix)
return _rules_library.get_default_season(date)
return _ruleset.library.get_default_season(date)
func _on_season_selected(index: int) -> void:
@ -2712,7 +2696,7 @@ func set_season(season: String) -> bool:
return false
if (
not cleaned.is_empty()
and (_rules_library == null or not _rules_library.has_season(cleaned))
and (_ruleset.library == null or not _ruleset.library.has_season(cleaned))
):
return false
_record_undo()
@ -2735,7 +2719,7 @@ func get_season() -> String:
## One line that explains why the check cannot use a rule package, if that is
## the case. Empty when the season resolved normally.
func _season_note() -> String:
if _rules_package != null:
if _ruleset.package != null:
return ""
var raw := _routine.season.strip_edges() if _routine != null else ""
if raw.is_empty():

View file

@ -20,6 +20,7 @@ func _init() -> void:
_test_season_pair_difference()
_test_upgrade_check()
_test_season_comparison_text()
_test_rules_context()
_cleanup()
_finish()
@ -291,6 +292,30 @@ func _test_season_comparison_text() -> void:
)
## Der Regelsatz als eigenes Objekt: Saison laden, wechseln, leeren.
func _test_rules_context() -> void:
var context := RulesContext.new()
var package := context.load_default()
_check(package != null, "the rules context loads the default season")
_check(
context.catalog != null and context.levels != null and context.adult != null,
"the context exposes the three documents"
)
_check(
context.season_label() == context.library.get_default_season(),
"the context reports the default season"
)
var older := context.apply("2025/26")
_check(older != null, "the context switches to an older season")
_check(context.season_label() == "2025/26", "the applied season is reported")
_check(context.adult != null, "2025/26 has adult rules")
_check(context.apply("") == null, "an empty season applies nothing")
_check(context.catalog == null, "an empty season clears the documents")
context.apply("2030/31")
_check(context.package == null, "an unknown season clears the documents")
func _make_pair_routine() -> Routine:
var routine := Routine.new()
routine.routine_id = "season-pair"