Add PokeApiJson class
This commit is contained in:
parent
0e89c5fa85
commit
9c4d3fbc1c
|
|
@ -37,8 +37,8 @@ func _import_ability(entry : Dictionary) -> void:
|
||||||
return
|
return
|
||||||
|
|
||||||
var a := AbilityData.new()
|
var a := AbilityData.new()
|
||||||
a.id = j["id"]
|
a.id = PokeApiJson.get_int(j, "id")
|
||||||
a.api_name = j["name"]
|
a.api_name = PokeApiJson.get_str(j, "name", entry["name"])
|
||||||
a.names = PokeApiText.names(j["names"], _languages)
|
a.names = PokeApiText.names(PokeApiJson.get_array(j, "names"), _languages)
|
||||||
a.descriptions = PokeApiText.flavors(j["flavor_text_entries"], _languages)
|
a.descriptions = PokeApiText.flavors(PokeApiJson.get_array(j, "flavor_text_entries"), _languages)
|
||||||
ResourceSaver.save(a, path)
|
ResourceSaver.save(a, path)
|
||||||
|
|
|
||||||
|
|
@ -42,15 +42,15 @@ func _import_move(entry : Dictionary) -> void:
|
||||||
return
|
return
|
||||||
|
|
||||||
var m := MoveData.new()
|
var m := MoveData.new()
|
||||||
m.id = j["id"]
|
m.id = PokeApiJson.get_int(j, "id")
|
||||||
m.api_name = j["name"]
|
m.api_name = PokeApiJson.get_str(j, "name", entry["name"])
|
||||||
m.names = PokeApiText.names(j["names"], _languages)
|
m.names = PokeApiText.names(PokeApiJson.get_array(j, "names"), _languages)
|
||||||
m.descriptions = PokeApiText.flavors(j["flavor_text_entries"], _languages)
|
m.descriptions = PokeApiText.flavors(PokeApiJson.get_array(j, "flavor_text_entries"), _languages)
|
||||||
m.type = j["type"]["name"]
|
m.type = PokeApiJson.get_dict_name(j, "type")
|
||||||
m.category = j["damage_class"]["name"]
|
m.category = PokeApiJson.get_dict_name(j, "damage_class")
|
||||||
m.power = int(j["power"]) if j["power"] != null else 0
|
m.power = PokeApiJson.get_int(j, "power")
|
||||||
m.accuracy = int(j["accuracy"]) if j["accuracy"] != null else 0
|
m.accuracy = PokeApiJson.get_int(j, "accuracy")
|
||||||
m.pp = int(j["pp"]) if j["pp"] != null else 0
|
m.pp = PokeApiJson.get_int(j, "pp")
|
||||||
m.priority = j["priority"]
|
m.priority = PokeApiJson.get_int(j, "priority")
|
||||||
m.target = j["target"]["name"]
|
m.target = PokeApiJson.get_dict_name(j, "target")
|
||||||
ResourceSaver.save(m, path)
|
ResourceSaver.save(m, path)
|
||||||
|
|
|
||||||
35
editor/pokeapi_json.gd
Normal file
35
editor/pokeapi_json.gd
Normal file
|
|
@ -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)
|
||||||
1
editor/pokeapi_json.gd.uid
Normal file
1
editor/pokeapi_json.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://b3ynf743w7rss
|
||||||
|
|
@ -48,17 +48,17 @@ func _import_type(entry : Dictionary) -> void:
|
||||||
if t == null:
|
if t == null:
|
||||||
t = TypeData.new()
|
t = TypeData.new()
|
||||||
|
|
||||||
t.id = j["id"]
|
t.id = PokeApiJson.get_int(j, "id")
|
||||||
t.api_name = j["name"]
|
t.api_name = PokeApiJson.get_str(j, "name", entry["name"])
|
||||||
t.names = PokeApiText.names(j["names"], _languages)
|
t.names = PokeApiText.names(PokeApiJson.get_array(j, "names"), _languages)
|
||||||
|
|
||||||
var rel : Dictionary = j["damage_relations"]
|
var rel := PokeApiJson.get_dict(j, "damage_relations")
|
||||||
t.double_damage_to = _names_of(rel["double_damage_to"])
|
t.double_damage_to = _names_of(PokeApiJson.get_array(rel, "double_damage_to"))
|
||||||
t.double_damage_from = _names_of(rel["double_damage_from"])
|
t.double_damage_from = _names_of(PokeApiJson.get_array(rel, "double_damage_from"))
|
||||||
t.half_damage_to = _names_of(rel["half_damage_to"])
|
t.half_damage_to = _names_of(PokeApiJson.get_array(rel, "half_damage_to"))
|
||||||
t.half_damage_from = _names_of(rel["half_damage_from"])
|
t.half_damage_from = _names_of(PokeApiJson.get_array(rel, "half_damage_from"))
|
||||||
t.no_damage_to = _names_of(rel["no_damage_to"])
|
t.no_damage_to = _names_of(PokeApiJson.get_array(rel, "no_damage_to"))
|
||||||
t.no_damage_from = _names_of(rel["no_damage_from"])
|
t.no_damage_from = _names_of(PokeApiJson.get_array(rel, "no_damage_from"))
|
||||||
# t.icon n'est jamais touché ici
|
# t.icon n'est jamais touché ici
|
||||||
ResourceSaver.save(t, path)
|
ResourceSaver.save(t, path)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue