492 lines
20 KiB
GDScript
492 lines
20 KiB
GDScript
class_name P100WaveBattleService
|
|
extends RefCounted
|
|
|
|
const Content = preload("res://p10_0/content_registry.gd")
|
|
|
|
const TICK_MS := 100
|
|
const MAX_TICKS := 1500
|
|
const ROLE_TAGS := [
|
|
"front_hold", "single_pressure", "reach_control", "reorder", "projectile",
|
|
"area", "restore", "enable", "construct", "impair", "tempo", "supply",
|
|
]
|
|
|
|
var catalog: Dictionary = Content.snapshot()
|
|
var battle: Dictionary = {}
|
|
var valid := false
|
|
var error_code := ""
|
|
|
|
|
|
func _init(run_snapshot: Dictionary = {}) -> void:
|
|
if run_snapshot.is_empty():
|
|
error_code = "run_snapshot_required"
|
|
return
|
|
var current = run_snapshot.get("current_run", {})
|
|
if not current is Dictionary or String(current.get("phase", "")) != "encounter":
|
|
error_code = "encounter_required"
|
|
return
|
|
var encounter = current.get("encounter", {})
|
|
if not encounter is Dictionary or encounter.is_empty():
|
|
error_code = "encounter_required"
|
|
return
|
|
if Array(encounter.get("deployed_recruit_ids", [])) != Array(current.get("deployed_recruit_ids", [])):
|
|
error_code = "encounter_line_mismatch"
|
|
return
|
|
_initialize_battle(current)
|
|
valid = true
|
|
|
|
|
|
func advance_ticks(count: int) -> Dictionary:
|
|
if not valid:
|
|
return {"ok": false, "code": error_code}
|
|
if count < 0:
|
|
return {"ok": false, "code": "invalid_tick_count"}
|
|
for _step in range(count):
|
|
if bool(battle.terminal):
|
|
break
|
|
_advance_one_tick()
|
|
return {"ok": true, "code": "advanced", "snapshot": snapshot()}
|
|
|
|
|
|
func run_to_terminal() -> Dictionary:
|
|
if not valid:
|
|
return {"ok": false, "code": error_code}
|
|
while not bool(battle.terminal) and int(battle.tick) < MAX_TICKS:
|
|
_advance_one_tick()
|
|
return {"ok": true, "code": "terminal", "snapshot": snapshot()}
|
|
|
|
|
|
func begin_retreat(expected_tick: int) -> Dictionary:
|
|
if not valid:
|
|
return {"ok": false, "code": error_code}
|
|
if bool(battle.terminal):
|
|
return {"ok": false, "code": "battle_terminal"}
|
|
if expected_tick != int(battle.tick):
|
|
return {"ok": false, "code": "stale_tick"}
|
|
if int(battle.retreat_requested_tick) >= 0:
|
|
return {"ok": false, "code": "retreat_already_requested"}
|
|
battle.retreat_requested_tick = int(battle.tick)
|
|
battle.retreat_completion_tick = int(battle.tick) + 20
|
|
_log_event("retreat_started", "system", "", {"completion_tick": int(battle.retreat_completion_tick)})
|
|
return {"ok": true, "code": "retreat_started", "snapshot": snapshot()}
|
|
|
|
|
|
func snapshot() -> Dictionary:
|
|
var result := battle.duplicate(true)
|
|
result.erase("snapshot_hash")
|
|
result["event_log_hash"] = _hash(result.get("event_log", []))
|
|
result["snapshot_hash"] = _hash(result)
|
|
return result
|
|
|
|
|
|
func _initialize_battle(current: Dictionary) -> void:
|
|
var encounter: Dictionary = current.encounter
|
|
var root_seed := _digest(String(current.run_seed), "wave-battle", [current.market_index, encounter.offer_id, encounter.risk])
|
|
battle = {
|
|
"schema": 1,
|
|
"ruleset": "p10.0-contract-r1",
|
|
"root_seed": root_seed,
|
|
"run_id": String(current.run_id),
|
|
"contract_id": String(current.contract_id),
|
|
"difficulty": String(current.difficulty),
|
|
"market_index": int(current.market_index),
|
|
"offer_id": String(encounter.offer_id),
|
|
"risk": int(encounter.risk),
|
|
"wave_count": int(encounter.wave_count),
|
|
"enemy_blueprint_ids": Array(encounter.enemy_ids).duplicate(),
|
|
"spawn_ticks": _spawn_ticks(int(encounter.wave_count)),
|
|
"next_wave": 0,
|
|
"tick": 0,
|
|
"terminal": false,
|
|
"outcome": "",
|
|
"ally_order": Array(encounter.deployed_recruit_ids).duplicate(),
|
|
"reserve_ids": Array(current.reserve_recruit_ids).duplicate(),
|
|
"enemy_order": [],
|
|
"actors": {},
|
|
"event_log": [],
|
|
"fallen_recruit_ids": [],
|
|
"first_decisive_event": {},
|
|
"retreat_requested_tick": -1,
|
|
"retreat_completion_tick": -1,
|
|
"max_ticks": MAX_TICKS,
|
|
"tick_ms": TICK_MS,
|
|
}
|
|
for recruit_id in battle.ally_order:
|
|
var record: Dictionary = current.recruit_records[recruit_id]
|
|
battle.actors[recruit_id] = _ally_actor(String(recruit_id), record, current)
|
|
_log_event("battle_started", "system", "", {"deployed": battle.ally_order.duplicate(), "reserve": battle.reserve_ids.duplicate(), "waves": battle.wave_count})
|
|
|
|
|
|
func _advance_one_tick() -> void:
|
|
_spawn_due_waves()
|
|
var acting_allies: Array = battle.ally_order.duplicate()
|
|
var acting_enemies: Array = battle.enemy_order.duplicate()
|
|
for actor_id in acting_allies + acting_enemies:
|
|
if not battle.actors.has(actor_id) or int(battle.actors[actor_id].hp) <= 0:
|
|
continue
|
|
battle.actors[actor_id].readiness = int(battle.actors[actor_id].readiness) + int(battle.actors[actor_id].tempo)
|
|
if int(battle.actors[actor_id].readiness) >= 1000:
|
|
battle.actors[actor_id].readiness = int(battle.actors[actor_id].readiness) - 1000
|
|
battle.actors[actor_id].action_sequence = int(battle.actors[actor_id].action_sequence) + 1
|
|
if String(battle.actors[actor_id].side) == "ally":
|
|
_resolve_ally_action(String(actor_id))
|
|
else:
|
|
_resolve_enemy_action(String(actor_id))
|
|
_evaluate_terminal()
|
|
if not bool(battle.terminal) and int(battle.retreat_completion_tick) >= 0 and int(battle.tick) >= int(battle.retreat_completion_tick):
|
|
battle.terminal = true
|
|
battle.outcome = "retreat"
|
|
_log_event("outcome", "system", "", {"outcome": "retreat", "requested_tick": int(battle.retreat_requested_tick)})
|
|
if not bool(battle.terminal):
|
|
battle.tick = int(battle.tick) + 1
|
|
if int(battle.tick) >= MAX_TICKS:
|
|
battle.terminal = true
|
|
battle.outcome = "timeout"
|
|
_log_event("outcome", "system", "", {"outcome": "timeout"})
|
|
|
|
|
|
func _spawn_due_waves() -> void:
|
|
while int(battle.next_wave) < int(battle.wave_count) and int(battle.tick) >= int(battle.spawn_ticks[int(battle.next_wave)]):
|
|
var wave_index := int(battle.next_wave)
|
|
var blueprint_id := String(battle.enemy_blueprint_ids[wave_index % battle.enemy_blueprint_ids.size()])
|
|
var enemy_id := "W%02d-%s" % [wave_index + 1, blueprint_id]
|
|
battle.actors[enemy_id] = _enemy_actor(enemy_id, blueprint_id, wave_index, int(battle.risk), int(battle.market_index))
|
|
battle.enemy_order.append(enemy_id)
|
|
battle.next_wave = wave_index + 1
|
|
_log_event("wave_arrived", enemy_id, "", {"wave": wave_index + 1, "blueprint_id": blueprint_id, "overlap": battle.enemy_order.size() > 1})
|
|
|
|
|
|
func _resolve_ally_action(actor_id: String) -> void:
|
|
var actor: Dictionary = battle.actors[actor_id]
|
|
var role := String(actor.role)
|
|
if role == "restore":
|
|
var heal_target := _lowest_hp_ally()
|
|
if not heal_target.is_empty() and int(battle.actors[heal_target].hp) < int(battle.actors[heal_target].max_hp):
|
|
_apply_heal(actor_id, heal_target, int(actor.heal), "profession_restore")
|
|
return
|
|
if role == "enable":
|
|
var front := _front_ally()
|
|
if not front.is_empty():
|
|
var guard_gain := 8 + int(actor.focus)
|
|
battle.actors[front].guard = int(battle.actors[front].guard) + guard_gain
|
|
_log_event("guard", actor_id, front, {"amount": guard_gain, "rule": "profession_enable"})
|
|
if role == "tempo":
|
|
var slowest := _slowest_ready_ally(actor_id)
|
|
if not slowest.is_empty():
|
|
battle.actors[slowest].readiness = mini(1000, int(battle.actors[slowest].readiness) + 180)
|
|
_log_event("readiness", actor_id, slowest, {"amount": 180, "rule": "profession_tempo"})
|
|
if battle.enemy_order.is_empty():
|
|
_log_event("idle", actor_id, "", {"reason": "no_enemy"})
|
|
return
|
|
var targets := _ally_targets(actor_id, role)
|
|
var damage := int(actor.power)
|
|
if role == "single_pressure":
|
|
damage += 5
|
|
elif role == "construct":
|
|
damage += 3 if int(actor.action_sequence) % 2 == 0 else 0
|
|
elif role == "impair":
|
|
damage += 1
|
|
elif role in ["restore", "enable", "tempo", "supply"]:
|
|
damage = maxi(4, int(floor(float(damage) * 0.65)))
|
|
for target_id in targets:
|
|
_apply_damage(actor_id, target_id, damage, "ally_%s" % role, "physical" if role not in ["area", "impair"] else "arcane")
|
|
if role == "impair" and battle.actors.has(target_id):
|
|
battle.actors[target_id].readiness = maxi(0, int(battle.actors[target_id].readiness) - 120)
|
|
_log_event("hinder", actor_id, target_id, {"amount": 120, "rule": "profession_impair"})
|
|
if role == "reorder" and actor_id in battle.ally_order:
|
|
var index: int = battle.ally_order.find(actor_id)
|
|
if index > 0:
|
|
var ahead = battle.ally_order[index - 1]
|
|
battle.ally_order[index - 1] = actor_id
|
|
battle.ally_order[index] = ahead
|
|
_log_event("reorder", actor_id, String(ahead), {"from": index, "to": index - 1, "rule": "profession_reorder"})
|
|
|
|
|
|
func _resolve_enemy_action(actor_id: String) -> void:
|
|
if battle.ally_order.is_empty():
|
|
return
|
|
var actor: Dictionary = battle.actors[actor_id]
|
|
var target_id := _enemy_target(actor)
|
|
if target_id.is_empty():
|
|
return
|
|
var intended := target_id
|
|
if target_id != _front_ally():
|
|
var interceptor := _front_interceptor()
|
|
if not interceptor.is_empty():
|
|
target_id = interceptor
|
|
_log_event("intercept", interceptor, intended, {"source": actor_id, "rule": "front_hold_intercept"})
|
|
var damage := int(actor.power)
|
|
var projectile: bool = "projectile" in actor.tags
|
|
_apply_damage(actor_id, target_id, damage, String(actor.blueprint_id), "projectile" if projectile else "physical")
|
|
if battle.actors.has(target_id) and "push" in actor.tags:
|
|
_apply_push(actor_id, target_id)
|
|
|
|
|
|
func _apply_damage(source_id: String, target_id: String, raw_damage: int, rule: String, damage_type: String) -> void:
|
|
if not battle.actors.has(target_id) or int(battle.actors[target_id].hp) <= 0:
|
|
return
|
|
var target: Dictionary = battle.actors[target_id]
|
|
var effective := raw_damage
|
|
var guard_before := int(target.guard)
|
|
if guard_before > 0:
|
|
var absorbed := mini(guard_before, maxi(1, int(floor(float(raw_damage) * 0.35))))
|
|
target.guard = guard_before - absorbed
|
|
effective = maxi(1, raw_damage - absorbed)
|
|
if damage_type == "projectile" and int(target.projectile_resists) > 0:
|
|
target.projectile_resists = int(target.projectile_resists) - 1
|
|
effective = maxi(1, int(floor(float(effective) * 0.5)))
|
|
_log_event("item_block", target_id, source_id, {"rule": "projectile_resist", "remaining": target.projectile_resists})
|
|
var hp_before := int(target.hp)
|
|
target.hp = maxi(0, hp_before - effective)
|
|
battle.actors[target_id] = target
|
|
_log_event("damage", source_id, target_id, {"rule": rule, "damage_type": damage_type, "raw": raw_damage, "effective": effective, "guard_before": guard_before, "hp_before": hp_before, "hp_after": int(target.hp)})
|
|
if battle.first_decisive_event.is_empty():
|
|
var decisive_ally_loss := String(target.side) == "ally" and (int(target.hp) == 0 or float(target.hp) / float(target.max_hp) <= 0.35)
|
|
var decisive_enemy_break := String(target.side) == "enemy" and (int(target.hp) == 0 or effective >= maxi(8, int(ceil(float(target.max_hp) * 0.20))))
|
|
if decisive_ally_loss or decisive_enemy_break:
|
|
battle.first_decisive_event = Dictionary(battle.event_log[-1]).duplicate(true)
|
|
if int(target.hp) == 0:
|
|
_mark_fallen(target_id, source_id, rule)
|
|
|
|
|
|
func _apply_heal(source_id: String, target_id: String, amount: int, rule: String) -> void:
|
|
var hp_before := int(battle.actors[target_id].hp)
|
|
var hp_after := mini(int(battle.actors[target_id].max_hp), hp_before + amount)
|
|
battle.actors[target_id].hp = hp_after
|
|
_log_event("heal", source_id, target_id, {"rule": rule, "amount": hp_after - hp_before, "hp_before": hp_before, "hp_after": hp_after})
|
|
|
|
|
|
func _apply_push(source_id: String, target_id: String) -> void:
|
|
var actor: Dictionary = battle.actors[target_id]
|
|
if int(actor.push_resists) > 0:
|
|
actor.push_resists = int(actor.push_resists) - 1
|
|
battle.actors[target_id] = actor
|
|
_log_event("push_resisted", target_id, source_id, {"rule": "equipment_or_profession", "remaining": actor.push_resists})
|
|
return
|
|
var index: int = battle.ally_order.find(target_id)
|
|
if index >= 0 and index < battle.ally_order.size() - 1:
|
|
var behind = battle.ally_order[index + 1]
|
|
battle.ally_order[index + 1] = target_id
|
|
battle.ally_order[index] = behind
|
|
_log_event("push", source_id, target_id, {"from": index, "to": index + 1})
|
|
else:
|
|
_apply_damage(source_id, target_id, 2, "push_collision", "physical")
|
|
|
|
|
|
func _mark_fallen(actor_id: String, source_id: String, rule: String) -> void:
|
|
var side := String(battle.actors[actor_id].side)
|
|
if side == "ally":
|
|
battle.ally_order.erase(actor_id)
|
|
battle.fallen_recruit_ids.append(actor_id)
|
|
else:
|
|
battle.enemy_order.erase(actor_id)
|
|
_log_event("fallen", source_id, actor_id, {"side": side, "rule": rule})
|
|
|
|
|
|
func _evaluate_terminal() -> void:
|
|
if battle.ally_order.is_empty():
|
|
battle.terminal = true
|
|
battle.outcome = "defeat"
|
|
_log_event("outcome", "system", "", {"outcome": "defeat"})
|
|
elif int(battle.next_wave) >= int(battle.wave_count) and battle.enemy_order.is_empty():
|
|
battle.terminal = true
|
|
battle.outcome = "victory"
|
|
_log_event("outcome", "system", "", {"outcome": "victory"})
|
|
|
|
|
|
func _ally_actor(recruit_id: String, record: Dictionary, current: Dictionary) -> Dictionary:
|
|
var professions := Content.rows_by_id(catalog, "professions")
|
|
var traits := Content.rows_by_id(catalog, "traits")
|
|
var equipment := Content.rows_by_id(catalog, "equipment")
|
|
var profession: Dictionary = professions.get(String(record.profession_id), {})
|
|
var profession_tags: Array = Array(profession.get("semantic_tags", [])).duplicate()
|
|
var role := _profession_role(profession_tags, String(profession.get("role", "")))
|
|
var trait_tags: Array = Array(traits.get(String(record.trait_id), {}).get("semantic_tags", [])).duplicate()
|
|
var item_tags: Array = []
|
|
var instance_id := String(current.equipped_by_recruit.get(recruit_id, ""))
|
|
if not instance_id.is_empty() and current.inventory.has(instance_id):
|
|
var catalog_id := String(current.inventory[instance_id].catalog_id)
|
|
item_tags = Array(equipment.get(catalog_id, {}).get("semantic_tags", [])).duplicate()
|
|
var attributes: Dictionary = record.get("attribute_rolls", {})
|
|
var force := _grade_value(String(attributes.get("force", "C")))
|
|
var focus := _grade_value(String(attributes.get("focus", "C")))
|
|
var finesse := _grade_value(String(attributes.get("finesse", "C")))
|
|
var resolve := _grade_value(String(attributes.get("resolve", "C")))
|
|
var level := int(record.get("level", 1))
|
|
var base_power := 7 + 2 * force + level
|
|
var base_tempo := 35 + 3 * finesse
|
|
var base_guard := 2 * resolve
|
|
if role == "front_hold":
|
|
base_guard += 14
|
|
if "reach_bonus" in trait_tags:
|
|
base_power += 2
|
|
if "damage_bonus" in profession_tags:
|
|
base_power += 3
|
|
if "damage_bonus" in item_tags or "repeat_bonus" in item_tags:
|
|
base_power += 3
|
|
return {
|
|
"id": recruit_id,
|
|
"side": "ally",
|
|
"profession_id": String(record.profession_id),
|
|
"trait_id": String(record.trait_id),
|
|
"role": role,
|
|
"tags": profession_tags,
|
|
"trait_tags": trait_tags,
|
|
"item_tags": item_tags,
|
|
"hp": int(record.current_hp),
|
|
"max_hp": int(record.max_hp),
|
|
"power": base_power,
|
|
"heal": 8 + 2 * focus + level,
|
|
"focus": focus,
|
|
"guard": base_guard,
|
|
"tempo": base_tempo,
|
|
"readiness": 120 + 15 * finesse,
|
|
"action_sequence": 0,
|
|
"push_resists": (1 if role == "front_hold" else 0) + (1 if "push_resist" in item_tags else 0),
|
|
"projectile_resists": 1 if "projectile_resist" in item_tags else 0,
|
|
}
|
|
|
|
|
|
func _enemy_actor(enemy_id: String, blueprint_id: String, wave_index: int, risk: int, market_index: int) -> Dictionary:
|
|
var enemies := Content.rows_by_id(catalog, "enemies")
|
|
var row: Dictionary = enemies[blueprint_id]
|
|
var tags: Array = Array(row.semantic_tags).duplicate()
|
|
var elite := String(row.get("class", "")) == "elite"
|
|
var rank := mini(6, 1 + int(floor(float(market_index - 1) / 3.0)) + risk)
|
|
var hp_multiplier := 0.90 if String(battle.difficulty) == "wayfinder" else (1.10 if String(battle.difficulty) == "iron_oath" else (1.05 if String(battle.difficulty) == "stormbound" else 1.0))
|
|
var damage_multiplier := 0.90 if String(battle.difficulty) == "wayfinder" else (1.12 if String(battle.difficulty) == "iron_oath" else (1.06 if String(battle.difficulty) == "stormbound" else 1.0))
|
|
var max_hp := int(round((42 + 8 * wave_index + (26 if elite else 0)) * (1.0 + 0.08 * float(rank - 1)) * hp_multiplier))
|
|
return {
|
|
"id": enemy_id,
|
|
"side": "enemy",
|
|
"blueprint_id": blueprint_id,
|
|
"tags": tags,
|
|
"hp": max_hp,
|
|
"max_hp": max_hp,
|
|
"power": int(round((8 + 3 * risk + (4 if elite else 0)) * (1.0 + 0.08 * float(rank - 1)) * damage_multiplier)),
|
|
"guard": 6 if "screen_rotation" in tags or "projectile_screen" in tags else 0,
|
|
"tempo": 36 + 4 * risk + (4 if elite else 0),
|
|
"readiness": 80 + 10 * wave_index,
|
|
"action_sequence": 0,
|
|
"push_resists": 0,
|
|
"projectile_resists": 0,
|
|
}
|
|
|
|
|
|
func _ally_targets(actor_id: String, role: String) -> Array[String]:
|
|
if battle.enemy_order.is_empty():
|
|
return []
|
|
if role == "projectile":
|
|
for enemy_id in battle.enemy_order:
|
|
if "projectile_screen" in battle.actors[enemy_id].tags or "screen_rotation" in battle.actors[enemy_id].tags:
|
|
return [String(enemy_id)]
|
|
return [String(battle.enemy_order[-1])]
|
|
if role == "area":
|
|
var result: Array[String] = []
|
|
for index in range(mini(2, battle.enemy_order.size())):
|
|
result.append(String(battle.enemy_order[index]))
|
|
return result
|
|
return [String(battle.enemy_order[0])]
|
|
|
|
|
|
func _enemy_target(actor: Dictionary) -> String:
|
|
if battle.ally_order.is_empty():
|
|
return ""
|
|
if "rearmost_target" in actor.tags or "rear_projectile" in actor.tags:
|
|
return String(battle.ally_order[-1])
|
|
if "long_reach_target" in actor.tags:
|
|
for recruit_id in battle.ally_order:
|
|
if String(battle.actors[recruit_id].role) in ["reach_control", "projectile", "area", "impair"]:
|
|
return String(recruit_id)
|
|
return String(battle.ally_order[0])
|
|
|
|
|
|
func _front_interceptor() -> String:
|
|
if battle.ally_order.is_empty():
|
|
return ""
|
|
var front := String(battle.ally_order[0])
|
|
return front if String(battle.actors[front].role) == "front_hold" and int(battle.actors[front].guard) > 0 else ""
|
|
|
|
|
|
func _front_ally() -> String:
|
|
return "" if battle.ally_order.is_empty() else String(battle.ally_order[0])
|
|
|
|
|
|
func _lowest_hp_ally() -> String:
|
|
var result := ""
|
|
var best_ratio := 2.0
|
|
for recruit_id in battle.ally_order:
|
|
var ratio := float(battle.actors[recruit_id].hp) / float(battle.actors[recruit_id].max_hp)
|
|
if ratio < best_ratio:
|
|
best_ratio = ratio
|
|
result = String(recruit_id)
|
|
return result
|
|
|
|
|
|
func _slowest_ready_ally(exclude_id: String) -> String:
|
|
var result := ""
|
|
var readiness := 1001
|
|
for recruit_id in battle.ally_order:
|
|
if recruit_id != exclude_id and int(battle.actors[recruit_id].readiness) < readiness:
|
|
readiness = int(battle.actors[recruit_id].readiness)
|
|
result = String(recruit_id)
|
|
return result
|
|
|
|
|
|
func _profession_role(tags: Array, role_text: String) -> String:
|
|
for role in ROLE_TAGS:
|
|
if role in tags:
|
|
return role
|
|
var primary := role_text.get_slice(";", 0).replace("-pressure", "").replace("-control", "").replace("-hold", "_hold")
|
|
return primary.replace("-", "_")
|
|
|
|
|
|
func _spawn_ticks(wave_count: int) -> Array[int]:
|
|
var ticks: Array[int] = []
|
|
for index in range(wave_count):
|
|
ticks.append(index * 90)
|
|
return ticks
|
|
|
|
|
|
func _grade_value(grade: String) -> int:
|
|
return {"S": 6, "A": 5, "B": 4, "C": 3, "D": 2}.get(grade, 3)
|
|
|
|
|
|
func _log_event(kind: String, source_id: String, target_id: String, detail: Dictionary) -> void:
|
|
var event := {
|
|
"event_id": "EV-%04d" % (battle.event_log.size() + 1),
|
|
"tick": int(battle.tick),
|
|
"kind": kind,
|
|
"source_id": source_id,
|
|
"target_id": target_id,
|
|
"detail": detail.duplicate(true),
|
|
}
|
|
battle.event_log.append(event)
|
|
|
|
|
|
func _digest(seed: String, label: String, parts: Array = []) -> String:
|
|
var framed: Array[String] = ["%d:%s" % [seed.length(), seed], "%d:%s" % [label.length(), label]]
|
|
for part in parts:
|
|
var value := str(part)
|
|
framed.append("%d:%s" % [value.length(), value])
|
|
return "|".join(framed).sha256_text()
|
|
|
|
|
|
func _hash(value: Variant) -> String:
|
|
return JSON.stringify(_canonical(value)).sha256_text()
|
|
|
|
|
|
func _canonical(value: Variant) -> Variant:
|
|
if value is Dictionary:
|
|
var result := {}
|
|
var keys: Array = value.keys()
|
|
keys.sort_custom(func(left, right): return String(left) < String(right))
|
|
for key in keys:
|
|
result[key] = _canonical(value[key])
|
|
return result
|
|
if value is Array:
|
|
var result: Array = []
|
|
for item in value:
|
|
result.append(_canonical(item))
|
|
return result
|
|
return value
|