Require API question loading

This commit is contained in:
hz
2026-07-09 21:35:54 -05:00
parent 930c35f181
commit 474c704aa9
4 changed files with 69 additions and 199 deletions
+27 -27
View File
@@ -1,47 +1,33 @@
extends Node
const QUESTION_FILE := "res://data/questions/sample_questions.json"
const DEFAULT_QUESTIONS_API_URL := "https://s3q.ranaze.com/api/questions/search"
signal questions_changed
signal questions_load_failed(message: String)
var questions: Array = []
var request: HTTPRequest
var questions_api_url := DEFAULT_QUESTIONS_API_URL
var is_loading := false
var last_error := ""
func _ready() -> void:
questions_api_url = str(ProjectSettings.get_setting("step3/questions_api_url", DEFAULT_QUESTIONS_API_URL))
request = HTTPRequest.new()
request.request_completed.connect(_on_api_questions_loaded)
add_child(request)
load_questions()
func load_questions() -> void:
questions.clear()
var file := FileAccess.open(QUESTION_FILE, FileAccess.READ)
if file == null:
push_error("Unable to open question file: %s" % QUESTION_FILE)
return
var parsed = JSON.parse_string(file.get_as_text())
if typeof(parsed) != TYPE_ARRAY:
push_error("Question file must contain an array.")
return
for item in parsed:
if _is_valid_question(item):
questions.append(item)
else:
push_warning("Skipping invalid question: %s" % str(item.get("id", "unknown")))
questions_changed.emit()
func load_questions_from_api() -> void:
if AuthState.access_token.is_empty():
_set_load_error("Login required before loading questions.")
return
is_loading = true
last_error = ""
questions.clear()
questions_changed.emit()
var headers := PackedStringArray([
"Content-Type: application/json",
"Authorization: Bearer %s" % AuthState.access_token
@@ -51,17 +37,18 @@ func load_questions_from_api() -> void:
}
var err := request.request(questions_api_url, headers, HTTPClient.METHOD_POST, JSON.stringify(payload))
if err != OK:
push_warning("Failed to request questions API. Using local question bank.")
_set_load_error("Failed to request questions API.")
func _on_api_questions_loaded(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
is_loading = false
if result != HTTPRequest.RESULT_SUCCESS or response_code < 200 or response_code >= 300:
push_warning("Questions API request failed. Using local question bank.")
_set_load_error("Questions API request failed with status %d." % response_code)
return
var parsed: Variant = JSON.parse_string(body.get_string_from_utf8())
if typeof(parsed) != TYPE_ARRAY:
push_warning("Questions API response was not an array. Using local question bank.")
_set_load_error("Questions API response was not an array.")
return
var api_questions: Array = []
@@ -71,10 +58,11 @@ func _on_api_questions_loaded(result: int, response_code: int, _headers: PackedS
api_questions.append(normalized)
if api_questions.is_empty():
push_warning("Questions API returned no usable questions. Using local question bank.")
_set_load_error("Questions API returned no usable questions.")
return
questions = api_questions
last_error = ""
questions_changed.emit()
@@ -109,6 +97,18 @@ func build_block(system_name: String, block_size: int) -> Array:
return pool.slice(0, min(block_size, pool.size()))
func has_questions() -> bool:
return not questions.is_empty()
func _set_load_error(message: String) -> void:
is_loading = false
last_error = message
push_warning(message)
questions_load_failed.emit(message)
questions_changed.emit()
func _is_valid_question(item: Variant) -> bool:
if typeof(item) != TYPE_DICTIONARY:
return false