// =========================================================================== // Asshat-Gaming Roleplay // https://github.com/VortrexFTW/gtac_asshat_rp // Copyright (c) 2020 Asshat-Gaming (https://asshatgaming.com) // --------------------------------------------------------------------------- // FILE: business.js // DESC: Provides business functions and usage // TYPE: Server (JavaScript) // =========================================================================== function initBusinessScript() { console.log("[Asshat.Business]: Initializing business script ..."); getServerData().businesses = loadBusinessesFromDatabase(); createAllBusinessPickups(); console.log("[Asshat.Business]: Business script initialized successfully!"); return true; } // --------------------------------------------------------------------------- function loadBusinessesFromDatabase() { console.log("[Asshat.Business]: Loading businesses from database ..."); let tempBusinesses = []; let dbConnection = connectToDatabase(); let dbQuery = null; let dbAssoc; if(dbConnection) { dbQuery = queryDatabase(dbConnection, "SELECT * FROM `biz_main` WHERE `biz_server` = " + toString(serverId)); if(dbQuery) { if(dbQuery.numRows > 0) { while(dbAssoc = fetchQueryAssoc(dbQuery)) { let tempBusinessData = new serverClasses.businessData(dbAssoc); tempBusinessData.locations = loadBusinessLocationsFromDatabase(tempBusinessData.databaseId); tempBusinesses.push(tempBusinessData); console.log(`[Asshat.Business]: Business '${tempBusinessData.name}' loaded from database successfully!`); } } freeDatabaseQuery(dbQuery); } disconnectFromDatabase(dbConnection); } console.log(`[Asshat.Business]: ${tempBusinesses.length} businesses loaded from database successfully!`); return tempBusinesses; } // --------------------------------------------------------------------------- function loadBusinessLocationsFromDatabase(businessId) { console.log("[Asshat.Business]: Loading locations from database ..."); let tempBusinessLocations = []; let dbConnection = connectToDatabase(); let dbQuery = null; let dbAssoc; if(dbConnection) { dbQuery = queryDatabase(dbConnection, "SELECT * FROM `biz_loc` WHERE `biz_loc_biz` = " + toString(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!`); } } freeDatabaseQuery(dbQuery); } disconnectFromDatabase(dbConnection); } console.log(`[Asshat.Business]: ${tempBusinessLocations.length} location for business ${businessId} loaded from database successfully!`); return tempBusinessLocations; } // --------------------------------------------------------------------------- function createBusinessCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } if(isClientSpawned(client)) { messageClientError("You must be spawned to use this command!"); return false; } createBusiness(params, client.player.position, getClientInterior(client), getClientVirtualWorld(client)); messageClientSuccess(client, "Business created in " + getAreaName(client.player.position) + " (" + params + ")"); } // --------------------------------------------------------------------------- function createBusinessLocationCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } if(isClientSpawned(client)) { messageClientError("You must be spawned to use this command!"); return false; } let locationType = toString(splitParams[0]); let businessId = toInteger(splitParams[1]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); createBusinessLocation(locationType, businessId); messageClientSuccess(client, "Business created in " + getAreaName(client.player.position) + " (" + params + ")"); } // --------------------------------------------------------------------------- function createBusiness(name, entrancePosition, interiorId, virtualWorld) { let dbConnection = connectToDatabase(); let escapedName = name; if(dbConnection) { escapedName = escapeDatabaseString(dbConnection, escapedName) let dbQuery = queryDatabase(dbConnection, "INSERT INTO `biz_main` (`biz_server`, `biz_name`, `biz_entrance_x`, `biz_entrance_y`, `biz_entrance_z`, `biz_entrance_int`, `biz_entrance_vw`) VALUES (" + toString(serverId) + ", '" + escapedName + "', " + toString(entrancePosition.x) + ", " + toString(entrancePosition.y) + ", " + toString(entrancePosition.z) + ", " + toString(interiorId) + ", " + toString(virtualWorld) + ")"); disconnectFromDatabase(dbConnection); let tempBusinessData = loadBusinessFromDatabaseById(dbConnection.insertID); if(tempBusinessData != false) { let tempBusiness = new serverClasses.businessData(tempBusinessData); getServerData().business.push(tempBusiness); } } return true; } // --------------------------------------------------------------------------- function deleteBusinessCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } let businessId = toInteger(splitParams[1]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); deleteBusiness(businessId); messageClientSuccess(client, `Business '${tempBusinessData.name} deleted!`); } // --------------------------------------------------------------------------- function deleteBusinessLocationCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } //let businessId = toInteger(splitParams[1]); //deleteBusinessLocation(businessId); //messageClientSuccess(client, `Business '${tempBusinessData.name} deleted!`); } // --------------------------------------------------------------------------- function setBusinessNameCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } let splitParams = params.split(" "); let newBusinessName = toString(splitParams[0]); let businessId = toInteger(splitParams[1]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); getServerData().businesses[businessId].name = newBusinessName; messageClientSuccess(client, `Business '${getServerData().businesses[businessId].name}' renamed to '${newBusinessName}'!`); } // --------------------------------------------------------------------------- function setBusinessOwnerCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } if(isClientSpawned(client)) { messageClientError("You must be spawned to use this command!"); return false; } let splitParams = params.split(" "); let newBusinessOwner = getClientFromParams(splitParams[0]); let businessId = toInteger(splitParams[1]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); if(!newBusinessOwner) { messageClientError("Player not found!"); return false; } if(!getBusinessDataFromDatabaseId(businessId)) { messageClientError("Business not found!"); return false; } getServerData().businesses[businessId].ownerId = getServerData().clients[newBusinessOwner.index].accountData.databaseId; messageClientSuccess(client, `Business '${getServerData().businesses[businessId].name}' owner set to '${newBusinessOwner.name}'!`); } // --------------------------------------------------------------------------- function lockBusinessCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } if(isClientSpawned(client)) { messageClientError("You must be spawned to use this command!"); return false; } let splitParams = params.split(" "); let businessId = toInteger(splitParams[0]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); getServerData().businesses[businessId].locked = !getServerData().businesses[businessId].locked; messageClientSuccess(client, "Business " + getServerData().businesses[businessId].name + " " + (getServerData().businesses[businessId].locked) ? "locked" : "unlocked" + "!"); } // --------------------------------------------------------------------------- function setBusinessEntranceFeeCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } let splitParams = params.split(" "); let entranceFee = toInteger(splitParams[0]) || 0; let businessId = toInteger(splitParams[1]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); getServerData().businesses[businessId].entranceFee = entranceFee; messageClientSuccess(client, `Business '${getServerData().businesses[businessId].name}' entrance fee to $'${entranceFee}'!`); } // --------------------------------------------------------------------------- function withdrawFromBusinessCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } let splitParams = params.split(" "); let amount = toInteger(splitParams[0]) || 0; let businessId = toInteger(splitParams[1]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); let tempBusinessData = getServerData().businesses.filter(b => b.databaseId == businessId)[0]; if(getServerData().businesses[businessId].till < amount) { messageClientError(client, `Business '${tempBusinessData.name}' doesn't have that much money! Use /bizbalance.`); return false; } getServerData().businesses[businessId].till -= amount; getClientCurrentSubAccount(client).cash += amount; updatePlayerCash(client); messageClientSuccess(client, `You withdrew $${amount} from business '${tempBusinessData.name}''s till'`); } // --------------------------------------------------------------------------- function depositIntoBusinessCommand(command, params, client) { if(areParamsEmpty(params)) { messageClientSyntax(client, getCommandSyntaxText(command)); return false; } let splitParams = params.split(" "); let amount = toInteger(splitParams[0]) || 0; let businessId = toInteger(splitParams[1]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); if(getClientCurrentSubAccount(client).cash < amount) { messageClientError(client, `You don't have that much money! You only have $${getClientCurrentSubAccount(client).cash}`); return false; } getServerData().businesses[businessId].till += amount; getClientCurrentSubAccount(client).cash -= amount; updatePlayerCash(client); messageClientSuccess(client, `You deposited $${amount} into business '${tempBusinessData.name}''s till'`); } // --------------------------------------------------------------------------- function viewBusinessTillAmountCommand(command, params, client) { //if(areParamsEmpty(params)) { // messageClientSyntax(client, getCommandSyntaxText(command)); // return false; //} let splitParams = params.split(" "); let businessId = toInteger(splitParams[0]) || (isPlayerInAnyBusiness(client.player)) ? getPlayerBusiness(client.player) : getClosestBusinessEntrance(client.player.position); messageClientSuccess(client, `Business '${getServerData().businesses[businessId].name}''s till has $'${getServerData().businesses[businessId].till}'!`); } // --------------------------------------------------------------------------- function getBusinessDataFromDatabaseId(databaseId) { let matchingBusinesses = getServerData().businesses.filter(b => b.databaseId == businessId) if(matchingBusinesses.length == 1) { return matchingBusinesses[0]; } return false; } // --------------------------------------------------------------------------- function getClosestBusinessEntrance(position) { return getServerData().businesses.reduce((i, j) => ((i.entrancePosition.distance(position) <= j.entrancePosition.distance(position)) ? i : j)); } // --------------------------------------------------------------------------- function isPlayerInAnyBusiness(client) { if(doesEntityDataExist(client, "ag.inBusiness")) { return true; } return false; } // --------------------------------------------------------------------------- function getPlayerBusiness(client) { if(doesEntityDataExist(client, "ag.inBusiness")) { return getEntityData(client, "ag.inBusiness"); } return false; } // --------------------------------------------------------------------------- function saveAllBusinessesToDatabase() { for(let i in getServerData().businesses) { saveBusinessToDatabase(i); } } // --------------------------------------------------------------------------- function saveBusinessToDatabase(businessId) { let tempBusinessData = getServerData().businesses[businessId] console.log(`[Asshat.Business]: Saving business '${tempBusinessData.name}' to database ...`); let dbConnection = connectToDatabase(); if(dbConnection) { if(tempBusinessData.databaseId == 0) { let dbQueryString = `INSERT INTO biz_main (biz_name, biz_owner_type, biz_owner_id, biz_locked, biz_entrance_fee, biz_till, biz_entrance_pos_x, biz_entrance_pos_y, biz_entrance_pos_z, biz_entrance_rot_z, biz_entrance_int, biz_entrance_vw, biz_exit_pos_x, biz_exit_pos_y, biz_exit_pos_z, biz_exit_rot_z, biz_exit_int, biz_exit_vw) VALUES ('${tempBusinessData.name}', ${tempBusinessData.ownerType}, ${tempBusinessData.ownerId}, ${boolToInt(tempBusinessData.locked)}, ${tempBusinessData.entranceFee}, ${tempBusinessData.till}, ${tempBusinessData.entrancePos.x}, ${tempBusinessData.entrancePos.y}, ${tempBusinessData.entrancePos.z}, ${tempBusinessData.entranceHeading}, ${tempBusinessData.entranceInterior}, ${tempBusinessData.entranceDimension}, ${tempBusinessData.exitPos.x}, ${tempBusinessData.exitPos.y}, ${tempBusinessData.exitPos.z}, ${tempBusinessData.exitHeading}, ${tempBusinessData.exitInterior}, ${tempBusinessData.exitDimension})`; queryDatabase(dbConnection, dbQueryString); getServerData().businesses[businessId].databaseId = getDatabaseInsertId(dbConnection); } else { let dbQueryString = `UPDATE biz_main SET biz_name=${tempBusinessData.name}, biz_owner_type=${tempBusinessData.ownerType}, biz_owner_id=${tempBusinessData.ownerId}, biz_locked=${boolToInt(tempBusinessData.locked)}, biz_entrance_fee=${tempBusinessData.entranceFee}, biz_till=${tempBusinessData.till}, biz_entrance_pos_x=${tempBusinessData.entrancePos.x}, biz_entrance_pos_y=${tempBusinessData.entrancePos.y}, biz_entrance_pos_z=${tempBusinessData.entrancePos.z}, biz_entrance_rot_z=${tempBusinessData.entranceHeading}, biz_entrance_int=${tempBusinessData.entranceInterior}, biz_entrance_vw=${tempBusinessData.entranceDimension}, biz_exit_pos_x=${tempBusinessData.exitPos.x}, biz_exit_pos_y=${tempBusinessData.exitPos.y}, biz_exit_pos_z=${tempBusinessData.exitPos.z}, biz_exit_rot_z=${tempBusinessData.exitHeading}, biz_exit_int=${tempBusinessData.exitInterior}, biz_exit_vw=${tempBusinessData.exitDimension} WHERE biz_id=${tempBusinessData.databaseId}`; queryDatabase(dbConnection, dbQueryString); } disconnectFromDatabase(dbConnection); return true; } console.log(`[Asshat.Business]: Saved business '${tempBusinessData.name}' to database!`); return false; } // --------------------------------------------------------------------------- function createAllBusinessPickups() { for(let i in getServerData().businesses) { getServerData().businesses.pickup = createPickup(getServerConfig().businessPickupModel, getServerData().businesses[i].position); getServerData().businesses[i].pickup.setData("ag.ownerType", AG_PICKUP_BUSINESS, true); getServerData().businesses[i].pickup.setData("ag.ownerId", i, true); } } // --------------------------------------------------------------------------- function deleteBusiness(businessId) { let tempBusinessData = getServerData().businesses[businessId]; let dbConnection = connectToDatabase(); let dbQuery = null; if(dbConnection) { dbQuery = queryDatabase(dbConnection, `UPDATE biz_main SET biz_deleted = 1 AND biz_who_deleted = ${getClientData(client).accountData.databaseId} AND biz_when_deleted = UNIX_TIMESTAMP() WHERE biz_id = ${tempBusinessData.databaseId} LIMIT 1`); if(dbQuery) { freeDatabaseQuery(dbQuery); } disconnectFromDatabase(dbConnection); } destroyElement(tempBusinessData.pickup); removePlayersFromBusiness(businessId); } // --------------------------------------------------------------------------- /* function deleteBusiness(businessId) { let tempBusinessData = getServerData().businesses[businessId]; let dbConnection = connectToDatabase(); let dbQuery = null; if(dbConnection) { dbQuery = queryDatabase(dbConnection, `UPDATE biz_main SET biz_deleted = 1 AND biz_who_deleted = ${getClientData(client).accountData.databaseId} AND biz_when_deleted = UNIX_TIMESTAMP() WHERE biz_id = ${tempBusinessData.databaseId} LIMIT 1`); if(dbQuery) { freeDatabaseQuery(dbQuery); } disconnectFromDatabase(dbConnection); } destroyElement(tempBusinessData.pickup); removePlayersFromBusiness(businessId); } */ // --------------------------------------------------------------------------- function removePlayersFromBusiness(businessId) { getClients().forEach(function(client) { if(doesEntityDataExist(client, "ag.inBusiness")) { if(getEntityData(client, "ag.inBusiness") == businessId) { exitBusiness(client); } } }); } // --------------------------------------------------------------------------- function exitBusiness(client) { let businessId = getEntityData(client, "ag.inBusiness"); if(isPlayerSpawned(client)) { triggerNetworkEvent("ag.interior", client, getServerData().businesses[businessId].entranceInterior); triggerNetworkEvent("ag.dimension", client, getServerData().businesses[businessId].entranceDimension); triggerNetworkEvent("ag.position", client, getServerData().businesses[businessId].entrancePosition); } } // ---------------------------------------------------------------------------