123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- class ShopLogic {
- getListOfInventory(shop, mobile) {
- var shopList = [];
- for(var itemIndex in shop.shopInventory) {
- var shopItem = world.mud_getItem(shop.shopInventory[itemIndex]);
- var itemValue = this.shopSellsFor(shop, mobile, shopItem);
- var itemName = "";
- itemName = shopItem.mud_getName();
- shopList.push({name: itemName, value: itemValue});
- }
- return shopList;
- }
- canSell(shop, mobile, inventoryItem) {
- var itemValue = this.shopBuysFor(shop, mobile, inventoryItem);
- if(shop.shopAccount < itemValue) {
- return false;
- }
- return true;
- }
- shopSellsFor(shop, mobile, item) {
- var marginModifier = 1;
- if(shop.haggledItem[mobile.id] == item.constructor.name) {
- marginModifier = 0.5;
- }
- var itemValue = parseInt(item.value || 1);
- return parseInt(itemValue * (1 + ((shop.shopMargin * marginModifier) / 100)));
- }
- shopBuysFor(shop, mobile, item) {
- var marginModifier = 1;
- if(shop.haggledItem[mobile.id] == item.constructor.name) {
- marginModifier = 0.5;
- }
- var itemValue = parseInt(item.value || 1);
- return parseInt(itemValue * (1 - ((shop.shopMargin * marginModifier) / 100)));
- }
- canBuy(shop, mobile, shopItem) {
- var itemValue = this.shopSellsFor(shop, mobile, shopItem);
- if(mobile.account < itemValue) {
- return false;
- }
- return true;
- }
- sellItemToShop(shop, mobile, item) {
- world.mud_removeItemFromMobile(mobile.id, item.id);
- shop.mud_addItemToInventory(item.id);
- var amount = this.shopBuysFor(shop, mobile, item);
- delete shop.haggledItem[mobile.id];
- shop.mud_removeCurrency(amount);
- mobile.mud_addCurrency(amount);
- return amount;
- }
- buyItemFromShop(shop, mobile, item) {
- world.mud_addItemToMobile(mobile.id, item.id);
- shop.mud_removeItemFromInventory(item.id);
- var amount = this.shopSellsFor(shop, mobile, item);
- delete shop.haggledItem[mobile.id];
- mobile.mud_removeCurrency(amount);
- shop.mud_addCurrency(amount);
- return amount;
- }
- trackHaggle(shop, mobile, item) {
- shop.haggledItem[mobile.id] = item.constructor.name;
- }
- }
- module.exports = new ShopLogic();
|