Cleanup of cancelled player flea offer code

This commit is contained in:
Dev 2024-01-26 10:49:06 +00:00
parent 0d1a5bc59f
commit 9b09319d3d
5 changed files with 74 additions and 56 deletions

View File

@ -103,7 +103,7 @@ export class RagfairCallbacks implements OnLoad, OnUpdate
/** Handle RagFairRemoveOffer event */ /** Handle RagFairRemoveOffer event */
public removeOffer(pmcData: IPmcData, info: IRemoveOfferRequestData, sessionID: string): IItemEventRouterResponse public removeOffer(pmcData: IPmcData, info: IRemoveOfferRequestData, sessionID: string): IItemEventRouterResponse
{ {
return this.ragfairController.removeOffer(info.offerId, sessionID); return this.ragfairController.removeOffer(info, sessionID);
} }
/** Handle RagFairRenewOffer event */ /** Handle RagFairRenewOffer event */

View File

@ -23,6 +23,7 @@ import { IGetMarketPriceRequestData } from "@spt-aki/models/eft/ragfair/IGetMark
import { IGetOffersResult } from "@spt-aki/models/eft/ragfair/IGetOffersResult"; import { IGetOffersResult } from "@spt-aki/models/eft/ragfair/IGetOffersResult";
import { IGetRagfairOfferByIdRequest } from "@spt-aki/models/eft/ragfair/IGetRagfairOfferByIdRequest"; import { IGetRagfairOfferByIdRequest } from "@spt-aki/models/eft/ragfair/IGetRagfairOfferByIdRequest";
import { IRagfairOffer } from "@spt-aki/models/eft/ragfair/IRagfairOffer"; import { IRagfairOffer } from "@spt-aki/models/eft/ragfair/IRagfairOffer";
import { IRemoveOfferRequestData } from "@spt-aki/models/eft/ragfair/IRemoveOfferRequestData";
import { ISearchRequestData } from "@spt-aki/models/eft/ragfair/ISearchRequestData"; import { ISearchRequestData } from "@spt-aki/models/eft/ragfair/ISearchRequestData";
import { IProcessBuyTradeRequestData } from "@spt-aki/models/eft/trade/IProcessBuyTradeRequestData"; import { IProcessBuyTradeRequestData } from "@spt-aki/models/eft/trade/IProcessBuyTradeRequestData";
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes"; import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
@ -666,64 +667,73 @@ export class RagfairController
/** /**
* User requested removal of the offer, actually reduces the time to 71 seconds, * User requested removal of the offer, actually reduces the time to 71 seconds,
* allowing for the possibility of extending the auction before it's end time * allowing for the possibility of extending the auction before it's end time
* @param offerId offer to 'remove' * @param removeRequest Remove offer request
* @param sessionID Players id * @param sessionId Players id
* @returns IItemEventRouterResponse * @returns IItemEventRouterResponse
*/ */
public removeOffer(offerId: string, sessionID: string): IItemEventRouterResponse public removeOffer(removeRequest: IRemoveOfferRequestData, sessionId: string): IItemEventRouterResponse
{ {
const pmcData = this.saveServer.getProfile(sessionID).characters.pmc; const output = this.eventOutputHolder.getOutput(sessionId);
const offers = pmcData.RagfairInfo.offers;
if (!offers) const pmcData = this.saveServer.getProfile(sessionId).characters.pmc;
const playerProfileOffers = pmcData.RagfairInfo.offers;
if (!playerProfileOffers)
{ {
this.logger.warning( this.logger.warning(
this.localisationService.getText("ragfair-unable_to_remove_offer_not_found_in_profile", { this.localisationService.getText("ragfair-unable_to_remove_offer_not_found_in_profile", {
profileId: sessionID, profileId: sessionId,
offerId: offerId, offerId: removeRequest.offerId,
}), }),
); );
pmcData.RagfairInfo.offers = []; pmcData.RagfairInfo.offers = [];
} }
const index = offers.findIndex((offer) => offer._id === offerId); const playerOfferIndex = playerProfileOffers.findIndex((offer) => offer._id === removeRequest.offerId);
if (index === -1) if (playerOfferIndex === -1)
{ {
this.logger.error( this.logger.error(
this.localisationService.getText("ragfair-offer_not_found_in_profile", { offerId: offerId }), this.localisationService.getText("ragfair-offer_not_found_in_profile", { offerId: removeRequest.offerId }),
); );
return this.httpResponse.appendErrorToOutput( return this.httpResponse.appendErrorToOutput(
this.eventOutputHolder.getOutput(sessionID), output,
this.localisationService.getText("ragfair-offer_not_found_in_profile_short"), this.localisationService.getText("ragfair-offer_not_found_in_profile_short"),
); );
} }
const differenceInSeconds = offers[index].endTime - this.timeUtil.getTimestamp(); const differenceInSeconds = playerProfileOffers[playerOfferIndex].endTime - this.timeUtil.getTimestamp();
if (differenceInSeconds > this.ragfairConfig.sell.expireSeconds) if (differenceInSeconds > this.ragfairConfig.sell.expireSeconds)
{ {
// expireSeconds Default is 71 seconds // `expireSeconds` Default is 71 seconds
const newEndTime = this.ragfairConfig.sell.expireSeconds + this.timeUtil.getTimestamp(); const newEndTime = this.ragfairConfig.sell.expireSeconds + this.timeUtil.getTimestamp();
offers[index].endTime = Math.round(newEndTime); playerProfileOffers[playerOfferIndex].endTime = Math.round(newEndTime);
} }
return this.eventOutputHolder.getOutput(sessionID); return output;
} }
public extendOffer(info: IExtendOfferRequestData, sessionID: string): IItemEventRouterResponse /**
* Extend a ragfair offers listing time
* @param extendRequest Extend offer request
* @param sessionId Players id
* @returns IItemEventRouterResponse
*/
public extendOffer(extendRequest: IExtendOfferRequestData, sessionId: string): IItemEventRouterResponse
{ {
const output = this.eventOutputHolder.getOutput(sessionID); const output = this.eventOutputHolder.getOutput(sessionId);
const pmcData = this.saveServer.getProfile(sessionID).characters.pmc;
const offers = pmcData.RagfairInfo.offers;
const index = offers.findIndex((offer) => offer._id === info.offerId);
const secondsToAdd = info.renewalTime * TimeUtil.ONE_HOUR_AS_SECONDS;
if (index === -1) const pmcData = this.saveServer.getProfile(sessionId).characters.pmc;
const playerOffers = pmcData.RagfairInfo.offers;
const playerOfferIndex = playerOffers.findIndex((offer) => offer._id === extendRequest.offerId);
const secondsToAdd = extendRequest.renewalTime * TimeUtil.ONE_HOUR_AS_SECONDS;
if (playerOfferIndex === -1)
{ {
this.logger.warning( this.logger.warning(
this.localisationService.getText("ragfair-offer_not_found_in_profile", { offerId: info.offerId }), this.localisationService.getText("ragfair-offer_not_found_in_profile", { offerId: extendRequest.offerId }),
); );
return this.httpResponse.appendErrorToOutput( return this.httpResponse.appendErrorToOutput(
this.eventOutputHolder.getOutput(sessionID), output,
this.localisationService.getText("ragfair-offer_not_found_in_profile_short"), this.localisationService.getText("ragfair-offer_not_found_in_profile_short"),
); );
} }
@ -731,19 +741,19 @@ export class RagfairController
// MOD: Pay flea market fee // MOD: Pay flea market fee
if (this.ragfairConfig.sell.fees) if (this.ragfairConfig.sell.fees)
{ {
const count = offers[index].sellInOnePiece const count = playerOffers[playerOfferIndex].sellInOnePiece
? 1 ? 1
: offers[index].items.reduce((sum, item) => sum += item.upd.StackObjectsCount, 0); : playerOffers[playerOfferIndex].items.reduce((sum, item) => sum += item.upd.StackObjectsCount, 0);
const tax = this.ragfairTaxService.calculateTax( const tax = this.ragfairTaxService.calculateTax(
offers[index].items[0], playerOffers[playerOfferIndex].items[0],
this.profileHelper.getPmcProfile(sessionID), this.profileHelper.getPmcProfile(sessionId),
offers[index].requirementsCost, playerOffers[playerOfferIndex].requirementsCost,
count, count,
offers[index].sellInOnePiece, playerOffers[playerOfferIndex].sellInOnePiece,
); );
const request = this.createBuyTradeRequestObject("RUB", tax); const request = this.createBuyTradeRequestObject("RUB", tax);
this.paymentService.payMoney(pmcData, request, sessionID, output); this.paymentService.payMoney(pmcData, request, sessionId, output);
if (output.warnings.length > 0) if (output.warnings.length > 0)
{ {
return this.httpResponse.appendErrorToOutput( return this.httpResponse.appendErrorToOutput(
@ -753,9 +763,10 @@ export class RagfairController
} }
} }
offers[index].endTime += Math.round(secondsToAdd); // Add extra time to offer
playerOffers[playerOfferIndex].endTime += Math.round(secondsToAdd);
return this.eventOutputHolder.getOutput(sessionID); return output;
} }
/** /**

View File

@ -152,7 +152,7 @@ export class RagfairOfferService
public removeAllOffersByTrader(traderId: string): void public removeAllOffersByTrader(traderId: string): void
{ {
this.ragfairOfferHandler.removeOfferByTrader(traderId); this.ragfairOfferHandler.removeAllOffersByTrader(traderId);
} }
/** /**
@ -228,7 +228,6 @@ export class RagfairOfferService
// Handle player offer - items need returning/XP adjusting. Checking if offer has actually expired or not. // Handle player offer - items need returning/XP adjusting. Checking if offer has actually expired or not.
if (isPlayer && staleOffer.endTime <= this.timeUtil.getTimestamp()) if (isPlayer && staleOffer.endTime <= this.timeUtil.getTimestamp())
{ {
// TODO: something feels wrong, func returns ItemEventRouterResponse but we dont pass it back to caller?
this.returnPlayerOffer(staleOffer); this.returnPlayerOffer(staleOffer);
} }
@ -236,37 +235,33 @@ export class RagfairOfferService
this.removeOfferById(staleOffer._id); this.removeOfferById(staleOffer._id);
} }
protected returnPlayerOffer(offer: IRagfairOffer): IItemEventRouterResponse protected returnPlayerOffer(playerOffer: IRagfairOffer): void
{ {
const pmcID = String(offer.user.id); const pmcId = String(playerOffer.user.id);
const profile = this.profileHelper.getProfileByPmcId(pmcID); const profile = this.profileHelper.getProfileByPmcId(pmcId);
const sessionID = profile.sessionId;
const offerIndex = profile.RagfairInfo.offers.findIndex((o) => o._id === offer._id);
if (offerIndex === -1) const offerinProfileIndex = profile.RagfairInfo.offers.findIndex((o) => o._id === playerOffer._id);
if (offerinProfileIndex === -1)
{ {
this.logger.warning(this.localisationService.getText("ragfair-unable_to_find_offer_to_remove", offer._id)); this.logger.warning(this.localisationService.getText("ragfair-unable_to_find_offer_to_remove", playerOffer._id));
return this.httpResponse.appendErrorToOutput( return;
this.eventOutputHolder.getOutput(sessionID),
this.localisationService.getText("ragfair-offer_not_found_in_profile_short"),
);
} }
// Reduce player ragfair rep // Reduce player ragfair rep
profile.RagfairInfo.rating -= this.databaseServer.getTables().globals.config.RagFair.ratingDecreaseCount; profile.RagfairInfo.rating -= this.databaseServer.getTables().globals.config.RagFair.ratingDecreaseCount;
profile.RagfairInfo.isRatingGrowing = false; profile.RagfairInfo.isRatingGrowing = false;
const firstOfferItem = offer.items[0]; const firstOfferItem = playerOffer.items[0];
if (firstOfferItem.upd.StackObjectsCount > firstOfferItem.upd.OriginalStackObjectsCount) if (firstOfferItem.upd.StackObjectsCount > firstOfferItem.upd.OriginalStackObjectsCount)
{ {
offer.items[0].upd.StackObjectsCount = firstOfferItem.upd.OriginalStackObjectsCount; playerOffer.items[0].upd.StackObjectsCount = firstOfferItem.upd.OriginalStackObjectsCount;
} }
delete offer.items[0].upd.OriginalStackObjectsCount; delete playerOffer.items[0].upd.OriginalStackObjectsCount;
// Remove player offer from flea
this.ragfairOfferHandler.removeOffer(playerOffer);
// Send failed offer items to player in mail // Send failed offer items to player in mail
this.ragfairServerHelper.returnItems(profile.sessionId, offer.items); this.ragfairServerHelper.returnItems(profile.sessionId, playerOffer.items);
profile.RagfairInfo.offers.splice(offerIndex, 1); profile.RagfairInfo.offers.splice(offerinProfileIndex, 1);
return this.eventOutputHolder.getOutput(sessionID);
} }
} }

View File

@ -37,8 +37,16 @@ export class RagfairTaxService
return this.playerOfferTaxCache[offerIdToGet]; return this.playerOfferTaxCache[offerIdToGet];
} }
/**
// This method, along with calculateItemWorth, is trying to mirror the client-side code found in the method "CalculateTaxPrice". // This method, along with calculateItemWorth, is trying to mirror the client-side code found in the method "CalculateTaxPrice".
// It's structured to resemble the client-side code as closely as possible - avoid making any big structure changes if it's not necessary. // It's structured to resemble the client-side code as closely as possible - avoid making any big structure changes if it's not necessary.
* @param item Item being sold on flea
* @param pmcData player profile
* @param requirementsValue
* @param offerItemCount Number of offers being created
* @param sellInOnePiece
* @returns Tax in roubles
*/
public calculateTax( public calculateTax(
item: Item, item: Item,
pmcData: IPmcData, pmcData: IPmcData,

View File

@ -67,6 +67,10 @@ export class RagfairOfferHolder
this.addOfferByTemplates(itemTpl, offer); this.addOfferByTemplates(itemTpl, offer);
} }
/**
* Purge offer from offer cache
* @param offer Offer to remove
*/
public removeOffer(offer: IRagfairOffer): void public removeOffer(offer: IRagfairOffer): void
{ {
if (this.offersById.has(offer._id)) if (this.offersById.has(offer._id))
@ -85,7 +89,7 @@ export class RagfairOfferHolder
} }
} }
public removeOfferByTrader(traderId: string): void public removeAllOffersByTrader(traderId: string): void
{ {
if (this.offersByTrader.has(traderId)) if (this.offersByTrader.has(traderId))
{ {