-
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.
- Loading branch information
Showing
18 changed files
with
442 additions
and
4 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
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
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
18 changes: 18 additions & 0 deletions
18
game-server/src/main/kotlin/work/fking/pangya/game/packet/outbound/CardholicReplies.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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package work.fking.pangya.game.packet.outbound | ||
|
||
import work.fking.pangya.game.player.CardInventory | ||
import work.fking.pangya.networking.protocol.OutboundPacket | ||
|
||
object CardholicReplies { | ||
|
||
fun inventory(cards: CardInventory): OutboundPacket { | ||
return OutboundPacket { buffer -> | ||
buffer.writeShortLE(0x138) | ||
|
||
buffer.writeIntLE(0) | ||
buffer.writeShortLE(cards.entries.size) | ||
|
||
cards.entries.forEach { it.encode(buffer) } | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
game-server/src/main/kotlin/work/fking/pangya/game/persistence/CardRepository.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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package work.fking.pangya.game.persistence | ||
|
||
import org.jooq.RecordMapper | ||
import work.fking.pangya.game.persistence.jooq.keys.PLAYER_INVENTORY_CARD_PKEY | ||
import work.fking.pangya.game.persistence.jooq.tables.records.PlayerInventoryCardRecord | ||
import work.fking.pangya.game.persistence.jooq.tables.references.PLAYER_INVENTORY_CARD | ||
import work.fking.pangya.game.player.Card | ||
import work.fking.pangya.game.player.CardInventory | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
interface CardRepository { | ||
fun load(txCtx: TransactionalContext, playerUid: Int): CardInventory | ||
fun saveCard(txCtx: TransactionalContext, playerUid: Int, card: Card): Card | ||
fun deleteCard(txCtx: TransactionalContext, playerUid: Int, card: Card) | ||
} | ||
|
||
class InMemoryCardRepository : CardRepository { | ||
private val uidSequence = AtomicInteger(1) | ||
private val cardInventories = mutableMapOf<Int, MutableList<Card>>() | ||
|
||
private fun findCardInventory(playerUid: Int): MutableList<Card> = cardInventories.computeIfAbsent(playerUid) { | ||
mutableListOf() | ||
} | ||
|
||
override fun load(txCtx: TransactionalContext, playerUid: Int): CardInventory { | ||
return CardInventory(findCardInventory(playerUid)) | ||
} | ||
|
||
override fun saveCard(txCtx: TransactionalContext, playerUid: Int, card: Card): Card { | ||
val persistedCard = card.copy(uid = uidSequence.get()) | ||
findCardInventory(playerUid).add(persistedCard) | ||
return persistedCard | ||
} | ||
|
||
override fun deleteCard(txCtx: TransactionalContext, playerUid: Int, card: Card) { | ||
findCardInventory(playerUid).remove(card) | ||
} | ||
} | ||
|
||
class JooqCardInventory : CardRepository { | ||
private val cardMapper = RecordMapper<PlayerInventoryCardRecord, Card> { | ||
Card( | ||
uid = it.uid!!, | ||
iffId = it.iffId, | ||
quantity = it.quantity ?: 1 | ||
) | ||
} | ||
|
||
override fun load(txCtx: TransactionalContext, playerUid: Int): CardInventory { | ||
val cards = txCtx.jooq().selectFrom(PLAYER_INVENTORY_CARD) | ||
.where(PLAYER_INVENTORY_CARD.ACCOUNT_UID.eq(playerUid)) | ||
.fetch(cardMapper) | ||
return CardInventory(cards) | ||
} | ||
|
||
override fun saveCard(txCtx: TransactionalContext, playerUid: Int, card: Card): Card { | ||
val insert = txCtx.jooq().insertInto(PLAYER_INVENTORY_CARD) | ||
|
||
if (card.uid != -1) insert.set(PLAYER_INVENTORY_CARD.UID, card.uid) | ||
|
||
val uid = insert | ||
.set(PLAYER_INVENTORY_CARD.ACCOUNT_UID, playerUid) | ||
.set(PLAYER_INVENTORY_CARD.IFF_ID, card.iffId) | ||
.set(PLAYER_INVENTORY_CARD.QUANTITY, card.quantity) | ||
.onConflict(PLAYER_INVENTORY_CARD_PKEY.fields) | ||
.doUpdate() | ||
.set(PLAYER_INVENTORY_CARD.QUANTITY, card.quantity) | ||
.returningResult(PLAYER_INVENTORY_CARD.UID) | ||
.fetchOneInto(Int::class.java) | ||
|
||
return card.copy( | ||
uid = uid!! | ||
) | ||
} | ||
|
||
override fun deleteCard(txCtx: TransactionalContext, playerUid: Int, card: Card) { | ||
txCtx.jooq().deleteFrom(PLAYER_INVENTORY_CARD) | ||
.where(PLAYER_INVENTORY_CARD.UID.eq(card.uid)) | ||
} | ||
} |
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
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
Oops, something went wrong.