-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inventory: add support for incrementing/decrementing item quantities
- Loading branch information
Showing
5 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
game-server/src/main/kotlin/work/fking/pangya/game/player/Inventory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,50 @@ | ||
package work.fking.pangya.game.player | ||
|
||
import work.fking.pangya.game.model.IffContainer | ||
import java.lang.Math.addExact | ||
import kotlin.math.max | ||
|
||
class Inventory(override val entries: MutableList<Item> = ArrayList()) : IffContainer<Item> { | ||
|
||
fun add(item: Item) { | ||
entries.add(item) | ||
} | ||
|
||
fun incrementByUid(uid: Int, amount: Int): Item { | ||
val item = findByUid(uid) ?: throw IllegalArgumentException("no such item with uid = $uid") | ||
decrement(item, amount) | ||
return item | ||
} | ||
|
||
fun incrementByIffId(iffId: Int, amount: Int): Item { | ||
val item = findByIffId(iffId) ?: throw IllegalArgumentException("no such item with iffId = $iffId") | ||
decrement(item, amount) | ||
return item | ||
} | ||
|
||
private fun increment(item: Item, amount: Int) { | ||
require(amount > 0) { "amount must be positive" } | ||
item.quantity = addExact(item.quantity, amount) | ||
} | ||
|
||
fun decrementByUid(uid: Int, amount: Int): Item { | ||
val item = findByUid(uid) ?: throw IllegalArgumentException("no such item with uid = $uid") | ||
decrement(item, amount) | ||
return item | ||
} | ||
|
||
fun decrementByIffId(iffId: Int, amount: Int): Item { | ||
val item = findByIffId(iffId) ?: throw IllegalArgumentException("no such item with iffId = $iffId") | ||
decrement(item, amount) | ||
return item | ||
} | ||
|
||
private fun decrement(item: Item, amount: Int) { | ||
require(amount > 0) { "amount must be positive" } | ||
item.quantity = max(item.quantity - amount, 0) | ||
|
||
if (item.quantity == 0) { | ||
entries.remove(item) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters