bdo-shopping-cart-package
Version:
For use with my bdo-crafting-profit projects
143 lines • 6.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShoppingCart = void 0;
var Action_1 = require("../optimizers/Action");
var ShoppingCartCore_1 = require("./ShoppingCartCore");
var ShoppingCart = /** @class */ (function () {
/**
*
* @param optimizer Instance of PPSOptimizer
*/
function ShoppingCart(optimizer) {
this.cart = [];
this.optimizer = optimizer;
}
ShoppingCart.prototype.clearCart = function () {
this.cart = [];
};
/**
*
* @param optimizer
*/
ShoppingCart.prototype.setOptimizer = function (optimizer) {
this.optimizer = optimizer;
};
/**
* Clears the cart and begins recursively adding items to the cart
* @param itemName
* @param quantity
* @param items
*/
ShoppingCart.prototype.calculateCosts = function (itemName, quantity, items) {
if (quantity === void 0) { quantity = 1; }
this.clearCart();
return this.addItem(itemName, quantity, items[itemName].activeRecipeId, "", items, "");
};
/**
* Adds item to cart if it doesn't exist. Ues recursion to add ingredients as well.
* @param itemName
* @param quantity
* @param selectedRecipeId
* @param parentName
* @param items
* @param parentPath
*/
ShoppingCart.prototype.addItem = function (itemName, quantity, selectedRecipeId, parentName, items, parentPath) {
// Ensure the input is valid
// console.log("ShoppingCart.js | Adding item to shopping cart:", itemName);
if (quantity === void 0) { quantity = 1; }
if (parentName === void 0) { parentName = ""; }
if (items === void 0) { items = {}; }
if (parentPath === void 0) { parentPath = ""; }
if (itemName == "" || quantity <= 0)
return { currentCart: this.cart, recipePrice: 0, cumulativeTimeSpent: 0 };
var item = items[itemName];
// console.log("ShoppingCart.js | Valid Item:", item);
if (item == null)
return { currentCart: this.cart, recipePrice: 0, cumulativeTimeSpent: 0 };
var currentPath = (parentPath || "") + "/" + itemName;
// console.log("ShoppingCart.js | Item Path:", currentPath);
if (item.usedInRecipes[currentPath] == null)
return { currentCart: this.cart, recipePrice: 0, cumulativeTimeSpent: 0 };
// Get the recipe object using the selectedRecipeId string
var recipe = item.recipes[selectedRecipeId];
// console.log("ShoppingCart.js | Recipe:", recipe);
if (recipe == null && selectedRecipeId != "")
return { currentCart: this.cart, recipePrice: 0, cumulativeTimeSpent: 0 };
// Prevent infinite looping
// let action = selectedRecipeId == "" ? ActionTaken.Buy : ActionTaken.Craft;
// const recipePathArr = currentPath.split("/");
// const containsLoop = new Set(recipePathArr).size !== recipePathArr.length;
// console.log("Shopping Cart - Path:", currentPath);
// if (containsLoop) {
// console.log("Contains Loop");
// action = ActionTaken.Buy;
// }
var action = item.usedInRecipes[currentPath].actionTaken;
// Calculate how many times the player must 'craft' the item
var craftCount = quantity;
if (action === Action_1.ActionTaken.Craft) {
craftCount = Math.ceil(quantity / recipe.quantityProduced);
}
// Add the ingredients of the recipe to the cart as well if the item is being crafted
var recipePrice = 0;
var cumulativeTimeSpent = 0;
if (action === Action_1.ActionTaken.Craft) {
for (var _i = 0, _a = recipe.ingredients; _i < _a.length; _i++) {
var ingredient = _a[_i];
var ingredientQuantity = ingredient["Amount"] * craftCount;
var ingredientName = ingredient["Item Name"];
var ingredientItem = items[ingredientName];
var activeRecipeId = ingredientItem == null ? "" : ingredientItem.activeRecipeId;
var _b = this.addItem(ingredientName, ingredientQuantity, activeRecipeId, itemName, items, currentPath), price = _b.recipePrice, timeSpentToCraftIngredient = _b.cumulativeTimeSpent;
recipePrice += price * ingredient["Amount"];
cumulativeTimeSpent +=
timeSpentToCraftIngredient * ingredient["Amount"];
}
cumulativeTimeSpent += recipe.timeToProduce;
cumulativeTimeSpent /= recipe.quantityProduced;
recipePrice /= recipe.quantityProduced;
}
else {
recipePrice = ShoppingCartCore_1.getMarketPriceForItem(item);
}
var shoppingCartData = {
// name: itemName,
action: action,
// recipe: recipe != null ? recipe.ingredients : null,
craftCount: craftCount,
expectedCount: action === "Craft" ? recipe.quantityProduced * craftCount : craftCount,
individualPrice: recipePrice,
cumulativeTimeSpent: cumulativeTimeSpent,
for: parentName,
};
this.cart.push(shoppingCartData);
// console.log('Added shopping cart data for ', itemName, shoppingCartData)
items[itemName].addShoppingCartData(currentPath, shoppingCartData);
return { currentCart: this.cart, recipePrice: recipePrice, cumulativeTimeSpent: cumulativeTimeSpent };
};
ShoppingCart.prototype.printShoppingCart = function () {
console.log(JSON.stringify(this.cart, null, 4));
};
/**
* @deprecated
* @param item Object of type Item
* @param recipeId The recipe Id you want the price calculated for
* @param items All items (From ItemManager.parseRecipes())
*/
ShoppingCart.prototype.getPriceForRecipe = function (item, recipeId, items) {
var recipe = item.recipes[recipeId];
if (recipeId == null)
return ShoppingCartCore_1.getMarketPriceForItem(item);
var totalCost = 0;
for (var _i = 0, _a = recipe.ingredients; _i < _a.length; _i++) {
var ingredient = _a[_i];
var ingredientName = ingredient["Item Name"];
totalCost += this.getPriceForRecipe(items[ingredientName], items[ingredientName].activeRecipeId, items);
}
return totalCost;
};
return ShoppingCart;
}());
exports.ShoppingCart = ShoppingCart;
//# sourceMappingURL=ShoppingCart.js.map