'use strict'; /** * ╔══════════════════════════════════════════════════════════════════════════╗ * Features Engine — v19.0 | محرك 100 ميزة + Presets ║ * ╠══════════════════════════════════════════════════════════════════════════╣ * ✅ 100 ميزة في 8 فئات: حماية، أداء، اقتصاد، لاعبين، عوالم، ترفيه، إدارة، بوتات ║ * ✅ 5 Presets: Survival Pro, Creative Build, Minigames Hub, Hardcore, Custom ║ * ✅ REST API: GET/POST /api/features + Preset Apply ║ * ✅ Auto-Apply: يطبق الميزات المفعلة عند التشغيل ║ * ✅ Plugin Detection: كشف البلاجن المثبتة وتطابقها مع الميزات ║ * ✅ Dependencies: تفعيل تلقائي للعلاقات ║ * ✅ Export/Import: حفظ وتحميل إعدادات الميزات ║ * ╚══════════════════════════════════════════════════════════════════════════╝ */ const fs = require('fs'); const path = require('path'); const { EventEmitter } = require('events'); const tsNow = () => new Date().toISOString().replace('T', ' ').slice(0, 19); class FeaturesEngine extends EventEmitter { constructor(config = {}) { super(); this.configPath = config.configPath || path.join(__dirname, 'features-config.json'); this.dataPath = config.dataPath || path.join(__dirname, 'features-data.json'); this.features = {}; this.categories = {}; this.presets = {}; this.activePreset = 'custom'; this.modifiedFeatures = new Set(); this._log('ENGINE', 'تم إنشاء Features Engine'); } _log(tag, msg) { const ts = tsNow(); console.log(`[${ts}] [FEATURES] ${tag} ${msg}`); } init() { this._loadConfig(); this._loadData(); this._log('✅', `تم تحميل ${Object.keys(this.features).length} ميزة في ${Object.keys(this.categories).length} فئة`); this._log('📦', `${Object.keys(this.presets).length} Presets متاحة`); } _loadConfig() { try { const raw = fs.readFileSync(this.configPath, 'utf8'); const data = JSON.parse(raw); this.features = data.features || {}; this.categories = data.categories || {}; this.presets = data.presets || {}; this._log('📂', 'تم تحميل features-config.json'); } catch (e) { this._log('⚠️', `خطأ في تحميل features-config.json: ${e.message}`); this.features = {}; this.categories = {}; this.presets = {}; } } _loadData() { try { if (fs.existsSync(this.dataPath)) { const raw = fs.readFileSync(this.dataPath, 'utf8'); const data = JSON.parse(raw); this.activePreset = data.activePreset || 'custom'; if (data.features) { for (const [id, overrides] of Object.entries(data.features)) { if (this.features[id]) { this.features[id] = { ...this.features[id], ...overrides }; } } } this._log('📂', `تم تحميل البيانات المحفوظة (Preset: ${this.activePreset})`); } } catch (e) { this._log('⚠️', `خطأ في تحميل البيانات: ${e.message}`); } } saveData() { try { const features = {}; for (const [id, f] of Object.entries(this.features)) { features[id] = { enabled: f.enabled, config: f.config }; } const data = { version: '19.0', activePreset: this.activePreset, features, savedAt: tsNow() }; fs.writeFileSync(this.dataPath, JSON.stringify(data, null, 2)); this._log('💾', 'تم حفظ البيانات'); } catch (e) { this._log('❌', `خطأ في الحفظ: ${e.message}`); } } applyPreset(presetId) { const preset = this.presets[presetId]; if (!preset) { this._log('❌', `Preset غير موجود: ${presetId}`); return false; } this._log('📦', `تطبيق Preset: ${preset.name}`); for (const f of Object.values(this.features)) { f.enabled = preset.enabledFeatures.includes(f.id); } for (const depId of preset.enabledFeatures) { this._enableDependencies(depId); } this.activePreset = presetId; this.saveData(); this.emit('presetApplied', presetId, preset); this._log('✅', `تم تطبيق ${preset.name} — ${preset.enabledFeatures.length} ميزة مفعلة`); return true; } _enableDependencies(featureId) { const f = this.features[featureId]; if (!f || !f.dependencies) return; for (const depId of f.dependencies) { if (this.features[depId] && !this.features[depId].enabled) { this.features[depId].enabled = true; this._log('🔗', `تفعيل تلقائي: ${depId} (dependency لـ ${featureId})`); this._enableDependencies(depId); } } } toggleFeature(featureId, enabled) { if (!this.features[featureId]) { this._log('❌', `ميزة غير موجودة: ${featureId}`); return false; } this.features[featureId].enabled = enabled; this.modifiedFeatures.add(featureId); if (enabled) { this._enableDependencies(featureId); } this.activePreset = 'custom'; this.saveData(); this.emit('featureToggled', featureId, enabled); this._log('🔄', `${enabled ? 'تفعيل' : 'تعطيل'}: ${featureId}`); return true; } updateFeatureConfig(featureId, config) { if (!this.features[featureId]) return false; this.features[featureId].config = { ...this.features[featureId].config, ...config }; this.modifiedFeatures.add(featureId); this.saveData(); this.emit('featureConfigUpdated', featureId, config); return true; } getFeature(id) { return this.features[id] || null; } getFeaturesByCategory(categoryId) { return Object.values(this.features).filter(f => f.category === categoryId); } getAllFeatures() { return Object.values(this.features); } getEnabledFeatures() { return Object.values(this.features).filter(f => f.enabled); } getDisabledFeatures() { return Object.values(this.features).filter(f => !f.enabled); } getCategories() { return Object.entries(this.categories) .sort(([, a], [, b]) => a.order - b.order) .map(([id, cat]) => ({ id, ...cat })); } getPresets() { return Object.entries(this.presets).map(([id, p]) => ({ id, ...p })); } getPreset(id) { return this.presets[id] || null; } getActivePreset() { return { id: this.activePreset, ...this.presets[this.activePreset] }; } getStats() { const all = Object.values(this.features); const enabled = all.filter(f => f.enabled).length; const disabled = all.length - enabled; const byCategory = {}; for (const f of all) { if (!byCategory[f.category]) byCategory[f.category] = { total: 0, enabled: 0 }; byCategory[f.category].total++; if (f.enabled) byCategory[f.category].enabled++; } return { total: all.length, enabled, disabled, byCategory, activePreset: this.activePreset }; } matchPlugins(detectedPlugins) { const matches = []; const unmatched = []; for (const plugin of detectedPlugins) { const nameLower = (plugin.name || '').toLowerCase().replace(/[\s\-_]/g, ''); let found = false; for (const f of Object.values(this.features)) { const featureName = (f.name || '').toLowerCase().replace(/[\s\-_]/g, ''); if (nameLower.includes(featureName) || featureName.includes(nameLower)) { matches.push({ plugin: plugin.name, feature: f.id, enabled: f.enabled }); found = true; break; } } if (!found) { unmatched.push(plugin.name); } } return { matches, unmatched, totalPlugins: detectedPlugins.length, totalFeatures: Object.keys(this.features).length }; } exportConfig() { return { version: '19.0', activePreset: this.activePreset, exportedAt: tsNow(), features: Object.fromEntries( Object.entries(this.features).map(([id, f]) => [id, { enabled: f.enabled, config: f.config }]) ) }; } importConfig(data) { if (!data || !data.features) return false; let imported = 0; for (const [id, overrides] of Object.entries(data.features)) { if (this.features[id]) { this.features[id] = { ...this.features[id], ...overrides }; imported++; } } if (data.activePreset && this.presets[data.activePreset]) { this.activePreset = data.activePreset; } this.saveData(); this._log('📥', `تم استيراد ${imported} ميزة`); this.emit('configImported', imported); return true; } getApiData() { return { version: '19.0', stats: this.getStats(), categories: this.getCategories(), features: Object.values(this.features).map(f => ({ id: f.id, name: f.name, category: f.category, enabled: f.enabled, description: f.description, config: f.config, dependencies: f.dependencies, commands: f.commands })), presets: this.getPresets(), activePreset: this.activePreset }; } } module.exports = { FeaturesEngine };