From 9c4d3fbc1c625478f9bd0c355a68d2ad41780aef Mon Sep 17 00:00:00 2001 From: TheThomaas Date: Sun, 4 Oct 2026 13:44:13 +0200 Subject: [PATCH] Add PokeApiJson class --- editor/ability_importer.gd | 8 ++++---- editor/move_importer.gd | 22 +++++++++++----------- editor/pokeapi_json.gd | 35 +++++++++++++++++++++++++++++++++++ editor/pokeapi_json.gd.uid | 1 + editor/type_importer.gd | 20 ++++++++++---------- 5 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 editor/pokeapi_json.gd create mode 100644 editor/pokeapi_json.gd.uid diff --git a/editor/ability_importer.gd b/editor/ability_importer.gd index 04d27c4..2b88d02 100644 --- a/editor/ability_importer.gd +++ b/editor/ability_importer.gd @@ -37,8 +37,8 @@ func _import_ability(entry : Dictionary) -> void: return var a := AbilityData.new() - a.id = j["id"] - a.api_name = j["name"] - a.names = PokeApiText.names(j["names"], _languages) - a.descriptions = PokeApiText.flavors(j["flavor_text_entries"], _languages) + a.id = PokeApiJson.get_int(j, "id") + a.api_name = PokeApiJson.get_str(j, "name", entry["name"]) + a.names = PokeApiText.names(PokeApiJson.get_array(j, "names"), _languages) + a.descriptions = PokeApiText.flavors(PokeApiJson.get_array(j, "flavor_text_entries"), _languages) ResourceSaver.save(a, path) diff --git a/editor/move_importer.gd b/editor/move_importer.gd index 824d2cb..470f307 100644 --- a/editor/move_importer.gd +++ b/editor/move_importer.gd @@ -42,15 +42,15 @@ func _import_move(entry : Dictionary) -> void: return var m := MoveData.new() - m.id = j["id"] - m.api_name = j["name"] - m.names = PokeApiText.names(j["names"], _languages) - m.descriptions = PokeApiText.flavors(j["flavor_text_entries"], _languages) - m.type = j["type"]["name"] - m.category = j["damage_class"]["name"] - m.power = int(j["power"]) if j["power"] != null else 0 - m.accuracy = int(j["accuracy"]) if j["accuracy"] != null else 0 - m.pp = int(j["pp"]) if j["pp"] != null else 0 - m.priority = j["priority"] - m.target = j["target"]["name"] + m.id = PokeApiJson.get_int(j, "id") + m.api_name = PokeApiJson.get_str(j, "name", entry["name"]) + m.names = PokeApiText.names(PokeApiJson.get_array(j, "names"), _languages) + m.descriptions = PokeApiText.flavors(PokeApiJson.get_array(j, "flavor_text_entries"), _languages) + m.type = PokeApiJson.get_dict_name(j, "type") + m.category = PokeApiJson.get_dict_name(j, "damage_class") + m.power = PokeApiJson.get_int(j, "power") + m.accuracy = PokeApiJson.get_int(j, "accuracy") + m.pp = PokeApiJson.get_int(j, "pp") + m.priority = PokeApiJson.get_int(j, "priority") + m.target = PokeApiJson.get_dict_name(j, "target") ResourceSaver.save(m, path) diff --git a/editor/pokeapi_json.gd b/editor/pokeapi_json.gd new file mode 100644 index 0000000..24aa0fe --- /dev/null +++ b/editor/pokeapi_json.gd @@ -0,0 +1,35 @@ +# editor/pokeapi_json.gd +@tool +class_name PokeApiJson +extends RefCounted + +## Tous les accesseurs tolèrent un dictionnaire absent, une clé manquante, +## ou une valeur JSON null — ils renvoient le défaut plutôt que de planter. + +static func get_str(j : Dictionary, key : String, default : String = "") -> String: + var v : Variant = j.get(key) + return String(v) if v != null else default + +static func get_int(j : Dictionary, key : String, default : int = 0) -> int: + var v : Variant = j.get(key) + return int(v) if v != null else default + +static func get_bool(j : Dictionary, key : String, default : bool = false) -> bool: + var v : Variant = j.get(key) + return bool(v) if v != null else default + +static func get_dict(j : Dictionary, key : String) -> Dictionary: + var v : Variant = j.get(key) + return v if v is Dictionary else {} + +static func get_array(j : Dictionary, key : String) -> Array: + var v : Variant = j.get(key) + return v if v is Array else [] + +## Cas très fréquent dans PokeAPI : {"name": "...", "url": "..."} imbriqué, parfois null. +## get_dict_name({"type": {"name": "electric"}}, "type") -> "electric" +static func get_dict_name(j : Dictionary, key : String, default : String = "") -> String: + return get_str(get_dict(j, key), "name", default) + +static func get_dict_url(j : Dictionary, key : String, default : String = "") -> String: + return get_str(get_dict(j, key), "url", default) diff --git a/editor/pokeapi_json.gd.uid b/editor/pokeapi_json.gd.uid new file mode 100644 index 0000000..791f77d --- /dev/null +++ b/editor/pokeapi_json.gd.uid @@ -0,0 +1 @@ +uid://b3ynf743w7rss diff --git a/editor/type_importer.gd b/editor/type_importer.gd index f509509..d834068 100644 --- a/editor/type_importer.gd +++ b/editor/type_importer.gd @@ -48,17 +48,17 @@ func _import_type(entry : Dictionary) -> void: if t == null: t = TypeData.new() - t.id = j["id"] - t.api_name = j["name"] - t.names = PokeApiText.names(j["names"], _languages) + t.id = PokeApiJson.get_int(j, "id") + t.api_name = PokeApiJson.get_str(j, "name", entry["name"]) + t.names = PokeApiText.names(PokeApiJson.get_array(j, "names"), _languages) - var rel : Dictionary = j["damage_relations"] - t.double_damage_to = _names_of(rel["double_damage_to"]) - t.double_damage_from = _names_of(rel["double_damage_from"]) - t.half_damage_to = _names_of(rel["half_damage_to"]) - t.half_damage_from = _names_of(rel["half_damage_from"]) - t.no_damage_to = _names_of(rel["no_damage_to"]) - t.no_damage_from = _names_of(rel["no_damage_from"]) + var rel := PokeApiJson.get_dict(j, "damage_relations") + t.double_damage_to = _names_of(PokeApiJson.get_array(rel, "double_damage_to")) + t.double_damage_from = _names_of(PokeApiJson.get_array(rel, "double_damage_from")) + t.half_damage_to = _names_of(PokeApiJson.get_array(rel, "half_damage_to")) + t.half_damage_from = _names_of(PokeApiJson.get_array(rel, "half_damage_from")) + t.no_damage_to = _names_of(PokeApiJson.get_array(rel, "no_damage_to")) + t.no_damage_from = _names_of(PokeApiJson.get_array(rel, "no_damage_from")) # t.icon n'est jamais touché ici ResourceSaver.save(t, path)