# Modules/data_manager.py import threading import time from flask import Flask, jsonify from PyQt5.QtCore import QMutex # Global datastore for character metrics data_store = { "hp_current": 0, "hp_total": 0, "mp_current": 0, "mp_total": 0, "fp_current": 0, "fp_total": 0, "exp": 0.0 } # Mutex for thread safety data_mutex = QMutex() # Flag to ensure only one character status collector node exists character_status_collector_exists = False # Flask Application app = Flask(__name__) @app.route('/data') def data_api(): """ Returns the current character metrics as JSON. """ return jsonify(get_data()) def start_api_server(): """ Starts the Flask API server in a separate daemon thread. """ def run(): app.run(host="127.0.0.1", port=5000) t = threading.Thread(target=run, daemon=True) t.start() def get_data(): """ Return a copy of the global data_store. """ data_mutex.lock() data_copy = data_store.copy() data_mutex.unlock() return data_copy def set_data(key, value): """ Set a single metric in the global data_store. """ data_mutex.lock() data_store[key] = value data_mutex.unlock() def set_data_bulk(metrics_dict): """ Update multiple metrics in the global data_store at once. """ data_mutex.lock() data_store.update(metrics_dict) data_mutex.unlock()