diff --git a/src/lib/classes/RCCService.js b/src/lib/classes/RCCService.js index 732dbd2..c448e95 100644 --- a/src/lib/classes/RCCService.js +++ b/src/lib/classes/RCCService.js @@ -32,6 +32,7 @@ class RCCService extends EventEmitter { return resolve(false) } + this.started = true return resolve(true) }) diff --git a/src/lib/classes/RenderJob.js b/src/lib/classes/RenderJob.js index 2bdc29d..076e6e0 100644 --- a/src/lib/classes/RenderJob.js +++ b/src/lib/classes/RenderJob.js @@ -1,19 +1,26 @@ -const { readFile } = require("fs/promises"); +const { readFile } = require("fs/promises") -const Job = require("./Job.js"); -const logger = require("../logger.js"); +const Job = require("./Job.js") +const logger = require("../logger.js") +const { randomUUID } = require("crypto") class RenderJob extends Job { constructor() { - super(); + super() } async RenderHeadshot(id) { - const started = await this.Start(); - if (!started) throw new Error("RCCService failed to start"); - if (!this.client) await this.CreateClient(); + this.id = randomUUID() - logger.info(`[${this.id}] Headshot RenderJob started for ${id}`); + const running = this.started + if (!running) { + const started = await this.Start() + if (!started) throw new Error("RCCService failed to start") + } + + if (!this.client) await this.CreateClient() + + logger.info(`[${this.id}] Headshot RenderJob started for ${id}`) const result = await this.OpenJobEx({ name: this.id, @@ -32,21 +39,27 @@ class RenderJob extends Job { { type: "LUA_TNUMBER", value: id }, ], }, - }).catch((e) => false); + }).catch((e) => false) - logger.info(`[${this.id}] Headshot RenderJob finished for ${id}`); + logger.info(`[${this.id}] Headshot RenderJob finished for ${id}`) - if (!result) return false; - return result[0]?.OpenJobExResult?.LuaValue[0]?.value; + if (!result) return false + return result[0]?.OpenJobExResult?.LuaValue[0]?.value } async RenderBodyshot(id, three_d = false) { - const started = await this.Start(); - if (!started) throw new Error("RCCService failed to start"); - if (!this.client) await this.CreateClient(); + this.id = randomUUID() - if (three_d) logger.info(`[${this.id}] 3D Bodyshot RenderJob started for ${id}`); - else logger.info(`[${this.id}] Bodyshot RenderJob started for ${id}`); + const running = this.started + if (!running) { + const started = await this.Start() + if (!started) throw new Error("RCCService failed to start") + } + + if (!this.client) await this.CreateClient() + + if (three_d) logger.info(`[${this.id}] 3D Bodyshot RenderJob started for ${id}`) + else logger.info(`[${this.id}] Bodyshot RenderJob started for ${id}`) const result = await this.OpenJobEx({ name: this.id, @@ -65,22 +78,28 @@ class RenderJob extends Job { { type: "LUA_TNUMBER", value: id }, ], }, - }).catch((e) => false); + }).catch((e) => false) - if (three_d) logger.info(`[${this.id}] 3D Bodyshot RenderJob finished for ${id}`); - else logger.info(`[${this.id}] Bodyshot RenderJob finished for ${id}`); + if (three_d) logger.info(`[${this.id}] 3D Bodyshot RenderJob finished for ${id}`) + else logger.info(`[${this.id}] Bodyshot RenderJob finished for ${id}`) - if (!result) return false; - return result[0]?.OpenJobExResult?.LuaValue[0]?.value; + if (!result) return false + return result[0]?.OpenJobExResult?.LuaValue[0]?.value } async RenderAsset(id, three_d = false) { - const started = await this.Start(); - if (!started) throw new Error("RCCService failed to start"); - if (!this.client) await this.CreateClient(); + this.id = randomUUID() - if (three_d) logger.info(`[${this.id}] 3D Asset RenderJob started for ${id}`); - else logger.info(`[${this.id}] Asset RenderJob started for ${id}`); + const running = this.started + if (!running) { + const started = await this.Start() + if (!started) throw new Error("RCCService failed to start") + } + + if (!this.client) await this.CreateClient() + + if (three_d) logger.info(`[${this.id}] 3D Asset RenderJob started for ${id}`) + else logger.info(`[${this.id}] Asset RenderJob started for ${id}`) const result = await this.OpenJobEx({ name: this.id, @@ -100,26 +119,30 @@ class RenderJob extends Job { { type: "LUA_TBOOLEAN", value: "true" }, ], }, - }).catch((e) => false); + }).catch((e) => false) - if (three_d) logger.info(`[${this.id}] 3D Asset RenderJob finished for ${id}`); - else logger.info(`[${this.id}] Asset RenderJob finished for ${id}`); + if (three_d) logger.info(`[${this.id}] 3D Asset RenderJob finished for ${id}`) + else logger.info(`[${this.id}] Asset RenderJob finished for ${id}`) - if (!result) return false; - return result[0]?.OpenJobExResult?.LuaValue[0]?.value; + if (!result) return false + return result[0]?.OpenJobExResult?.LuaValue[0]?.value } async RenderPlace(id) { - const response = await axios(`${process.env.BASE_URL}/API/Game/${id}?t=${process.env.ARBITER_TOKEN}`).catch((_) => reject(_)); - const { server_token, server_port, server_owner_id } = response.data; + const response = await axios(`${process.env.BASE_URL}/API/Game/${id}?t=${process.env.ARBITER_TOKEN}`).catch((_) => reject(_)) + const { server_token, server_port, server_owner_id } = response.data - this.id = server_token; + this.id = server_token - const started = await this.Start(); - if (!started) throw new Error("RCCService failed to start"); - if (!this.client) await this.CreateClient(); + const running = this.started + if (!running) { + const started = await this.Start() + if (!started) throw new Error("RCCService failed to start") + } - logger.info(`[${this.id}] Place RenderJob started for ${id}`); + if (!this.client) await this.CreateClient() + + logger.info(`[${this.id}] Place RenderJob started for ${id}`) const result = await this.OpenJobEx({ name: this.id, @@ -138,13 +161,13 @@ class RenderJob extends Job { { type: "LUA_TNUMBER", value: id }, ], }, - }).catch((e) => false); + }).catch((e) => false) - logger.info(`[${this.id}] Place RenderJob finished for ${id}`); + logger.info(`[${this.id}] Place RenderJob finished for ${id}`) - if (!result) return false; - return result[0]?.OpenJobExResult?.LuaValue[0]?.value; + if (!result) return false + return result[0]?.OpenJobExResult?.LuaValue[0]?.value } } -module.exports = RenderJob; +module.exports = RenderJob diff --git a/src/routes/render/user.js b/src/routes/render/user.js index 0933628..76f7376 100644 --- a/src/routes/render/user.js +++ b/src/routes/render/user.js @@ -1,39 +1,39 @@ -const express = require("express"); -const app = express.Router(); +const express = require("express") +const app = express.Router() -const RenderJob = require("../../lib/classes/RenderJob.js"); +const RenderJob = require("../../lib/classes/RenderJob.js") app.get("/:id", async (request, response) => { - const { params, query } = request; - const job = new RenderJob(); - let body = {}; + const { params, query } = request + const job = new RenderJob() + let body = {} - const headshot = await job.RenderHeadshot(params.id).catch((_) => _); + const headshot = await job.RenderHeadshot(params.id).catch((_) => _) if (headshot?.message) { - job.Stop(); - return response.status(500).json({ error: headshot.message }); + job.Stop() + return response.status(500).json({ error: headshot.message }) } - body.headshot = headshot; + body.headshot = headshot - const bodyshot = await job.RenderBodyshot(params.id).catch((_) => _); + const bodyshot = await job.RenderBodyshot(params.id).catch((_) => _) if (bodyshot?.message) { - job.Stop(); - return response.status(500).json({ error: bodyshot.message }); + job.Stop() + return response.status(500).json({ error: bodyshot.message }) } - body.bodyshot = bodyshot; + body.bodyshot = bodyshot if (query.three_d) { - const three_d = await job.RenderBodyshot(params.id, true).catch((_) => _); + const three_d = await job.RenderBodyshot(params.id, true).catch((_) => _) if (three_d?.message) { - job.Stop(); - return response.status(500).json({ error: three_d.message }); + job.Stop() + return response.status(500).json({ error: three_d.message }) } - body.three_d = three_d; + body.three_d = three_d } - job.Stop(); + job.Stop() - return response.json(body); -}); + return response.json(body) +}) -module.exports = app; +module.exports = app