Move location generation code into matchcontroller

This commit is contained in:
Dev 2024-07-06 14:04:51 +01:00
parent 494fd66f51
commit 1c3fb5f926
2 changed files with 81 additions and 83 deletions

View File

@ -1,23 +1,14 @@
import { inject, injectable } from "tsyringe"; import { inject, injectable } from "tsyringe";
import { ApplicationContext } from "@spt/context/ApplicationContext";
import { ContextVariableType } from "@spt/context/ContextVariableType";
import { LocationLootGenerator } from "@spt/generators/LocationLootGenerator";
import { ILocationBase } from "@spt/models/eft/common/ILocationBase";
import { ILocationsGenerateAllResponse } from "@spt/models/eft/common/ILocationsSourceDestinationBase"; import { ILocationsGenerateAllResponse } from "@spt/models/eft/common/ILocationsSourceDestinationBase";
import { ILooseLoot, SpawnpointTemplate } from "@spt/models/eft/common/ILooseLoot";
import { IGetAirdropLootResponse } from "@spt/models/eft/location/IGetAirdropLootResponse"; import { IGetAirdropLootResponse } from "@spt/models/eft/location/IGetAirdropLootResponse";
import { ConfigTypes } from "@spt/models/enums/ConfigTypes"; import { ConfigTypes } from "@spt/models/enums/ConfigTypes";
import { ILocationConfig } from "@spt/models/spt/config/ILocationConfig"; import { ILocationConfig } from "@spt/models/spt/config/ILocationConfig";
import { IRaidChanges } from "@spt/models/spt/location/IRaidChanges";
import { ILocations } from "@spt/models/spt/server/ILocations"; import { ILocations } from "@spt/models/spt/server/ILocations";
import { ILogger } from "@spt/models/spt/utils/ILogger"; import { ILogger } from "@spt/models/spt/utils/ILogger";
import { ConfigServer } from "@spt/servers/ConfigServer"; import { ConfigServer } from "@spt/servers/ConfigServer";
import { AirdropService } from "@spt/services/AirdropService"; import { AirdropService } from "@spt/services/AirdropService";
import { DatabaseService } from "@spt/services/DatabaseService"; import { DatabaseService } from "@spt/services/DatabaseService";
import { LocalisationService } from "@spt/services/LocalisationService";
import { RaidTimeAdjustmentService } from "@spt/services/RaidTimeAdjustmentService";
import { ICloner } from "@spt/utils/cloners/ICloner"; import { ICloner } from "@spt/utils/cloners/ICloner";
import { TimeUtil } from "@spt/utils/TimeUtil";
@injectable() @injectable()
export class LocationController export class LocationController
@ -26,87 +17,15 @@ export class LocationController
constructor( constructor(
@inject("PrimaryLogger") protected logger: ILogger, @inject("PrimaryLogger") protected logger: ILogger,
@inject("LocationLootGenerator") protected locationLootGenerator: LocationLootGenerator,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("RaidTimeAdjustmentService") protected raidTimeAdjustmentService: RaidTimeAdjustmentService,
@inject("DatabaseService") protected databaseService: DatabaseService, @inject("DatabaseService") protected databaseService: DatabaseService,
@inject("AirdropService") protected airdropService: AirdropService, @inject("AirdropService") protected airdropService: AirdropService,
@inject("TimeUtil") protected timeUtil: TimeUtil,
@inject("ConfigServer") protected configServer: ConfigServer, @inject("ConfigServer") protected configServer: ConfigServer,
@inject("ApplicationContext") protected applicationContext: ApplicationContext,
@inject("PrimaryCloner") protected cloner: ICloner, @inject("PrimaryCloner") protected cloner: ICloner,
) )
{ {
this.locationConfig = this.configServer.getConfig(ConfigTypes.LOCATION); this.locationConfig = this.configServer.getConfig(ConfigTypes.LOCATION);
} }
/**
* Generate a maps base location and loot
* @param name Map name
* @returns ILocationBase
*/
public generate(name: string): ILocationBase
{
const location = this.databaseService.getLocation(name);
const locationBaseClone = this.cloner.clone(location.base);
// Update datetime property to now
locationBaseClone.UnixDateTime = this.timeUtil.getTimestamp();
// Don't generate loot for hideout
if (name === "hideout")
{
return locationBaseClone;
}
// Check for a loot multipler adjustment in app context and apply if one is found
let locationConfigClone: ILocationConfig;
const raidAdjustments = this.applicationContext
.getLatestValue(ContextVariableType.RAID_ADJUSTMENTS)
?.getValue<IRaidChanges>();
if (raidAdjustments)
{
locationConfigClone = this.cloner.clone(this.locationConfig); // Clone values so they can be used to reset originals later
this.raidTimeAdjustmentService.makeAdjustmentsToMap(raidAdjustments, locationBaseClone);
}
const staticAmmoDist = this.cloner.clone(location.staticAmmo);
// Create containers and add loot to them
const staticLoot = this.locationLootGenerator.generateStaticContainers(locationBaseClone, staticAmmoDist);
locationBaseClone.Loot.push(...staticLoot);
// Add dynamic loot to output loot
const dynamicLootDistClone: ILooseLoot = this.cloner.clone(location.looseLoot);
const dynamicSpawnPoints: SpawnpointTemplate[] = this.locationLootGenerator.generateDynamicLoot(
dynamicLootDistClone,
staticAmmoDist,
name,
);
for (const spawnPoint of dynamicSpawnPoints)
{
locationBaseClone.Loot.push(spawnPoint);
}
// Done generating, log results
this.logger.success(
this.localisationService.getText("location-dynamic_items_spawned_success", dynamicSpawnPoints.length),
);
this.logger.success(this.localisationService.getText("location-generated_success", name));
// Reset loot multipliers back to original values
if (raidAdjustments)
{
this.logger.debug("Resetting loot multipliers back to their original values");
this.locationConfig.staticLootMultiplier = locationConfigClone.staticLootMultiplier;
this.locationConfig.looseLootMultiplier = locationConfigClone.looseLootMultiplier;
this.applicationContext.clearValues(ContextVariableType.RAID_ADJUSTMENTS);
}
return locationBaseClone;
}
/** /**
* Handle client/locations * Handle client/locations
* Get all maps base location properties without loot data * Get all maps base location properties without loot data
@ -135,6 +54,7 @@ export class LocationController
return { locations: locations, paths: locationsFromDb.base.paths }; return { locations: locations, paths: locationsFromDb.base.paths };
} }
/** Handle client/airdrop/loot */
public getAirdropLoot(): IGetAirdropLootResponse public getAirdropLoot(): IGetAirdropLootResponse
{ {
return this.airdropService.generateAirdropLoot(); return this.airdropService.generateAirdropLoot();

View File

@ -3,12 +3,14 @@ import { ApplicationContext } from "@spt/context/ApplicationContext";
import { ContextVariableType } from "@spt/context/ContextVariableType"; import { ContextVariableType } from "@spt/context/ContextVariableType";
import { InraidController } from "@spt/controllers/InraidController"; import { InraidController } from "@spt/controllers/InraidController";
import { LocationController } from "@spt/controllers/LocationController"; import { LocationController } from "@spt/controllers/LocationController";
import { LocationLootGenerator } from "@spt/generators/LocationLootGenerator";
import { LootGenerator } from "@spt/generators/LootGenerator"; import { LootGenerator } from "@spt/generators/LootGenerator";
import { PlayerScavGenerator } from "@spt/generators/PlayerScavGenerator"; import { PlayerScavGenerator } from "@spt/generators/PlayerScavGenerator";
import { HealthHelper } from "@spt/helpers/HealthHelper"; import { HealthHelper } from "@spt/helpers/HealthHelper";
import { InRaidHelper } from "@spt/helpers/InRaidHelper"; import { InRaidHelper } from "@spt/helpers/InRaidHelper";
import { ProfileHelper } from "@spt/helpers/ProfileHelper"; import { ProfileHelper } from "@spt/helpers/ProfileHelper";
import { TraderHelper } from "@spt/helpers/TraderHelper"; import { TraderHelper } from "@spt/helpers/TraderHelper";
import { ILocationBase } from "@spt/models/eft/common/ILocationBase";
import { IPmcData } from "@spt/models/eft/common/IPmcData"; import { IPmcData } from "@spt/models/eft/common/IPmcData";
import { Common } from "@spt/models/eft/common/tables/IBotBase"; import { Common } from "@spt/models/eft/common/tables/IBotBase";
import { Item } from "@spt/models/eft/common/tables/IItem"; import { Item } from "@spt/models/eft/common/tables/IItem";
@ -26,10 +28,12 @@ import { MessageType } from "@spt/models/enums/MessageType";
import { Traders } from "@spt/models/enums/Traders"; import { Traders } from "@spt/models/enums/Traders";
import { IHideoutConfig } from "@spt/models/spt/config/IHideoutConfig"; import { IHideoutConfig } from "@spt/models/spt/config/IHideoutConfig";
import { IInRaidConfig } from "@spt/models/spt/config/IInRaidConfig"; import { IInRaidConfig } from "@spt/models/spt/config/IInRaidConfig";
import { ILocationConfig } from "@spt/models/spt/config/ILocationConfig";
import { IMatchConfig } from "@spt/models/spt/config/IMatchConfig"; import { IMatchConfig } from "@spt/models/spt/config/IMatchConfig";
import { IPmcConfig } from "@spt/models/spt/config/IPmcConfig"; import { IPmcConfig } from "@spt/models/spt/config/IPmcConfig";
import { IRagfairConfig } from "@spt/models/spt/config/IRagfairConfig"; import { IRagfairConfig } from "@spt/models/spt/config/IRagfairConfig";
import { ITraderConfig } from "@spt/models/spt/config/ITraderConfig"; import { ITraderConfig } from "@spt/models/spt/config/ITraderConfig";
import { IRaidChanges } from "@spt/models/spt/location/IRaidChanges";
import { ILogger } from "@spt/models/spt/utils/ILogger"; import { ILogger } from "@spt/models/spt/utils/ILogger";
import { ConfigServer } from "@spt/servers/ConfigServer"; import { ConfigServer } from "@spt/servers/ConfigServer";
import { SaveServer } from "@spt/servers/SaveServer"; import { SaveServer } from "@spt/servers/SaveServer";
@ -43,6 +47,8 @@ import { MatchBotDetailsCacheService } from "@spt/services/MatchBotDetailsCacheS
import { MatchLocationService } from "@spt/services/MatchLocationService"; import { MatchLocationService } from "@spt/services/MatchLocationService";
import { PmcChatResponseService } from "@spt/services/PmcChatResponseService"; import { PmcChatResponseService } from "@spt/services/PmcChatResponseService";
import { ProfileSnapshotService } from "@spt/services/ProfileSnapshotService"; import { ProfileSnapshotService } from "@spt/services/ProfileSnapshotService";
import { RaidTimeAdjustmentService } from "@spt/services/RaidTimeAdjustmentService";
import { ICloner } from "@spt/utils/cloners/ICloner";
import { HashUtil } from "@spt/utils/HashUtil"; import { HashUtil } from "@spt/utils/HashUtil";
import { RandomUtil } from "@spt/utils/RandomUtil"; import { RandomUtil } from "@spt/utils/RandomUtil";
import { TimeUtil } from "@spt/utils/TimeUtil"; import { TimeUtil } from "@spt/utils/TimeUtil";
@ -56,6 +62,7 @@ export class MatchController
protected pmcConfig: IPmcConfig; protected pmcConfig: IPmcConfig;
protected ragfairConfig: IRagfairConfig; protected ragfairConfig: IRagfairConfig;
protected hideoutConfig: IHideoutConfig; protected hideoutConfig: IHideoutConfig;
protected locationConfig: ILocationConfig;
constructor( constructor(
@inject("PrimaryLogger") protected logger: ILogger, @inject("PrimaryLogger") protected logger: ILogger,
@ -81,8 +88,11 @@ export class MatchController
@inject("ProfileSnapshotService") protected profileSnapshotService: ProfileSnapshotService, @inject("ProfileSnapshotService") protected profileSnapshotService: ProfileSnapshotService,
@inject("BotGenerationCacheService") protected botGenerationCacheService: BotGenerationCacheService, @inject("BotGenerationCacheService") protected botGenerationCacheService: BotGenerationCacheService,
@inject("MailSendService") protected mailSendService: MailSendService, @inject("MailSendService") protected mailSendService: MailSendService,
@inject("RaidTimeAdjustmentService") protected raidTimeAdjustmentService: RaidTimeAdjustmentService,
@inject("LootGenerator") protected lootGenerator: LootGenerator, @inject("LootGenerator") protected lootGenerator: LootGenerator,
@inject("ApplicationContext") protected applicationContext: ApplicationContext, @inject("ApplicationContext") protected applicationContext: ApplicationContext,
@inject("LocationLootGenerator") protected locationLootGenerator: LocationLootGenerator,
@inject("PrimaryCloner") protected cloner: ICloner,
) )
{ {
this.matchConfig = this.configServer.getConfig(ConfigTypes.MATCH); this.matchConfig = this.configServer.getConfig(ConfigTypes.MATCH);
@ -91,6 +101,7 @@ export class MatchController
this.pmcConfig = this.configServer.getConfig(ConfigTypes.PMC); this.pmcConfig = this.configServer.getConfig(ConfigTypes.PMC);
this.ragfairConfig = this.configServer.getConfig(ConfigTypes.RAGFAIR); this.ragfairConfig = this.configServer.getConfig(ConfigTypes.RAGFAIR);
this.hideoutConfig = this.configServer.getConfig(ConfigTypes.HIDEOUT); this.hideoutConfig = this.configServer.getConfig(ConfigTypes.HIDEOUT);
this.locationConfig = this.configServer.getConfig(ConfigTypes.LOCATION);
} }
public getEnabled(): boolean public getEnabled(): boolean
@ -373,14 +384,13 @@ export class MatchController
public startLocalRaid(sessionId: string, request: IStartLocalRaidRequestData): IStartLocalRaidResponseData public startLocalRaid(sessionId: string, request: IStartLocalRaidRequestData): IStartLocalRaidResponseData
{ {
// TODO - remove usage of locationController - controller use inside match controller = bad
const playerProfile = this.profileHelper.getPmcProfile(sessionId); const playerProfile = this.profileHelper.getPmcProfile(sessionId);
const result: IStartLocalRaidResponseData = { const result: IStartLocalRaidResponseData = {
serverId: `${request.location}.${request.playerSide}.${this.timeUtil.getTimestamp()}`, // TODO - does this need to be more verbose - investigate client? serverId: `${request.location}.${request.playerSide}.${this.timeUtil.getTimestamp()}`, // TODO - does this need to be more verbose - investigate client?
serverSettings: this.databaseService.getLocationServices(), // TODO - is this per map or global? serverSettings: this.databaseService.getLocationServices(), // TODO - is this per map or global?
profile: { insuredItems: playerProfile.InsuredItems }, profile: { insuredItems: playerProfile.InsuredItems },
locationLoot: this.locationController.generate(request.location), // Move out of controller locationLoot: this.generateLocationAndLoot(request.location),
}; };
// Clear bot cache ready for a fresh raid // Clear bot cache ready for a fresh raid
@ -389,6 +399,74 @@ export class MatchController
return result; return result;
} }
/**
* Generate a maps base location and loot
* @param name Map name
* @returns ILocationBase
*/
protected generateLocationAndLoot(name: string): ILocationBase
{
const location = this.databaseService.getLocation(name);
const locationBaseClone = this.cloner.clone(location.base);
// Update datetime property to now
locationBaseClone.UnixDateTime = this.timeUtil.getTimestamp();
// Don't generate loot for hideout
if (name === "hideout")
{
return locationBaseClone;
}
// Check for a loot multipler adjustment in app context and apply if one is found
let locationConfigClone: ILocationConfig;
const raidAdjustments = this.applicationContext
.getLatestValue(ContextVariableType.RAID_ADJUSTMENTS)
?.getValue<IRaidChanges>();
if (raidAdjustments)
{
locationConfigClone = this.cloner.clone(this.locationConfig); // Clone values so they can be used to reset originals later
this.raidTimeAdjustmentService.makeAdjustmentsToMap(raidAdjustments, locationBaseClone);
}
const staticAmmoDist = this.cloner.clone(location.staticAmmo);
// Create containers and add loot to them
const staticLoot = this.locationLootGenerator.generateStaticContainers(locationBaseClone, staticAmmoDist);
locationBaseClone.Loot.push(...staticLoot);
// Add dynamic loot to output loot
const dynamicLootDistClone = this.cloner.clone(location.looseLoot);
const dynamicSpawnPoints = this.locationLootGenerator.generateDynamicLoot(
dynamicLootDistClone,
staticAmmoDist,
name,
);
for (const spawnPoint of dynamicSpawnPoints)
{
locationBaseClone.Loot.push(spawnPoint);
}
// Done generating, log results
this.logger.success(
this.localisationService.getText("location-dynamic_items_spawned_success", dynamicSpawnPoints.length),
);
this.logger.success(this.localisationService.getText("location-generated_success", name));
// Reset loot multipliers back to original values
if (raidAdjustments)
{
this.logger.debug("Resetting loot multipliers back to their original values");
this.locationConfig.staticLootMultiplier = locationConfigClone.staticLootMultiplier;
this.locationConfig.looseLootMultiplier = locationConfigClone.looseLootMultiplier;
this.applicationContext.clearValues(ContextVariableType.RAID_ADJUSTMENTS);
}
return locationBaseClone;
}
public endLocalRaid(sessionId: string, request: IEndLocalRaidRequestData): void public endLocalRaid(sessionId: string, request: IEndLocalRaidRequestData): void
{ {
const fullProfile = this.profileHelper.getFullProfile(sessionId); const fullProfile = this.profileHelper.getFullProfile(sessionId);