68 lines
2.6 KiB
GDScript
68 lines
2.6 KiB
GDScript
class_name FactionWar
|
||
extends RefCounted
|
||
|
||
# The imported faction entries remain data-only. This run-owned service keeps
|
||
# their kill-derived standings and exposes neutral flags for titles/achievements.
|
||
var _catalog: ContentCatalog
|
||
var _state: Dictionary
|
||
|
||
func _init(catalog: ContentCatalog, state: Dictionary = {}) -> void:
|
||
_catalog = catalog
|
||
_state = state
|
||
if not _state.has("faction_war_scores"):
|
||
_state["faction_war_scores"] = {}
|
||
if not _state.has("faction_war_notified_leader"):
|
||
_state["faction_war_notified_leader"] = ""
|
||
|
||
func record_kill(faction_id: StringName) -> StringName:
|
||
var faction := definition(faction_id)
|
||
var rival_id := StringName(faction.get("rival_id", ""))
|
||
if faction.is_empty() or rival_id.is_empty() or definition(rival_id).is_empty():
|
||
return &""
|
||
var scores: Dictionary = _state["faction_war_scores"]
|
||
scores[rival_id] = kill_count(rival_id) + 1
|
||
return rival_id
|
||
|
||
func kill_count(faction_id: StringName) -> int:
|
||
return int((_state["faction_war_scores"] as Dictionary).get(faction_id, 0))
|
||
|
||
func leader() -> Dictionary:
|
||
var ranked: Array[Dictionary] = []
|
||
for faction_id: StringName in faction_ids():
|
||
ranked.append({"id": faction_id, "score": kill_count(faction_id)})
|
||
ranked.sort_custom(func(left: Dictionary, right: Dictionary) -> bool:
|
||
return int(left.score) > int(right.score) or (int(left.score) == int(right.score) and String(left.id) < String(right.id))
|
||
)
|
||
if ranked.is_empty() or int(ranked[0].score) <= 0:
|
||
return {}
|
||
var top: Dictionary = ranked[0]
|
||
var runner_up_score := int(ranked[1].score) if ranked.size() > 1 else 0
|
||
var margin := int(top.score) - runner_up_score
|
||
return {"id": StringName(top.id), "margin": margin} if margin > 0 else {}
|
||
|
||
func notified_leader() -> StringName:
|
||
return StringName(_state["faction_war_notified_leader"])
|
||
|
||
func set_notified_leader(faction_id: StringName) -> void:
|
||
_state["faction_war_notified_leader"] = faction_id
|
||
|
||
func flags() -> Dictionary:
|
||
var current_leader := leader()
|
||
return {
|
||
&"faction_war_leader": StringName(current_leader.get("id", "")),
|
||
&"faction_war_margin": int(current_leader.get("margin", 0)),
|
||
}
|
||
|
||
func standings_label() -> String:
|
||
var current_leader := leader()
|
||
if current_leader.is_empty():
|
||
return "Aucune guerre de factions engagée pour l’instant."
|
||
var faction := definition(StringName(current_leader.id))
|
||
return "%s domine (marge : %d)." % [String(faction.get("name", current_leader.id)), int(current_leader.margin)]
|
||
|
||
func faction_ids() -> Array[StringName]:
|
||
return _catalog.get_ids(&"faction") if _catalog != null else []
|
||
|
||
func definition(faction_id: StringName) -> Dictionary:
|
||
return _catalog.get_faction(faction_id) if _catalog != null else {}
|