Borealis-Legacy/Nodes/flyff_character_status_node.py

85 lines
3.5 KiB
Python

#!/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.
"""
from OdenGraphQt import BaseNode
from Qt import QtCore
import requests
import traceback
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"
}
for stat in self.values.keys():
self.add_output(stat)
self.set_name("Flyff - Character Status (API Disconnected)")
# Removed self-contained polling timer; global timer now drives updates.
def process_input(self):
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
if updated:
self.set_name("Flyff - Character Status (API Connected)")
self.transmit_data()
else:
print("[ERROR] Unexpected API response format (not a dict):", data)
self.set_name("Flyff - Character Status (API Disconnected)")
else:
print(f"[ERROR] API request failed with status code {response.status_code}")
self.set_name("Flyff - Character Status (API Disconnected)")
except Exception as e:
self.set_name("Flyff - Character Status (API Disconnected)")
print("[ERROR] Error polling API in CharacterStatusNode:", str(e))
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