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

Fixed free refreshes still costing roubles/rep to replace

Refactor of `useFreeRefreshIfAvailable()` to improve readability
This commit is contained in:
Dev 2024-07-01 10:42:16 +01:00
parent bd28cbca57
commit a63d2307df

View File

@ -535,7 +535,7 @@ export class RepeatableQuestController
const fullProfile = this.profileHelper.getFullProfile(sessionID); const fullProfile = this.profileHelper.getFullProfile(sessionID);
let repeatableToChange: IPmcDataRepeatableQuest; let repeatableToChange: IPmcDataRepeatableQuest = undefined;
let changeRequirement: IChangeRequirement; let changeRequirement: IChangeRequirement;
// The trader existing quest is linked to // The trader existing quest is linked to
@ -545,7 +545,8 @@ export class RepeatableQuestController
for (const repeatablesInProfile of pmcData.RepeatableQuests) for (const repeatablesInProfile of pmcData.RepeatableQuests)
{ {
// Check for existing quest in (daily/weekly/scav arrays) // Check for existing quest in (daily/weekly/scav arrays)
const questToReplace = repeatablesInProfile.activeQuests.find((x) => x._id === changeRequest.qid); const questToReplace = repeatablesInProfile.activeQuests
.find((repeatable) => repeatable._id === changeRequest.qid);
if (!questToReplace) if (!questToReplace)
{ {
// Not found, skip to next repeatable sub-type // Not found, skip to next repeatable sub-type
@ -581,10 +582,6 @@ export class RepeatableQuestController
// Add newly generated quest to daily/weekly/scav type array // Add newly generated quest to daily/weekly/scav type array
newRepeatableQuest.side = repeatableConfig.side; newRepeatableQuest.side = repeatableConfig.side;
repeatablesInProfile.activeQuests.push(newRepeatableQuest); repeatablesInProfile.activeQuests.push(newRepeatableQuest);
repeatablesInProfile.changeRequirement[newRepeatableQuest._id] = {
changeCost: newRepeatableQuest.changeCost,
changeStandingCost: this.randomUtil.getArrayValue([0, 0.01]),
};
// Find quest we're replacing in pmc profile quests array and remove it // Find quest we're replacing in pmc profile quests array and remove it
this.questHelper.findAndRemoveQuestFromArrayIfExists(questToReplace._id, pmcData.Quests); this.questHelper.findAndRemoveQuestFromArrayIfExists(questToReplace._id, pmcData.Quests);
@ -595,13 +592,21 @@ export class RepeatableQuestController
fullProfile.characters.scav?.Quests ?? [], fullProfile.characters.scav?.Quests ?? [],
); );
this.handleFreeRefreshUses(fullProfile, repeatablesInProfile, repeatableTypeLower); const isFreeToReplace = this.useFreeRefreshIfAvailable(fullProfile, repeatablesInProfile, repeatableTypeLower);
if (!isFreeToReplace)
{
// not free, add change requirement cost
repeatablesInProfile.changeRequirement[newRepeatableQuest._id] = {
changeCost: newRepeatableQuest.changeCost,
changeStandingCost: this.randomUtil.getArrayValue([0, 0.01]),
};
}
} }
// Not sure why we clone but we do // Not sure why we clone but we do
repeatableToChange = this.cloner.clone(repeatablesInProfile); repeatableToChange = this.cloner.clone(repeatablesInProfile);
// Get rid of inactives // Purge inactive repeatables
repeatableToChange.inactiveQuests = []; repeatableToChange.inactiveQuests = [];
break; break;
@ -616,7 +621,6 @@ export class RepeatableQuestController
return this.httpResponse.appendErrorToOutput(output, message); return this.httpResponse.appendErrorToOutput(output, message);
} }
// Charge player money for replacing quest
for (const cost of changeRequirement.changeCost) for (const cost of changeRequirement.changeCost)
{ {
this.paymentService.addPaymentToOutput(pmcData, cost.templateId, cost.count, sessionID, output); this.paymentService.addPaymentToOutput(pmcData, cost.templateId, cost.count, sessionID, output);
@ -678,40 +682,46 @@ export class RepeatableQuestController
} }
/** /**
* Some accounts have access to repeatable quest refreshes for free * Some accounts have access to free repeatable quest refreshes
* Track the usage of them inside players profile * Track the usage of them inside players profile
* @param fullProfile Profile of player * @param fullProfile Player profile
* @param repeatableSubType Can be daily/weekly/scav repeatables * @param repeatableSubType Can be daily / weekly / scav repeatable
* @param repeatableTypeName Subtype of repeatables: daily / weekly / scav * @param repeatableTypeName Subtype of repeatable quest: daily / weekly / scav
* @returns Is the repeatable being replaced for free
*/ */
protected handleFreeRefreshUses( protected useFreeRefreshIfAvailable(
fullProfile: ISptProfile, fullProfile: ISptProfile,
repeatableSubType: IPmcDataRepeatableQuest, repeatableSubType: IPmcDataRepeatableQuest,
repeatableTypeName: string): void repeatableTypeName: string): boolean
{ {
// Only certain game versions have access // No free refreshes, exit early
const hasAccessToFreeRefreshSystem if (repeatableSubType.freeChangesAvailable <= 0)
= this.profileHelper.hasAccessToRepeatableFreeRefreshSystem(fullProfile.characters.pmc);
// Initialize/retrieve free refresh count for the desired subtype: daily/weekly
fullProfile.spt.freeRepeatableRefreshUsedCount ||= {};
const repeatableRefreshCounts = fullProfile.spt.freeRepeatableRefreshUsedCount;
repeatableRefreshCounts[repeatableTypeName] ||= 0; // Set to 0 if undefined
if (!hasAccessToFreeRefreshSystem)
{ {
// Reset the available count if the player does not have free refreshes. // Reset counter to 0
repeatableSubType.freeChangesAvailable = 0; repeatableSubType.freeChangesAvailable = 0;
return; return false;
} }
if (repeatableSubType.freeChangesAvailable > 0) // Only certain game versions have access to free refreshes
const hasAccessToFreeRefreshSystem
= this.profileHelper.hasAccessToRepeatableFreeRefreshSystem(fullProfile.characters.pmc);
// If the player has access and available refreshes:
if (hasAccessToFreeRefreshSystem)
{ {
// Increment the used count if the player has free refreshes and available changes. // Initialize/retrieve free refresh count for the desired subtype: daily/weekly
repeatableRefreshCounts[repeatableTypeName]++; fullProfile.spt.freeRepeatableRefreshUsedCount ||= {};
const repeatableRefreshCounts = fullProfile.spt.freeRepeatableRefreshUsedCount;
repeatableRefreshCounts[repeatableTypeName] ||= 0; // Set to 0 if undefined
// Update the available count. // Increment the used count and decrement the available count.
repeatableRefreshCounts[repeatableTypeName]++;
repeatableSubType.freeChangesAvailable--; repeatableSubType.freeChangesAvailable--;
return true;
} }
return false;
} }
} }