-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwstest.js
187 lines (156 loc) · 5.36 KB
/
wstest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var { WebSocket, WebSocketServer } = require('ws');
var fs = require('fs');
var express = require('express');
const wss = new WebSocketServer({ port: 8080});
var users = [];
var submissions = [];
//two value position: round and position in round
var images = [];
var thisCycleImagesGenerated = 0;
var round = 1;
var maxRounds = 2;
//express server that serves all files here over port 2060
var app = express();
app.use(express.static('./'));
app.listen(2080, () => {
console.log("Server running on port 2080");
});
wss.on('connection', function connection(ws) {
ws.on('error', console.error);
//on recieve message
ws.on('message', function incoming(data) {
data = JSON.parse(data);
console.log(data);
if (data.type == "login") {
users.push({name: data.name, ws: ws, position: users.length});
console.log(data.name + " has logged in. Current active users: " + users.length);
}
else if (data.type == "promptSubmit") {
var user = users.find(u => u.ws == ws);
console.log(data.name + " submitted prompt " + data.message);
OnPromptSubmitted(user.position, data.message, user.name);
ws.send(JSON.stringify({ type: "PromptSubmitted" }));
}
else {
console.log("Invalid data type");
}
});
//on close
ws.on('close', function close() {
//find user with ws
var user = users.find(u => u.ws == ws);
//remove user from list
users = users.filter(u => u.ws !== ws);
console.log("A User has disconnected. Current active users: " + users.length);
});
});
async function OnPromptSubmitted(data, prompt, username) {
submissions.push(data);
GenerateImage(data, prompt, username);
console.log(submissions);
if(submissions.length == users.length) {
console.log("All users have submitted a prompt");
console.log(submissions);
//SendToAllUsers("RoundOver");
users.forEach(user => {
user.ws.send(JSON.stringify({type: "roundOver"}));
});
}
}
async function GenerateImage(position, prompt, username){
//just async wait for 5 seconds
//const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
//await delay(5000)
var filename = "./images/" + position + username + Math.random().toString(36).slice(2, 7) + ".png";
await genImage(prompt, "", filename)
//remove string ", photography, render, detailed, masterpiece, 8k, (realistic, UE5, deviantart)," from prompt if it exists
var newPrompt = prompt.replace(", photography, render, detailed, masterpiece, 8k, (realistic, UE5, deviantart),", "");
images.push({ url: filename, position: position, round: round, prompt: newPrompt, username: username});
thisCycleImagesGenerated++;
if(thisCycleImagesGenerated == users.length) {
StartNewRound();
}
}
function StartNewRound() {
if(round < maxRounds)
{
console.log("All images have been generated. New round start");
console.log(images);
users.forEach(user => {
//get image at user position +1 for current round
user.position++;
if (user.position == users.length) {
console.log("User position of " + user.position + "for" + user.name + " is out of bounds. Moving to 0.")
user.position = 0;
}
var image = GetImage(round, user.position);
user.ws.send(JSON.stringify({ type: "newRoundImage", url: image.url }));
});
submissions = [];
thisCycleImagesGenerated = 0;
round++;
}
else {
EndGame()
}
}
function EndGame() {
console.log("Game over, man!");
console.log(images);
users.forEach(user => {
user.ws.send(JSON.stringify({ type: "gameOver", message: JSON.stringify(images) }));
});
images = [];
round = 1;
submissions = [];
thisCycleImagesGenerated = 0;
users = [];
}
function SendToAllUsers(data) {
users.forEach(user => {
user.ws.send(data);
});
}
function GetImage(round, position)
{
return images.find(i => i.round == round && i.position == position);
}
function base64ToPNG(base64String, outputPath) {
const base64Data = base64String.replace(/^data:image\/png;base64,/, '');
const buffer = Buffer.from(base64Data, 'base64');
fs.writeFile(outputPath, buffer, (err) => {
if (err) {
console.error('Error saving PNG image:', err);
} else {
console.log('PNG image saved successfully!');
}
});
}
async function genImage(posPrompt, negPrompt, filename, sampleMode) {
//sample mode isn't used (YET!!!)
if(sampleMode == null) {
sampleMode = "Euler a";
}
else if(sampleMode == "Default") {
sampleMode = "Euler a";
}
else if(sampleMode == "Artsy") {
sampleMode = "DDIM";
}
const response = await fetch('http://127.0.0.1:7860/sdapi/v1/txt2img', {
method: 'POST',
headers: {
'accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"prompt": posPrompt,
"negative_prompt": negPrompt,
"steps": 20
})
});
var resj = await response.json();
//console.log(resj)
base64ToPNG(resj.images[0], filename)
return (filename)
}