Updated example 2 to use DatabaseService + added extra example

This commit is contained in:
Dev 2024-05-29 10:00:19 +01:00
parent d73272ad2d
commit bac95b3617

View File

@ -1,54 +1,58 @@
import { DependencyContainer } from "tsyringe"; import { DependencyContainer } from "tsyringe";
import { IPostDBLoadMod } from "@spt/models/external/IPostDBLoadMod"; import { IPostDBLoadMod } from "@spt/models/external/IPostDBLoadMod";
import { DatabaseServer } from "@spt/servers/DatabaseServer"; import { DatabaseService } from "@spt/services/DatabaseService";
import { IDatabaseTables } from "@spt/models/spt/server/IDatabaseTables";
import { ItemHelper } from "@spt/helpers/ItemHelper"; import { ItemHelper } from "@spt/helpers/ItemHelper";
import { BaseClasses } from "@spt/models/enums/BaseClasses"; import { BaseClasses } from "@spt/models/enums/BaseClasses";
class Mod implements IPostDBLoadMod class Mod implements IPostDBLoadMod {
{
public postDBLoad(container: DependencyContainer): void public postDBLoad(container: DependencyContainer): void
{ {
// get database from server // Get database service from server
const databaseServer = container.resolve<DatabaseServer>("DatabaseServer"); const databaseService = container.resolve<DatabaseService>("DatabaseService");
// Get all the in-memory json found in /assets/database // Get all the in-memory item data found in /assets/database/templates/items.json
const tables: IDatabaseTables = databaseServer.getTables(); const itemsInDb = databaseService.getItems();
// --------------------------------------------------------- // ------------------------------------------------------------------
// example #1 // Example #1
// Make the LEDX item sellable on flea market // Make the LedX item sellable on flea market
// Find the ledx item by its Id // Find the LedX item by its Id
const ledx = tables.templates.items["5c0530ee86f774697952d952"]; const ledx = itemsInDb["5c0530ee86f774697952d952"]; // The long string of characters is the ledx Id, you can find these Ids via: https://db.sp-tarkov.com/search
// Update one of its properties to be true // Update the LedX `CanSellOnRagfair` property to be true
// This will alter ALL LedX items
ledx._props.CanSellOnRagfair = true; ledx._props.CanSellOnRagfair = true;
// --------------------------------------------------------- // ------------------------------------------------------------------
// example #2 // Example #2
// Get globals settings and set flea market min level to be 1 // Set flea market min level to be 1 for all players
tables.globals.config.RagFair.minUserLevel = 1;
// --------------------------------------------------------- // Get globals data from database
const globals = databaseService.getGlobals();
// Set ragfair property `minUserLevel` to 1
globals.config.RagFair.minUserLevel = 1;
// ------------------------------------------------------------------
// Example #3 // Example #3
// Loop over all magazines and make them weigh nothing // Loop over all magazines in database and make them weigh nothing
// Get ItemHelper ready to use // Prepare the ItemHelper class so we can use it
const itemHelper: ItemHelper = container.resolve<ItemHelper>("ItemHelper"); const itemHelper: ItemHelper = container.resolve<ItemHelper>("ItemHelper");
// Get all items in the database as an array so we can loop over them later // Get all items in the database as an array so we can loop over them later
// tables.templates.items is a dictionary, the key being the items template id, the value being the objects data, // `databaseService.getItems()` returns a dictionary, the key being the items template id, the value being the item itself,
// we want to convert it into an array so we can loop over all the items easily // We want to convert it into an array so we can loop over all the items easily
// Object.values lets us grab the 'value' part as an array and ignore the 'key' part // Object.values lets us grab the 'value' part as an array and ignore the 'key' part
const items = Object.values(tables.templates.items); const items = Object.values(itemsInDb);
// Use the itemHelper class to assist us in getting only magazines // Use the `ItemHelper` class to assist us in getting only magazines
// We are filtering all items to only those with a base class of MAGAZINE (5448bc234bdc2d3c308b4569) // We are filtering all items to only those with a base class of MAGAZINE (5448bc234bdc2d3c308b4569)
const magazines = items.filter(x => itemHelper.isOfBaseclass(x._id, BaseClasses.MAGAZINE)); const magazines = items.filter(x => itemHelper.isOfBaseclass(x._id, BaseClasses.MAGAZINE));
// Loop over all the magazines the above code found // Loop over all the magazines we found using above code
for (const magazine of magazines) for (const magazine of magazines)
{ {
// Check the magazine has a weight property before we edit it // Check the magazine has a weight property before we edit it
@ -58,6 +62,17 @@ class Mod implements IPostDBLoadMod
magazine._props.Weight = 0; magazine._props.Weight = 0;
} }
} }
// ------------------------------------------------------------------
// Example #4
// Adjust the red keycards flea price to be one million
// Get all flea prices
// The prices are stored as a dictionary too, the key is the items ID, the value is the rouble price
const fleaPrices = databaseService.getPrices();
// Edit the red keycard price
fleaPrices["5c1d0efb86f7744baf2e7b7b"] = 1000000; // We can use this site to find item Ids: https://db.sp-tarkov.com/search
} }
} }