NPC class, data load and save
This commit is contained in:
@@ -1468,8 +1468,10 @@ class NPCData {
|
||||
this.middleName = "Q";
|
||||
this.skin = 0;
|
||||
this.cash = 0;
|
||||
this.spawnPosition = toVector3(0.0, 0.0, 0.0);
|
||||
this.spawnHeading = 0.0;
|
||||
this.position = toVector3(0.0, 0.0, 0.0);
|
||||
this.rotation = toVector3(0.0, 0.0, 0.0);
|
||||
this.scale = toVector3(1.0, 1.0, 1.0);
|
||||
this.heading = 0.0;
|
||||
this.clan = 0;
|
||||
this.isWorking = false;
|
||||
this.jobUniform = this.skin;
|
||||
@@ -1478,7 +1480,6 @@ class NPCData {
|
||||
this.weapons = [];
|
||||
this.interior = 0;
|
||||
this.dimension = 0;
|
||||
this.pedScale = toVector3(1.0, 1.0, 1.0);
|
||||
this.walkStyle = 0;
|
||||
this.fightStyle = 0;
|
||||
this.health = 100;
|
||||
@@ -1514,17 +1515,16 @@ class NPCData {
|
||||
this.middleName = dbAssoc["npc_name_middle"] || "";
|
||||
this.skin = toInteger(dbAssoc["npc_skin"]);
|
||||
this.cash = toInteger(dbAssoc["npc_cash"]);
|
||||
this.spawnPosition = toVector3(toFloat(dbAssoc["npc_pos_x"]), toFloat(dbAssoc["npc_pos_y"]), toFloat(dbAssoc["npc_pos_z"]));
|
||||
this.spawnHeading = toFloat(dbAssoc["npc_angle"]);
|
||||
this.position = toVector3(toFloat(dbAssoc["npc_pos_x"]), toFloat(dbAssoc["npc_pos_y"]), toFloat(dbAssoc["npc_pos_z"]));
|
||||
this.rotation = toVector3(toFloat(dbAssoc["npc_rot_x"]), toFloat(dbAssoc["npc_rot_y"]), toFloat(dbAssoc["npc_rot_z"]));
|
||||
this.scale = toVector3(toFloat(dbAssoc["npc_scale_x"]), toFloat(dbAssoc["npc_scale_y"]), toFloat(dbAssoc["npc_scale_z"]));
|
||||
this.heading = toFloat(dbAssoc["npc_rot_z"]);
|
||||
this.lastLogin = toInteger(dbAssoc["npc_when_lastlogin"]);
|
||||
this.clan = toInteger(dbAssoc["npc_clan"]);
|
||||
this.clanFlags = toInteger(dbAssoc["npc_clan_flags"]);
|
||||
this.clanRank = toInteger(dbAssoc["npc_clan_rank"]);
|
||||
this.clanTitle = toInteger(dbAssoc["npc_clan_title"]);
|
||||
this.rank = toInteger(dbAssoc["npc_rank"]);
|
||||
this.title = toInteger(dbAssoc["npc_title"]);
|
||||
this.job = toInteger(dbAssoc["npc_job"]);
|
||||
this.interior = toInteger(dbAssoc["npc_int"]);
|
||||
this.dimension = toInteger(dbAssoc["npc_vw"]);
|
||||
this.pedScale = toVector3(toFloat(dbAssoc["npc_scale_x"]), toFloat(dbAssoc["npc_scale_y"]), toFloat(dbAssoc["npc_scale_z"]));
|
||||
this.walkStyle = toInteger(dbAssoc["npc_walkstyle"]);
|
||||
this.fightStyle = toInteger(dbAssoc["npc_fightstyle"]);
|
||||
this.health = toInteger(dbAssoc["npc_health"]);
|
||||
|
||||
@@ -163,101 +163,70 @@ function saveAllNPCsToDatabase() {
|
||||
|
||||
// ===========================================================================
|
||||
|
||||
function saveNPCToDatabase(npc) {
|
||||
if(getNPCData(vehicleDataId) == null) {
|
||||
// Invalid vehicle data
|
||||
function saveNPCToDatabase(npcDataId) {
|
||||
if(getNPCData(npcDataId) == null) {
|
||||
// Invalid NPC data
|
||||
return false;
|
||||
}
|
||||
|
||||
let tempVehicleData = getServerData().vehicles[vehicleDataId];
|
||||
let tempNPCData = getNPCData(npcDataId);
|
||||
|
||||
if(tempVehicleData.databaseId == -1) {
|
||||
// Temp vehicle, no need to save
|
||||
if(tempNPCData.databaseId == -1) {
|
||||
// Temp NPC, no need to save
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!tempVehicleData.needsSaved) {
|
||||
// Vehicle hasn't changed. No need to save.
|
||||
if(!tempNPCData.needsSaved) {
|
||||
// NPC hasn't changed. No need to save.
|
||||
return false;
|
||||
}
|
||||
|
||||
logToConsole(LOG_VERBOSE, `[VRR.Vehicle]: Saving vehicle ${tempVehicleData.databaseId} to database ...`);
|
||||
logToConsole(LOG_VERBOSE, `[VRR.NPC]: Saving NPC ${tempNPCData.databaseId} to database ...`);
|
||||
let dbConnection = connectToDatabase();
|
||||
if(dbConnection) {
|
||||
if(tempVehicleData.vehicle != false) {
|
||||
if(!tempVehicleData.spawnLocked) {
|
||||
if(tempNPCData.ped != false) {
|
||||
if(!tempNPCData.spawnLocked) {
|
||||
if(areServerElementsSupported()) {
|
||||
tempVehicleData.spawnPosition = tempVehicleData.vehicle.position;
|
||||
tempVehicleData.spawnRotation = tempVehicleData.vehicle.heading;
|
||||
tempNPCData.spawnPosition = tempNPCData.vehicle.position;
|
||||
tempNPCData.spawnRotation = tempNPCData.vehicle.heading;
|
||||
} else {
|
||||
tempVehicleData.spawnPosition = tempVehicleData.syncPosition;
|
||||
tempVehicleData.spawnRotation = tempVehicleData.syncHeading;
|
||||
tempNPCData.spawnPosition = tempNPCData.syncPosition;
|
||||
tempNPCData.spawnRotation = tempNPCData.syncHeading;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let data = [
|
||||
["veh_server", getServerId()],
|
||||
["veh_model", toInteger(tempVehicleData.model)],
|
||||
["veh_owner_type", toInteger(tempVehicleData.ownerType)],
|
||||
["veh_owner_id", toInteger(tempVehicleData.ownerId)],
|
||||
["veh_locked", boolToInt(tempVehicleData.locked)],
|
||||
["veh_spawn_lock", boolToInt(tempVehicleData.spawnLocked)],
|
||||
["veh_buy_price", toInteger(tempVehicleData.buyPrice)],
|
||||
["veh_rent_price", toInteger(tempVehicleData.rentPrice)],
|
||||
["veh_pos_x", toFloat(tempVehicleData.spawnPosition.x)],
|
||||
["veh_pos_y", toFloat(tempVehicleData.spawnPosition.y)],
|
||||
["veh_pos_z", toFloat(tempVehicleData.spawnPosition.z)],
|
||||
["veh_rot_z", toFloat(tempVehicleData.spawnRotation)],
|
||||
["veh_col1", toInteger(tempVehicleData.colour1)],
|
||||
["veh_col2", toInteger(tempVehicleData.colour2)],
|
||||
["veh_col3", toInteger(tempVehicleData.colour3)],
|
||||
["veh_col4", toInteger(tempVehicleData.colour4)],
|
||||
["veh_col1_isrgb", boolToInt(tempVehicleData.colour1IsRGBA)],
|
||||
["veh_col2_isrgb", boolToInt(tempVehicleData.colour2IsRGBA)],
|
||||
["veh_col3_isrgb", boolToInt(tempVehicleData.colour3IsRGBA)],
|
||||
["veh_col4_isrgb", boolToInt(tempVehicleData.colour4IsRGBA)],
|
||||
["veh_extra1", tempVehicleData.extras[0]],
|
||||
["veh_extra2", tempVehicleData.extras[1]],
|
||||
["veh_extra3", tempVehicleData.extras[2]],
|
||||
["veh_extra4", tempVehicleData.extras[3]],
|
||||
["veh_extra5", tempVehicleData.extras[4]],
|
||||
["veh_extra6", tempVehicleData.extras[5]],
|
||||
["veh_extra7", tempVehicleData.extras[6]],
|
||||
["veh_extra8", tempVehicleData.extras[7]],
|
||||
["veh_extra9", tempVehicleData.extras[8]],
|
||||
["veh_extra10", tempVehicleData.extras[9]],
|
||||
["veh_extra11", tempVehicleData.extras[10]],
|
||||
["veh_extra12", tempVehicleData.extras[11]],
|
||||
["veh_extra13", tempVehicleData.extras[12]],
|
||||
["veh_engine", intToBool(tempVehicleData.engine)],
|
||||
["veh_lights", intToBool(tempVehicleData.lights)],
|
||||
["veh_health", toInteger(tempVehicleData.health)],
|
||||
["veh_damage_engine", toInteger(tempVehicleData.engineDamage)],
|
||||
["veh_damage_visual", toInteger(tempVehicleData.visualDamage)],
|
||||
["veh_dirt_level", toInteger(tempVehicleData.dirtLevel)],
|
||||
["veh_int", toInteger(tempVehicleData.interior)],
|
||||
["veh_vw", toInteger(tempVehicleData.dimension)],
|
||||
["veh_livery", toInteger(tempVehicleData.livery)],
|
||||
["npc_server", getServerId()],
|
||||
["npc_skin", toInteger(tempNPCData.model)],
|
||||
["npc_owner_type", toInteger(tempNPCData.ownerType)],
|
||||
["npc_owner_id", toInteger(tempNPCData.ownerId)],
|
||||
["npc_pos_x", toFloat(tempNPCData.position.x)],
|
||||
["npc_pos_y", toFloat(tempNPCData.position.y)],
|
||||
["npc_pos_z", toFloat(tempNPCData.position.z)],
|
||||
["npc_rot_z", toFloat(tempNPCData.heading)],
|
||||
["npc_scale_x", toFloat(tempNPCData.scale.x)],
|
||||
["npc_scale_y", toFloat(tempNPCData.scale.y)],
|
||||
["npc_scale_z", toFloat(tempNPCData.scale.z)],
|
||||
];
|
||||
|
||||
let dbQuery = null;
|
||||
if(tempVehicleData.databaseId == 0) {
|
||||
let queryString = createDatabaseInsertQuery("veh_main", data);
|
||||
if(tempNPCData.databaseId == 0) {
|
||||
let queryString = createDatabaseInsertQuery("npc_main", data);
|
||||
dbQuery = queryDatabase(dbConnection, queryString);
|
||||
getServerData().vehicles[vehicleDataId].databaseId = getDatabaseInsertId(dbConnection);
|
||||
getServerData().vehicles[vehicleDataId].needsSaved = false;
|
||||
tempNPCData.databaseId = getDatabaseInsertId(dbConnection);
|
||||
tempNPCData.needsSaved = false;
|
||||
} else {
|
||||
let queryString = createDatabaseUpdateQuery("veh_main", data, `veh_id=${tempVehicleData.databaseId}`);
|
||||
let queryString = createDatabaseUpdateQuery("npc_main", data, `npc_id=${tempNPCData.databaseId}`);
|
||||
dbQuery = queryDatabase(dbConnection, queryString);
|
||||
getServerData().vehicles[vehicleDataId].needsSaved = false;
|
||||
tempNPCData.needsSaved = false;
|
||||
}
|
||||
|
||||
freeDatabaseQuery(dbQuery);
|
||||
disconnectFromDatabase(dbConnection);
|
||||
return true;
|
||||
}
|
||||
logToConsole(LOG_VERBOSE, `[VRR.Vehicle]: Saved vehicle ${vehicleDataId} to database!`);
|
||||
logToConsole(LOG_VERBOSE, `[VRR.NPC]: Saved NPC ${npcDataId} to database!`);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user