fixed a bug that allowed purchasing 0-stock items (you would only lose gold without getting an item)

This commit is contained in:
Ceikry 2022-05-14 06:24:47 +00:00 committed by Ryan
parent 2f09f2b940
commit 7478f68d40
2 changed files with 34 additions and 0 deletions

View file

@ -259,6 +259,11 @@ class Shop(val title: String, val stock: Array<ShopItem>, val general: Boolean =
if(inStock.amount < amount)
item.amount = inStock.amount
if(inStock.amount == 0) {
sendMessage(player, "This item is out of stock.")
return TransactionStatus.Failure("Shop item out of stock.")
}
if(isMainStock && inStock.amount > stock[slot].amount && !getServerConfig().getBoolean(Shops.personalizedShops, false) && player.ironmanManager.isIronman)
{
sendDialogue(player, "As an ironman, you cannot buy overstocked items from shops.")

View file

@ -274,4 +274,33 @@ class ShopTests {
Assertions.assertEquals(2, stock.size)
Assertions.assertEquals(20, stock[0].amount)
}
@Test fun buying0StockItemFromNormalStockShouldNotSucceedNorDeductGold() {
testPlayer.inventory.clear()
testPlayer.inventory.add(Item(995, Integer.MAX_VALUE))
testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer))
testPlayer.setAttribute("shop-main", true)
val inStockAmount = general.getContainer(testPlayer).get(0).amount
//Buy out existing stock
var status: Shop.TransactionStatus = Shop.TransactionStatus.Success()
Assertions.assertDoesNotThrow {
status = general.buy(testPlayer, 0, inStockAmount)
}
Assertions.assertEquals(true, status is Shop.TransactionStatus.Success, "Transaction failure: ${if(status is Shop.TransactionStatus.Failure) (status as Shop.TransactionStatus.Failure).reason else ""}")
Assertions.assertEquals(0, general.getContainer(testPlayer).getAmount(0), "Buying all stock didn't... buy all stock.")
testPlayer.inventory.replace(null, 1) //Remove the items we purchased
val remainingGP = testPlayer.inventory.getAmount(995)
Assertions.assertDoesNotThrow {
status = general.buy(testPlayer, 0, 10)
}
Assertions.assertEquals(true, status is Shop.TransactionStatus.Failure, "Status was not Failure for buying 0-stock item!")
Assertions.assertEquals(remainingGP, testPlayer.inventory.getAmount(995), "Coins were deducted for buying 0-stock item!")
Assertions.assertNull(testPlayer.inventory.get(1), "Player received purchased item despite being 0-stock!")
}
}