Skip to content

Commit

Permalink
Refactored how flea offer quantity is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Chomp committed Feb 21, 2025
1 parent 8ee4e01 commit 73753d7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
1 change: 1 addition & 0 deletions project/src/controllers/RagfairController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ export class RagfairController {
formattedItems,
formattedRequirements,
loyalLevel,
items[0].upd?.StackObjectsCount ?? 1,
sellInOnePiece,
);
}
Expand Down
10 changes: 5 additions & 5 deletions project/src/controllers/TradeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ export class TradeController {
scheme_items: requestOffer.items,
};

// Reduce flea offer quanity
fleaOffer.quantity -= requestOffer.count;

this.tradeHelper.buyItem(pmcData, buyData, sessionId, this.traderConfig.purchasesAreFoundInRaid, output);

// Remove/lower offer quantity of item purchased from trader flea offer
this.ragfairServer.reduceOfferQuantity(fleaOffer._id, requestOffer.count);
}

/**
Expand Down Expand Up @@ -225,8 +225,8 @@ export class TradeController {
return;
}

// Remove/lower stack count of item purchased from PMC flea offer
this.ragfairServer.removeOfferStack(fleaOffer._id, requestOffer.count);
// Remove/lower offer quantity of item purchased from PMC flea offer
this.ragfairServer.reduceOfferQuantity(fleaOffer._id, requestOffer.count);
}

/**
Expand Down
23 changes: 19 additions & 4 deletions project/src/generators/RagfairOfferGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class RagfairOfferGenerator {
* @param items Items in the offer
* @param barterScheme Cost of item (currency or barter)
* @param loyalLevel Loyalty level needed to buy item
* @param quantity Amount of item being listed
* @param sellInOnePiece Flags sellInOnePiece to be true
* @returns Created flea offer
*/
Expand All @@ -92,9 +93,10 @@ export class RagfairOfferGenerator {
items: IItem[],
barterScheme: IBarterScheme[],
loyalLevel: number,
quantity: number,
sellInOnePiece = false,
): IRagfairOffer {
const offer = this.createOffer(userID, time, items, barterScheme, loyalLevel, sellInOnePiece);
const offer = this.createOffer(userID, time, items, barterScheme, loyalLevel, quantity, sellInOnePiece);
this.ragfairOfferService.addOffer(offer);

return offer;
Expand All @@ -107,6 +109,7 @@ export class RagfairOfferGenerator {
* @param items Items in the offer
* @param barterScheme Cost of item (currency or barter)
* @param loyalLevel Loyalty level needed to buy item
* @param quantity Amount of item being listed
* @param isPackOffer Is offer being created flaged as a pack
* @returns IRagfairOffer
*/
Expand All @@ -116,6 +119,7 @@ export class RagfairOfferGenerator {
items: IItem[],
barterScheme: IBarterScheme[],
loyalLevel: number,
quantity: number,
isPackOffer = false,
): IRagfairOffer {
const isTrader = this.ragfairServerHelper.isTrader(userID);
Expand Down Expand Up @@ -164,7 +168,7 @@ export class RagfairOfferGenerator {
loyaltyLevel: loyalLevel,
sellInOnePiece: isPackOffer,
locked: false,
quantity: items[0].upd?.StackObjectsCount ?? 1,
quantity: quantity,
};

this.offerCounter++;
Expand Down Expand Up @@ -473,7 +477,9 @@ export class RagfairOfferGenerator {
): Promise<void> {
// Set stack size to random value
var desiredStackSize = this.ragfairServerHelper.calculateDynamicStackCount(itemWithChildren[0]._tpl, isPreset);
itemWithChildren[0].upd.StackObjectsCount = desiredStackSize;

// Reset stack count to 1 from whatever it was prior
itemWithChildren[0].upd.StackObjectsCount = 1;

const isBarterOffer = this.randomUtil.getChance100(this.ragfairConfig.dynamic.barter.chancePercent);
const isPackOffer =
Expand Down Expand Up @@ -531,6 +537,7 @@ export class RagfairOfferGenerator {
itemWithChildren,
barterScheme,
1,
desiredStackSize,
isPackOffer, // sellAsOnePiece - pack offer
);
}
Expand Down Expand Up @@ -600,7 +607,15 @@ export class RagfairOfferGenerator {
const barterSchemeItems = assortsClone.barter_scheme[item._id][0];
const loyalLevel = assortsClone.loyal_level_items[item._id];

const offer = this.createAndAddFleaOffer(traderID, time, items, barterSchemeItems, loyalLevel, false);
const offer = this.createAndAddFleaOffer(
traderID,
time,
items,
barterSchemeItems,
loyalLevel,
item.upd?.StackObjectsCount ?? 1,
false,
);

// Refresh complete, reset flag to false
trader.base.refreshTraderRagfairOffers = false;
Expand Down
8 changes: 2 additions & 6 deletions project/src/helpers/RagfairOfferHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,10 @@ export class RagfairOfferHelper {
const sellerProfile = this.profileHelper.getPmcProfile(offerOwnerSessionId);

// Pack or ALL items of a multi-offer were bought - remove entire ofer
if (offer.sellInOnePiece || boughtAmount === offerStackCount) {
if (offer.sellInOnePiece || boughtAmount === offer.quantity) {
this.deleteOfferById(offerOwnerSessionId, offer._id);
} else {
// Partial purchase
const offerRootItem = offer.items[0];

// Reduce offer root items stack count
offerRootItem.upd.StackObjectsCount -= boughtAmount;
// Partial purchase, reduce quantity by amount purchased
offer.quantity -= boughtAmount;
}

Expand Down
4 changes: 1 addition & 3 deletions project/src/helpers/TradeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ export class TradeHelper {
let offerItems: IItem[] = [];
let buyCallback: (buyCount: number) => void;
if (buyRequestData.tid.toLocaleLowerCase() === "ragfair") {
// Called when player purchases PMC offer from ragfair
buyCallback = (buyCount: number) => {
const allOffers = this.ragfairServer.getOffers();

// We store ragfair offerid in buyRequestData.item_id
const offerWithItem = allOffers.find((x) => x._id === buyRequestData.item_id);
const rootItemPurchased = offerWithItem.items[0];

// Update offer quantity
offerWithItem.quantity -= buyCount;

// Ensure purchase does not exceed trader item limit
const assortHasBuyRestrictions = this.itemHelper.hasBuyRestrictions(rootItemPurchased);
if (assortHasBuyRestrictions) {
Expand Down
4 changes: 2 additions & 2 deletions project/src/servers/RagfairServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export class RagfairServer {
return this.ragfairOfferService.getOffers();
}

public removeOfferStack(offerID: string, amount: number): void {
this.ragfairOfferService.removeOfferStack(offerID, amount);
public reduceOfferQuantity(offerID: string, amount: number): void {
this.ragfairOfferService.reduceOfferQuantity(offerID, amount);
}

public doesOfferExist(offerId: string): boolean {
Expand Down
5 changes: 2 additions & 3 deletions project/src/services/RagfairOfferService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,11 @@ export class RagfairOfferService {
* @param offerId Offer to adjust stack size of
* @param amount How much to deduct from offers stack size
*/
public removeOfferStack(offerId: string, amount: number): void {
public reduceOfferQuantity(offerId: string, amount: number): void {
const offer = this.ragfairOfferHandler.getOfferById(offerId);
if (offer) {
offer.quantity -= amount;
offer.items[0].upd.StackObjectsCount -= amount;
if (offer.items[0].upd.StackObjectsCount <= 0) {
if (offer.quantity <= 0) {
this.processStaleOffer(offer);
}
}
Expand Down

0 comments on commit 73753d7

Please sign in to comment.