From 16650b01070b2a9ce11e32326d10543b313075bd Mon Sep 17 00:00:00 2001 From: Hecht Date: Fri, 14 Jun 2024 23:29:19 +0200 Subject: [PATCH] Added more timelines renamed Global to API --- project.godot | 7 +- scenes/Global/API.gd | 141 ++++++++++++++++++++++++ scenes/Global/{Global.tscn => API.tscn} | 8 +- scenes/Global/APIRequest.gd | 4 +- scenes/Global/Global.gd | 60 ---------- scenes/Main/Wizard.gd | 65 ++++++----- scenes/util/Response.gd | 2 +- timelines/characters.dtl | 4 + timelines/main.dtl | 8 ++ timelines/new_player_wizard.dtl | 3 +- timelines/tournaments.dtl | 4 + 11 files changed, 207 insertions(+), 99 deletions(-) create mode 100644 scenes/Global/API.gd rename scenes/Global/{Global.tscn => API.tscn} (66%) delete mode 100644 scenes/Global/Global.gd create mode 100644 timelines/characters.dtl create mode 100644 timelines/main.dtl create mode 100644 timelines/tournaments.dtl diff --git a/project.godot b/project.godot index 1ff250e..ac5699c 100644 --- a/project.godot +++ b/project.godot @@ -17,7 +17,7 @@ config/icon="res://icon.svg" [autoload] -Global="*res://scenes/Global/Global.tscn" +API="*res://scenes/Global/API.tscn" Dialogic="*res://addons/dialogic/Core/DialogicGameHandler.gd" [dialogic] @@ -26,7 +26,10 @@ directories/dch_directory={ "Master": "res://timelines/Master.dch" } directories/dtl_directory={ -"new_player_wizard": "res://timelines/new_player_wizard.dtl" +"characters": "res://timelines/characters.dtl", +"main": "res://timelines/main.dtl", +"new_player_wizard": "res://timelines/new_player_wizard.dtl", +"tournaments": "res://timelines/tournaments.dtl" } layout/style_list=["res://assets/Dialogic/Styles/VisualNovel.tres"] layout/default_style="res://assets/Dialogic/Styles/VisualNovel.tres" diff --git a/scenes/Global/API.gd b/scenes/Global/API.gd new file mode 100644 index 0000000..588dfb7 --- /dev/null +++ b/scenes/Global/API.gd @@ -0,0 +1,141 @@ +extends Node2D + +@onready var _http_request = $HTTPRequest + +const Response = preload("res://scenes/util/Response.gd") + +var user_name : String = "FooBar" + +var _user_ulid : String = "" +var _dojo_ulid : String = "" + +var _dojos: Dictionary = {} +var _users: Dictionary = {} + +var _own_characters: Dictionary = {} +var _own_characters_array : Array = [] +var _all_characters: Dictionary = {} + +var _techniques: Dictionary = {} +var _techniques_array : Array = [] + + +# Called when the node enters the scene tree for the first time. +func _ready(): + _http_request.base_address = "http://localhost:8000/api" + _http_request.auth_token = "_sGFrChRV6ML0eWZC2qSUZzEGNLPbAhP4dwmnWhQ5bQ9xIi2pwHWQ1CzybFPDLeK8dTNx6GgWdR-Jcz-Z9_aC0hlY2h0fDIwMjQtMDYtMTNUMTk6MTU6MzkrMDA6MDA=" + + for argument in OS.get_cmdline_user_args(): + print_debug("argument: %s" % argument) + var key_value = argument.split("=", true, 1) + if "--token" == key_value[0]: + _http_request.auth_token = key_value[1] + elif "--env" == key_value[0]: + if key_value[1] == "dev": + _http_request.base_address = "http://localhost:8000/api" + else: + _http_request.base_address = "https://dojo.animegame.eu/api" + + var tmp = _http_request.auth_token.replace("-", "/").replace("_", "+") + + # replace \-_ with /\+ + var chunks = Marshalls.base64_to_raw(tmp) + user_name = chunks.slice(64).get_string_from_utf8().split('|')[0] + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(_delta): + pass + +func get_uri(type: String, entity: Variant) -> String: + return "api/%s/%s" % [type, entity if entity is String else entity["id"]] + +func create_dojo(dojo_name : String) -> Response: + var body = JSON.stringify({"name": dojo_name}) + return await _http_request.post_http_request("dojos", body) + +func create_character(name: String, strength: int, constitution: int, agility: int, chi: int, techniques: Array) -> bool: + var technique_uris : Array[String] = [] + + for technique in techniques: + technique_uris.append(get_uri("techniques", technique)) + + var body = JSON.stringify({ + "dojo": "/api/dojos/%s" % _dojo_ulid, # meh! + "name": name, + "strength": strength, + "constitution": constitution, + "agility": agility, + "chi": chi, + "techniques": technique_uris + }) + var response: Response = await _http_request.post_http_request("characters", body) + return response.status == OK + +func update_user_preferences(values: Dictionary) -> Response: + var body = JSON.stringify({"properties": values}) + var user = await get_user() + return await _http_request.patch_http_request("users/%s" % user["id"], body) + +func get_dojo() -> Dictionary: + if _dojo_ulid == "": + var result: Response = await _http_request.get_http_request_sync("dojo") + if result.status == OK: + _dojo_ulid = result.data["id"] + _dojos[_dojo_ulid] = result.data + else: + return {} + + return _dojos[_dojo_ulid] + +func get_dojo_by_id(id: String) -> Dictionary: + return await __get_entity_by_id(_dojos, "dojos", id) + +func get_user() -> Dictionary: + if _user_ulid == "": + var result = await _http_request.get_http_request_sync("users/authName/%s" % user_name) + if result.status == OK: + _user_ulid = result.data["id"] + _users[_user_ulid] = result.data + else: + return {} + + return _users[_user_ulid] + +func get_user_by_id(id: String) -> Dictionary: + return await __get_entity_by_id(_users, "users", id) + +func get_own_characters() -> Array: + return await __get_entity_list(_own_characters, _own_characters_array, "dojo/characters") + +func get_techniques() -> Array: + return await __get_entity_list(_techniques, _techniques_array, "techniques") + +func get_tournaments() -> Array: + return await _http_request.get_http_request_sync("tournaments") + +func get_character_by_id(id: String) -> Dictionary: + if _own_characters.has(id): + return _own_characters[id] + + return await __get_entity_by_id(_all_characters, "characters", id) + +func __get_entity_by_id(entities: Dictionary, type: String, id: String) -> Dictionary: + if not entities.has(id): + var result: Response = await _http_request.get_http_request_sync("%s/%s" % [type, id]) + if result.status == OK: + entities[id] = result.data + else: + return {} + return entities[id]; + +func __get_entity_list(entities: Dictionary, _entities_array: Array, type: String) -> Array: + if entities.is_empty(): + var result: Response = await _http_request.get_http_request_sync(type) + if result.status == OK: + for entity in result.data: + entities[entity["id"]] = entity + _entities_array.append(entity) + else: + return [] + + return _entities_array diff --git a/scenes/Global/Global.tscn b/scenes/Global/API.tscn similarity index 66% rename from scenes/Global/Global.tscn rename to scenes/Global/API.tscn index 69fc738..e861ab5 100644 --- a/scenes/Global/Global.tscn +++ b/scenes/Global/API.tscn @@ -1,12 +1,12 @@ [gd_scene load_steps=3 format=3 uid="uid://3qgip1nsocyh"] -[ext_resource type="Script" path="res://scenes/Global/Global.gd" id="1_yoteo"] -[ext_resource type="Script" path="res://scenes/Global/APIRequest.gd" id="2_jxuea"] +[ext_resource type="Script" path="res://scenes/Global/API.gd" id="1_hqdog"] +[ext_resource type="Script" path="res://scenes/Global/APIRequest.gd" id="2_yhpn3"] [node name="Global" type="Node2D"] -script = ExtResource("1_yoteo") +script = ExtResource("1_hqdog") [node name="HTTPRequest" type="HTTPRequest" parent="."] -script = ExtResource("2_jxuea") +script = ExtResource("2_yhpn3") [connection signal="request_completed" from="HTTPRequest" to="HTTPRequest" method="_on_request_completed"] diff --git a/scenes/Global/APIRequest.gd b/scenes/Global/APIRequest.gd index 946707e..8077c50 100644 --- a/scenes/Global/APIRequest.gd +++ b/scenes/Global/APIRequest.gd @@ -1,6 +1,6 @@ extends HTTPRequest -const Response = preload("../util/Response.gd") +const Response = preload("res://scenes/util/Response.gd") var _callback : Callable var auth_token: String @@ -74,5 +74,7 @@ func _on_request_completed(result, response_code, headers, body): var json = JSON.new() json.parse(body.get_string_from_utf8()) response.data = json.get_data() + else: + print_debug("%s: %s" % [result, response_code]) _callback.call(response) diff --git a/scenes/Global/Global.gd b/scenes/Global/Global.gd deleted file mode 100644 index dcbb7c6..0000000 --- a/scenes/Global/Global.gd +++ /dev/null @@ -1,60 +0,0 @@ -extends Node2D - -@onready var _http_request = $HTTPRequest - -const Response = preload("../util/Response.gd") - -var user_name : String = "FooBar" -var _dojo : Dictionary = {} -var _user: Dictionary = {} - - -# Called when the node enters the scene tree for the first time. -func _ready(): - _http_request.base_address = "http://localhost:8000/api" - _http_request.auth_token = "_sGFrChRV6ML0eWZC2qSUZzEGNLPbAhP4dwmnWhQ5bQ9xIi2pwHWQ1CzybFPDLeK8dTNx6GgWdR-Jcz-Z9_aC0hlY2h0fDIwMjQtMDYtMTNUMTk6MTU6MzkrMDA6MDA=" - - for argument in OS.get_cmdline_user_args(): - print_debug("argument: %s" % argument) - var key_value = argument.split("=", true, 1) - if "--token" == key_value[0]: - _http_request.auth_token = key_value[1] - elif "--env" == key_value[0]: - if key_value[1] == "dev": - _http_request.base_address = "http://localhost:8000/api" - else: - _http_request.base_address = "https://dojo.animegame.eu/api" - - var tmp = _http_request.auth_token.replace("-", "/").replace("_", "+") - - # replace \-_ with /\+ - var chunks = Marshalls.base64_to_raw(tmp) - user_name = chunks.slice(64).get_string_from_utf8().split('|')[0] - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(_delta): - pass - -func create_dojo(dojo_name : String) -> Response: - var body = JSON.stringify({"name": dojo_name}) - return await _http_request.post_http_request("dojos", body) - -func update_user_preferences(values: Dictionary) -> Response: - var body = JSON.stringify({"properties": values}) - var user = await get_user() - return await _http_request.patch_http_request("users/%s" % user["id"], body) - -func get_dojo() -> Dictionary: - if _dojo.is_empty(): - var result: Response = await _http_request.get_http_request_sync("dojo") - if result.status == OK: - _dojo = result.data - - return _dojo - -func get_user() -> Dictionary: - if _user.is_empty(): - var result = await _http_request.get_http_request_sync("users/authName/%s" % user_name) - if result.status == OK: - _user = result.data - return _user diff --git a/scenes/Main/Wizard.gd b/scenes/Main/Wizard.gd index 3e22070..9f40fda 100644 --- a/scenes/Main/Wizard.gd +++ b/scenes/Main/Wizard.gd @@ -1,27 +1,30 @@ extends Control -@onready var _global_scn = get_node("/root/Global") +const Response = preload("res://scenes/util/Response.gd") -const Response = preload("../util/Response.gd") +@onready var _api = get_node("/root/API") # Called when the node enters the scene tree for the first time. func _ready(): - if _global_scn.user_name: - Dialogic.VAR.user = _global_scn.user_name + Dialogic.signal_event.connect(_on_dialogic_signal) + if _api.user_name: + Dialogic.VAR.user = _api.user_name + + var timeline: Variant = "res://timelines/new_player_wizard.dtl" - var dojo : Dictionary = await _global_scn.get_dojo() + var dojo : Dictionary = await _api.get_dojo() if not dojo.is_empty(): Dialogic.VAR.dojo = dojo['name'] - var user : Dictionary = await _global_scn.get_user() + var user : Dictionary = await _api.get_user() if user.has("properties") and user["properties"] is Dictionary: var properties: Dictionary = user["properties"] Dialogic.VAR.bg.clan = properties.get("bg.clan", "") Dialogic.VAR.bg.community = properties.get("bg.community", "") Dialogic.VAR.bg.country = properties.get("bg.country", "") Dialogic.VAR.bg.username = properties.get("bg.username", "") + timeline = "res://timelines/main.dtl" - Dialogic.signal_event.connect(_on_dialogic_signal) - Dialogic.start("res://timelines/new_player_wizard.dtl") + Dialogic.start(timeline) # Called every frame. 'delta' is the elapsed time since the previous frame. @@ -29,24 +32,28 @@ func _process(_delta): pass func _on_dialogic_signal(argument:String): - Dialogic.paused = true - if argument == "create_dojo": - var result: Response = await _global_scn.create_dojo(Dialogic.VAR.newdojo) - if result.status == OK: - Dialogic.VAR.dojo = Dialogic.VAR.newdojo - else: - print_debug("%s failed: %s - %s" % [argument, result.code, result.data]) - elif argument == "update_preferences": - var result: Response = await _global_scn.update_user_preferences({ - "bg.clan": Dialogic.VAR.bg.clan, - "bg.community": Dialogic.VAR.bg.community, - "bg.country": Dialogic.VAR.bg.country, - "bg.username": Dialogic.VAR.bg.username - }) - if result.status == OK: - print_debug("Update preferences was successful!") - else: - print_debug("Update preferences failed: %s - %s" % [result.code, result.data]) - elif argument == "the_end": - return - Dialogic.paused = false + match argument: + "create_dojo": + create_dojo() + "update_preferences": + update_preferences() + +func create_dojo(): + var result: Response = await _api.create_dojo(Dialogic.VAR.newdojo) + if result.status == OK: + Dialogic.VAR.dojo = Dialogic.VAR.newdojo + else: + print_debug("create_dojo failed: %s - %s" % [result.code, result.data]) + +func update_preferences(): + var result: Response = await _api.update_user_preferences({ + "bg.clan": Dialogic.VAR.bg.clan, + "bg.community": Dialogic.VAR.bg.community, + "bg.country": Dialogic.VAR.bg.country, + "bg.username": Dialogic.VAR.bg.username + }) + if result.status == OK: + print_debug("update_preferences was successful!") + else: + print_debug("update_preferences failed: %s - %s" % [result.code, result.data]) + diff --git a/scenes/util/Response.gd b/scenes/util/Response.gd index ef9685b..a75d64d 100644 --- a/scenes/util/Response.gd +++ b/scenes/util/Response.gd @@ -1,4 +1,4 @@ var status : Error var code : HTTPClient.ResponseCode -var data : Dictionary +var data : Variant diff --git a/timelines/characters.dtl b/timelines/characters.dtl new file mode 100644 index 0000000..4662b9d --- /dev/null +++ b/timelines/characters.dtl @@ -0,0 +1,4 @@ +[background arg="res://assets/Background/Gymzaal.png" fade="0.0"] +join Master 0 +Master: Dieses Menü ist leider noch eine Baustelle +jump main/ diff --git a/timelines/main.dtl b/timelines/main.dtl new file mode 100644 index 0000000..8851eda --- /dev/null +++ b/timelines/main.dtl @@ -0,0 +1,8 @@ +[background arg="res://assets/Background/Gymzaal.png" fade="0.0"] +join Master 0 +Master: Was möchtest du machen? +- Charactere + jump characters/ +- Turniere + jump tournaments/ +[end_timeline] diff --git a/timelines/new_player_wizard.dtl b/timelines/new_player_wizard.dtl index a771a16..8c39556 100644 --- a/timelines/new_player_wizard.dtl +++ b/timelines/new_player_wizard.dtl @@ -47,6 +47,5 @@ if {bg.clan} == "": [text_input var="bg.clan"] Vielen Dank für die Informationen! [signal arg="update_preferences"] -Das wars leider schon wieder! \:( -[signal arg="the_end"] +jump main/ [end_timeline] diff --git a/timelines/tournaments.dtl b/timelines/tournaments.dtl new file mode 100644 index 0000000..4662b9d --- /dev/null +++ b/timelines/tournaments.dtl @@ -0,0 +1,4 @@ +[background arg="res://assets/Background/Gymzaal.png" fade="0.0"] +join Master 0 +Master: Dieses Menü ist leider noch eine Baustelle +jump main/