From 8d88b311101772e1f816a2cff9a96563102a4c83 Mon Sep 17 00:00:00 2001 From: Vortrex <3858226+VortrexFTW@users.noreply.github.com> Date: Thu, 31 Dec 2020 13:12:34 -0600 Subject: [PATCH] Test --- scripts/server/business.js | 206 ++++++++++++++++++---------------- scripts/server/class.js | 110 +++++++++++++++--- scripts/server/house.js | 223 +++++++++++++++++++++---------------- scripts/server/job.js | 147 +++++++++++++++++++++++- scripts/server/timers.js | 1 + 5 files changed, 472 insertions(+), 215 deletions(-) diff --git a/scripts/server/business.js b/scripts/server/business.js index 7b34b6d0..14fa8ec6 100644 --- a/scripts/server/business.js +++ b/scripts/server/business.js @@ -53,7 +53,7 @@ function loadBusinessesFromDatabase() { let tempBusinessData = new serverClasses.businessData(dbAssoc); tempBusinessData.locations = loadBusinessLocationsFromDatabase(tempBusinessData.databaseId); tempBusinesses.push(tempBusinessData); - console.log(`[Asshat.Business]: Business '${tempBusinessData.name}' (ID ${tempBusinessData.databaseId}) loaded from database successfully!`); + console.log(`[Asshat.Business]: Business '${tempBusinessData.name}' (ID ${tempBusinessData.databaseId}) loaded from database successfully`); } } freeDatabaseQuery(dbQuery); @@ -68,7 +68,7 @@ function loadBusinessesFromDatabase() { // --------------------------------------------------------------------------- function loadBusinessLocationsFromDatabase(businessId) { - //console.log("[Asshat.Business]: Loading locations from database ..."); + console.log(`[Asshat.Business]: Loading locations for business '${getBusinessData(businessId).name}' from database ...`); let tempBusinessLocations = []; let dbConnection = connectToDatabase(); @@ -76,13 +76,13 @@ function loadBusinessLocationsFromDatabase(businessId) { let dbAssoc; if(dbConnection) { - dbQuery = queryDatabase(dbConnection, "SELECT * FROM `biz_loc` WHERE `biz_loc_biz` = " + toString(businessId)); + dbQuery = queryDatabase(dbConnection, `SELECT * FROM biz_loc WHERE biz_loc_biz=${businessId}`); if(dbQuery) { if(dbQuery.numRows > 0) { while(dbAssoc = fetchQueryAssoc(dbQuery)) { let tempBusinessLocationData = new serverClasses.businessLocationData(dbAssoc); tempBusinessLocations.push(tempBusinessLocationData); - //console.log(`[Asshat.Business]: Location '${tempBusinessLocationData.name}' loaded from database successfully!`); + console.log(`[Asshat.Business]: Location for business '${getBusinessData(businessId).name}' loaded from database successfully!`); } } freeDatabaseQuery(dbQuery); @@ -90,7 +90,7 @@ function loadBusinessLocationsFromDatabase(businessId) { disconnectFromDatabase(dbConnection); } - //console.log(`[Asshat.Business]: ${tempBusinessLocations.length} location for business ${businessId} loaded from database successfully!`); + console.log(`[Asshat.Business]: ${tempBusinessLocations.length} locations for business '${getBusinessData(businessId).name}' loaded from database successfully`); return tempBusinessLocations; } @@ -644,8 +644,10 @@ function saveBusinessToDatabase(businessId) { function createAllBusinessPickups() { for(let i in getServerData().businesses) { - createBusinessEntrancePickup(i); - createBusinessExitPickup(i); + for(let j in getServerData().businesses[i].locations) { + createBusinessLocationEntrancePickup(i, j); + createBusinessLocationExitPickup(i, j); + } } } @@ -653,98 +655,97 @@ function createAllBusinessPickups() { function createAllBusinessBlips() { for(let i in getServerData().businesses) { - createBusinessEntranceBlip(i); - createBusinessExitBlip(i); + for(let j in getServerData().businesses[i].locations) { + createBusinessLocationEntranceBlip(i, j); + createBusinessLocationExitBlip(i, j); + } } } // --------------------------------------------------------------------------- -function createBusinessEntrancePickup(businessId) { - if(getBusinessData(businessId).entrancePickupModel != -1) { +function createBusinessLocationEntrancePickup(businessId, locationId) { + if(getBusinessData(businessId).locations[locationId].entrancePickupModel != -1) { let pickupModelId = getGameConfig().pickupModels[getServerGame()].business; - if(getServerData().businesses[businessId].entrancePickupModel != 0) { - pickupModelId = getBusinessData(businessId).entrancePickupModel; + if(getServerData().businesses[businessId].locations[locationId].entrancePickupModel != 0) { + pickupModelId = getBusinessData(businessId).locations[locationId].entrancePickupModel; } - getBusinessData(businessId).entrancePickup = gta.createPickup(pickupModelId, getBusinessData(businessId).entrancePosition); - getBusinessData(businessId).entrancePickup.onAllDimensions = false; - getBusinessData(businessId).entrancePickup.dimension = getBusinessData(businessId).entranceDimension; - //getBusinessData(businessId).entrancePickup.interior = getBusinessData(businessId).entranceInterior; - getBusinessData(businessId).entrancePickup.setData("ag.owner.type", AG_PICKUP_BUSINESS_ENTRANCE, false); - getBusinessData(businessId).entrancePickup.setData("ag.owner.id", businessId, false); - getBusinessData(businessId).entrancePickup.setData("ag.label.type", AG_LABEL_BUSINESS, true); - getBusinessData(businessId).entrancePickup.setData("ag.label.name", getBusinessData(businessId).name, true); - getBusinessData(businessId).entrancePickup.setData("ag.label.locked", getBusinessData(businessId).locked, true); + getBusinessData(businessId).locations[locationId].entrancePickup = gta.createPickup(pickupModelId, getBusinessData(businessId).locations[locationId].entrancePosition); + getBusinessData(businessId).locations[locationId].entrancePickup.onAllDimensions = false; + getBusinessData(businessId).locations[locationId].entrancePickup.dimension = getBusinessData(businessId).locations[locationId].entranceDimension; + getBusinessData(businessId).locations[locationId].entrancePickup.setData("ag.owner.type", AG_PICKUP_BUSINESS_ENTRANCE, false); + getBusinessData(businessId).locations[locationId].entrancePickup.setData("ag.owner.id", businessId, false); + getBusinessData(businessId).locations[locationId].entrancePickup.setData("ag.label.type", AG_LABEL_BUSINESS, true); + getBusinessData(businessId).locations[locationId].entrancePickup.setData("ag.label.name", getBusinessData(businessId).name, true); + getBusinessData(businessId).locations[locationId].entrancePickup.setData("ag.label.locked", getBusinessData(businessId).locked, true); if(getBusinessData(businessId).buyPrice > 0) { - getBusinessData(businessId).entrancePickup.setData("ag.label.price", getBusinessData(businessId).buyPrice, true); + getBusinessData(businessId).locations[locationId].entrancePickup.setData("ag.label.price", getBusinessData(businessId).buyPrice, true); } - addToWorld(getBusinessData(businessId).entrancePickup); + addToWorld(getBusinessData(businessId).locations[locationId].entrancePickup); } } // --------------------------------------------------------------------------- -function createBusinessEntranceBlip(businessId) { - if(getBusinessData(businessId).entranceBlipModel != -1) { +function createBusinessLocationEntranceBlip(businessId, locationId) { + if(getBusinessData(businessId).locations[locationId].entranceBlipModel != -1) { let blipModelId = getGameConfig().blipSprites[getServerGame()].business; if(getServerData().businesses[businessId].entranceBlipModel != 0) { - blipModelId = getBusinessData(businessId).entranceBlipModel; + blipModelId = getBusinessData(businessId).locations[locationId].entranceBlipModel; } - getBusinessData(businessId).entranceBlip = gta.createBlip(getBusinessData(businessId).entrancePosition, blipModelId, 1, getColourByName("businessBlue")); - getBusinessData(businessId).entranceBlip.onAllDimensions = false; - getBusinessData(businessId).entranceBlip.dimension = getBusinessData(businessId).entranceDimension; - //getBusinessData(businessId).entranceBlip.interior = getBusinessData(businessId).entranceInterior; - getBusinessData(businessId).entranceBlip.setData("ag.owner.type", AG_BLIP_BUSINESS_ENTRANCE, false); - getBusinessData(businessId).entranceBlip.setData("ag.owner.id", businessId, false); - addToWorld(getBusinessData(businessId).entranceBlip); + getBusinessData(businessId).locations[locationId].entranceBlip = gta.createBlip(getBusinessData(businessId).locations[locationId].entrancePosition, blipModelId, 1, getColourByName("businessBlue")); + getBusinessData(businessId).locations[locationId].entranceBlip.onAllDimensions = false; + getBusinessData(businessId).locations[locationId].entranceBlip.dimension = getBusinessData(businessId).locations[locationId].entranceDimension; + getBusinessData(businessId).locations[locationId].entranceBlip.setData("ag.owner.type", AG_BLIP_BUSINESS_ENTRANCE, false); + getBusinessData(businessId).locations[locationId].entranceBlip.setData("ag.owner.id", businessId, false); + addToWorld(getBusinessData(businessId).locations[locationId].entranceBlip); } } // --------------------------------------------------------------------------- -function createBusinessExitPickup(businessId) { +function createBusinessLocationExitPickup(businessId, locationId) { if(getBusinessData(businessId).hasInterior) { - if(getBusinessData(businessId).exitPickupModel != -1) { + if(getBusinessData(businessId).locations[locationId].exitPickupModel != -1) { let pickupModelId = getGameConfig().pickupModels[getServerGame()].exit; - if(getServerData().businesses[businessId].exitPickupModel != 0) { - pickupModelId = getBusinessData(businessId).exitPickupModel; + if(getServerData().businesses[businessId].locations[locationId].exitPickupModel != 0) { + pickupModelId = getBusinessData(businessId).locations[locationId].exitPickupModel; } - getBusinessData(businessId).exitPickup = gta.createPickup(pickupModelId, getBusinessData(businessId).exitPosition); - getBusinessData(businessId).exitPickup.onAllDimensions = false; - getBusinessData(businessId).exitPickup.dimension = getBusinessData(businessId).exitDimension; - //getBusinessData(businessId).exitPickup.interior = getBusinessData(businessId).exitInterior; - getBusinessData(businessId).exitPickup.setData("ag.owner.type", AG_PICKUP_BUSINESS_EXIT, false); - getBusinessData(businessId).exitPickup.setData("ag.owner.id", businessId, false); - getBusinessData(businessId).exitPickup.setData("ag.label.type", AG_LABEL_EXIT, true); - addToWorld(getBusinessData(businessId).exitPickup); + getBusinessData(businessId).locations[locationId].exitPickup = gta.createPickup(pickupModelId, getBusinessData(businessId).locations[locationId].exitPosition); + getBusinessData(businessId).locations[locationId].exitPickup.onAllDimensions = false; + getBusinessData(businessId).locations[locationId].exitPickup.dimension = getBusinessData(businessId).locations[locationId].exitDimension; + getBusinessData(businessId).locations[locationId].exitPickup.setData("ag.owner.type", AG_PICKUP_BUSINESS_EXIT, false); + getBusinessData(businessId).locations[locationId].exitPickup.setData("ag.owner.id", businessId, false); + getBusinessData(businessId).locations[locationId].exitPickup.setData("ag.label.type", AG_LABEL_EXIT, true); + addToWorld(getBusinessData(businessId).locations[locationId].exitPickup); } } } // --------------------------------------------------------------------------- -function createBusinessExitBlip(businessId) { +function createBusinessLocationExitBlip(businessId, locationId) { if(getBusinessData(businessId).hasInterior) { - if(getBusinessData(businessId).exitBlipModel != -1) { + if(getBusinessData(businessId).locations[locationId].exitBlipModel != -1) { let blipModelId = getGameConfig().blipSprites[getServerGame()].business; - if(getServerData().businesses[businessId].exitBlipModel != 0) { - blipModelId = getBusinessData(businessId).exitBlipModel; + if(getServerData().businesses[businessId].locations[locationId].exitBlipModel != 0) { + blipModelId = getBusinessData(businessId).locations[locationId].exitBlipModel; } - getBusinessData(businessId).exitBlip = gta.createBlip(getBusinessData(businessId).exitPosition, blipModelId, 1, getColourByName("businessBlue")); - getBusinessData(businessId).exitBlip.onAllDimensions = false; - getBusinessData(businessId).exitBlip.dimension = getBusinessData(businessId).entranceDimension; + getBusinessData(businessId).locations[locationId].exitBlip = gta.createBlip(getBusinessData(businessId).locations[locationId].exitPosition, blipModelId, 1, getColourByName("businessBlue")); + getBusinessData(businessId).locations[locationId].exitBlip.onAllDimensions = false; + getBusinessData(businessId).locations[locationId].exitBlip.dimension = getBusinessData(businessId).locations[locationId].entranceDimension; //getBusinessData(businessId).exitBlip.interior = getBusinessData(businessId).exitInterior; - getBusinessData(businessId).exitBlip.setData("ag.owner.type", AG_BLIP_BUSINESS_EXIT, false); - getBusinessData(businessId).exitBlip.setData("ag.owner.id", businessId, false); - addToWorld(getBusinessData(businessId).exitBlip); + getBusinessData(businessId).locations[locationId].exitBlip.setData("ag.owner.type", AG_BLIP_BUSINESS_EXIT, false); + getBusinessData(businessId).locations[locationId].exitBlip.setData("ag.owner.id", businessId, false); + addToWorld(getBusinessData(businessId).locations[locationId].exitBlip); } } } @@ -762,14 +763,19 @@ function deleteBusiness(businessId, deletedBy = 0) { if(dbQuery) { freeDatabaseQuery(dbQuery); } + + dbQuery = queryDatabase(dbConnection, `DELETE FROM biz_loc WHERE biz_loc_biz = ${tempBusinessData.databaseId}`); + if(dbQuery) { + freeDatabaseQuery(dbQuery); + } disconnectFromDatabase(dbConnection); } - deleteBusinessEntrancePickup(businessId); - deleteBusinessExitPickup(businessId); + deleteBusinessEntrancePickups(businessId); + deleteBusinessExitPickups(businessId); - deleteBusinessEntranceBlip(businessId); - deleteBusinessExitBlip(businessId); + deleteBusinessEntranceBlips(businessId); + deleteBusinessExitBlips(businessId); removePlayersFromBusiness(businessId); @@ -849,63 +855,69 @@ function doesBusinessHaveInterior(businessId) { // --------------------------------------------------------------------------- -function sendAllBusinessLabelsToPlayer(client) { - let tempBusinessLabels = []; - let totalBusinesses = getServerData().businesses.length; - let businessesPerNetworkEvent = 100; - let totalNetworkEvents = Math.ceil(totalBusinesses/businessesPerNetworkEvent); - for(let i = 0 ; i < totalNetworkEvents ; i++) { - for(let j = 0 ; j < businessesPerNetworkEvent ; j++) { - let tempBusinessId = (i*businessesPerNetworkEvent)+j; - if(typeof getServerData().businesses[tempBusinessId] != "undefined") { - let tempBusinessLabels = []; - tempBusinessLabels.push([tempBusinessId, getServerData().businesses[tempBusinessId].entrancePosition, getGameConfig().propertyLabelHeight[getServerGame()], getServerData().businesses[tempBusinessId].description, getServerData().businesses[tempBusinessId].locked, false]); - } - } - triggerNetworkEvent("ag.bizlabel.all", client, tempBusinessLabels); - tempBusinessLabels = []; +function deleteBusinessEntrancePickups(businessId) { + for(let i in getServerData().businesses[businessId].locations) { + deleteBusinessLocationEntrancePickup(businessId, i); } } // --------------------------------------------------------------------------- -function sendBusinessLabelToPlayers(businessId) { - triggerNetworkEvent("ag.bizlabel.add", null, businessId, getServerData().businesses[businessId].entrancePosition, getGameConfig().propertyLabelHeight[getServerGame()], getServerData().businesses[businessId].name, getServerData().businesses[businessId].locked, false); -} - -// --------------------------------------------------------------------------- - -function deleteBusinessEntrancePickup(businessId) { - if(getBusinessData(businessId).entrancePickup) { - destroyElement(getBusinessData(businessId).entrancePickup); - getBusinessData(businessId).entrancePickup = false; +function deleteBusinessEntranceBlips(businessId) { + for(let i in getServerData().businesses[businessId].locations) { + deleteBusinessLocationEntranceBlip(businessId, i); } } // --------------------------------------------------------------------------- -function deleteBusinessExitPickup(businessId) { - if(getBusinessData(businessId).exitPickup) { - destroyElement(getBusinessData(businessId).exitPickup); - getBusinessData(businessId).exitPickup = false; +function deleteBusinessExitPickups(businessId) { + for(let i in getServerData().businesses[businessId].locations) { + deleteBusinessLocationExitPickup(businessId, i); + } +} + +// --------------------------------------------------------------------------- + +function deleteBusinessExitBlips(businessId) { + for(let i in getServerData().businesses[businessId].locations) { + deleteBusinessLocationExitBlip(businessId, i); + } +} + +// --------------------------------------------------------------------------- + +function deleteBusinessLocationEntrancePickup(businessId, locationId) { + if(getBusinessData(businessId).locations[locationId].entrancePickup) { + destroyElement(getBusinessData(businessId).locations[locationId].entrancePickup); + getBusinessData(businessId).locations[locationId].entrancePickup = false; + } +} + +// --------------------------------------------------------------------------- + +function deleteBusinessLocationExitPickup(businessId, locationId) { + if(getBusinessData(businessId).locations[locationId].exitPickup) { + destroyElement(getBusinessData(businessId).locations[locationId].exitPickup); + getBusinessData(businessId).locations[locationId].exitPickup = false; } } // --------------------------------------------------------------------------- -function deleteBusinessEntranceBlip(businessId) { - if(getBusinessData(businessId).entranceBlip) { - destroyElement(getBusinessData(businessId).entranceBlip); - getBusinessData(businessId).entranceBlip = false; +function deleteBusinessLocationEntranceBlip(businessId, locationId) { + if(getBusinessData(businessId).locations[locationId].entranceBlip) { + destroyElement(getBusinessData(businessId).locations[locationId].entranceBlip); + getBusinessData(businessId).locations[locationId].entranceBlip = false; } } // --------------------------------------------------------------------------- -function deleteBusinessExitBlip(businessId) { - if(getBusinessData(businessId).exitBlip) { - destroyElement(getBusinessData(businessId).exitBlip); - getBusinessData(businessId).exitBlip = false; +function deleteBusinessLocationExitBlip(businessId, locationId) { + if(getBusinessData(businessId).locations[locationId].exitBlip) { + destroyElement(getBusinessData(businessId).locations[locationId].exitBlip); + getBusinessData(businessId).locations[locationId].exitBlip = false; } } diff --git a/scripts/server/class.js b/scripts/server/class.js index 2ca2342c..72698fd5 100644 --- a/scripts/server/class.js +++ b/scripts/server/class.js @@ -294,22 +294,101 @@ function initClassTable() { } }, businessLocationData: class { - constructor(businessLocationAssoc) { - if(!businessLocationAssoc) { - return; + constructor(dbAssoc) { + this.databaseId = 0; + this.name = "Unnamed"; + this.business = 0; + this.enabled = false; + + this.entrancePosition = false; + this.entranceRotation = 0.0; + this.entranceInterior = 0; + this.entranceDimension = 0; + this.entrancePickupModel = -1; + this.entranceBlipModel = -1; + this.entrancePickup = null; + this.entranceBlip = null; + + this.exitPosition = false; + this.exitRotation = 0.0; + this.exitInterior = 0; + this.exitDimension = -1; + this.exitPickupModel = -1; + this.exitBlipModel = -1; + this.exitPickup = null; + this.exitBlip = null; + + if(dbAssoc) { + this.databaseId = toInteger(dbAssoc("biz_loc_id")); + this.name = toString(dbAssoc("biz_loc_name")); + this.type = toInteger(dbAssoc("biz_loc_type")); + this.business = toInteger(dbAssoc("biz_loc_biz")); + this.enabled = intToBool(toInteger(dbAssoc("biz_loc_enabled"))); + + this.entrancePosition = toVector3(toFloat(dbAssoc["biz_loc_entrance_pos_x"]), toFloat(dbAssoc["biz_loc_entrance_pos_y"]), toFloat(dbAssoc["biz_loc_entrance_pos_z"])); + this.entranceRotation = toFloat(dbAssoc["biz_loc_entrance_rot_z"]); + this.entranceInterior = toInteger(dbAssoc["biz_loc_entrance_int"]); + this.entranceDimension = toInteger(dbAssoc["biz_loc_entrance_vw"]); + this.entrancePickupModel = toInteger(dbAssoc["biz_loc_entrance_pickup"]); + this.entranceBlipModel = toInteger(dbAssoc["biz_loc_entrance_blip"]); + + this.exitPosition = toVector3(toFloat(dbAssoc["biz_loc_exit_pos_x"]), toFloat(dbAssoc["biz_loc_exit_pos_y"]), toFloat(dbAssoc["biz_loc_exit_pos_z"])); + this.exitRotation = toFloat(dbAssoc["biz_loc_exit_rot_z"]); + this.exitInterior = toInteger(dbAssoc["biz_loc_exit_int"]); + this.exitDimension = toInteger(dbAssoc["biz_loc_exit_vw"]); + this.exitPickupModel = toInteger(dbAssoc["biz_loc_exit_pickup"]); + this.exitBlipModel = toInteger(dbAssoc["biz_loc_exit_blip"]); } - - this.databaseId = toInteger(businessLocationAssoc("biz_loc_id")); - this.name = toString(businessLocationAssoc("biz_loc_name")); - this.type = toInteger(businessLocationAssoc("biz_loc_type")); - this.business = toInteger(businessLocationAssoc("biz_loc_biz")); - this.enabled = intToBool(toInteger(businessLocationAssoc("biz_loc_enabled"))); - - this.position = toVector3(toFloat(businessLocationAssoc("biz_loc_pos_x")), toFloat(businessLocationAssoc("biz_loc_pos_y")), toFloat(businessLocationAssoc("biz_loc_pos_z"))); - this.interior = toInteger(businessLocationAssoc["biz_loc_int"]); - this.dimension = toInteger(businessLocationAssoc["biz_loc_vw"]); } }, + houseLocationData: class { + constructor(dbAssoc) { + this.databaseId = 0; + this.name = "Unnamed"; + this.house = 0; + this.enabled = false; + + this.entrancePosition = false; + this.entranceRotation = 0.0; + this.entranceInterior = 0; + this.entranceDimension = 0; + this.entrancePickupModel = -1; + this.entranceBlipModel = -1; + this.entrancePickup = null; + this.entranceBlip = null; + + this.exitPosition = false; + this.exitRotation = 0.0; + this.exitInterior = 0; + this.exitDimension = -1; + this.exitPickupModel = -1; + this.exitBlipModel = -1; + this.exitPickup = null; + this.exitBlip = null; + + if(dbAssoc) { + this.databaseId = toInteger(dbAssoc("house_loc_id")); + this.name = toString(dbAssoc("house_loc_name")); + this.type = toInteger(dbAssoc("house_loc_type")); + this.business = toInteger(dbAssoc("house_loc_biz")); + this.enabled = intToBool(toInteger(dbAssoc("house_loc_enabled"))); + + this.entrancePosition = toVector3(toFloat(dbAssoc["house_loc_entrance_pos_x"]), toFloat(dbAssoc["house_loc_entrance_pos_y"]), toFloat(dbAssoc["house_loc_entrance_pos_z"])); + this.entranceRotation = toFloat(dbAssoc["house_loc_entrance_rot_z"]); + this.entranceInterior = toInteger(dbAssoc["house_loc_entrance_int"]); + this.entranceDimension = toInteger(dbAssoc["house_loc_entrance_vw"]); + this.entrancePickupModel = toInteger(dbAssoc["house_loc_entrance_pickup"]); + this.entranceBlipModel = toInteger(dbAssoc["house_loc_entrance_blip"]); + + this.exitPosition = toVector3(toFloat(dbAssoc["house_loc_exit_pos_x"]), toFloat(dbAssoc["house_loc_exit_pos_y"]), toFloat(dbAssoc["house_loc_exit_pos_z"])); + this.exitRotation = toFloat(dbAssoc["house_loc_exit_rot_z"]); + this.exitInterior = toInteger(dbAssoc["house_loc_exit_int"]); + this.exitDimension = toInteger(dbAssoc["house_loc_exit_vw"]); + this.exitPickupModel = toInteger(dbAssoc["house_loc_exit_pickup"]); + this.exitBlipModel = toInteger(dbAssoc["house_loc_exit_blip"]); + } + } + }, houseData: class { constructor(houseAssoc) { this.databaseId = 0 @@ -353,8 +432,6 @@ function initClassTable() { this.entranceDimension = toInteger(houseAssoc["house_entrance_vw"]); this.entrancePickupModel = toInteger(houseAssoc["house_entrance_pickup"]); this.entranceBlipModel = toInteger(houseAssoc["house_entrance_blip"]); - this.entrancePickup = null; - this.entranceBlip = null; this.exitPosition = toVector3(toFloat(houseAssoc["house_exit_pos_x"]), toFloat(houseAssoc["house_exit_pos_y"]), toFloat(houseAssoc["house_exit_pos_z"])); this.exitRotation = toFloat(houseAssoc["house_exit_rot_z"]); @@ -362,8 +439,6 @@ function initClassTable() { this.exitDimension = toInteger(houseAssoc["house_exit_vw"]); this.exitPickupModel = toInteger(houseAssoc["house_exit_pickup"]); this.exitBlipModel = toInteger(houseAssoc["house_exit_blip"]); - this.exitPickup = null; - this.exitBlip = null; } } }, @@ -524,6 +599,7 @@ function initClassTable() { this.blipModel = jobAssoc["job_blip"]; this.pickupModel = jobAssoc["job_pickup"]; this.colour = toColour(jobAssoc["job_colour_r"], jobAssoc["job_colour_g"], jobAssoc["job_colour_b"], 255); + this.colourArray = [jobAssoc["job_colour_r"], jobAssoc["job_colour_g"], jobAssoc["job_colour_b"]]; this.whiteListEnabled = jobAssoc["job_whitelist"]; this.blackListEnabled = jobAssoc["job_blacklist"]; diff --git a/scripts/server/house.js b/scripts/server/house.js index 806bf4c3..19e6ac23 100644 --- a/scripts/server/house.js +++ b/scripts/server/house.js @@ -31,6 +31,7 @@ function loadHousesFromDatabase() { if(dbQuery.numRows > 0) { while(dbAssoc = fetchQueryAssoc(dbQuery)) { let tempHouseData = new serverClasses.houseData(dbAssoc); + tempHouseData.locations = loadHouseLocationsFromDatabase(tempHouseData.databaseId); tempHouses.push(tempHouseData); console.log(`[Asshat.House]: House '${tempHouseData.description}' (ID ${tempHouseData.databaseId}) loaded!`); } @@ -45,6 +46,35 @@ function loadHousesFromDatabase() { // --------------------------------------------------------------------------- +function loadHouseLocationsFromDatabase(houseId) { + console.log(`[Asshat.House]: Loading locations for business '${getBusinessData(businessId).name}' from database ...`); + + let tempHouseLocations = []; + let dbConnection = connectToDatabase(); + let dbQuery = null; + let dbAssoc; + + if(dbConnection) { + dbQuery = queryDatabase(dbConnection, `SELECT * FROM house_loc WHERE house_loc_house=${houseId}`); + if(dbQuery) { + if(dbQuery.numRows > 0) { + while(dbAssoc = fetchQueryAssoc(dbQuery)) { + let tempHouseLocationData = new serverClasses.houseLocationData(dbAssoc); + tempHouseLocations.push(tempHouseLocationData); + console.log(`[Asshat.House]: Location for house '${getHouseData(houseId).name}' loaded from database successfully!`); + } + } + freeDatabaseQuery(dbQuery); + } + disconnectFromDatabase(dbConnection); + } + + console.log(`[Asshat.House]: ${tempHouseLocations.length} locations for house '${getHouseData(houseId).name}' loaded from database successfully`); + return tempHouseLocations; +} + +// --------------------------------------------------------------------------- + function createHouseCommand(command, params, client) { let tempHouseData = createHouse(params, getPlayerPosition(client), toVector3(0.0, 0.0, 0.0), getGameConfig().pickupModels[getServerGame()].house, getGameConfig().blipSprites[getServerGame()].house, getPlayerInterior(client), getPlayerVirtualWorld(client)); getServerData().houses.push(tempHouseData); @@ -378,10 +408,36 @@ function saveHouseToDatabase(houseId) { // --------------------------------------------------------------------------- +function saveHouseLocationToDatabase(houseId, locationId) { + let tempHouseLocationData = getHouseData(houseId).locations[locationId]; + + console.log(`[Asshat.House]: Saving house location ${tempHouseLocationData.databaseId} to database ...`); + let dbConnection = connectToDatabase(); + if(dbConnection) { + if(tempHouseLocationData.databaseId == 0) { + let dbQueryString = `INSERT INTO house_loc (house_loc_house, house_loc_locked, house_loc_entrance_pos_x, house_loc_entrance_pos_y, house_loc_entrance_pos_z, house_loc_entrance_rot_z, house_loc_entrance_int, house_loc_entrance_vw, house_loc_exit_pos_x, house_loc_exit_pos_y, house_loc_exit_pos_z, house_loc_exit_rot_z, house_loc_exit_int, house_loc_exit_vw) VALUES (${tempHouseLocationData.databaseId}, ${boolToInt(tempHouseLocationData.locked)}, ${tempHouseLocationData.entrancePosition.x}, ${tempHouseLocationData.entrancePosition.y}, ${tempHouseLocationData.entrancePosition.z}, ${tempHouseLocationData.entranceRotation}, ${tempHouseLocationData.entranceInterior}, ${tempHouseLocationData.entranceDimension}, ${tempHouseLocationData.exitPosition.x}, ${tempHouseLocationData.exitPosition.y}, ${tempHouseLocationData.exitPosition.z}, ${tempHouseLocationData.exitRotation}, ${tempHouseLocationData.exitInterior}, ${tempHouseLocationData.exitDimension})`; + queryDatabase(dbConnection, dbQueryString); + tempHouseLocationData.databaseId = getDatabaseInsertId(dbConnection); + } else { + let dbQueryString = `UPDATE house_loc SET house_loc_house=${getHouseData(houseId).databaseId}', house_loc_locked=${tempHouseLocationData.locked}, house_loc_entrance_pos_x=${tempHouseLocationData.entrancePosition.x}, house_loc_entrance_pos_y=${tempHouseLocationData.entrancePosition.y}, house_loc_entrance_pos_z=${tempHouseLocationData.entrancePosition.z}, house_loc_entrance_rot_z=${tempHouseLocationData.entranceRotation}, house_loc_entrance_int=${tempHouseLocationData.entranceInterior}, house_loc_entrance_vw=${tempHouseLocationData.entranceDimension}, house_loc_exit_pos_x=${tempHouseLocationData.exitPosition.x}, house_loc_exit_pos_y=${tempHouseLocationData.exitPosition.y}, house_loc_exit_pos_z=${tempHouseLocationData.exitPosition.z}, house_loc_exit_rot_z=${tempHouseData.exitRotation}, house_loc_exit_int=${tempHouseLocationData.exitInterior}, house_loc_exit_vw=${tempHouseLocationData.exitDimension} WHERE house_loc_id=${tempHouseLocationData.databaseId}`; + queryDatabase(dbConnection, dbQueryString); + } + disconnectFromDatabase(dbConnection); + return true; + } + console.log(`[Asshat.house]: Saved house location ${tempHouseLocationData.databaseId} to database!`); + + return false; +} + +// --------------------------------------------------------------------------- + function createAllHousePickups() { for(let i in getServerData().houses) { - createHouseEntrancePickup(i); - createHouseExitPickup(i); + for(let j in getServerData().houses[i].locations) { + createHouseLocationEntrancePickup(i, j); + createHouseLocationExitPickup(i, j); + } } } @@ -389,98 +445,96 @@ function createAllHousePickups() { function createAllHouseBlips() { for(let i in getServerData().houses) { - createHouseEntranceBlip(i); - createHouseExitBlip(i); + for(let j in getServerData().houses[i].locations) { + createHouseLocationEntranceBlip(i, j); + createHouseLocationExitBlip(i, j); + } } } // --------------------------------------------------------------------------- -function createHouseEntrancePickup(houseId) { - if(getHouseData(houseId).entrancePickupModel != -1) { +function createHouseEntrancePickup(houseId, locationId) { + if(getHouseData(houseId).locations[locationId].entrancePickupModel != -1) { let pickupModelId = getGameConfig().pickupModels[getServerGame()].house; if(getServerData().houses[houseId].entrancePickupModel != 0) { - pickupModelId = getHouseData(houseId).entrancePickupModel; + pickupModelId = getHouseData(houseId).locations[locationId].entrancePickupModel; } - getHouseData(houseId).entrancePickup = gta.createPickup(pickupModelId, getHouseData(houseId).entrancePosition); - getHouseData(houseId).entrancePickup.onAllDimensions = false; - getHouseData(houseId).entrancePickup.dimension = getHouseData(houseId).entranceDimension; - //getHouseData(houseId).entrancePickup.interior = getHouseData(houseId).entranceInterior; - getHouseData(houseId).entrancePickup.setData("ag.owner.type", AG_PICKUP_HOUSE_ENTRANCE, false); - getHouseData(houseId).entrancePickup.setData("ag.owner.id", houseId, false); - getHouseData(houseId).entrancePickup.setData("ag.label.type", AG_LABEL_HOUSE, true); - getHouseData(houseId).entrancePickup.setData("ag.label.name", getHouseData(houseId).description, true); - getHouseData(houseId).entrancePickup.setData("ag.label.locked", getHouseData(houseId).locked, true); + getHouseData(houseId).locations[locationId].entrancePickup = gta.createPickup(pickupModelId, getHouseData(houseId).locations[locationId].entrancePosition); + getHouseData(houseId).locations[locationId].entrancePickup.onAllDimensions = false; + getHouseData(houseId).locations[locationId].entrancePickup.dimension = getHouseData(houseId).locations[locationId].entranceDimension; + getHouseData(houseId).locations[locationId].entrancePickup.setData("ag.owner.type", AG_PICKUP_HOUSE_ENTRANCE, false); + getHouseData(houseId).locations[locationId].entrancePickup.setData("ag.owner.id", houseId, false); + getHouseData(houseId).locations[locationId].entrancePickup.setData("ag.label.type", AG_LABEL_HOUSE, true); + getHouseData(houseId).locations[locationId].entrancePickup.setData("ag.label.name", getHouseData(houseId).description, true); + getHouseData(houseId).locations[locationId].entrancePickup.setData("ag.label.locked", getHouseData(houseId).locked, true); if(getHouseData(houseId).buyPrice > 0) { - getHouseData(houseId).entrancePickup.setData("ag.label.price", getHouseData(houseId).buyPrice, true); + getHouseData(houseId).locations[locationId].entrancePickup.setData("ag.label.price", getHouseData(houseId).buyPrice, true); } - addToWorld(getHouseData(houseId).entrancePickup); + addToWorld(getHouseData(houseId).locations[locationId].entrancePickup); } } // --------------------------------------------------------------------------- -function createHouseEntranceBlip(houseId) { - if(getHouseData(houseId).entranceBlipModel != -1) { +function createHouseEntranceBlip(houseId, locationId) { + if(getHouseData(houseId).locations[locationId].entranceBlipModel != -1) { let blipModelId = getGameConfig().blipSprites[getServerGame()].house; - if(getServerData().houses[houseId].entranceBlipModel != 0) { - blipModelId = getHouseData(houseId).entranceBlipModel; + if(getServerData().houses[houseId].locations[locationId].entranceBlipModel != 0) { + blipModelId = getHouseData(houseId).locations[locationId].entranceBlipModel; } - getHouseData(houseId).entranceBlip = gta.createBlip(getHouseData(houseId).entrancePosition, blipModelId, 1, getColourByName("houseGreen")); - getHouseData(houseId).entranceBlip.onAllDimensions = false; - getHouseData(houseId).entranceBlip.dimension = getHouseData(houseId).entranceDimension; - //getHouseData(houseId).entranceBlip.interior = getHouseData(houseId).entranceInterior; - getHouseData(houseId).entranceBlip.setData("ag.owner.type", AG_BLIP_HOUSE_ENTRANCE, false); - getHouseData(houseId).entranceBlip.setData("ag.owner.id", houseId, false); - addToWorld(getHouseData(houseId).entranceBlip); + getHouseData(houseId).locations[locationId].entranceBlip = gta.createBlip(getHouseData(houseId).locations[locationId].entrancePosition, blipModelId, 1, getColourByName("houseGreen")); + getHouseData(houseId).locations[locationId].entranceBlip.onAllDimensions = false; + getHouseData(houseId).locations[locationId].entranceBlip.dimension = getHouseData(houseId).locations[locationId].entranceDimension; + getHouseData(houseId).locations[locationId].entranceBlip.setData("ag.owner.type", AG_BLIP_HOUSE_ENTRANCE, false); + getHouseData(houseId).locations[locationId].entranceBlip.setData("ag.owner.id", houseId, false); + addToWorld(getHouseData(houseId).locations[locationId].entranceBlip); } } // --------------------------------------------------------------------------- -function createHouseExitPickup(houseId) { +function createHouseExitPickup(houseId, locationId) { if(getHouseData(houseId).hasInterior) { - if(getHouseData(houseId).exitPickupModel != -1) { + if(getHouseData(houseId).locations[locationId].exitPickupModel != -1) { let pickupModelId = getGameConfig().pickupModels[getServerGame()].exit; - if(getServerData().houses[houseId].exitPickupModel != 0) { - pickupModelId = getHouseData(houseId).exitPickupModel; + if(getServerData().houses[houseId].locations[locationId].exitPickupModel != 0) { + pickupModelId = getHouseData(houseId).locations[locationId].exitPickupModel; } - getHouseData(houseId).exitPickup = gta.createPickup(pickupModelId, getHouseData(houseId).exitPosition); - getHouseData(houseId).exitPickup.onAllDimensions = false; - getHouseData(houseId).exitPickup.dimension = getHouseData(houseId).exitDimension; - //getHouseData(houseId).exitPickup.interior = getHouseData(houseId).exitInterior; - getHouseData(houseId).exitPickup.setData("ag.owner.type", AG_PICKUP_HOUSE_EXIT, false); - getHouseData(houseId).exitPickup.setData("ag.owner.id", houseId, false); - getHouseData(houseId).exitPickup.setData("ag.label.type", AG_LABEL_EXIT, true); - addToWorld(getHouseData(houseId).exitPickup); + getHouseData(houseId).locations[locationId].exitPickup = gta.createPickup(pickupModelId, getHouseData(houseId).locations[locationId].exitPosition); + getHouseData(houseId).locations[locationId].exitPickup.onAllDimensions = false; + getHouseData(houseId).locations[locationId].exitPickup.dimension = getHouseData(houseId).locations[locationId].exitDimension; + getHouseData(houseId).locations[locationId].exitPickup.setData("ag.owner.type", AG_PICKUP_HOUSE_EXIT, false); + getHouseData(houseId).locations[locationId].exitPickup.setData("ag.owner.id", houseId, false); + getHouseData(houseId).locations[locationId].exitPickup.setData("ag.label.type", AG_LABEL_EXIT, true); + addToWorld(getHouseData(houseId).locations[locationId].exitPickup); } } } // --------------------------------------------------------------------------- -function createHouseExitBlip(houseId) { +function createHouseExitBlip(houseId, locationId) { if(getHouseData(houseId).hasInterior) { - if(getHouseData(houseId).exitBlipModel != -1) { - let blipModelId = getGameConfig().blipSprites[getServerGame()].house; + if(getHouseData(houseId).locations[locationId].exitBlipModel != -1) { + let blipModelId = getGameConfig().locations[locationId].blipSprites[getServerGame()].house; if(getServerData().houses[houseId].exitBlipModel != 0) { - blipModelId = getHouseData(houseId).exitBlipModel; + blipModelId = getHouseData(houseId).locations[locationId].exitBlipModel; } - getHouseData(houseId).exitBlip = gta.createBlip(getHouseData(houseId).exitPosition, blipModelId, 1, getColourByName("houseGreen")); - getHouseData(houseId).exitBlip.onAllDimensions = false; - getHouseData(houseId).exitBlip.dimension = getHouseData(houseId).entranceDimension; - //getHouseData(houseId).exitBlip.interior = getHouseData(houseId).exitInterior; - getHouseData(houseId).exitBlip.setData("ag.owner.type", AG_BLIP_HOUSE_EXIT, false); - getHouseData(houseId).exitBlip.setData("ag.owner.id", houseId, false); - addToWorld(getHouseData(houseId).exitBlip); + getHouseData(houseId).locations[locationId].exitBlip = gta.createBlip(getHouseData(houseId).locations[locationId].exitPosition, blipModelId, 1, getColourByName("houseGreen")); + getHouseData(houseId).locations[locationId].exitBlip.onAllDimensions = false; + getHouseData(houseId).locations[locationId].exitBlip.dimension = getHouseData(houseId).locations[locationId].entranceDimension; + getHouseData(houseId).locations[locationId].exitBlip.setData("ag.owner.type", AG_BLIP_HOUSE_EXIT, false); + getHouseData(houseId).locations[locationId].exitBlip.setData("ag.owner.id", houseId, false); + addToWorld(getHouseData(houseId).locations[locationId].exitBlip); } } } @@ -572,62 +626,37 @@ function doesHouseHaveInterior(houseId) { // --------------------------------------------------------------------------- -function sendAllHouseLabelsToPlayer(client) { - let tempHouseLabels = []; - let totalHouses = getServerData().houses.length; - let housesPerNetworkEvent = 100; - let totalNetworkEvents = Math.ceil(totalHouses/housesPerNetworkEvent); - for(let i = 0 ; i < totalNetworkEvents ; i++) { - for(let j = 0 ; j < housesPerNetworkEvent ; j++) { - let tempHouseId = (i*housesPerNetworkEvent)+j; - if(typeof getServerData().houses[tempHouseId] != "undefined") { - tempHouseLabels.push([tempHouseId, getServerData().houses[tempHouseId].entrancePosition, getGameConfig().propertyLabelHeight[getServerGame()], getServerData().houses[tempHouseId].description, getServerData().houses[tempHouseId].locked, false]); - } - } - triggerNetworkEvent("ag.houselabel.all", client, tempHouseLabels); - tempHouseLabels = []; +function deleteHouseLocationEntrancePickup(houseId) { + if(getHouseData(houseId).locations[locationId].entrancePickup != null) { + destroyElement(getHouseData(houseId).locations[locationId].entrancePickup); + getHouseData(houseId).locations[locationId].entrancePickup = null; } } // --------------------------------------------------------------------------- -function sendHouseLabelToPlayers(houseId) { - triggerNetworkEvent("ag.houselabel.add", null, houseId, getServerData().houses[houseId].entrancePosition, getGameConfig().propertyLabelHeight[getServerGame()], getServerData().houses[houseId].description, getServerData().houses[houseId].locked, false); -} - -// --------------------------------------------------------------------------- - -function deleteHouseEntrancePickup(houseId) { - if(getHouseData(houseId).entrancePickup != null) { - destroyElement(getHouseData(houseId).entrancePickup); - getHouseData(houseId).entrancePickup = null; - } -} - -// --------------------------------------------------------------------------- - -function deleteHouseExitPickup(houseId) { - if(getHouseData(houseId).exitPickup != null) { - destroyElement(getHouseData(houseId).exitPickup); - getHouseData(houseId).exitPickup = null; +function deleteHouseLocationExitPickup(houseId, locationId) { + if(getHouseData(houseId).locations[locationId].exitPickup != null) { + destroyElement(getHouseData(houseId).locations[locationId].exitPickup); + getHouseData(houseId).locations[locationId].exitPickup = null; } } // --------------------------------------------------------------------------- -function deleteHouseEntranceBlip(houseId) { - if(getHouseData(houseId).entranceBlip != null) { - destroyElement(getHouseData(houseId).entranceBlip); - getHouseData(houseId).entranceBlip = null; +function deleteHouseLocationEntranceBlip(houseId, locationId) { + if(getHouseData(houseId).locations[locationId].entranceBlip != null) { + destroyElement(getHouseData(houseId).locations[locationId].entranceBlip); + getHouseData(houseId).locations[locationId].entranceBlip = null; } } // --------------------------------------------------------------------------- -function deleteHouseExitBlip(houseId) { - if(getHouseData(houseId).exitBlip != null) { - destroyElement(getHouseData(houseId).exitBlip); - getHouseData(houseId).exitBlip = null; +function deleteHouseLocationExitBlip(houseId, locationId) { + if(getHouseData(houseId).locations[locationId].exitBlip != null) { + destroyElement(getHouseData(houseId).locations[locationId].exitBlip); + getHouseData(houseId).locations[locationId].exitBlip = null; } } @@ -642,10 +671,10 @@ function reloadAllHousesCommand(command, params, client) { } for(let i in getServerData().houses) { - deleteHouseExitBlip(i); - deleteHouseEntranceBlip(i); - deleteHouseExitPickup(i); - deleteHouseEntrancePickup(i); + deleteHouseExitBlips(i); + deleteHouseEntranceBlips(i); + deleteHouseExitPickups(i); + deleteHouseEntrancePickups(i); } getServerData().houses = null; diff --git a/scripts/server/job.js b/scripts/server/job.js index 31327723..21d2af9d 100644 --- a/scripts/server/job.js +++ b/scripts/server/job.js @@ -1243,9 +1243,38 @@ function createJobLocation(job, position, interior, dimension) { // --------------------------------------------------------------------------- +function saveJobToDatabase(jobData) { + if(jobData == null) { + // Invalid job data + return false; + } + + console.log(`[Asshat.Job]: Saving job ${jobData.name} to database ...`); + let dbConnection = connectToDatabase(); + if(dbConnection) { + let safeName = escapeDatabaseString(dbConnection, jobData.name); + // If job hasn't been added to database, ID will be 0 + if(jobData.databaseId == 0) { + let dbQueryString = `INSERT INTO job_main (job_name, job_enabled, job_pickup, job_blip, job_type, job_colour_r, job_colour_g, job_colour_b, job_whitelist, job_blacklist) VALUES ('${safeName}', ${boolToInt(jobData.enabled)}, ${jobData.pickupModel}, ${jobData.blipModel}, ${jobData.type}, ${jobData.colourArray[0]}, ${jobData.colourArray[1]}, ${jobData.colourArray[2]})`; + queryDatabase(dbConnection, dbQueryString); + jobData.databaseId = getDatabaseInsertId(dbConnection); + } else { + let dbQueryString = `UPDATE job_main SET job_name='${safeName}', job_enabled=${boolToInt(jobData.enabled)}, job_pickup=${jobData.pickupModel}, job_blip=${jobData.blipModel}, job_type=${jobData.type}, job_colour_r=${jobData.colourArray[0]}, job_colour_g=${jobData.colourArray[1]}, job_colour_b=${jobData.colourArray[2]} WHERE job_id=${jobData.databaseId}`; + queryDatabase(dbConnection, dbQueryString); + } + disconnectFromDatabase(dbConnection); + return true; + } + console.log(`[Asshat.Job]: Saved job ${jobData.name} to database!`); + + return false; +} + +// --------------------------------------------------------------------------- + function saveJobLocationToDatabase(jobLocationData) { if(jobLocationData == null) { - // Invalid vehicle data + // Invalid job location data return false; } @@ -1253,20 +1282,130 @@ function saveJobLocationToDatabase(jobLocationData) { let dbConnection = connectToDatabase(); if(dbConnection) { // If job location hasn't been added to database, ID will be 0 - if(vehicleData.databaseId == 0) { + if(jobLocationData.databaseId == 0) { let dbQueryString = `INSERT INTO job_loc (job_loc_job, job_loc_enabled, job_loc_pos_x, job_loc_pos_y, job_loc_pos_z, job_loc_int, job_loc_vw) VALUES (${jobLocationData.job}, ${boolToInt(jobLocationData.enabled)}, ${jobLocationData.position.x}, ${jobLocationData.position.y}, ${jobLocationData.position.z}, ${jobLocationData.interior}, ${jobLocationData.dimension})`; queryDatabase(dbConnection, dbQueryString); jobLocationData.databaseId = getDatabaseInsertId(dbConnection); } else { - let dbQueryString = `UPDATE job_loc SET job_loc_job=${jobLocationData.job}, job_loc_enabled=${boolToInt(jobLocationData.enabled)}, job_loc_pos_x=${jobLocationData.position.x}, job_loc_pos_y=${jobLocationData.jobLocationData.y}, job_loc_pos_z=${jobLocationData.jobLocationData.z}, job_loc_int=${jobLocationData.interior}, job_loc_vw=${jobLocationData.dimension} WHERE job_loc_id=${jobLocationData.databaseId}`; + let dbQueryString = `UPDATE job_loc SET job_loc_job=${jobLocationData.job}, job_loc_enabled=${boolToInt(jobLocationData.enabled)}, job_loc_pos_x=${jobLocationData.position.x}, job_loc_pos_y=${jobLocationData.position.y}, job_loc_pos_z=${jobLocationData.position.z}, job_loc_int=${jobLocationData.interior}, job_loc_vw=${jobLocationData.dimension} WHERE job_loc_id=${jobLocationData.databaseId}`; queryDatabase(dbConnection, dbQueryString); } disconnectFromDatabase(dbConnection); return true; } - console.log(`[Asshat.Job]: Saved job location ${jobLocationData.vehicle.id} to database!`); + console.log(`[Asshat.Job]: Saved job location ${jobLocationData.databaseId} to database`); return false; } +// --------------------------------------------------------------------------- + +function saveJobEquipmentToDatabase(jobEquipmentData) { + if(jobEquipmentData == null) { + // Invalid job equipment data + return false; + } + + console.log(`[Asshat.Job]: Saving job equipment ${jobEquipmentData.databaseId} to database ...`); + let dbConnection = connectToDatabase(); + if(dbConnection) { + let safeName = escapeDatabaseString(dbConnection, jobEquipmentData.name); + // If job equipment hasn't been added to database, ID will be 0 + if(jobEquipmentData.databaseId == 0) { + let dbQueryString = `INSERT INTO job_equip (job_equip_job, job_equip_enabled, job_equip_minrank, job_equip_name) VALUES (${jobEquipmentData.job}, ${boolToInt(jobEquipmentData.enabled)}, ${jobEquipmentData.requiredRank}, '${safeName}')`; + queryDatabase(dbConnection, dbQueryString); + jobEquipmentData.databaseId = getDatabaseInsertId(dbConnection); + } else { + let dbQueryString = `UPDATE job_equip SET job_equip_job=${jobEquipmentData.job}, job_equip_enabled=${boolToInt(jobEquipmentData.enabled)}, job_equip_minrank=${jobEquipmentData.requiredRank}, job_equip_name='${safeName}' WHERE job_equip_id=${jobEquipmentData.databaseId}`; + queryDatabase(dbConnection, dbQueryString); + } + disconnectFromDatabase(dbConnection); + return true; + } + console.log(`[Asshat.Job]: Saved job equipment ${jobEquipmentData.databaseId} to database`); + + return false; +} + +// --------------------------------------------------------------------------- + +function saveJobEquipmentWeaponToDatabase(jobEquipmentWeaponData) { + if(jobEquipmentWeaponData == null) { + // Invalid job equipment weapon data + return false; + } + + console.log(`[Asshat.Job]: Saving job equipment weapon ${jobEquipmentWeaponData.databaseId} to database ...`); + let dbConnection = connectToDatabase(); + if(dbConnection) { + // If job equipment weapon hasn't been added to database, ID will be 0 + if(jobEquipmentWeaponData.databaseId == 0) { + let dbQueryString = `INSERT INTO job_equip_wep (job_equip_wep_equip, job_equip_wep_enabled, job_equip_wep_wep, job_equip_wep_ammo) VALUES (${jobEquipmentWeaponData.equipmentId}, ${boolToInt(jobEquipmentWeaponData.enabled)}, ${jobEquipmentWeaponData.weaponId}, ${jobEquipmentWeaponData.ammo})`; + queryDatabase(dbConnection, dbQueryString); + jobEquipmentWeaponData.databaseId = getDatabaseInsertId(dbConnection); + } else { + let dbQueryString = `UPDATE job_equip_wep SET job_equip_wep_equip=${jobEquipmentWeaponData.equipmentId}, job_equip_wep_enabled=${boolToInt(jobEquipmentWeaponData.enabled)}, job_equip_wep_wep=${jobEquipmentWeaponData.weaponId}, job_equip_wep_ammo=${jobEquipmentWeaponData.ammo} WHERE job_equip_wep_id=${jobEquipmentWeaponData.databaseId}`; + queryDatabase(dbConnection, dbQueryString); + } + disconnectFromDatabase(dbConnection); + return true; + } + console.log(`[Asshat.Job]: Saved job equipment weapon ${jobEquipmentWeaponData.databaseId} to database`); + + return false; +} + +// --------------------------------------------------------------------------- + +function saveJobUniformToDatabase(jobUniformData) { + if(jobUniformData == null) { + // Invalid job uniform data + return false; + } + + console.log(`[Asshat.Job]: Saving job uniform ${jobUniformData.databaseId} to database ...`); + let dbConnection = connectToDatabase(); + if(dbConnection) { + let safeName = escapeDatabaseString(dbConnection, jobUniformData.name); + // If job uniform hasn't been added to database, ID will be 0 + if(jobUniformData.databaseId == 0) { + let dbQueryString = `INSERT INTO job_uniform (job_uniform_job, job_uniform_enabled, job_uniform_minrank, job_uniform_name) VALUES (${jobUniformData.job}, ${boolToInt(jobUniformData.enabled)}, ${jobUniformData.requiredRank}, '${safeName}')`; + queryDatabase(dbConnection, dbQueryString); + jobUniformData.databaseId = getDatabaseInsertId(dbConnection); + } else { + let dbQueryString = `UPDATE job_uniform SET job_uniform_job=${jobUniformData.job}, job_uniform_enabled=${boolToInt(jobUniformData.enabled)}, job_uniform_minrank=${jobUniformData.requiredRank}, job_uniform_name='${safeName}', job_uniform_skin=${jobUniformData.skin} WHERE job_uniform_id=${jobUniformData.databaseId}`; + queryDatabase(dbConnection, dbQueryString); + } + disconnectFromDatabase(dbConnection); + return true; + } + console.log(`[Asshat.Job]: Saved job uniform ${jobUniformData.databaseId} to database`); + + return false; +} + +// --------------------------------------------------------------------------- + +function saveAllJobsToDatabase() { + for(let i in getServerData().jobs) { + saveJobToDatabase(getServerData().jobs[i]); + + for(let j in getServerData().jobs[i].locations) { + saveJobLocationToDatabase(getServerData().jobs[i].locations[j]); + } + + for(let k in getServerData().jobs[i].uniforms) { + saveJobUniformToDatabase(getServerData().jobs[i].uniforms[k]); + } + + for(let m in getServerData().jobs[i].equipment) { + saveJobEquipmentToDatabase(getServerData().jobs[i].equipment[m]); + + for(let n in getServerData().jobs[i].equipment[m].weapons) { + saveJobEquipmentWeaponToDatabase(getServerData().jobs[i].equipment[m].weapons[n]); + } + } + } +} + // --------------------------------------------------------------------------- \ No newline at end of file diff --git a/scripts/server/timers.js b/scripts/server/timers.js index a72a64e6..87a49571 100644 --- a/scripts/server/timers.js +++ b/scripts/server/timers.js @@ -25,6 +25,7 @@ function saveAllServerDataToDatabase() { saveAllBusinessesToDatabase(); saveServerConfigToDatabase(getServerConfig()); saveAllVehiclesToDatabase(); + saveAllJobsToDatabase(); saveAllClientsToDatabase(); console.log("[Asshat.Utilities]: Saved all server data to database!"); }