NPC class, data load and save
This commit is contained in:
@@ -1468,8 +1468,10 @@ class NPCData {
|
|||||||
this.middleName = "Q";
|
this.middleName = "Q";
|
||||||
this.skin = 0;
|
this.skin = 0;
|
||||||
this.cash = 0;
|
this.cash = 0;
|
||||||
this.spawnPosition = toVector3(0.0, 0.0, 0.0);
|
this.position = toVector3(0.0, 0.0, 0.0);
|
||||||
this.spawnHeading = 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.clan = 0;
|
||||||
this.isWorking = false;
|
this.isWorking = false;
|
||||||
this.jobUniform = this.skin;
|
this.jobUniform = this.skin;
|
||||||
@@ -1478,7 +1480,6 @@ class NPCData {
|
|||||||
this.weapons = [];
|
this.weapons = [];
|
||||||
this.interior = 0;
|
this.interior = 0;
|
||||||
this.dimension = 0;
|
this.dimension = 0;
|
||||||
this.pedScale = toVector3(1.0, 1.0, 1.0);
|
|
||||||
this.walkStyle = 0;
|
this.walkStyle = 0;
|
||||||
this.fightStyle = 0;
|
this.fightStyle = 0;
|
||||||
this.health = 100;
|
this.health = 100;
|
||||||
@@ -1514,17 +1515,16 @@ class NPCData {
|
|||||||
this.middleName = dbAssoc["npc_name_middle"] || "";
|
this.middleName = dbAssoc["npc_name_middle"] || "";
|
||||||
this.skin = toInteger(dbAssoc["npc_skin"]);
|
this.skin = toInteger(dbAssoc["npc_skin"]);
|
||||||
this.cash = toInteger(dbAssoc["npc_cash"]);
|
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.position = toVector3(toFloat(dbAssoc["npc_pos_x"]), toFloat(dbAssoc["npc_pos_y"]), toFloat(dbAssoc["npc_pos_z"]));
|
||||||
this.spawnHeading = toFloat(dbAssoc["npc_angle"]);
|
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.lastLogin = toInteger(dbAssoc["npc_when_lastlogin"]);
|
||||||
this.clan = toInteger(dbAssoc["npc_clan"]);
|
this.rank = toInteger(dbAssoc["npc_rank"]);
|
||||||
this.clanFlags = toInteger(dbAssoc["npc_clan_flags"]);
|
this.title = toInteger(dbAssoc["npc_title"]);
|
||||||
this.clanRank = toInteger(dbAssoc["npc_clan_rank"]);
|
|
||||||
this.clanTitle = toInteger(dbAssoc["npc_clan_title"]);
|
|
||||||
this.job = toInteger(dbAssoc["npc_job"]);
|
this.job = toInteger(dbAssoc["npc_job"]);
|
||||||
this.interior = toInteger(dbAssoc["npc_int"]);
|
this.interior = toInteger(dbAssoc["npc_int"]);
|
||||||
this.dimension = toInteger(dbAssoc["npc_vw"]);
|
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.walkStyle = toInteger(dbAssoc["npc_walkstyle"]);
|
||||||
this.fightStyle = toInteger(dbAssoc["npc_fightstyle"]);
|
this.fightStyle = toInteger(dbAssoc["npc_fightstyle"]);
|
||||||
this.health = toInteger(dbAssoc["npc_health"]);
|
this.health = toInteger(dbAssoc["npc_health"]);
|
||||||
|
|||||||
@@ -163,101 +163,70 @@ function saveAllNPCsToDatabase() {
|
|||||||
|
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
|
|
||||||
function saveNPCToDatabase(npc) {
|
function saveNPCToDatabase(npcDataId) {
|
||||||
if(getNPCData(vehicleDataId) == null) {
|
if(getNPCData(npcDataId) == null) {
|
||||||
// Invalid vehicle data
|
// Invalid NPC data
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let tempVehicleData = getServerData().vehicles[vehicleDataId];
|
let tempNPCData = getNPCData(npcDataId);
|
||||||
|
|
||||||
if(tempVehicleData.databaseId == -1) {
|
if(tempNPCData.databaseId == -1) {
|
||||||
// Temp vehicle, no need to save
|
// Temp NPC, no need to save
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!tempVehicleData.needsSaved) {
|
if(!tempNPCData.needsSaved) {
|
||||||
// Vehicle hasn't changed. No need to save.
|
// NPC hasn't changed. No need to save.
|
||||||
return false;
|
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();
|
let dbConnection = connectToDatabase();
|
||||||
if(dbConnection) {
|
if(dbConnection) {
|
||||||
if(tempVehicleData.vehicle != false) {
|
if(tempNPCData.ped != false) {
|
||||||
if(!tempVehicleData.spawnLocked) {
|
if(!tempNPCData.spawnLocked) {
|
||||||
if(areServerElementsSupported()) {
|
if(areServerElementsSupported()) {
|
||||||
tempVehicleData.spawnPosition = tempVehicleData.vehicle.position;
|
tempNPCData.spawnPosition = tempNPCData.vehicle.position;
|
||||||
tempVehicleData.spawnRotation = tempVehicleData.vehicle.heading;
|
tempNPCData.spawnRotation = tempNPCData.vehicle.heading;
|
||||||
} else {
|
} else {
|
||||||
tempVehicleData.spawnPosition = tempVehicleData.syncPosition;
|
tempNPCData.spawnPosition = tempNPCData.syncPosition;
|
||||||
tempVehicleData.spawnRotation = tempVehicleData.syncHeading;
|
tempNPCData.spawnRotation = tempNPCData.syncHeading;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = [
|
let data = [
|
||||||
["veh_server", getServerId()],
|
["npc_server", getServerId()],
|
||||||
["veh_model", toInteger(tempVehicleData.model)],
|
["npc_skin", toInteger(tempNPCData.model)],
|
||||||
["veh_owner_type", toInteger(tempVehicleData.ownerType)],
|
["npc_owner_type", toInteger(tempNPCData.ownerType)],
|
||||||
["veh_owner_id", toInteger(tempVehicleData.ownerId)],
|
["npc_owner_id", toInteger(tempNPCData.ownerId)],
|
||||||
["veh_locked", boolToInt(tempVehicleData.locked)],
|
["npc_pos_x", toFloat(tempNPCData.position.x)],
|
||||||
["veh_spawn_lock", boolToInt(tempVehicleData.spawnLocked)],
|
["npc_pos_y", toFloat(tempNPCData.position.y)],
|
||||||
["veh_buy_price", toInteger(tempVehicleData.buyPrice)],
|
["npc_pos_z", toFloat(tempNPCData.position.z)],
|
||||||
["veh_rent_price", toInteger(tempVehicleData.rentPrice)],
|
["npc_rot_z", toFloat(tempNPCData.heading)],
|
||||||
["veh_pos_x", toFloat(tempVehicleData.spawnPosition.x)],
|
["npc_scale_x", toFloat(tempNPCData.scale.x)],
|
||||||
["veh_pos_y", toFloat(tempVehicleData.spawnPosition.y)],
|
["npc_scale_y", toFloat(tempNPCData.scale.y)],
|
||||||
["veh_pos_z", toFloat(tempVehicleData.spawnPosition.z)],
|
["npc_scale_z", toFloat(tempNPCData.scale.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)],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let dbQuery = null;
|
let dbQuery = null;
|
||||||
if(tempVehicleData.databaseId == 0) {
|
if(tempNPCData.databaseId == 0) {
|
||||||
let queryString = createDatabaseInsertQuery("veh_main", data);
|
let queryString = createDatabaseInsertQuery("npc_main", data);
|
||||||
dbQuery = queryDatabase(dbConnection, queryString);
|
dbQuery = queryDatabase(dbConnection, queryString);
|
||||||
getServerData().vehicles[vehicleDataId].databaseId = getDatabaseInsertId(dbConnection);
|
tempNPCData.databaseId = getDatabaseInsertId(dbConnection);
|
||||||
getServerData().vehicles[vehicleDataId].needsSaved = false;
|
tempNPCData.needsSaved = false;
|
||||||
} else {
|
} 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);
|
dbQuery = queryDatabase(dbConnection, queryString);
|
||||||
getServerData().vehicles[vehicleDataId].needsSaved = false;
|
tempNPCData.needsSaved = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeDatabaseQuery(dbQuery);
|
freeDatabaseQuery(dbQuery);
|
||||||
disconnectFromDatabase(dbConnection);
|
disconnectFromDatabase(dbConnection);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
logToConsole(LOG_VERBOSE, `[VRR.Vehicle]: Saved vehicle ${vehicleDataId} to database!`);
|
logToConsole(LOG_VERBOSE, `[VRR.NPC]: Saved NPC ${npcDataId} to database!`);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user