Skip to content

Commit

Permalink
inventory: add support for incrementing/decrementing item quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
hex-agon committed Aug 5, 2023
1 parent 8e900d9 commit 182e92f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class GameServer(
player.currentRoom?.removePlayer(player)
player.currentChannel?.removePlayer(player)
players.remove(player)
LOGGER.debug("{} disconnected", player.nickname)
LOGGER.info("{} disconnected", player.nickname)
}

fun start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.util.concurrent.atomic.AtomicInteger
interface InventoryRepository {
fun load(txCtx: TransactionalContext, playerUid: Int): Inventory
fun saveItem(txCtx: TransactionalContext, playerUid: Int, item: Item): Item
fun deleteItem(txCtx: TransactionalContext, playerUid: Int, item: Item)
}

class InMemoryInventoryRepository : InventoryRepository {
Expand All @@ -33,6 +34,10 @@ class InMemoryInventoryRepository : InventoryRepository {
findInventory(playerUid).add(persistedItem)
return persistedItem
}

override fun deleteItem(txCtx: TransactionalContext, playerUid: Int, item: Item) {
findInventory(playerUid).remove(item)
}
}

class JooqInventoryRepository : InventoryRepository {
Expand Down Expand Up @@ -73,4 +78,9 @@ class JooqInventoryRepository : InventoryRepository {
uid = uid!!
)
}

override fun deleteItem(txCtx: TransactionalContext, playerUid: Int, item: Item) {
txCtx.jooq().deleteFrom(PLAYER_INVENTORY_ITEM)
.where(PLAYER_INVENTORY_ITEM.UID.eq(item.uid))
}
}
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)
}
}
}
15 changes: 12 additions & 3 deletions game-server/src/main/kotlin/work/fking/pangya/game/player/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import work.fking.pangya.game.model.IFF_TYPE_NOEQUIP_ITEM
import work.fking.pangya.game.model.IffObject
import work.fking.pangya.game.model.iffTypeFromId

data class Item(
class Item(
override val uid: Int = -1,
override val iffId: Int,
val quantity: Int = 0,
var quantity: Int = 0,
val stats: IntArray = IntArray(5)
) : IffObject {

private fun quantifiable(): Boolean {
fun quantifiable(): Boolean {
return when (iffTypeFromId(iffId)) {
IFF_TYPE_EQUIPITEM_ITEM, IFF_TYPE_NOEQUIP_ITEM, IFF_TYPE_BALL -> true
else -> false
Expand All @@ -37,6 +37,15 @@ data class Item(
}
}

fun copy(uid: Int): Item {
return Item(
uid = uid,
iffId = iffId,
quantity = quantity,
stats = stats
)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LoginServer(
}

private fun onPlayerDisconnect(player: Player) {
LOGGER.debug("{} disconnected", player.username)
LOGGER.info("{} disconnected", player.username)
sessionClient.unregisterSession(player)
}

Expand Down

0 comments on commit 182e92f

Please sign in to comment.