everydaycats commited on
Commit
dd3f8ec
·
verified ·
1 Parent(s): 63870b6

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +48 -15
app.js CHANGED
@@ -113,44 +113,77 @@ app.post('/onboarding/create', validateRequest, async (req, res) => {
113
  let thumbnailBase64 = null;
114
 
115
  if (isFailure) {
116
- console.log(`[Onboarding] ❌ Project Failed Grading (${grading.rating}). Skipping Image.`);
117
  } else {
118
  console.log(`[Onboarding] ✅ Passed. Generating Thumbnail...`);
119
  const imagePrompt = `Game Title: ${grading.title}. Core Concept: ${description}`;
120
  thumbnailBase64 = await AIEngine.generateImage(imagePrompt);
121
  }
122
 
123
- const projectData = {
 
 
 
 
124
  id: projectId,
125
  userId,
126
  title: grading.title || "Untitled Project",
127
  description,
128
  answers,
129
  stats: grading,
130
- thumbnail: thumbnailBase64,// ? `data:image/png;base64,${thumbnailBase64}` : null,
131
- createdAt: Date.now(),
132
- status: isFailure ? "rejected" : "initialized"
 
 
 
 
133
  };
134
 
135
- // Initialize in StateManager (handles Memory + DB write)
136
- if (!isFailure) {
137
- await StateManager.updateProject(projectId, {
138
- ...projectData,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  workerHistory: [],
140
  pmHistory: [],
 
141
  failureCount: 0
142
- });
143
- } else if (db) {
144
- // If failed, just write to DB for records, don't load to active memory
145
- await db.ref(`projects/${projectId}`).set(projectData);
 
 
 
 
 
146
  }
147
 
148
  res.json({
149
  success: !isFailure,
150
  projectId,
151
  stats: grading,
152
- title: projectData.title,
153
- thumbnail: projectData.thumbnail
154
  });
155
 
156
  } catch (err) {
 
113
  let thumbnailBase64 = null;
114
 
115
  if (isFailure) {
116
+ console.log(`[Onboarding] ❌ Project Failed Grading. Skipping Image.`);
117
  } else {
118
  console.log(`[Onboarding] ✅ Passed. Generating Thumbnail...`);
119
  const imagePrompt = `Game Title: ${grading.title}. Core Concept: ${description}`;
120
  thumbnailBase64 = await AIEngine.generateImage(imagePrompt);
121
  }
122
 
123
+ const timestamp = Date.now();
124
+ const status = isFailure ? "rejected" : "initialized";
125
+
126
+ // 1. Prepare Memory Object (Flat for easy internal usage)
127
+ const memoryObject = {
128
  id: projectId,
129
  userId,
130
  title: grading.title || "Untitled Project",
131
  description,
132
  answers,
133
  stats: grading,
134
+ thumbnail: thumbnailBase64, // ? `data:image/png;base64,${thumbnailBase64}` : null,
135
+ createdAt: timestamp,
136
+ status,
137
+ workerHistory: [],
138
+ pmHistory: [],
139
+ commandQueue: [],
140
+ failureCount: 0
141
  };
142
 
143
+ // 2. Prepare Database Object (Nested for performance)
144
+ if (db) {
145
+ const updates = {};
146
+
147
+ // Bucket 1: Info (Lightweight)
148
+ updates[`projects/${projectId}/info`] = {
149
+ id: projectId,
150
+ userId,
151
+ title: grading.title || "Untitled Project",
152
+ description,
153
+ answers,
154
+ stats: grading,
155
+ createdAt: timestamp,
156
+ status
157
+ };
158
+
159
+ // Bucket 2: Thumbnail (Heavy - Isolated)
160
+ if (thumbnailBase64) {
161
+ updates[`projects/${projectId}/thumbnail`] = thumbnailBase64;
162
+ }
163
+
164
+ // Bucket 3: State (History)
165
+ updates[`projects/${projectId}/state`] = {
166
  workerHistory: [],
167
  pmHistory: [],
168
+ commandQueue: [],
169
  failureCount: 0
170
+ };
171
+
172
+ // Atomic multi-path update
173
+ await db.ref().update(updates);
174
+ }
175
+
176
+ // 3. Update Memory
177
+ if (!isFailure) {
178
+ await StateManager.updateProject(projectId, memoryObject);
179
  }
180
 
181
  res.json({
182
  success: !isFailure,
183
  projectId,
184
  stats: grading,
185
+ title: grading.title || "Untitled Project",
186
+ thumbnail: memoryObject.thumbnail
187
  });
188
 
189
  } catch (err) {