Skip to content

Commit 642ed66

Browse files
authored
Merge pull request #157 from TaloDev/develop
Release 0.36.0
2 parents 20f6fa0 + 9100c7e commit 642ed66

File tree

9 files changed

+99
-25
lines changed

9 files changed

+99
-25
lines changed

addons/talo/apis/health_check_api.gd

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,37 @@ class_name HealthCheckAPI extends TaloAPI
55
##
66
## @tutorial: https://docs.trytalo.com/docs/godot/continuity
77

8+
enum HealthCheckStatus {
9+
OK,
10+
FAILED,
11+
UNKNOWN
12+
}
13+
14+
## Emitted when internet connectivity is lost.
15+
signal connection_lost()
16+
17+
## Emitted when internet connectivity is restored.
18+
signal connection_restored()
19+
20+
var _last_health_check_status := HealthCheckStatus.UNKNOWN
21+
822
## Ping the Talo Health Check API to check if Talo can be reached.
923
func ping() -> bool:
1024
var res := await client.make_request(HTTPClient.METHOD_GET, "")
11-
return res.status == 204
25+
var success := true if res.status == 204 else false
26+
var failed_last_health_check := true if _last_health_check_status == HealthCheckStatus.FAILED else false
27+
28+
if success:
29+
_last_health_check_status = HealthCheckStatus.OK
30+
if failed_last_health_check:
31+
connection_restored.emit()
32+
else:
33+
_last_health_check_status = HealthCheckStatus.FAILED
34+
if not failed_last_health_check:
35+
connection_lost.emit()
36+
37+
return success
38+
39+
## Get the latest known health check status.
40+
func get_last_status() -> HealthCheckStatus:
41+
return _last_health_check_status

addons/talo/apis/players_api.gd

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ signal identification_failed()
1717
## Emitted after calling clear_identity().
1818
signal identity_cleared()
1919

20+
func _ready() -> void:
21+
await Talo.init_completed
22+
Talo.health_check.connection_restored.connect(_on_connection_restored)
23+
2024
func _handle_identify_success(alias: TaloPlayerAlias, socket_token: String = "") -> TaloPlayer:
2125
if not await Talo.is_offline():
2226
Talo.socket.reset_connection()
@@ -137,6 +141,24 @@ func clear_identity() -> void:
137141

138142
identity_cleared.emit()
139143

144+
## Create a new socket token. The Talo socket will use this token to identify the player.
145+
func create_socket_token() -> String:
146+
if Talo.identity_check() != OK:
147+
return ""
148+
149+
var res := await client.make_request(HTTPClient.METHOD_POST, "/socket-token")
150+
match res.status:
151+
200:
152+
return res.body.socketToken
153+
_:
154+
return ""
155+
156+
func _on_connection_restored():
157+
await Talo.socket.reset_connection()
158+
if Talo.identity_check() == OK:
159+
var socket_token := await create_socket_token()
160+
Talo.socket.set_socket_token(socket_token)
161+
140162
class SearchPage:
141163
var players: Array[TaloPlayer]
142164
var count: int

addons/talo/apis/saves_api.gd

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func get_saves() -> Array[TaloGameSave]:
5757
match res.status:
5858
200:
5959
online_saves.append_array(res.body.saves.map(func (data: Dictionary): return TaloGameSave.new(data)))
60-
saves.append_array(await _saves_manager.get_synced_saves(online_saves))
60+
var synced_saves := await _saves_manager.get_synced_saves(online_saves)
61+
saves.append_array(synced_saves)
6162

6263
_saves_manager.all_saves = saves
6364
saves_loaded.emit()
@@ -146,10 +147,8 @@ func delete_save(save: TaloGameSave) -> void:
146147
return
147148

148149
var res := await client.make_request(HTTPClient.METHOD_DELETE, "/%s" % save.id)
149-
150-
match res.status:
151-
_:
152-
return
150+
if res.status != 204:
151+
return
153152

154153
_saves_manager.all_saves = _saves_manager.all_saves.filter(func (s: TaloGameSave): s.id != save.id)
155154
_saves_manager.delete_offline_save(save)

addons/talo/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="Talo Game Services"
44
description="Talo (https://trytalo.com) is an open-source game backend. Talo's Godot plugin is the easiest way to add leaderboards, player authentication, socket-based multiplayer and more to your game."
55
author="trytalo"
6-
version="0.35.0"
6+
version="0.36.0"
77
script="talo_autoload.gd"

addons/talo/talo_client.gd

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class_name TaloClient extends Node
22

33
# automatically updated with a pre-commit hook
4-
const TALO_CLIENT_VERSION = "0.35.0"
4+
const TALO_CLIENT_VERSION = "0.36.0"
55

66
var _base_url: String
77

@@ -65,10 +65,10 @@ func make_request(method: HTTPClient.Method, url: String, body: Dictionary = {},
6565
])
6666

6767
if Talo.settings.log_responses:
68-
print_rich("[color=green]--> %s %s %s %s[/color]" % [
68+
print_rich("[color=green]--> %s %s [%s] %s[/color]" % [
6969
_get_method_name(method),
7070
full_url,
71-
"[%s]" % status,
71+
status,
7272
json.data
7373
])
7474

@@ -78,8 +78,9 @@ func make_request(method: HTTPClient.Method, url: String, body: Dictionary = {},
7878
}
7979

8080
if ret.status >= 400:
81-
handle_error(ret)
81+
handle_error(method, url, ret)
8282

83+
await Talo.continuity_manager.handle_post_response_healthcheck(full_url, res)
8384
if Talo.continuity_manager.request_can_be_replayed(method, full_url, res):
8485
Talo.continuity_manager.push_request(method, full_url, body, all_headers, continuity_timestamp)
8586

@@ -118,17 +119,17 @@ func _build_full_url(url: String) -> String:
118119
url.replace(" ", "%20")
119120
]
120121

121-
func handle_error(res: Dictionary) -> void:
122+
func handle_error(method: HTTPClient.Method, url: String, res: Dictionary) -> void:
122123
if res.body != null:
123124
if res.body.has("message"):
124-
push_error("%s: %s" % [res.status, res.body.message])
125+
push_error("%s %s [%s]: %s" % [_get_method_name(method), url, res.status, res.body.message])
125126
return
126127

127128
if res.body.has("errors"):
128-
push_error("%s: %s" % [res.status, res.body.errors])
129+
push_error("%s %s [%s]: %s" % [_get_method_name(method), url, res.status, res.body.errors])
129130
return
130131

131-
push_error("%s: Unknown error" % res.status)
132+
push_error("%s %s [%s]: Unknown error" % [_get_method_name(method), url, res.status])
132133

133134
class TaloClientResponse:
134135
var result: int

addons/talo/talo_settings.gd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var _config_file: ConfigFile
88
const SETTINGS_PATH := "res://addons/talo/settings.cfg"
99
const DEFAULT_API_URL := "https://api.trytalo.com"
1010

11+
const DEV_FEATURE_TAG := "talo_dev"
12+
const LIVE_FEATURE_TAG := "talo_live"
13+
1114
## Your Talo access key, allowing you to connect to the Talo API and access data based on its scopes
1215
var access_key: String:
1316
get:
@@ -109,7 +112,11 @@ func _init() -> void:
109112
print_rich("[color=yellow]Warning: Talo access_key in settings.cfg is empty[/color]")
110113

111114
func is_debug_build() -> bool:
112-
return OS.is_debug_build()
115+
if OS.has_feature(LIVE_FEATURE_TAG):
116+
return false
117+
if OS.has_feature(DEV_FEATURE_TAG):
118+
return true
119+
return OS.is_debug_build()
113120

114121
## Save the Talo settings to the config file
115122
func save_config():

addons/talo/talo_socket.gd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ func close_connection(code: int = 1000, reason: String = "") -> void:
103103

104104
## Close the current connection and create a new connection to the Talo Socket server.
105105
func reset_connection() -> void:
106-
if not _identified:
107-
return
108-
109106
close_connection()
110107
_reset_socket()
111108
open_connection()

addons/talo/utils/continuity_manager.gd

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ var _requests: Array = []
66
const _CONTINUITY_PATH = "user://tc.bin"
77
const _CONTINUITY_TIMESTAMP_HEADER = "X-Talo-Continuity-Timestamp"
88

9+
const _HEALTH_CHECK_ENDPOINT := "/v1/health-check"
910
const _EXCLUDED_ENDPOINTS: Array[String] = [
10-
"/v1/health-check",
11+
_HEALTH_CHECK_ENDPOINT,
1112
"/v1/players/auth",
1213
"/v1/players/identify",
1314
"/v1/socket-tickets"
@@ -96,3 +97,18 @@ func request_can_be_replayed(method: HTTPClient.Method, url: String, res: TaloCl
9697
func clear_requests() -> void:
9798
_requests.clear()
9899
_write_requests()
100+
101+
func handle_post_response_healthcheck(url: String, res: TaloClient.TaloClientResponse):
102+
if url.find(_HEALTH_CHECK_ENDPOINT) != -1:
103+
return
104+
105+
var success := true if res.result == HTTPRequest.RESULT_SUCCESS else false
106+
107+
if success:
108+
# if offline mode is enabled, check if it should be disabled
109+
if Talo.health_check.get_last_status() == Talo.health_check.HealthCheckStatus.FAILED:
110+
await Talo.health_check.ping()
111+
else:
112+
# if offline mode isn't enabled, check if it should be enabled
113+
if Talo.health_check.get_last_status() != Talo.health_check.HealthCheckStatus.FAILED:
114+
await Talo.health_check.ping()

addons/talo/utils/saves_manager.gd

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ func get_synced_saves(online_saves: Array[TaloGameSave]) -> Array[TaloGameSave]:
6464
var saves: Array[TaloGameSave] = []
6565
var offline_saves: Array[TaloGameSave] = read_offline_saves()
6666

67-
if not offline_saves.is_empty():
67+
if offline_saves.is_empty():
68+
saves.append_array(online_saves)
69+
else:
6870
for online_save in online_saves:
6971
var filtered := offline_saves.filter(func (save: TaloGameSave): return save.id == online_save.id)
7072
if not filtered.is_empty():
71-
var save := await sync_save(online_save, filtered.front())
72-
saves.push_back(save)
73+
var synced_save := await sync_save(online_save, filtered.front())
74+
saves.push_back(synced_save)
7375
else:
7476
saves.push_back(online_save)
7577

76-
var synced_saves := await sync_offline_saves(offline_saves)
77-
saves.append_array(synced_saves)
78+
var synced_offline_saves := await sync_offline_saves(offline_saves)
79+
saves.append_array(synced_offline_saves)
7880

7981
return saves
8082

0 commit comments

Comments
 (0)