+
+ const index = images.findIndex(image => image.id == comment.image_id);
+
+ // Before adding the comment to server's state, drop the image_id
+ delete comment.image_id;
+
+ // Now add the comment to the image, remove the image from the
+ // images array and then add it back at the end, (so it appears
+ // as the most-recently-modified image for any new clients)
+ const image = images[index];
+ image.comments.push(comment);
+ images.splice(index, 1);
+ images.push(image);
+ });
+
+ // Generate an image when requested
+ socket.on('generate', (request) => {
+ console.log(`Generating image for ${socket.request.session.name} with code=${request['code']} and prompt=${request['prompt']}`);
+ async function generate_image(code, prompt) {
+ function emit_image(image, target) {
+ image.id = state.images.length;
+ image.censored = false;
+ image.link = "";
+ if (target) {
+ image.comments = [{
+ "name": "ZomboCom",
+ "text": target.response
+ }];
+ if (! state.targets.includes(target.short)) {
+ state.targets.push(target.short);
+ }
+ } else {
+ image.comments = [];
+ }
+ io.emit('image', image);
+ state.images.push(image);
+ }
+
+ var promise;
+
+ // Before doing any generation, check for a target image
+ const normal_target = prompt.replace(/[^a-zA-Z]/g, "").toLowerCase() + code.toString() + ".png";
+ const target_arr = targets.filter(item => item.normal === normal_target);
+ if (target_arr.length) {
+ const target = target_arr[0];
+ const target_file = `${targets_dir}/${normal_target}`;
+ const normal_prompt = prompt.replace(/[^-_.a-zA-Z]/g, "_");
+ var counter = 1;
+ var base;
+ var filename;
+ while (true) {
+ if (counter > 1) {
+ base = `${code}_${normal_prompt}_${counter}.png`
+ } else {
+ base = `${code}_${normal_prompt}.png`
+ }
+ filename = `${images_dir}/${base}`
+ if (! fs.existsSync(filename)) {
+ break;
+ }
+ counter = counter + 1;
+ }
+ fs.copyFile(target_file, filename, 0, (err) => {
+ if (err) {
+ console.log("Error copying " + target_file + " to " + filename + ": " + err);
+ }
+ });
+ const image = {
+ "code": code,
+ "prompt": prompt,
+ "filename": '/images/' + base
+ };
+ emit_image(image, target);
+ } else {
+
+ // Inject the target seed for the "dice" prompt once every
+ // 4 requests for a random seed (and only if the word
+ // "dice" does not appear in the prompt).
+ if (!code && !prompt.toLowerCase().includes("dice")) {
+ if (state.images.length % 4 == 0) {
+ code = 319630254;
+ }
+ }
+
+ if (code) {
+ promise = execFile(python_path, [generate_image_script, `--seed=${code}`, prompt])
+ } else {
+ promise = execFile(python_path, [generate_image_script, prompt])
+ }
+ const child = promise.child;
+ child.stdout.on('data', (data) => {
+ const images = JSON.parse(data);
+ images.forEach((image) => {
+ emit_image(image, null);
+ });
+ });
+ child.stderr.on('data', (data) => {
+ console.log("Error occurred during generate-image: " + data);
+ });
+ try {
+ const { stdout, stderr } = await promise;
+ } catch(e) {
+ console.error(e);
+ }
+ }
+ socket.emit('generation-done');
+ }
+
+ generate_image(request['code'], request['prompt']);