Files
BreakEscape/public/break_escape/js/state-sync.js
Z. Cliffe Schreuders 006ed4af3e feat: Add client-side API integration layer
- 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
2025-11-21 15:27:54 +00:00

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();