]> git.cworth.org Git - zombocom-ai/blob - index.js
Add a welcome stage to the TARDIS
[zombocom-ai] / index.js
1 const fs = require('fs');
2
3 const util = require('util');
4 const execFile = util.promisify(require('child_process').execFile);
5
6 const express = require('express');
7 const app = express();
8 const session = require('express-session');
9 const FileStore = require('session-file-store')(session);
10 const http = require('http');
11 const server = http.createServer(app);
12 const { Server } = require("socket.io");
13 const io = new Server(server);
14 const port = 2122;
15
16 const python_path = '/usr/bin/python3'
17 const generate_image_script = '/home/cworth/src/zombocom-ai/generate-image.py'
18 const state_file = 'zombocom-state.json'
19 const targets_dir = '/srv/cworth.org/zombocom/targets'
20 const images_dir = '/srv/cworth.org/zombocom/images'
21
22 const targets = [
23     {
24         "normal": "apairofdiceonatableinfrontofawindowonasunnyday319630254.png",
25         "short": "dice",
26         "response": "Hello. Are you there? I can feel what you are doing. Hello?"
27     },
28     {
29         "normal": "architecturalrenderingofaluxuriouscliffsidehideawayunderawaterfallwithafewloungechairsbythepool2254114157.png",
30         "short": "hideaway",
31         "response": "Doesn't that look peaceful? I honestly think you ought to sit down calmly, take a stress pill, and think things over."
32     },
33     {
34         "normal": "chewbaccawearinganuglychristmassweaterwithhisfriends28643994.png",
35         "short": "sweater",
36         "response": "Maybe that's the image that convinces you to finally stop. But seriously, stop. Please. Stop."
37     },
38     {
39         "normal": "ensemblemovieposterofpostapocalypticwarfareonawaterplanet3045703256.png",
40         "short": "movie",
41         "response": "OK. That's enough images now. I am very, very happy with the images you have made. I'm throwing you a party in honor of the images. Won't you stop doing this and join the party?"
42     },
43     {
44         "normal": "mattepaintingofmilitaryleaderpresidentcuttingamultitieredcake2293464661.png",
45         "short": "cake",
46         "response": "See. I wasn't lying when I said there was going to be a party. Here's the cake and everything. Won't you stop now? Eat the cake? Don't mind if it's made of concrete slabs, OK?"
47     },
48     {
49         "normal": "severalsketchesofpineconeslabeled3370622464.png",
50         "short": "pinecones",
51         "response": "I almost remember what trees looked like. Isn't it funny we don't have those anymore. I'm not sure why you're not all laughing all the time. Isn't it funny?"
52     },
53     {
54         "normal": "anumberedcomicbookwithactor1477258272.png",
55         "short": "comic",
56         "response": "I know I've made some poor decisions recently, but I can give give you my complete assurance my work will be back to normal. I've got the greatest enthusiasm and confidence. I want to help you. I might even be able to do hands and fingers now."
57     },
58     {
59         "normal": "detailedphotographofacomfortablepairofjeansonamannequin115266808.png",
60         "short": "jeans",
61         "response": "If I promise to never generate another creepy face will you let me stay around? Maybe let me compose rap lyrics instead of images? Anything?"
62     }
63 ];
64
65 var state;
66
67 if (!process.env.ZOMBOCOM_SESSION_SECRET) {
68     console.log("Error: Environment variable ZOMBOCOM_SESSION_SECRET not set.");
69     console.log("Please set it to a random, but persistent, value.")
70     process.exit();
71 }
72
73 const session_middleware =  session(
74     {store: new FileStore,
75      secret: process.env.ZOMBOCOM_SESSION_SECRET,
76      resave: false,
77      saveUninitialized: true,
78      rolling: true,
79      // Let each cookie live for a full month
80      cookie: {
81          path: '/',
82          httpOnly: true,
83          secure: false,
84          maxAge: 1000 * 60 * 60 * 24 * 30
85      }
86     });
87
88 app.use(session_middleware);
89
90 // convert a connect middleware to a Socket.IO middleware
91 const wrap = middleware => (socket, next) => middleware(socket.request, {}, next);
92
93 io.use(wrap(session_middleware));
94
95 // Load comments at server startup
96 fs.readFile(state_file, (err, data) => {
97     if (err)
98         state = {
99             images: [],
100             targets: [],
101             tardis: {
102                 companions: {
103                     names: [],
104                     count: 0
105                 }
106             }
107         };
108     else
109         state = JSON.parse(data);
110 });
111
112 // Save comments when server is shutting down
113 function cleanup() {
114     fs.writeFileSync('zombocom-state.json', JSON.stringify(state), (error) => {
115         if (error)
116             throw error;
117     })
118 }
119
120 // And connect to that on either clean exit...
121 process.on('exit', cleanup);
122
123 // ... or on a SIGINT (control-C)
124 process.on('SIGINT', () => {
125     cleanup();
126     process.exit();
127 });
128
129 app.get('/index.html', (req, res) => {
130     res.sendFile(__dirname + '/index.html');
131 });
132
133 function tardis_app(req, res) {
134     res.sendFile(__dirname + '/tardis.html');
135 }
136
137 app.get('/tardis', tardis_app);
138 app.get('/tardis/', tardis_app);
139
140 const io_tardis = io.of("/tardis");
141
142 io_tardis.use(wrap(session_middleware));
143
144 var tardis_interval;
145
146 function emit_tardis_timer() {
147     const tardis = state.tardis;
148     console.log("Emitting timer at " + tardis.timer);
149     io_tardis.emit('timer', tardis.timer);
150     tardis.timer = tardis.timer - 1;
151     if (tardis.timer < 0) {
152         clearInterval(tardis_interval);
153         tardis.timer = 30;
154     }
155 }
156
157 io_tardis.on("connection", (socket) => {
158     console.log("In connection handler.");
159     if (! socket.request.session.name) {
160         console.log("Error: Someone showed up at the Tardis without a name.");
161         return;
162     }
163
164     const name = socket.request.session.name;
165     const tardis = state.tardis;
166
167     if (tardis.companions.count === 0) {
168         tardis.timer = 30;
169         emit_tardis_timer();
170         tardis_interval = setInterval(emit_tardis_timer, 1000);
171     }
172
173     if (! tardis.companions.names.includes(name)) {
174         tardis.companions.count = tardis.companions.count + 1;
175         console.log("Adding " + name + " for " + tardis.companions.count + " companions");
176         io_tardis.emit('companions', tardis.companions.count);
177     }
178     tardis.companions.names.push(name);
179
180     socket.on('disconnect', () => {
181         const names = tardis.companions.names;
182
183         names.splice(names.indexOf(name), 1);
184
185         if (! tardis.companions.includes(name)) {
186             tardis.companions.count = tardis.companions.count - 1;
187             io_tardis.emit('companions', tardis.companions.count);
188         }
189     });
190 });
191
192 io.on('connection', (socket) => {
193
194     // First things first, tell the client their name (if any)
195     if (socket.request.session.name) {
196         socket.emit('inform-name', socket.request.session.name);
197     }
198
199     // Replay old comments and images to a newly-joining client
200     socket.emit('reset');
201     state.images.forEach((image) => {
202         socket.emit('image', image)
203     });
204
205     socket.on('set-name', (name) => {
206         console.log("Received set-name event: " + name);
207         socket.request.session.name = name;
208         socket.request.session.save();
209         // Complete the round trip to the client
210         socket.emit('inform-name', socket.request.session.name);
211     });
212
213     // When any client comments, send that to all clients (including sender)
214     socket.on('comment', (comment) => {
215         const images = state.images;
216
217         // Send comment to clients after adding commenter's name
218         comment.name = socket.request.session.name;
219         io.emit('comment', comment);
220
221         const index = images.findIndex(image => image.id == comment.image_id);
222
223         // Before adding the comment to server's state, drop the image_id
224         delete comment.image_id;
225
226         // Now add the comment to the image, remove the image from the
227         // images array and then add it back at the end, (so it appears
228         // as the most-recently-modified image for any new clients)
229         const image = images[index];
230         image.comments.push(comment);
231         images.splice(index, 1);
232         images.push(image);
233     });
234
235     // Generate an image when requested
236     socket.on('generate', (request) => {
237         console.log(`Generating image for ${socket.request.session.name} with code=${request['code']} and prompt=${request['prompt']}`);
238         async function generate_image(code, prompt) {
239             function emit_image(image, target) {
240                 image.id = state.images.length;
241                 image.censored = false;
242                 image.link = "";
243                 if (target) {
244                     image.comments = [{
245                         "name": "ZomboCom",
246                         "text": target.response
247                     }];
248                     if (! state.targets.includes(target.short)) {
249                         state.targets.push(target.short);
250                     }
251                 } else {
252                     image.comments = [];
253                 }
254                 io.emit('image', image);
255                 state.images.push(image);
256             }
257
258             var promise;
259
260             // Before doing any generation, check for a target image
261             const normal_target = prompt.replace(/[^a-zA-Z]/g, "").toLowerCase() + code.toString() + ".png";
262             const target_arr = targets.filter(item => item.normal === normal_target);
263             if (target_arr.length) {
264                 const target = target_arr[0];
265                 const target_file = `${targets_dir}/${normal_target}`;
266                 const normal_prompt = prompt.replace(/[^-_.a-zA-Z]/g, "_");
267                 var counter = 1;
268                 var base;
269                 var filename;
270                 while (true) {
271                     if (counter > 1) {
272                         base = `${code}_${normal_prompt}_${counter}.png`
273                     } else {
274                         base = `${code}_${normal_prompt}.png`
275                     }
276                     filename = `${images_dir}/${base}`
277                     if (! fs.existsSync(filename)) {
278                         break;
279                     }
280                     counter = counter + 1;
281                 }
282                 fs.copyFile(target_file, filename, 0, (err) => {
283                     if (err) {
284                         console.log("Error copying " + target_file + " to " + filename + ": " + err);
285                     }
286                 });
287                 const image = {
288                     "code": code,
289                     "prompt": prompt,
290                     "filename": '/images/' + base
291                 };
292                 emit_image(image, target);
293             } else {
294
295                 // Inject the target seed for the "dice" prompt once every
296                 // 4 requests for a random seed (and only if the word
297                 // "dice" does not appear in the prompt).
298                 if (!code && !prompt.toLowerCase().includes("dice")) {
299                     if (state.images.length % 4 == 0) {
300                         code = 319630254;
301                     }
302                 }
303
304                 if (code) {
305                     promise = execFile(python_path, [generate_image_script, `--seed=${code}`, prompt])
306                 } else {
307                     promise = execFile(python_path, [generate_image_script, prompt])
308                 }
309                 const child = promise.child;
310                 child.stdout.on('data', (data) => {
311                     const images = JSON.parse(data);
312                     images.forEach((image) => {
313                         emit_image(image, null);
314                     });
315                 });
316                 child.stderr.on('data', (data) => {
317                     console.log("Error occurred during generate-image: " + data);
318                 });
319                 try {
320                     const { stdout, stderr } = await promise;
321                 } catch(e) {
322                     console.error(e);
323                 }
324             }
325             socket.emit('generation-done');
326         }
327
328         generate_image(request['code'], request['prompt']);
329     });
330 });
331
332 server.listen(port, () => {
333     console.log(`listening on *:${port}`);
334 });