-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
264 lines (220 loc) · 8.9 KB
/
render.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const { ethers, utils } = require("ethers");
// var fetch = require("node-fetch")
const fs = require('fs');
const client = require('https');
const { Viper } = require('viper')
const { createCanvas, loadImage } = require('canvas')
const { spawn } = require('child_process');
const { extractBiteId, refreshOpensea, getNetwork, getProvider, formatName } = require('./utils.js')
const contracts = require('viper-contracts')
const path = require('path')
const preloads = {}
let lastCheckedQueueLength = 0
const queue = []
const currentSpawns = []
var os = require('os');
var cores = os.cpus().length
const servers = process.env.SERVERS ? parseInt(process.env.SERVERS) : 1
const maxSpawns = Math.ceil((cores > 2 ? cores - 2 : 1) / servers)
console.log(`max spawns: ${maxSpawns}`)
const v = new Viper()
const GENERATE_GIFS = process.env.GENERATE_GIFS == "true" ? true : false
const preload = GENERATE_GIFS ? v.allVipers.length : 0
const minLength = 1
const maxLength = getNetwork() == "homestead" ? 1 : 1
let totalTime = 0
let numberOfVipers = 0
for (let j = minLength; j <= maxLength; j++) {
for (let i = 1; i <= preload; i++) {
queue.push(formatName(i, j))
}
}
const queueChecker = setInterval(() => {
if (lastCheckedQueueLength !== queue.length) {
lastCheckedQueueLength = queue.length
console.log(`queue length: ${queue.length}`)
}
while (queue.length > 0 && currentSpawns.length < maxSpawns) {
console.log('spawning')
const next = queue.shift()
console.log(`next: ${next}`, `There are ${currentSpawns.length} current spawns, and ${queue.length} in the queue. The max spawn is ${maxSpawns}`)
const [tokenId, viperLength] = next.split("/")
generateGif(tokenId, viperLength)
}
}, 5000)
var addToQueue = async function (tokenId, viperLength) {
const queueIndex = queue.indexOf(formatName(tokenId, viperLength))
const currentSpawnsIndex = currentSpawns.indexOf(formatName(tokenId, viperLength))
// make sure that gif is in queue
if (queueIndex < 0 && currentSpawnsIndex < 0) {
console.log(`adding ${formatName(tokenId, viperLength, false)} to queue`)
queue.unshift(`${formatName(tokenId, viperLength)}`)
} else if (queueIndex > -1) {
console.log(`${formatName(tokenId, viperLength, false)} already in queue at position ${queueIndex}`)
if (queueIndex != 0) {
console.log(`moving ${formatName(tokenId, viperLength, false)} to front of queue`)
// move to front of queue
queue.splice(queueIndex, 1)
queue.unshift(formatName(tokenId, viperLength))
}
} else {
console.log(`${formatName(tokenId, viperLength, false)} already in currentSpawns at position ${currentSpawnsIndex}`)
}
}
var generatePlaceholderAndGif = async function (tokenId, viperLength) {
const dirPrefix = "public/" + (process.env.network == "homestead" ? "" : process.env.network + "-") + "gifs/"
// check if gif is already generated
// if so, return gif
const filename = path.join(__dirname, dirPrefix + `${formatName(tokenId, viperLength, false)}/complete.gif`)
try {
fs.accessSync(filename)
return filename
} catch (_) {
console.log(`no gif found at ${filename}`)
}
addToQueue(tokenId, viperLength)
// check if placeholder img is already generated
// if so, return placeholder img
// const placeHolderFilename = path.join(__dirname, `output/placeholder/${formatName(tokenId, viperLength)}.png`)
const placeHolderFilename = path.join(__dirname, `public/viper-loading-loop.gif`)
try {
fs.accessSync(placeHolderFilename)
return placeHolderFilename
} catch (_) {
console.log(`no placeholder found at ${placeHolderFilename}, begin generation`)
}
// create placeholder img
// return placeholder img filename
return await generatePlaceholder(tokenId, viperLength)
}
const pokeOS = async (tokenId, viperLength) => {
if (tokenId.length < 5) {
tokenId = parseInt(tokenId)
}
viperLength = parseInt(viperLength)
// if token exists on chain, refresh it on opensea
let contract
const nameCheck = formatName(tokenId, viperLength, false)
if (nameCheck.indexOf("b") > -1) {
contract = contracts.BiteByViper
} else {
contract = contracts.Viper
}
try {
const address = contract.networks[getNetwork()].address
// const instantiaedContract = new ethers.Contract(
// address,
// contract.abi,
// getProvider()
// )
try {
refreshOpensea(getNetwork(), address, tokenId.toString()).then((response) => {
console.log(`refresh metadata for ${tokenId} on opensea resulted in ${response.status}`, { response })
})
} catch (e) {
console.log(`refresh metadata error from opensea api call`, { e })
}
} catch (e) {
console.log(`failed to refresh metadata on opensea`, { e })
}
}
const generateGif = async function (tokenId, viperLength) {
// generate gif
console.log(`generateGif ${formatName(tokenId, viperLength, false)}`)
if (currentSpawns.length >= maxSpawns) {
console.log('max spawns reached, returning without running')
return
}
currentSpawns.push(`${formatName(tokenId, viperLength)}`)
const dirPrefix = "public/" + (process.env.network == "homestead" ? "" : process.env.network + "-") + "gifs/"
// check if gif is already generated
// if so, return gif
const filename = path.join(__dirname, dirPrefix + `${formatName(tokenId, viperLength, false)}/complete.gif`)
try {
fs.accessSync(filename)
console.log(`gif already exists at ${filename}, removing from currentSpans queue`)
currentSpawns.splice(currentSpawns.indexOf(`${formatName(tokenId, viperLength, false)}`), 1)
pokeOS(tokenId, viperLength)
return
} catch (_) { }
const start = new Date().getTime();
const child = spawn(`./node_modules/viper/bin/viper-cli.js`, ['generate-gif', tokenId, viperLength, dirPrefix])
child.stdout.on('data', data => {
console.log(`stdout-${formatName(tokenId, viperLength, false)}:\n${data}`);
});
child.stderr.on('data', data => {
console.error(`stderr-${formatName(tokenId, viperLength, false)}: ${data}`);
});
child.on('error', (error) => {
console.error(`error-${formatName(tokenId, viperLength, false)}: ${error.message}`);
});
child.on('close', async (code) => {
const end = new Date().getTime();
console.log(`child process exited with code ${code} while running on ${formatName(tokenId, viperLength, false)}`);
const filename = path.join(__dirname, dirPrefix + `${formatName(tokenId, viperLength, false)}/complete.gif`)
console.log(`checking if ${filename} exists`)
try {
fs.accessSync(filename)
const duration = end - start
totalTime += duration
numberOfVipers++
console.log(`gif completed at : ${filename} in a time of ${duration / 1000} s, average time: ${(totalTime / numberOfVipers) / 1000} s`)
} catch (e) {
console.log({ e })
console.log(`exited without completing the gif, adding back to queue: ${formatName(tokenId, viperLength, false)}`)
currentSpawns.splice(currentSpawns.indexOf(`${formatName(tokenId, viperLength)}`), 1)
queue.unshift(`${formatName(tokenId, viperLength)}`)
return
}
try {
console.log('now that gif is complete, try optimizing it')
await optimizeGif(filename)
} catch (e) {
console.log(`failed to optimize gif, exited with error:`, { e })
}
pokeOS(tokenId, viperLength)
console.log('done trying to optimize gif, OK to remove from queue whether optimization worked or not')
currentSpawns.splice(currentSpawns.indexOf(`${formatName(tokenId, viperLength)}`), 1)
});
}
var optimizeGif = async function (filename) {
return new Promise((resolve, reject) => {
const child = spawn(`gifsicle`, ['-b', '-O2', filename])
child.stdout.on('data', data => {
console.log(`stdout-gifsicle:\n${data}`);
});
child.stderr.on('data', data => {
console.error(`stderr-gifsicle: ${data}`);
});
child.on('error', (error) => {
console.error(`error-gifsicle: ${error.message}`);
});
child.on('close', async (code) => {
console.log(`child process exited with code ${code} while running gifsicle`);
if (code == 0) {
resolve()
} else {
reject(code)
}
})
})
}
var generatePlaceholder = async function (tokenId, viperLength) {
// generate placeholder img
console.log('generate placeholder')
const canvas = createCanvas(686, 686)
const ctx = canvas.getContext('2d')
ctx.font = "bold 32px serif";
ctx.fillText(`please wait while viper #${tokenId}`, 100, 243)
ctx.fillText(` is being generated`, 100, 343)
// save placeholder img
const filename = `${__dirname}/output/placeholder/${formatName(tokenId, viperLength, false)}.png`
const out = fs.createWriteStream(filename)
const stream = canvas.createPNGStream()
stream.pipe(out)
return new Promise((resolve, reject) => {
out.on('finish', () => resolve(filename))
out.on('error', reject)
})
}
module.exports = { generateGif, generatePlaceholder, generatePlaceholderAndGif, addToQueue }