2026-04-14 17:41:39 +02:00

173 lines
6.8 KiB
JavaScript

/**
* @param {string} key
* @param {string} faction
* @param {string} type
* @returns {string}
*/
app.use("/changeFactionType.lvorex", express.json())
app.post("/changeFactionType.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, "Factions", 2)
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
if (!config.Framework.includes("qb")) return res.json({ code: 404, message: "This function only works on QBCore." })
let result = await query(`
update \`madmin_factions\`
set \`type\` = '${postBody.type}'
where \`faction\` = '${postBody.faction}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: "Changed." })
})
/**
* @param {string} key
* @param {File} file
* @param {string} faction
* @returns {string}
*/
app.use("/changeFactionLogo.lvorex", express.json())
app.post("/changeFactionLogo.lvorex", UploadFactionLogo.single("file"), 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, "Factions", 3)
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
const file = req.file
let result = await query(`
update \`madmin_factions\`
set \`image\` = 'img/FactionLogos/${file.filename}'
where \`faction\` = '${postBody.faction}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: "Done." })
})
/**
* @param {string} key
* @param {string} faction
* @param {string} link
* @returns {string}
*/
app.use("/changeFactionLogoByLink.lvorex", express.json())
app.post("/changeFactionLogoByLink.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, "Factions", 3)
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
let result = await query(`
update \`madmin_factions\`
set \`image\` = '${postBody.link}'
where \`faction\` = '${postBody.faction}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: "Changed." })
})
/**
* @param {string} key
* @param {string} faction
* @returns {string}
*/
app.use("/clearFactionLogo.lvorex", express.json())
app.post("/clearFactionLogo.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, "Factions", 3)
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
let result = await query(`
select * from \`madmin_factions\`
where \`faction\` = '${postBody.faction}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
result = result [0]
if (result.image === "img/DefaultFactionIcon.png") return res.json({ code: 404, message: "Image already clear." })
if (result.image.includes("http")) {
result = await query(`
update \`madmin_factions\`
set \`image\` = 'img/DefaultFactionIcon.png'
where \`faction\` = '${postBody.faction}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
res.json({ code: 200, message: "Cleared." })
return
}
const fileName = result.image.split("FactionLogos/")[1]
result = await query(`
update \`madmin_factions\`
set \`image\` = 'img/DefaultFactionIcon.png'
where \`faction\` = '${postBody.faction}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
const filePath = path.join(rootDir, `Web/Panel/Factions/img/FactionLogos/${fileName}`)
await fs.unlink(filePath)
res.json({ code: 200, message: "Cleared." })
})
/**
* @param {string} key
* @param {string} pIdentifier
* @param {string} faction
* @param {string} factionLevel
* @returns {string}
*/
app.use("/changePlayerFaction.lvorex", express.json())
app.post("/changePlayerFaction.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, "Factions", 4)
if (!PermissionCheck) return res.json({ code: 401, message: "Your rank is not enough." })
if (!config.Framework.includes("qb")) return res.json({ code: 404, message: "This function only works on QBCore." })
if (!isNaN(Number(postBody.pIdentifier))) {
const NeededPlayer = FrameworkObject.Functions.GetPlayer(postBody.pIdentifier)
if (NeededPlayer) {
NeededPlayer.Functions.SetGang(postBody.faction, postBody.factionLevel)
res.json({ code: 200, message: "Changed." })
return
} else return res.json({ code: 404, message: "Try it again." })
} else {
// {"label":"No Gang Affiliaton","name":"none","grade":{"level":0,"name":"none"},"isboss":false}
const factionCredentials = FrameworkObject.Shared.Gangs[postBody.faction]
const gradeCredentials = factionCredentials.grades[String(postBody.factionLevel)]
const toInsertFactionJson = {
label: factionCredentials.label,
name: postBody.faction,
grade: { level: Number(postBody.factionLevel), name: gradeCredentials.name },
isboss: factionCredentials.isboss ? factionCredentials.isboss : false
}
let result = await query(`
update \`players\`
set \`gang\` = '${JSON.stringify(toInsertFactionJson)}'
where \`citizenid\` = '${postBody.pIdentifier}'
`)
if (result === false) return res.json({ code: 404, message: "SQL Error Appeared." })
}
res.json({ code: 200, message: "Changed." })
})