#!/usr/bin/env python3 """ Flask server voor IJsbreker spel Serveert statische bestanden en biedt API endpoints voor config management """ from flask import Flask, send_from_directory, request, jsonify from flask_cors import CORS import json import os app = Flask(__name__) CORS(app) # Enable CORS voor alle routes CONFIG_FILE = 'config.json' SETS_FILE = 'sets.json' # Serve static files @app.route('/') def serve_index(): return send_from_directory('.', 'index.html') @app.route('/') def serve_static(path): if os.path.exists(path): return send_from_directory('.', path) return "File not found", 404 # API endpoint om config te laden @app.route('/api/config', methods=['GET']) def get_config(): try: with open(CONFIG_FILE, 'r', encoding='utf-8') as f: config = json.load(f) return jsonify(config) except Exception as e: return jsonify({'error': str(e)}), 500 # API endpoint om config op te slaan @app.route('/api/save-config', methods=['POST']) def save_config(): try: data = request.json # Validatie: check of stellingen array bestaat if 'stellingen' not in data: return jsonify({'error': 'Geen stellingen gevonden'}), 400 # Filter lege stellingen eruit data['stellingen'] = [ s for s in data['stellingen'] if s.get('links', '').strip() or s.get('rechts', '').strip() ] # Schrijf naar config.json met mooie formatting with open(CONFIG_FILE, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2, ensure_ascii=False) return jsonify({'success': True, 'message': 'Config opgeslagen!'}) except Exception as e: return jsonify({'error': str(e)}), 500 # API endpoint om alle sets te laden @app.route('/api/sets', methods=['GET']) def get_sets(): try: # Als sets.json niet bestaat, maak initieel bestand aan if not os.path.exists(SETS_FILE): initialize_sets_file() with open(SETS_FILE, 'r', encoding='utf-8') as f: sets_data = json.load(f) return jsonify(sets_data) except Exception as e: return jsonify({'error': str(e)}), 500 # API endpoint om sets op te slaan @app.route('/api/sets', methods=['POST']) def save_sets(): try: data = request.json # Validatie: check of sets array bestaat if 'sets' not in data: return jsonify({'error': 'Geen sets gevonden'}), 400 # Schrijf naar sets.json met mooie formatting with open(SETS_FILE, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2, ensure_ascii=False) # Update config.json met actieve set als die gezet is if 'activeSetId' in data and data['activeSetId']: active_set = next((s for s in data['sets'] if s['id'] == data['activeSetId']), None) if active_set and 'config' in active_set: update_active_config(active_set['config']) return jsonify({'success': True, 'message': 'Sets opgeslagen!'}) except Exception as e: return jsonify({'error': str(e)}), 500 # Helper: Update config.json met set configuratie def update_active_config(set_config): with open(CONFIG_FILE, 'w', encoding='utf-8') as f: json.dump(set_config, f, indent=2, ensure_ascii=False) # Helper: Initialiseer sets.json met huidige config als default set def initialize_sets_file(): try: # Lees huidige config.json with open(CONFIG_FILE, 'r', encoding='utf-8') as f: current_config = json.load(f) # Maak initiële sets structuur initial_sets = { 'sets': [ { 'id': 'default', 'name': 'Standaard Set', 'config': current_config } ], 'activeSetId': 'default' } # Schrijf naar sets.json with open(SETS_FILE, 'w', encoding='utf-8') as f: json.dump(initial_sets, f, indent=2, ensure_ascii=False) print("✅ sets.json aangemaakt met default set") except Exception as e: print(f"⚠️ Fout bij initialiseren sets.json: {e}") if __name__ == '__main__': print("🎯 IJsbreker Server gestart!") print("📝 Editor: http://localhost:8000/editor.html") print("🎮 Spel: http://localhost:8000/") print("\nDruk Ctrl+C om te stoppen") app.run(debug=True, host='0.0.0.0', port=8000)