UNPKG

advanced-games-library

Version:

Advanced Gaming Library for React Native - Four Complete Games with iOS Compatibility Fixes

157 lines (125 loc) 3.61 kB
// Game data caching utility class GameDataCache { constructor() { this.cache = new Map(); this.playerDataCache = new Map(); this.defaultTTL = 60 * 60 * 1000; // 1 hour } async cacheGameState(gameId, state) { const cacheEntry = { data: state, timestamp: Date.now(), ttl: this.defaultTTL }; this.cache.set(`game_${gameId}`, cacheEntry); console.log(`💾 Cached game state for ${gameId}`); } async getGameState(gameId) { const entry = this.cache.get(`game_${gameId}`); if (!entry) { return null; } // Check if expired if (Date.now() - entry.timestamp > entry.ttl) { this.cache.delete(`game_${gameId}`); return null; } return entry.data; } async cachePlayerData(playerId, data) { const cacheEntry = { data: { ...data }, timestamp: Date.now(), ttl: this.defaultTTL * 24 // 24 hours for player data }; this.playerDataCache.set(`player_${playerId}`, cacheEntry); console.log(`💾 Cached player data for ${playerId}`); } async getPlayerData(playerId) { const entry = this.playerDataCache.get(`player_${playerId}`); if (!entry) { return null; } // Check if expired if (Date.now() - entry.timestamp > entry.ttl) { this.playerDataCache.delete(`player_${playerId}`); return null; } return entry.data; } async clearCache() { this.cache.clear(); this.playerDataCache.clear(); console.log('💾 Cleared all cache data'); } getCacheStats() { return { gameStates: this.cache.size, playerData: this.playerDataCache.size, totalEntries: this.cache.size + this.playerDataCache.size, lastCleared: null // Would track in real implementation }; } // Advanced caching with compression (simulated) async cacheWithOptions(key, data, options = {}) { const cacheEntry = { data: options.compress ? this.compressData(data) : data, timestamp: Date.now(), ttl: options.ttl || this.defaultTTL, compressed: !!options.compress, tags: options.tags || [] }; this.cache.set(key, cacheEntry); console.log(`💾 Cached ${key} with options:`, options); } async getWithOptions(key) { const entry = this.cache.get(key); if (!entry) { return null; } // Check if expired if (Date.now() - entry.timestamp > entry.ttl) { this.cache.delete(key); return null; } return entry.compressed ? this.decompressData(entry.data) : entry.data; } clearByTag(tag) { let cleared = 0; for (const [key, entry] of this.cache.entries()) { if (entry.tags && entry.tags.includes(tag)) { this.cache.delete(key); cleared++; } } console.log(`💾 Cleared ${cleared} entries with tag: ${tag}`); return cleared; } compressData(data) { // Simple compression simulation - in real app would use actual compression return JSON.stringify(data); } decompressData(data) { // Simple decompression simulation return JSON.parse(data); } } export const gameDataCache = new GameDataCache(); // Cache Manager for advanced features export class CacheManager { constructor() { this.cache = new Map(); } async set(key, data, options = {}) { await gameDataCache.cacheWithOptions(key, data, options); } async get(key) { return await gameDataCache.getWithOptions(key); } clearByTag(tag) { return gameDataCache.clearByTag(tag); } clear() { return gameDataCache.clearCache(); } }