shoplogic.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. class ShopLogic {
  2. getListOfInventory(shop, mobile) {
  3. var shopList = [];
  4. for(var itemIndex in shop.shopInventory) {
  5. var shopItem = world.mud_getItem(shop.shopInventory[itemIndex]);
  6. var itemValue = this.shopSellsFor(shop, mobile, shopItem);
  7. var itemName = "";
  8. itemName = shopItem.mud_getName();
  9. shopList.push({name: itemName, value: itemValue});
  10. }
  11. return shopList;
  12. }
  13. canSell(shop, mobile, inventoryItem) {
  14. var itemValue = this.shopBuysFor(shop, mobile, inventoryItem);
  15. if(shop.shopAccount < itemValue) {
  16. return false;
  17. }
  18. return true;
  19. }
  20. shopSellsFor(shop, mobile, item) {
  21. var marginModifier = 1;
  22. if(shop.haggledItem[mobile.id] == item.constructor.name) {
  23. marginModifier = 0.5;
  24. }
  25. var itemValue = parseInt(item.value || 1);
  26. return parseInt(itemValue * (1 + ((shop.shopMargin * marginModifier) / 100)));
  27. }
  28. shopBuysFor(shop, mobile, item) {
  29. var marginModifier = 1;
  30. if(shop.haggledItem[mobile.id] == item.constructor.name) {
  31. marginModifier = 0.5;
  32. }
  33. var itemValue = parseInt(item.value || 1);
  34. return parseInt(itemValue * (1 - ((shop.shopMargin * marginModifier) / 100)));
  35. }
  36. canBuy(shop, mobile, shopItem) {
  37. var itemValue = this.shopSellsFor(shop, mobile, shopItem);
  38. if(mobile.account < itemValue) {
  39. return false;
  40. }
  41. return true;
  42. }
  43. sellItemToShop(shop, mobile, item) {
  44. world.mud_removeItemFromMobile(mobile.id, item.id);
  45. shop.mud_addItemToInventory(item.id);
  46. var amount = this.shopBuysFor(shop, mobile, item);
  47. delete shop.haggledItem[mobile.id];
  48. shop.mud_removeCurrency(amount);
  49. mobile.mud_addCurrency(amount);
  50. return amount;
  51. }
  52. buyItemFromShop(shop, mobile, item) {
  53. world.mud_addItemToMobile(mobile.id, item.id);
  54. shop.mud_removeItemFromInventory(item.id);
  55. var amount = this.shopSellsFor(shop, mobile, item);
  56. delete shop.haggledItem[mobile.id];
  57. mobile.mud_removeCurrency(amount);
  58. shop.mud_addCurrency(amount);
  59. return amount;
  60. }
  61. trackHaggle(shop, mobile, item) {
  62. shop.haggledItem[mobile.id] = item.constructor.name;
  63. }
  64. }
  65. module.exports = new ShopLogic();