mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 08:23:45 +00:00
feat: persist matchmaking recovery state
This commit is contained in:
@@ -8,6 +8,7 @@ signal request_succeeded(operation: String, payload: Dictionary)
|
||||
signal request_failed(operation: String, http_code: int, detail: String)
|
||||
|
||||
const DEFAULT_BASE_URL := "http://127.0.0.1:8080"
|
||||
const PERSIST_PATH := "user://matchmaking_state.cfg"
|
||||
|
||||
var base_url := DEFAULT_BASE_URL
|
||||
var access_token := ""
|
||||
@@ -19,6 +20,8 @@ var _operation := ""
|
||||
|
||||
func _ready() -> void:
|
||||
state = MatchmakingState.new()
|
||||
_load_persisted_state()
|
||||
state.changed.connect(_persist_state)
|
||||
_request = HTTPRequest.new()
|
||||
_request.timeout = 10.0
|
||||
add_child(_request)
|
||||
@@ -145,3 +148,21 @@ func _on_request_completed(result: HTTPRequest.Result, response_code: int, _head
|
||||
|
||||
func _idempotency_key(prefix: String) -> String:
|
||||
return "%s-%s-%s" % [prefix, str(Time.get_ticks_usec()), str(randi())]
|
||||
|
||||
|
||||
func _persist_state(snapshot: Dictionary) -> void:
|
||||
var config := ConfigFile.new()
|
||||
config.set_value("matchmaking", "snapshot", JSON.stringify(snapshot))
|
||||
config.save(PERSIST_PATH)
|
||||
|
||||
|
||||
func _load_persisted_state() -> void:
|
||||
var config := ConfigFile.new()
|
||||
if config.load(PERSIST_PATH) != OK:
|
||||
return
|
||||
var raw = config.get_value("matchmaking", "snapshot", "")
|
||||
if not raw is String or String(raw).is_empty():
|
||||
return
|
||||
var parsed = JSON.parse_string(String(raw))
|
||||
if parsed is Dictionary and not state.restore_snapshot(parsed):
|
||||
state.fail("Saved matchmaking state is invalid")
|
||||
|
||||
Reference in New Issue
Block a user