mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-10-12 10:28:05 +00:00

Features: - Remove random objectIDs entirely - Replace random objectIDs with persistentIDs - Remove the need to contact the MASTER server for a persistent ID - Add persistent ID logic to WorldServers that use transactions to guarantee unique IDs no matter when they are generated - Default character xml version to be the most recent one Fixes: - Return optional from GetModel (and check for nullopt where it may exist) - Regenerate inventory item ids on first login to be unique item IDs (fixes all those random IDs Pet IDs and subkeys are left alone and are assumed to be reserved (checks are there to prevent this) There is also duplicate check logic in place for properties and UGC/Models
203 lines
6.7 KiB
C++
203 lines
6.7 KiB
C++
#include "SQLiteDatabase.h"
|
|
#include "ePropertySortType.h"
|
|
|
|
IProperty::Info ReadPropertyInfo(CppSQLite3Query& propertyEntry) {
|
|
IProperty::Info toReturn;
|
|
toReturn.id = propertyEntry.getInt64Field("id");
|
|
toReturn.ownerId = propertyEntry.getInt64Field("owner_id");
|
|
toReturn.cloneId = propertyEntry.getInt64Field("clone_id");
|
|
toReturn.name = propertyEntry.getStringField("name");
|
|
toReturn.description = propertyEntry.getStringField("description");
|
|
toReturn.privacyOption = propertyEntry.getIntField("privacy_option");
|
|
toReturn.rejectionReason = propertyEntry.getStringField("rejection_reason");
|
|
toReturn.lastUpdatedTime = propertyEntry.getIntField("last_updated");
|
|
toReturn.claimedTime = propertyEntry.getIntField("time_claimed");
|
|
toReturn.reputation = propertyEntry.getIntField("reputation");
|
|
toReturn.modApproved = propertyEntry.getIntField("mod_approved");
|
|
toReturn.performanceCost = propertyEntry.getFloatField("performance_cost");
|
|
return toReturn;
|
|
}
|
|
|
|
std::optional<IProperty::PropertyEntranceResult> SQLiteDatabase::GetProperties(const IProperty::PropertyLookup& params) {
|
|
std::optional<IProperty::PropertyEntranceResult> result;
|
|
std::string query;
|
|
std::pair<CppSQLite3Statement, CppSQLite3Query> propertiesRes;
|
|
|
|
if (params.sortChoice == SORT_TYPE_FEATURED || params.sortChoice == SORT_TYPE_FRIENDS) {
|
|
query = R"QUERY(
|
|
FROM properties as p
|
|
JOIN charinfo as ci
|
|
ON ci.prop_clone_id = p.clone_id
|
|
where p.zone_id = ?
|
|
AND (
|
|
p.description LIKE ?
|
|
OR p.name LIKE ?
|
|
OR ci.name LIKE ?
|
|
)
|
|
AND p.privacy_option >= ?
|
|
AND p.owner_id IN (
|
|
SELECT fr.requested_player AS player FROM (
|
|
SELECT CASE
|
|
WHEN player_id = ? THEN friend_id
|
|
WHEN friend_id = ? THEN player_id
|
|
END AS requested_player FROM friends
|
|
) AS fr
|
|
JOIN charinfo AS ci ON ci.id = fr.requested_player
|
|
WHERE fr.requested_player IS NOT NULL AND fr.requested_player != ?
|
|
) ORDER BY ci.name ASC
|
|
)QUERY";
|
|
const auto completeQuery = "SELECT p.* " + query + " LIMIT ? OFFSET ?;";
|
|
propertiesRes = ExecuteSelect(
|
|
completeQuery,
|
|
params.mapId,
|
|
"%" + params.searchString + "%",
|
|
"%" + params.searchString + "%",
|
|
"%" + params.searchString + "%",
|
|
params.playerSort,
|
|
params.playerId,
|
|
params.playerId,
|
|
params.playerId,
|
|
params.numResults,
|
|
params.startIndex
|
|
);
|
|
const auto countQuery = "SELECT COUNT(*) as count" + query + ";";
|
|
auto [_, count] = ExecuteSelect(
|
|
countQuery,
|
|
params.mapId,
|
|
"%" + params.searchString + "%",
|
|
"%" + params.searchString + "%",
|
|
"%" + params.searchString + "%",
|
|
params.playerSort,
|
|
params.playerId,
|
|
params.playerId,
|
|
params.playerId
|
|
);
|
|
if (!count.eof()) {
|
|
result = IProperty::PropertyEntranceResult();
|
|
result->totalEntriesMatchingQuery = count.getIntField("count");
|
|
}
|
|
} else {
|
|
if (params.sortChoice == SORT_TYPE_REPUTATION) {
|
|
query = R"QUERY(
|
|
FROM properties as p
|
|
JOIN charinfo as ci
|
|
ON ci.prop_clone_id = p.clone_id
|
|
where p.zone_id = ?
|
|
AND (
|
|
p.description LIKE ?
|
|
OR p.name LIKE ?
|
|
OR ci.name LIKE ?
|
|
)
|
|
AND p.privacy_option >= ?
|
|
ORDER BY p.reputation DESC, p.last_updated DESC
|
|
)QUERY";
|
|
} else {
|
|
query = R"QUERY(
|
|
FROM properties as p
|
|
JOIN charinfo as ci
|
|
ON ci.prop_clone_id = p.clone_id
|
|
where p.zone_id = ?
|
|
AND (
|
|
p.description LIKE ?
|
|
OR p.name LIKE ?
|
|
OR ci.name LIKE ?
|
|
)
|
|
AND p.privacy_option >= ?
|
|
ORDER BY p.last_updated DESC
|
|
)QUERY";
|
|
}
|
|
const auto completeQuery = "SELECT p.* " + query + " LIMIT ? OFFSET ?;";
|
|
propertiesRes = ExecuteSelect(
|
|
completeQuery,
|
|
params.mapId,
|
|
"%" + params.searchString + "%",
|
|
"%" + params.searchString + "%",
|
|
"%" + params.searchString + "%",
|
|
params.playerSort,
|
|
params.numResults,
|
|
params.startIndex
|
|
);
|
|
const auto countQuery = "SELECT COUNT(*) as count" + query + ";";
|
|
auto [_, count] = ExecuteSelect(
|
|
countQuery,
|
|
params.mapId,
|
|
"%" + params.searchString + "%",
|
|
"%" + params.searchString + "%",
|
|
"%" + params.searchString + "%",
|
|
params.playerSort
|
|
);
|
|
if (!count.eof()) {
|
|
result = IProperty::PropertyEntranceResult();
|
|
result->totalEntriesMatchingQuery = count.getIntField("count");
|
|
}
|
|
}
|
|
|
|
auto& [_, properties] = propertiesRes;
|
|
if (!properties.eof() && !result.has_value()) result = IProperty::PropertyEntranceResult();
|
|
while (!properties.eof()) {
|
|
result->entries.push_back(ReadPropertyInfo(properties));
|
|
properties.nextRow();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
std::optional<IProperty::Info> SQLiteDatabase::GetPropertyInfo(const LWOMAPID mapId, const LWOCLONEID cloneId) {
|
|
auto [_, propertyEntry] = ExecuteSelect(
|
|
"SELECT id, owner_id, clone_id, name, description, privacy_option, rejection_reason, last_updated, time_claimed, reputation, mod_approved, performance_cost "
|
|
"FROM properties WHERE zone_id = ? AND clone_id = ?;", mapId, cloneId);
|
|
|
|
if (propertyEntry.eof()) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
return ReadPropertyInfo(propertyEntry);
|
|
}
|
|
|
|
void SQLiteDatabase::UpdatePropertyModerationInfo(const IProperty::Info& info) {
|
|
ExecuteUpdate("UPDATE properties SET privacy_option = ?, rejection_reason = ?, mod_approved = ? WHERE id = ?;",
|
|
info.privacyOption,
|
|
info.rejectionReason,
|
|
info.modApproved,
|
|
info.id);
|
|
}
|
|
|
|
void SQLiteDatabase::UpdatePropertyDetails(const IProperty::Info& info) {
|
|
ExecuteUpdate("UPDATE properties SET name = ?, description = ? WHERE id = ?;", info.name, info.description, info.id);
|
|
}
|
|
|
|
void SQLiteDatabase::UpdateLastSave(const IProperty::Info& info) {
|
|
ExecuteUpdate("UPDATE properties SET last_updated = ? WHERE id = ?;", info.lastUpdatedTime, info.id);
|
|
}
|
|
|
|
void SQLiteDatabase::UpdatePerformanceCost(const LWOZONEID& zoneId, const float performanceCost) {
|
|
ExecuteUpdate("UPDATE properties SET performance_cost = ? WHERE zone_id = ? AND clone_id = ?;", performanceCost, zoneId.GetMapID(), zoneId.GetCloneID());
|
|
}
|
|
|
|
void SQLiteDatabase::InsertNewProperty(const IProperty::Info& info, const uint32_t templateId, const LWOZONEID& zoneId) {
|
|
auto insertion = ExecuteInsert(
|
|
"INSERT INTO properties"
|
|
" (id, owner_id, template_id, clone_id, name, description, zone_id, rent_amount, rent_due, privacy_option, last_updated, time_claimed, rejection_reason, reputation, performance_cost)"
|
|
" VALUES (?, ?, ?, ?, ?, ?, ?, 0, 0, 0, CAST(strftime('%s', 'now') as INT), CAST(strftime('%s', 'now') as INT), '', 0, 0.0)",
|
|
info.id,
|
|
info.ownerId,
|
|
templateId,
|
|
zoneId.GetCloneID(),
|
|
info.name,
|
|
info.description,
|
|
zoneId.GetMapID()
|
|
);
|
|
}
|
|
|
|
std::optional<IProperty::Info> SQLiteDatabase::GetPropertyInfo(const LWOOBJID id) {
|
|
auto [_, propertyEntry] = ExecuteSelect(
|
|
"SELECT id, owner_id, clone_id, name, description, privacy_option, rejection_reason, last_updated, time_claimed, reputation, mod_approved, performance_cost "
|
|
"FROM properties WHERE id = ?;", id);
|
|
|
|
if (propertyEntry.eof()) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
return ReadPropertyInfo(propertyEntry);
|
|
}
|