195 lines
6.7 KiB
JavaScript
195 lines
6.7 KiB
JavaScript
const CreateBlips = async (src, first) => {
|
|
await cooldownWait(1000)
|
|
let tried = 0
|
|
while (!FrameworkObject) {
|
|
tried++
|
|
if (tried === 20) break
|
|
await cooldownWait(1000)
|
|
continue
|
|
}
|
|
if (tried === 20) return console.log("^1Blip timeout.^7")
|
|
|
|
let result = await query(`
|
|
select * from \`madmin_blips\`
|
|
`)
|
|
if (result === false) return
|
|
if (result.length === 0) return
|
|
|
|
for await (const blip of result) {
|
|
const coords = JSON.parse(blip.coords)
|
|
emitNet("mAdmin:createWantedBlip", src, blip.name, blip.sprite, JSON.stringify(coords), blip.id, blip.color, blip.scale)
|
|
}
|
|
|
|
if (first) {
|
|
console.log("^2Blips Set.^7")
|
|
}
|
|
}
|
|
|
|
on('onResourceStart', async (resourceName) => {
|
|
if (GetCurrentResourceName() !== resourceName) return
|
|
await CreateBlips(-1, true)
|
|
})
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @returns {Array}
|
|
*/
|
|
|
|
app.use("/getScriptBlips.lvorex", express.json())
|
|
app.post("/getScriptBlips.lvorex", async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "LiveMap", 0)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
|
|
let result = await query(`
|
|
select * from \`madmin_blips\`
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
if (result.length === 0) return res.json({ code: 200, message: [] })
|
|
|
|
const blips = []
|
|
result.forEach(blip => {
|
|
blips.push({
|
|
id: blip.id,
|
|
name: blip.name,
|
|
coords: JSON.parse(blip.coords),
|
|
sprite: blip.sprite,
|
|
color: blip.color,
|
|
scale: blip.scale
|
|
})
|
|
})
|
|
|
|
res.json({ code: 200, message: blips })
|
|
})
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {string} name
|
|
* @param {number} sprite
|
|
* @param {string} coords
|
|
* @returns {string}
|
|
*/
|
|
|
|
app.use("/createScriptBlip.lvorex", express.json())
|
|
app.post("/createScriptBlip.lvorex", async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "LiveMap", 1)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
|
|
const coords = postBody.coords.split(" ")
|
|
if (coords.some(coord => String(coord).includes(","))) return res.json({ code: 404, message: "Commas not allowed in coords." })
|
|
|
|
let result = await query(`
|
|
select * from \`madmin_blips\`
|
|
where \`coords\` = '${JSON.stringify(coords)}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
if (result.length !== 0) return res.json({ code: 404, message: "There is a blip at the same coords." })
|
|
|
|
result = await query(`
|
|
insert into \`madmin_blips\` (
|
|
\`name\`,
|
|
\`coords\`,
|
|
\`sprite\`,
|
|
\`color\`,
|
|
\`scale\`
|
|
) values (
|
|
'${postBody.name.replaceAll("'", "\\'")}',
|
|
'${JSON.stringify(coords)}',
|
|
${postBody.sprite},
|
|
${postBody.color},
|
|
'${postBody.scale}'
|
|
)
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
result = await query(`
|
|
select * from \`madmin_blips\`
|
|
where \`coords\` = '${JSON.stringify(coords)}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
result = result[0]
|
|
|
|
emitNet("mAdmin:createWantedBlip", -1, postBody.name, postBody.sprite, JSON.stringify(coords), result.id, postBody.color, postBody.scale)
|
|
|
|
res.json({ code: 200, message: { blip: result.id } })
|
|
})
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {number} id
|
|
* @returns {string}
|
|
*/
|
|
|
|
app.use("/deleteScriptBlip.lvorex", express.json())
|
|
app.post("/deleteScriptBlip.lvorex", async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "LiveMap", 1)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
|
|
let result = await query(`
|
|
delete from \`madmin_blips\`
|
|
where \`id\` = ${postBody.id}
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
|
|
emitNet("mAdmin:deleteBlipInClient", -1, postBody.id)
|
|
res.json({ code: 200, message: "Blip deleted." })
|
|
})
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @param {number} id
|
|
* @param {string} name
|
|
* @param {number} sprite
|
|
* @param {string} coords
|
|
* @returns {string}
|
|
*/
|
|
|
|
app.use("/editScriptBlip.lvorex", express.json())
|
|
app.post("/editScriptBlip.lvorex", async (req, res) => {
|
|
const postBody = req.body
|
|
const { keyFound, userKey } = await controlKey(req, postBody.key)
|
|
|
|
if (keyFound === false) return res.json({ code: 404, message: "Not authorized." })
|
|
const PermissionCheck = await checkPermission(userKey.rank, "LiveMap", 1)
|
|
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
|
|
const coords = postBody.coords.split(" ")
|
|
|
|
let result = await query(`
|
|
select * from \`madmin_blips\`
|
|
where \`coords\` = '${JSON.stringify(coords)}'
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
if (
|
|
result.length !== 0 &&
|
|
result[0].id !== postBody.id
|
|
) return res.json({ code: 404, message: "There is a blip at the same coords." })
|
|
|
|
result = await query(`
|
|
update \`madmin_blips\`
|
|
set \`name\` = '${postBody.name}',
|
|
\`coords\` = '${JSON.stringify(coords)}',
|
|
\`sprite\` = ${postBody.sprite}
|
|
where \`id\` = ${postBody.id}
|
|
`)
|
|
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
|
|
|
|
emitNet("mAdmin:editBlipInClient", -1, postBody.id, postBody.name, postBody.sprite, JSON.stringify(coords), postBody.color, postBody.scale)
|
|
res.json({ code: 200, message: "Blip edited." })
|
|
})
|
|
|
|
// Server Events
|
|
|
|
RegisterServerEvent("madmin:server:createblips")
|
|
on("madmin:server:createblips", async (src) => {
|
|
await CreateBlips(src, false)
|
|
}) |