mirror of
https://github.com/cliffe/BreakEscape.git
synced 2026-02-21 11:18:08 +00:00
- Add config.js for API configuration and CSRF token handling - Add api-client.js wrapper for server communication - Add state-sync.js for periodic state synchronization - Support multiple CSRF token sources (config object and meta tag) - Provide detailed error messages for configuration issues - Enable GET/POST/PUT requests with proper auth headers - Expose ApiClient globally for game code integration
41 lines
931 B
JavaScript
41 lines
931 B
JavaScript
import { ApiClient } from './api-client.js';
|
|
|
|
/**
|
|
* Periodic state synchronization with server
|
|
*/
|
|
export class StateSync {
|
|
constructor(interval = 30000) { // 30 seconds
|
|
this.interval = interval;
|
|
this.timer = null;
|
|
}
|
|
|
|
start() {
|
|
this.timer = setInterval(() => this.sync(), this.interval);
|
|
console.log('State sync started (every 30s)');
|
|
}
|
|
|
|
stop() {
|
|
if (this.timer) {
|
|
clearInterval(this.timer);
|
|
this.timer = null;
|
|
}
|
|
}
|
|
|
|
async sync() {
|
|
try {
|
|
// Get current game state
|
|
const currentRoom = window.currentRoom?.name;
|
|
const globalVariables = window.gameState?.globalVariables || {};
|
|
|
|
// Sync to server
|
|
await ApiClient.syncState(currentRoom, globalVariables);
|
|
console.log('✓ State synced to server');
|
|
} catch (error) {
|
|
console.error('State sync failed:', error);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Create global instance
|
|
window.stateSync = new StateSync();
|