#!/usr/bin/env python3 """ Standardized Flyff Character Status Node: - Polls an API for character stats and updates values dynamically. - Uses a global update timer for processing. - Immediately transmits updated values to connected nodes. - If the API is unreachable, it will wait for 5 seconds before retrying and log the error only once per retry period. - Calls self.view.draw_node() after updating the node name, ensuring the node's bounding box recalculates even when the API is disconnected. - Port colors adjusted to match earlier styling. """ from OdenGraphQt import BaseNode from Qt import QtCore import requests import traceback import time class FlyffCharacterStatusNode(BaseNode): __identifier__ = 'bunny-lab.io.flyff_character_status_node' NODE_NAME = 'Flyff - Character Status' def __init__(self): super(FlyffCharacterStatusNode, self).__init__() self.values = { "HP: Current": "N/A", "HP: Total": "N/A", "MP: Current": "N/A", "MP: Total": "N/A", "FP: Current": "N/A", "FP: Total": "N/A", "EXP": "N/A" } # Set each output with a custom color: # (Choose values close to the screenshot for each port.) self.add_output('HP: Current', color=(126, 36, 57)) self.add_output('HP: Total', color=(126, 36, 57)) self.add_output('MP: Current', color=(35, 89, 144)) self.add_output('MP: Total', color=(35, 89, 144)) self.add_output('FP: Current', color=(36, 116, 32)) self.add_output('FP: Total', color=(36, 116, 32)) self.add_output('EXP', color=(48, 116, 143)) self.set_name("Flyff - Character Status (API Disconnected)") self.view.draw_node() # ensure bounding box updates initially # Variables to handle API downtime gracefully: self._api_down = False self._last_api_attempt = 0 self._retry_interval = 5 # seconds to wait before retrying after a failure self._last_error_printed = 0 def process_input(self): current_time = time.time() # If the API is down, only retry after _retry_interval seconds if self._api_down and (current_time - self._last_api_attempt < self._retry_interval): return self._last_api_attempt = current_time try: response = requests.get("http://127.0.0.1:5000/data", timeout=1) if response.status_code == 200: data = response.json() if isinstance(data, dict): mapping = { "hp_current": "HP: Current", "hp_total": "HP: Total", "mp_current": "MP: Current", "mp_total": "MP: Total", "fp_current": "FP: Current", "fp_total": "FP: Total", "exp": "EXP" } updated = False for key, value in data.items(): if key in mapping: formatted_key = mapping[key] if str(value) != self.values.get(formatted_key, None): self.values[formatted_key] = str(value) updated = True self._api_down = False if updated: self.set_name("Flyff - Character Status (API Connected)") self.view.draw_node() # recalc bounding box on connect self.transmit_data() else: if current_time - self._last_error_printed >= self._retry_interval: print("[ERROR] Unexpected API response format (not a dict):", data) self._last_error_printed = current_time self.set_name("Flyff - Character Status (API Disconnected)") self.view.draw_node() # recalc bounding box on disconnect self._api_down = True else: if current_time - self._last_error_printed >= self._retry_interval: print(f"[ERROR] API request failed with status code {response.status_code}") self._last_error_printed = current_time self.set_name("Flyff - Character Status (API Disconnected)") self.view.draw_node() self._api_down = True except Exception as e: if current_time - self._last_error_printed >= self._retry_interval: print("[ERROR] Error polling API in CharacterStatusNode:", str(e)) self._last_error_printed = current_time self.set_name("Flyff - Character Status (API Disconnected)") self.view.draw_node() self._api_down = True def transmit_data(self): for stat, value in self.values.items(): port = self.get_output(stat) if port and port.connected_ports(): for connected_port in port.connected_ports(): connected_node = connected_port.node() if hasattr(connected_node, 'receive_data'): try: connected_node.receive_data(value, stat) except Exception as e: print(f"[ERROR] Error transmitting data to {connected_node}: {e}") print("[ERROR] Stack Trace:\n", traceback.format_exc()) def receive_data(self, data, source_port_name=None): # This node only transmits data; it does not receive external data. pass