0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 09:50:43 -05:00
server/project/src/services/DatabaseService.ts

301 lines
9.4 KiB
TypeScript
Raw Normal View History

import { inject, injectable } from "tsyringe";
import { IGlobals } from "@spt/models/eft/common/IGlobals";
import { ILocation } from "@spt/models/eft/common/ILocation";
import { IAchievement } from "@spt/models/eft/common/tables/IAchievement";
import { ICustomizationItem } from "@spt/models/eft/common/tables/ICustomizationItem";
import { IHandbookBase } from "@spt/models/eft/common/tables/IHandbookBase";
import { IMatch } from "@spt/models/eft/common/tables/IMatch";
import { IProfileTemplates } from "@spt/models/eft/common/tables/IProfileTemplate";
import { IQuest } from "@spt/models/eft/common/tables/IQuest";
import { ITemplateItem } from "@spt/models/eft/common/tables/ITemplateItem";
import { ITrader } from "@spt/models/eft/common/tables/ITrader";
import { IBots } from "@spt/models/spt/bots/IBots";
import { ILocationConfig } from "@spt/models/spt/config/ILocationConfig";
import { IHideout } from "@spt/models/spt/hideout/IHideout";
import { IDatabaseTables } from "@spt/models/spt/server/IDatabaseTables";
import { ILocaleBase } from "@spt/models/spt/server/ILocaleBase";
import { ILocations } from "@spt/models/spt/server/ILocations";
import { IServerBase } from "@spt/models/spt/server/IServerBase";
import { ISettingsBase } from "@spt/models/spt/server/ISettingsBase";
import { ITemplates } from "@spt/models/spt/templates/ITemplates";
import { ILogger } from "@spt/models/spt/utils/ILogger";
import { DatabaseServer } from "@spt/servers/DatabaseServer";
import { LocalisationService } from "./LocalisationService";
@injectable()
export class DatabaseService
{
protected locationConfig: ILocationConfig;
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("LocalisationService") protected localisationService: LocalisationService,
)
{
}
/**
* @returns assets/database/
*/
public getTables(): IDatabaseTables
{
return this.databaseServer.getTables();
}
/**
* @returns assets/database/bots/
*/
public getBots(): IBots
{
if (!this.databaseServer.getTables().bots)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/bots"));
}
return this.databaseServer.getTables().bots!;
}
/**
* @returns assets/database/globals.json
*/
public getGlobals(): IGlobals
{
if (!this.databaseServer.getTables().globals)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/globals.json"));
}
return this.databaseServer.getTables().globals!;
}
/**
* @returns assets/database/hideout/
*/
public getHideout(): IHideout
{
if (!this.databaseServer.getTables().hideout)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/hideout"));
}
return this.databaseServer.getTables().hideout!;
}
/**
* @returns assets/database/locales/
*/
public getLocales(): ILocaleBase
{
if (!this.databaseServer.getTables().locales)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/locales"));
}
return this.databaseServer.getTables().locales!;
}
/**
* @returns assets/database/locations
*/
public getLocations(): ILocations
{
if (!this.databaseServer.getTables().locales)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/locales"));
}
return this.databaseServer.getTables().locations!;
}
/**
* Get specific location by its Id
* @param locationId Desired trader id
* @returns assets/database/locations/
*/
public getLocation(locationId: string): ILocation
{
const locations = this.getLocations();
const desiredLocation = locations[locationId];
if (!desiredLocation)
{
throw new Error(this.localisationService.getText("database-no_location_found_with_id", locationId));
}
return desiredLocation!;
}
/**
* @returns assets/database/match/
*/
public getMatch(): IMatch
{
if (!this.databaseServer.getTables().locales)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/locales"));
}
return this.databaseServer.getTables().match!;
}
/**
* @returns assets/database/server.json
*/
public getServer(): IServerBase
{
if (!this.databaseServer.getTables().locales)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/locales"));
}
return this.databaseServer.getTables().server!;
}
/**
* @returns assets/database/settings.json
*/
public getSettings(): ISettingsBase
{
if (!this.databaseServer.getTables().locales)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/locales"));
}
return this.databaseServer.getTables().settings!;
}
/**
* @returns assets/database/templates/
*/
public getTemplates(): ITemplates
{
if (!this.databaseServer.getTables().templates)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates"));
}
return this.databaseServer.getTables().templates!;
}
/**
* @returns assets/database/templates/achievements.json
*/
public getAchievements(): IAchievement[]
{
if (!this.databaseServer.getTables().templates!.achievements)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/achievements.json"));
}
return this.databaseServer.getTables().templates!.achievements!;
}
/**
* @returns assets/database/templates/customisation.json
*/
public getCustomization(): Record<string, ICustomizationItem>
{
if (!this.databaseServer.getTables().templates!.customization)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/customization.json"));
}
return this.databaseServer.getTables().templates!.customization!;
}
/**
* @returns assets/database/templates/items.json
*/
public getHandbook(): IHandbookBase
{
if (!this.databaseServer.getTables().templates!.handbook)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/handbook.json"));
}
return this.databaseServer.getTables().templates!.handbook!;
}
/**
* @returns assets/database/templates/items.json
*/
public getItems(): Record<string, ITemplateItem>
{
if (!this.databaseServer.getTables().templates!.items)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/items.json"));
}
return this.databaseServer.getTables().templates!.items!;
}
/**
* @returns assets/database/templates/prices.json
*/
public getPrices(): Record<string, number>
{
if (!this.databaseServer.getTables().templates!.prices)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/prices.json"));
}
return this.databaseServer.getTables().templates!.prices!;
}
/**
* @returns assets/database/templates/profiles.json
*/
public getProfiles(): IProfileTemplates
{
if (!this.databaseServer.getTables().templates!.profiles)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/profiles.json"));
}
return this.databaseServer.getTables().templates!.profiles!;
}
/**
* @returns assets/database/templates/items.json
*/
public getQuests(): Record<string, IQuest>
{
if (!this.databaseServer.getTables().templates!.quests)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/templates/quests.json"));
}
return this.databaseServer.getTables().templates!.quests!;
}
/**
* @returns assets/database/traders/
*/
public getTraders(): Record<string, ITrader>
{
if (!this.databaseServer.getTables().traders)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-data_at_path_missing", "assets/database/traders"));
}
return this.databaseServer.getTables().traders!;
}
/**
* Get specific trader by their Id
* @param traderId Desired trader id
* @returns assets/database/traders/
*/
public getTrader(traderId: string): ITrader
{
const traders = this.getTraders();
const desiredTrader = traders[traderId];
if (!desiredTrader)
{
2024-05-28 14:23:01 +01:00
throw new Error(this.localisationService.getText("database-no_trader_found_with_id", traderId));
}
return desiredTrader!;
}
}