api_client.gd 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # API客户端 - 与FastAPI后端通信
  2. extends Node
  3. # 信号定义
  4. signal chat_response_received(npc_name: String, message: String)
  5. signal chat_error(error_message: String)
  6. signal npc_status_received(dialogues: Dictionary)
  7. signal npc_list_received(npcs: Array)
  8. # HTTP请求节点
  9. var http_chat: HTTPRequest
  10. var http_status: HTTPRequest
  11. var http_npcs: HTTPRequest
  12. func _ready():
  13. # 创建HTTP请求节点
  14. http_chat = HTTPRequest.new()
  15. http_status = HTTPRequest.new()
  16. http_npcs = HTTPRequest.new()
  17. add_child(http_chat)
  18. add_child(http_status)
  19. add_child(http_npcs)
  20. # 连接信号
  21. http_chat.request_completed.connect(_on_chat_request_completed)
  22. http_status.request_completed.connect(_on_status_request_completed)
  23. http_npcs.request_completed.connect(_on_npcs_request_completed)
  24. print("[INFO] API客户端初始化完成")
  25. # ==================== 对话API ====================
  26. func send_chat(npc_name: String, message: String) -> void:
  27. """发送对话请求"""
  28. var data = {
  29. "npc_name": npc_name,
  30. "message": message
  31. }
  32. var json_string = JSON.stringify(data)
  33. var headers = ["Content-Type: application/json"]
  34. print("[API] POST /chat -> ", data)
  35. var error = http_chat.request(
  36. Config.API_CHAT,
  37. headers,
  38. HTTPClient.METHOD_POST,
  39. json_string
  40. )
  41. if error != OK:
  42. print("[ERROR] 发送对话请求失败: ", error)
  43. chat_error.emit("网络请求失败")
  44. func _on_chat_request_completed(_result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
  45. """处理对话响应"""
  46. if response_code != 200:
  47. print("[ERROR] 对话请求失败: HTTP ", response_code)
  48. chat_error.emit("服务器错误: " + str(response_code))
  49. return
  50. var json = JSON.new()
  51. var parse_result = json.parse(body.get_string_from_utf8())
  52. if parse_result != OK:
  53. print("[ERROR] 解析响应失败")
  54. chat_error.emit("响应解析失败")
  55. return
  56. var response = json.data
  57. if response.has("success") and response["success"]:
  58. var npc_name = response["npc_name"]
  59. var msg = response["message"]
  60. print("[INFO] 收到NPC回复: ", npc_name, " -> ", msg)
  61. chat_response_received.emit(npc_name, msg)
  62. else:
  63. chat_error.emit("对话失败")
  64. # ==================== NPC状态API ====================
  65. func get_npc_status() -> void:
  66. """获取NPC状态"""
  67. # 检查是否正在处理请求
  68. if http_status.get_http_client_status() != HTTPClient.STATUS_DISCONNECTED:
  69. print("[WARN] NPC状态请求正在处理中,跳过本次请求")
  70. return
  71. print("[API] GET /npcs/status")
  72. var error = http_status.request(Config.API_NPC_STATUS)
  73. if error != OK:
  74. print("[ERROR] 获取NPC状态失败: ", error)
  75. func _on_status_request_completed(_result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
  76. """处理NPC状态响应"""
  77. if response_code != 200:
  78. print("[ERROR] NPC状态请求失败: HTTP ", response_code)
  79. return
  80. var json = JSON.new()
  81. var parse_result = json.parse(body.get_string_from_utf8())
  82. if parse_result != OK:
  83. print("[ERROR] 解析NPC状态失败")
  84. return
  85. var response = json.data
  86. if response.has("dialogues"):
  87. var dialogues = response["dialogues"]
  88. print("[INFO] 收到NPC状态更新: ", dialogues.size(), "个NPC")
  89. npc_status_received.emit(dialogues)
  90. # ==================== NPC列表API ====================
  91. func get_npc_list() -> void:
  92. """获取NPC列表"""
  93. print("[API] GET /npcs")
  94. var error = http_npcs.request(Config.API_NPCS)
  95. if error != OK:
  96. print("[ERROR] 获取NPC列表失败: ", error)
  97. func _on_npcs_request_completed(_result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
  98. """处理NPC列表响应"""
  99. if response_code != 200:
  100. print("[ERROR] NPC列表请求失败: HTTP ", response_code)
  101. return
  102. var json = JSON.new()
  103. var parse_result = json.parse(body.get_string_from_utf8())
  104. if parse_result != OK:
  105. print("[ERROR] 解析NPC列表失败")
  106. return
  107. var response = json.data
  108. if response.has("npcs"):
  109. var npcs = response["npcs"]
  110. print("[INFO] 收到NPC列表: ", npcs.size(), "个NPC")
  111. npc_list_received.emit(npcs)