Add veh set owner cmds, add log msgs, fix INSERT query for save veh

This commit is contained in:
VortrexFTW
2020-09-09 03:56:14 -05:00
parent 3b5b66db53
commit 6ed43c4611

View File

@@ -57,10 +57,12 @@ function loadVehiclesFromDatabase() {
// ---------------------------------------------------------------------------
function saveAllVehiclesToDatabase() {
console.log("[Asshat.Vehicle]: Saving all vehicles to database ...");
let vehicles = serverData.vehicles;
for(let i in vehicles) {
saveVehicleToDatabase(vehicles[i]);
}
console.log("[Asshat.Vehicle]: Saved all vehicles to database!");
return true;
}
@@ -68,6 +70,7 @@ function saveAllVehiclesToDatabase() {
// ---------------------------------------------------------------------------
function saveVehicleToDatabase(vehicleData) {
console.log(`[Asshat.Vehicle]: Saving vehicles ${vehicleData.vehicle.id} to database ...`);
let dbConnection = connectToDatabase();
if(dbConnection) {
// If vehicle hasn't been added to database, ID will be 0
@@ -77,16 +80,25 @@ function saveVehicleToDatabase(vehicleData) {
// dbQueryColourFields = "`veh_col1_rgba`, `veh_col2_rgba`, `veh_col3_rgba`, `veh_col4_rgba`";
// dbQueryColourValues = vehicleData.colour1Red, `veh_col1_g`, `veh_col1_b`, `veh_col1_a`, `veh_col2_r`, `veh_col2_g`, `veh_col2_b`, `veh_col2_a`, `veh_col3_r`, `veh_col3_g`, `veh_col3_b`, `veh_col3_a`, `veh_col4_r`, `veh_col4_g`, `veh_col4_b`, `veh_col4_a`,";
//}
let dbQueryString = `INSERT INTO veh_main (veh_model, veh_pos_x, veh_pos_y, veh_pos_z, veh_owner_type, veh_owner_id) VALUES (${vehicleData.model}, ${vehicleData.spawnPosition.x}, ${vehicleData.spawnPosition.y}, ${vehicleData.spawnPosition.z}, ${vehicleData.spawnRotation.z}, ${vehicleData.ownerType}, ${vehicleData.ownerId})`;
let dbQuery = dbConnection.query(dbQueryString);
let dbQueryString = `INSERT INTO veh_main (veh_model, veh_pos_x, veh_pos_y, veh_pos_z, veh_rot_z, veh_owner_type, veh_owner_id) VALUES (${vehicleData.model}, ${vehicleData.spawnPosition.x}, ${vehicleData.spawnPosition.y}, ${vehicleData.spawnPosition.z}, ${vehicleData.spawnRotation}, ${vehicleData.ownerType}, ${vehicleData.ownerId})`;
dbConnection.query(dbQueryString);
if(dbConnection.error) {
console.warn(`[Asshat.Vehicle]: There was a problem saving vehicle ${vehicleData.vehicle.id} to the database (INSERT). Error: ${dbConnection.error}`);
return false;
}
getVehicleData(vehicleData.vehicle).databaseId = dbConnection.insertId;
} else {
let dbQueryString = `UPDATE veh_main SET veh_model=${vehicleData.model}, veh_pos_x=${vehicleData.spawnPosition.x}, veh_pos_y=${vehicleData.spawnPosition.y}, veh_pos_z=${vehicleData.spawnPosition.z}, veh_rot_z=${vehicleData.spawnRotation}, veh_owner_type=${vehicleData.ownerType}, veh_owner_id=${vehicleData.ownerId} WHERE veh_id=${vehicleData.databaseId}`;
let dbQuery = dbConnection.query(dbQueryString);
dbConnection.query(dbQueryString);
if(dbConnection.error) {
console.warn(`[Asshat.Vehicle]: There was a problem saving vehicle ${vehicleData.vehicle.id} to the database (UPDATE). Error: ${dbConnection.error}`);
return false;
}
}
disconnectFromDatabase(dbConnection);
return true;
}
console.log(`[Asshat.Vehicle]: Saved vehicle ${vehicleData.vehicle.id} to database!`);
return false;
}
@@ -163,6 +175,7 @@ function createVehicleCommand(command, params, client) {
let frontPos = getPosInFrontOfPos(client.player.position, client.player.heading, serverConfig.spawnCarDistance);
let vehicle = createVehicle(modelId, frontPos, client.player.heading);
vehicle.heading = client.player.heading;
let tempVehicleData = new serverClasses.vehicleData(false, vehicle);
let vehiclesLength = serverData.vehicles.push(tempVehicleData);
@@ -421,4 +434,302 @@ function getVehicleName(vehicle) {
return vehicleName;
}
// ---------------------------------------------------------------------------
function setVehicleJobCommand(command, params, client) {
if(getCommand(command).requireLogin) {
if(!isClientLoggedIn(client)) {
messageClientError(client, "You must be logged in to use this command!");
return false;
}
}
if(isClientFromDiscord(client)) {
if(!isCommandAllowedOnDiscord(command)) {
messageClientError(client, "That command isn't available on discord!");
return false;
}
}
if(!doesClientHaveStaffPermission(client, getCommandRequiredPermissions(command))) {
messageClientError(client, "You do not have permission to use this command!");
return false;
}
if(!client.player.vehicle) {
messageClientError(client, "You need to be in a vehicle!");
return false;
}
let vehicle = client.player.vehicle;
let jobId = getClosestJobPointId(vehicle.position);
if(!areParamsEmpty(params)) {
jobId = getJobIdFromParams(params);
}
//if(!jobId) {
// messageClientError(client, "That job is invalid!");
// messageClientInfo(client, "Please specify a job ID or leave it out to get the closest job.");
// return false;
//}
getVehicleData(vehicle).ownerType = AG_VEHOWNER_JOB;
getVehicleData(vehicle).ownerId = jobId;
messageClientSuccess(client, `You set the ${getVehicleName(vehicle)}'s owner to the ${getJobData(jobId).name} job! (ID ${jobId})`);
}
// ---------------------------------------------------------------------------
function setVehicleClanCommand(command, params, client) {
if(getCommand(command).requireLogin) {
if(!isClientLoggedIn(client)) {
messageClientError(client, "You must be logged in to use this command!");
return false;
}
}
if(isClientFromDiscord(client)) {
if(!isCommandAllowedOnDiscord(command)) {
messageClientError(client, "That command isn't available on discord!");
return false;
}
}
if(!doesClientHaveStaffPermission(client, getCommandRequiredPermissions(command))) {
messageClientError(client, "You do not have permission to use this command!");
return false;
}
if(!client.player.vehicle) {
messageClientError(client, "You need to be in a vehicle!");
return false;
}
let vehicle = client.player.vehicle;
let clan = getClanFromParams(params);
if(!clan) {
messageClientError(client, "That clan is invalid or doesn't exist!");
return false;
}
getVehicleData(vehicle).ownerType = AG_VEHOWNER_CLAN;
getVehicleData(vehicle).ownerId = clan.databaseId;
messageClientSuccess(client, `You set the ${getVehicleName(vehicle)}'s owner to the ${clan.name} clan!`);
}
// ---------------------------------------------------------------------------
function setVehicleOwnerCommand(command, params, client) {
if(getCommand(command).requireLogin) {
if(!isClientLoggedIn(client)) {
messageClientError(client, "You must be logged in to use this command!");
return false;
}
}
if(isClientFromDiscord(client)) {
if(!isCommandAllowedOnDiscord(command)) {
messageClientError(client, "That command isn't available on discord!");
return false;
}
}
if(!doesClientHaveStaffPermission(client, getCommandRequiredPermissions(command))) {
messageClientError(client, "You do not have permission to use this command!");
return false;
}
if(!client.player.vehicle) {
messageClientError(client, "You need to be in a vehicle!");
return false;
}
let vehicle = client.player.vehicle;
let targetClient = getClientFromParams(params);
if(!targetClient) {
messageClientError(client, "That player is invalid or isn't connected!");
return false;
}
getVehicleData(vehicle).ownerType = AG_VEHOWNER_PLAYER;
getVehicleData(vehicle).ownerId = getClientCurrentSubAccount(client).databaseId;
messageClientSuccess(client, `You set the ${getVehicleName(vehicle)}'s owner to ${getClientSubAccountName(client)}!`);
}
// ---------------------------------------------------------------------------
function removeVehicleOwnerCommand(command, params, client) {
if(getCommand(command).requireLogin) {
if(!isClientLoggedIn(client)) {
messageClientError(client, "You must be logged in to use this command!");
return false;
}
}
if(isClientFromDiscord(client)) {
if(!isCommandAllowedOnDiscord(command)) {
messageClientError(client, "That command isn't available on discord!");
return false;
}
}
if(!doesClientHaveStaffPermission(client, getCommandRequiredPermissions(command))) {
messageClientError(client, "You do not have permission to use this command!");
return false;
}
if(!client.player.vehicle) {
messageClientError(client, "You need to be in a vehicle!");
return false;
}
let vehicle = client.player.vehicle;
let targetClient = getClientFromParams(params);
if(!targetClient) {
messageClientError(client, "That player is invalid or isn't connected!");
return false;
}
getVehicleData(vehicle).ownerType = AG_VEHOWNER_NONE;
getVehicleData(vehicle).ownerId = 0;
messageClientSuccess(client, `You set the ${getVehicleName(vehicle)}'s owner to nobody!`);
messageClientInfo(client, `Nobody will be able to use this vehicle until it receives a new owner (either bought or set by admin).`);
}
// ---------------------------------------------------------------------------
function getVehicleInfoCommand(command, params, client) {
if(getCommand(command).requireLogin) {
if(!isClientLoggedIn(client)) {
messageClientError(client, "You must be logged in to use this command!");
return false;
}
}
if(isClientFromDiscord(client)) {
if(!isCommandAllowedOnDiscord(command)) {
messageClientError(client, "That command isn't available on discord!");
return false;
}
}
if(!doesClientHaveStaffPermission(client, getCommandRequiredPermissions(command))) {
messageClientError(client, "You do not have permission to use this command!");
return false;
}
if(!client.player.vehicle) {
messageClientError(client, "You need to be in a vehicle!");
return false;
}
let vehicle = client.player.vehicle;
let vehicleData = getVehicleData(vehicle);
let ownerName = "Nobody";
let ownerType = "None";
switch(vehicleData.ownerType) {
case AG_VEHOWNER_CLAN:
ownerName = getClanData(vehicleData.ownerId).name;
ownerType = "clan";
break;
case AG_VEHOWNER_JOB:
ownerName = getJobData(vehicleData.ownerId).name;
ownerType = "job";
break;
case AG_VEHOWNER_PLAYER:
let accountData = loadAccountFromId(vehicleData.ownerId);
ownerName = `${accountData.name} [${accountData.databaseId}]`;
ownerType = "player";
break;
default:
break;
}
messageClientInfo(client, `[#0099FF][Vehicle Info] [#FFFFFF]ID: [#CCCCCC]${vehicle.id}, [#FFFFFF]DatabaseID: [#CCCCCC]${vehicleData.databaseId}, [#FFFFFF]Owner: [#CCCCCC]${ownerName}[ID ${vehicleData.ownerId}] (${ownerType}), [#FFFFFF]Type: [#CCCCCC]${getVehicleName(vehicle)}[${vehicle.modelIndex}], [#FFFFFF]BuyPrice: [#CCCCCC]${vehicleData.buyPrice}, [#FFFFFF]RentPrice: [#CCCCCC]${vehicleData.rentPrice}`);
}
// ---------------------------------------------------------------------------
function parkVehicleCommand(command, params, client) {
if(getCommand(command).requireLogin) {
if(!isClientLoggedIn(client)) {
messageClientError(client, "You must be logged in to use this command!");
return false;
}
}
if(isClientFromDiscord(client)) {
if(!isCommandAllowedOnDiscord(command)) {
messageClientError(client, "That command isn't available on discord!");
return false;
}
}
if(!doesClientHaveStaffPermission(client, getCommandRequiredPermissions(command))) {
messageClientError(client, "You do not have permission to use this command!");
return false;
}
if(!client.player.vehicle) {
messageClientError(client, "You need to be in a vehicle!");
return false;
}
let vehicle = client.player.vehicle;
getVehicleData(vehicle).spawnPosition = vehicle.position;
getVehicleData(vehicle).spawnRotation = vehicle.heading;
messageClientInfo(client, `This vehicle will now spawn here.`);
}
// ---------------------------------------------------------------------------
function toggleVehicleSpawnLockCommand(command, params, client) {
if(getCommand(command).requireLogin) {
if(!isClientLoggedIn(client)) {
messageClientError(client, "You must be logged in to use this command!");
return false;
}
}
if(isClientFromDiscord(client)) {
if(!isCommandAllowedOnDiscord(command)) {
messageClientError(client, "That command isn't available on discord!");
return false;
}
}
if(!doesClientHaveStaffPermission(client, getCommandRequiredPermissions(command))) {
messageClientError(client, "You do not have permission to use this command!");
return false;
}
if(!client.player.vehicle) {
messageClientError(client, "You need to be in a vehicle!");
return false;
}
let vehicle = client.player.vehicle;
if(!getVehicleData(vehicle).spawnLocked) {
getVehicleData(vehicle).spawnPosition = vehicle.position;
getVehicleData(vehicle).spawnRotation = vehicle.heading;
}
getVehicleData(vehicle).spawnLocked = !getVehicleData(vehicle).spawnLocked
messageClientInfo(client, `This vehicle will now spawn ${(getVehicleData(vehicle).spawnLocked) ? "here" : "wherever a player leaves it."}`);
}
// ---------------------------------------------------------------------------