Files
novafarma/docs/guides/STEAM_INTEGRATION_PLAN.md
2025-12-12 13:48:49 +01:00

8.1 KiB

🎮 STEAM INTEGRATION - GREENWORKS SDK

Datum: 12. December 2025
Prioriteta: LOW (za Steam release)
Estimated Time: 2-3 ure


🎯 CILJ:

Integracija Greenworks SDK za Steam funkcionalnosti:

  • Cloud Save Sync
  • Achievements
  • Offline vs Online mode

📋 IMPLEMENTATION:

1. Greenworks SDK Setup (30 min)

Install:

npm install greenworks --save

Package.json:

{
  "dependencies": {
    "greenworks": "^0.18.0"
  }
}

Main.js (Electron):

const greenworks = require('./greenworks');

if (greenworks.init()) {
    console.log('✅ Steam initialized!');
    console.log('Steam ID:', greenworks.getSteamId().steamId);
} else {
    console.log('⚠️ Steam not running or game not launched via Steam');
}

2. Cloud Save Sync (60 min)

SteamCloudSystem.js:

class SteamCloudSystem {
    constructor() {
        this.enabled = false;
        this.steamId = null;
        
        if (typeof greenworks !== 'undefined') {
            this.enabled = greenworks.init();
            if (this.enabled) {
                this.steamId = greenworks.getSteamId().steamId;
                console.log('☁️ Steam Cloud: Enabled');
            }
        }
    }
    
    // Save to Steam Cloud
    saveToCloud(filename, data) {
        if (!this.enabled) {
            console.log('⚠️ Steam Cloud not available');
            return false;
        }
        
        try {
            const json = JSON.stringify(data);
            greenworks.saveTextToFile(filename, json, (err) => {
                if (err) {
                    console.error('❌ Cloud save failed:', err);
                } else {
                    console.log('✅ Saved to Steam Cloud:', filename);
                }
            });
            return true;
        } catch (e) {
            console.error('❌ Cloud save error:', e);
            return false;
        }
    }
    
    // Load from Steam Cloud
    loadFromCloud(filename, callback) {
        if (!this.enabled) {
            console.log('⚠️ Steam Cloud not available');
            callback(null);
            return;
        }
        
        greenworks.readTextFromFile(filename, (err, data) => {
            if (err) {
                console.error('❌ Cloud load failed:', err);
                callback(null);
            } else {
                try {
                    const parsed = JSON.parse(data);
                    console.log('✅ Loaded from Steam Cloud:', filename);
                    callback(parsed);
                } catch (e) {
                    console.error('❌ Cloud parse error:', e);
                    callback(null);
                }
            }
        });
    }
    
    // Check if file exists in cloud
    fileExists(filename, callback) {
        if (!this.enabled) {
            callback(false);
            return;
        }
        
        greenworks.isCloudEnabledForUser((enabled) => {
            if (!enabled) {
                callback(false);
                return;
            }
            
            greenworks.getFileCount((count) => {
                // Check if file exists
                // (Greenworks doesn't have direct exists check)
                this.loadFromCloud(filename, (data) => {
                    callback(data !== null);
                });
            });
        });
    }
}

3. Offline vs Online Mode (30 min)

Connection Detection:

class ConnectionSystem {
    constructor() {
        this.isOnline = navigator.onLine;
        this.isSteamOnline = false;
        
        // Check Steam connection
        if (typeof greenworks !== 'undefined' && greenworks.init()) {
            this.isSteamOnline = true;
        }
        
        // Listen for connection changes
        window.addEventListener('online', () => {
            this.isOnline = true;
            console.log('🌐 Connection: Online');
        });
        
        window.addEventListener('offline', () => {
            this.isOnline = false;
            console.log('📴 Connection: Offline');
        });
        
        console.log(`🌐 Connection: ${this.isOnline ? 'Online' : 'Offline'}`);
        console.log(`🎮 Steam: ${this.isSteamOnline ? 'Online' : 'Offline'}`);
    }
    
    // Save strategy: Cloud if online, local if offline
    saveGame(data) {
        if (this.isSteamOnline) {
            // Try cloud save first
            steamCloudSystem.saveToCloud('savegame.json', data);
        }
        
        // Always save locally as backup
        localStorage.setItem('novafarma_savefile', JSON.stringify(data));
        
        console.log('💾 Game saved (Cloud + Local)');
    }
    
    // Load strategy: Cloud if available, fallback to local
    loadGame(callback) {
        if (this.isSteamOnline) {
            // Try cloud load
            steamCloudSystem.loadFromCloud('savegame.json', (cloudData) => {
                if (cloudData) {
                    console.log('☁️ Loaded from Steam Cloud');
                    callback(cloudData);
                } else {
                    // Fallback to local
                    this.loadLocal(callback);
                }
            });
        } else {
            // Load local
            this.loadLocal(callback);
        }
    }
    
    loadLocal(callback) {
        const data = localStorage.getItem('novafarma_savefile');
        if (data) {
            console.log('💾 Loaded from local storage');
            callback(JSON.parse(data));
        } else {
            callback(null);
        }
    }
}

4. Testing (30 min)

Test Cases:

A. Cloud Sync Test:

// Test 1: Save to cloud
steamCloudSystem.saveToCloud('test.json', { test: 'data' });

// Test 2: Load from cloud
steamCloudSystem.loadFromCloud('test.json', (data) => {
    console.log('Loaded:', data);
});

// Test 3: File exists check
steamCloudSystem.fileExists('test.json', (exists) => {
    console.log('File exists:', exists);
});

B. Offline/Online Test:

// Test 1: Save while online
connectionSystem.saveGame({ player: 'data' });

// Test 2: Disconnect (simulate offline)
// - Disable network in browser DevTools
// - Try to save again
connectionSystem.saveGame({ player: 'data2' });

// Test 3: Reconnect
// - Enable network
// - Load game
connectionSystem.loadGame((data) => {
    console.log('Loaded:', data);
});

📝 INTEGRATION:

GameScene.js:

// In create():
if (typeof greenworks !== 'undefined') {
    this.steamCloudSystem = new SteamCloudSystem();
    this.connectionSystem = new ConnectionSystem();
}

// Replace existing save/load:
saveGame() {
    const data = this.saveSystem.getSaveData();
    
    if (this.connectionSystem) {
        this.connectionSystem.saveGame(data);
    } else {
        // Fallback to local only
        localStorage.setItem('novafarma_savefile', JSON.stringify(data));
    }
}

loadGame() {
    if (this.connectionSystem) {
        this.connectionSystem.loadGame((data) => {
            if (data) {
                this.saveSystem.loadSaveData(data);
            }
        });
    } else {
        // Fallback to local only
        const data = localStorage.getItem('novafarma_savefile');
        if (data) {
            this.saveSystem.loadSaveData(JSON.parse(data));
        }
    }
}

🔧 DATOTEKE:

Nove:

  • src/systems/SteamCloudSystem.js (~150 vrstic)
  • src/systems/ConnectionSystem.js (~100 vrstic)

Posodobljene:

  • package.json - Greenworks dependency
  • main.js - Greenworks init
  • GameScene.js - Integration

⚠️ POMEMBNO:

Greenworks zahteva:

  1. Steam Client running
  2. Game launched via Steam
  3. Valid Steam App ID
  4. Steam SDK files

Za testiranje brez Steama:

  • Sistem bo avtomatsko fallback na local storage
  • Vse deluje tudi brez Steama

🎯 PRIORITETA:

LOW - Potrebno samo za Steam release.

Priporočam: Implementacija pred Steam release, ne zdaj.


Status: PLAN PRIPRAVLJEN

Estimated time: 2-3 ure

Kdaj: Pred Steam release (ne kritično za testiranje)

Želite implementacijo zdaj ali kasneje? 🎮