import { type Pixel, type InsertPixel, type CanvasConfig, type InsertCanvasConfig, type UserCooldown, type InsertUserCooldown } from "@shared/schema"; import { randomUUID } from "crypto"; import { config } from "./config"; export interface IStorage { // Pixel operations getPixel(x: number, y: number): Promise; getAllPixels(): Promise; placePixel(pixel: InsertPixel): Promise; // Config operations getCanvasConfig(): Promise; updateCanvasConfig(config: InsertCanvasConfig): Promise; // User cooldown operations getUserCooldown(userId: string): Promise; setUserCooldown(cooldown: InsertUserCooldown): Promise; // Recent activity getRecentPlacements(limit?: number): Promise; } export class MemStorage implements IStorage { private pixels: Map; private config: CanvasConfig; private userCooldowns: Map; constructor() { this.pixels = new Map(); this.userCooldowns = new Map(); this.config = { id: randomUUID(), canvasWidth: config.canvasWidth, canvasHeight: config.canvasHeight, defaultCooldown: config.defaultCooldown, enableAutomaticEvents: config.enableAutomaticEvents, eventDuration: config.eventDurationMinutes, eventInterval: config.eventIntervalHours, showGridByDefault: config.showGridByDefault, updatedAt: new Date(), }; } private getPixelKey(x: number, y: number): string { return `${x},${y}`; } async getPixel(x: number, y: number): Promise { return this.pixels.get(this.getPixelKey(x, y)); } async getAllPixels(): Promise { return Array.from(this.pixels.values()); } async placePixel(insertPixel: InsertPixel): Promise { const id = randomUUID(); const pixel: Pixel = { ...insertPixel, id, createdAt: new Date(), }; this.pixels.set(this.getPixelKey(pixel.x, pixel.y), pixel); return pixel; } async getCanvasConfig(): Promise { return this.config; } async updateCanvasConfig(configUpdate: InsertCanvasConfig): Promise { this.config = { ...this.config, ...configUpdate, updatedAt: new Date(), }; return this.config; } async getUserCooldown(userId: string): Promise { return this.userCooldowns.get(userId); } async setUserCooldown(insertCooldown: InsertUserCooldown): Promise { const id = randomUUID(); const cooldown: UserCooldown = { ...insertCooldown, id, lastPlacement: new Date(), }; this.userCooldowns.set(cooldown.userId, cooldown); return cooldown; } async getRecentPlacements(limit: number = 10): Promise { const allPixels = Array.from(this.pixels.values()); return allPixels .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()) .slice(0, limit); } } export const storage = new MemStorage();