everydaycats commited on
Commit
50ca5e6
·
verified ·
1 Parent(s): 44af7ff

Create stateManager.js

Browse files
Files changed (1) hide show
  1. stateManager.js +94 -0
stateManager.js ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // stateManager.js
2
+ import admin from 'firebase-admin';
3
+
4
+ // Initialize Firebase (Placeholder - expects serviceAccountKey.json)
5
+ // In production, uncomment the lines below and add your key file
6
+ /*
7
+ import serviceAccount from './serviceAccountKey.json' with { type: "json" };
8
+ admin.initializeApp({
9
+ credential: admin.credential.cert(serviceAccount),
10
+ databaseURL: "https://YOUR-PROJECT-ID.firebaseio.com"
11
+ });
12
+ const db = admin.database();
13
+ */
14
+
15
+ // In-Memory Storage
16
+ const activeProjects = new Map();
17
+
18
+ export const StateManager = {
19
+ /**
20
+ * Retrieves a project. Checks memory first, then DB (mocked).
21
+ */
22
+ getProject: async (projectId) => {
23
+ // 1. Check Memory
24
+ if (activeProjects.has(projectId)) {
25
+ const data = activeProjects.get(projectId);
26
+ data.lastActive = Date.now();
27
+ return data;
28
+ }
29
+
30
+ // 2. Check DB (Mock implementation)
31
+ // const snapshot = await db.ref(`projects/${projectId}`).once('value');
32
+ // if (snapshot.exists()) {
33
+ // const data = snapshot.val();
34
+ // activeProjects.set(projectId, data);
35
+ // return data;
36
+ // }
37
+
38
+ return null;
39
+ },
40
+
41
+ /**
42
+ * Creates or Updates a project state.
43
+ */
44
+ updateProject: async (projectId, data) => {
45
+ const currentData = activeProjects.get(projectId) || {};
46
+ const newData = { ...currentData, ...data, lastActive: Date.now() };
47
+
48
+ // Update Memory
49
+ activeProjects.set(projectId, newData);
50
+
51
+ // Update DB (Mock implementation)
52
+ // await db.ref(`projects/${projectId}`).set(newData);
53
+
54
+ return newData;
55
+ },
56
+
57
+ /**
58
+ * Adds a command to the plugin queue.
59
+ */
60
+ queueCommand: (projectId, commandResponse) => {
61
+ const project = activeProjects.get(projectId);
62
+ if (!project) return;
63
+
64
+ if (!project.commandQueue) project.commandQueue = [];
65
+
66
+ // Extract Markdown Code for the plugin
67
+ const codeMatch = commandResponse.match(/```lua([\s\S]*?)```/);
68
+ const code = codeMatch ? codeMatch[1].trim() : null;
69
+
70
+ project.commandQueue.push({
71
+ raw: commandResponse,
72
+ code: code, // This is what the plugin executes
73
+ timestamp: Date.now()
74
+ });
75
+ },
76
+
77
+ /**
78
+ * Removes inactive projects from memory.
79
+ */
80
+ cleanupInactivity: () => {
81
+ const now = Date.now();
82
+ const THREE_HOURS = 3 * 60 * 60 * 1000;
83
+ let removedCount = 0;
84
+
85
+ for (const [id, data] of activeProjects.entries()) {
86
+ if (now - data.lastActive > THREE_HOURS) {
87
+ activeProjects.delete(id);
88
+ removedCount++;
89
+ // Note: Data persists in Firebase, just removed from RAM
90
+ }
91
+ }
92
+ return removedCount;
93
+ }
94
+ };