Skip to content

Commit

Permalink
clubset: fix clubset downgrading
Browse files Browse the repository at this point in the history
  • Loading branch information
hex-agon committed Aug 12, 2023
1 parent 6eeb4df commit 59b25f8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package work.fking.pangya.game.packet.handler
import io.netty.buffer.ByteBuf
import work.fking.pangya.game.GameServer
import work.fking.pangya.game.net.ClientPacketHandler
import work.fking.pangya.game.packet.outbound.ClubSetReplies
import work.fking.pangya.game.player.Player
import work.fking.pangya.game.player.statById
import work.fking.pangya.game.task.ChangeClubSetStatTask
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package work.fking.pangya.game.packet.outbound

import work.fking.pangya.networking.protocol.OutboundPacket

object ClubSetUpgradeReplies {

fun upgradeAck(result: UpgradeResult, stat: Int, itemUid: Int, cost: Long = 0): OutboundPacket {
return OutboundPacket { buffer ->
buffer.writeShortLE(0xa5)
buffer.writeByte(result.code)
buffer.writeByte(1) // 0 doesn't upgrade a clubset but 1 does?
buffer.writeByte(stat)
buffer.writeIntLE(itemUid)
buffer.writeLongLE(cost)
}
}

enum class UpgradeResult(val code: Int) {
UPGRADE_SUCCESS(1),
DOWNGRADE_SUCCESS(2),
INSUFFICIENT_PANG(3),
INSUFFICIENT_SLOTS(4),
CANNOT_DOWNGRADE_ANYMORE(5),
FAILED_TO_UPGRADE(6)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package work.fking.pangya.game.task

import work.fking.pangya.game.model.IFF_TYPE_CLUBSET
import work.fking.pangya.game.model.iffTypeFromId
import work.fking.pangya.game.packet.outbound.ClubSetReplies
import work.fking.pangya.game.packet.outbound.ClubSetUpgradeReplies
import work.fking.pangya.game.packet.outbound.ClubSetUpgradeReplies.UpgradeResult.CANNOT_DOWNGRADE_ANYMORE
import work.fking.pangya.game.packet.outbound.ClubSetUpgradeReplies.UpgradeResult.DOWNGRADE_SUCCESS
import work.fking.pangya.game.packet.outbound.ClubSetUpgradeReplies.UpgradeResult.INSUFFICIENT_PANG
import work.fking.pangya.game.packet.outbound.ClubSetUpgradeReplies.UpgradeResult.INSUFFICIENT_SLOTS
import work.fking.pangya.game.packet.outbound.ClubSetUpgradeReplies.UpgradeResult.UPGRADE_SUCCESS
import work.fking.pangya.game.persistence.PersistenceContext
import work.fking.pangya.game.player.Item
import work.fking.pangya.game.player.Player
Expand Down Expand Up @@ -45,7 +50,10 @@ class ChangeClubSetStatTask(
SPIN, CURVE -> 1900
}
val wallet = player.wallet
require(wallet.pangBalance >= cost) { "Player ${player.nickname} tried to upgrade an item but doesn't have enough pang" }

if (wallet.pangBalance < cost) {
player.writeAndFlush(ClubSetUpgradeReplies.upgradeAck(result = INSUFFICIENT_PANG, stat = stat.ordinal, itemUid = itemUid))
}
// TODO: verify clubset upgrade limits

persistenceCtx.transactional { tx ->
Expand All @@ -54,15 +62,18 @@ class ChangeClubSetStatTask(
persistenceCtx.playerRepository.saveWallet(tx, player.uid, wallet)
persistenceCtx.inventoryRepository.saveItem(tx, player.uid, clubSet)
}
player.writeAndFlush(ClubSetReplies.upgradeAck(type, stat.ordinal, itemUid, cost))
player.writeAndFlush(ClubSetUpgradeReplies.upgradeAck(result = UPGRADE_SUCCESS, stat = stat.ordinal, itemUid = itemUid, cost = cost))
}

private fun downgrade(clubSet: Item) {
require(clubSet.stats[stat.ordinal] > 0) { "Player ${player.nickname} tried to downgrade an item but the stat is already at 0" }
if (clubSet.stats[stat.ordinal] <= 0) {
player.writeAndFlush(ClubSetUpgradeReplies.upgradeAck(result = CANNOT_DOWNGRADE_ANYMORE, stat = stat.ordinal, itemUid = itemUid))
return
}

clubSet.stats[stat.ordinal]--
persistenceCtx.inventoryRepository.saveItem(persistenceCtx.noTxContext(), player.uid, clubSet)
player.writeAndFlush(ClubSetReplies.upgradeAck(type, stat.ordinal, itemUid, 0))
player.writeAndFlush(ClubSetUpgradeReplies.upgradeAck(result = DOWNGRADE_SUCCESS, stat = stat.ordinal, itemUid = itemUid))
}
}

0 comments on commit 59b25f8

Please sign in to comment.