-
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.
clubsets: add upgrade/downgrade logic
- Loading branch information
Showing
6 changed files
with
140 additions
and
0 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
30 changes: 30 additions & 0 deletions
30
...rver/src/main/kotlin/work/fking/pangya/game/packet/handler/ClubSetUpgradePacketHandler.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,30 @@ | ||
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 | ||
|
||
class ClubSetUpgradePacketHandler : ClientPacketHandler { | ||
|
||
override fun handle(server: GameServer, player: Player, packet: ByteBuf) { | ||
val type = packet.readByte() // clubset upgrade = 1, clubset downgrade = 3 | ||
val statId = packet.readUnsignedByte() | ||
val itemUid = packet.readIntLE() | ||
|
||
val stat = statById(statId.toInt()) ?: throw IllegalStateException("Player ${player.nickname} tried to upgrade an item with an invalid stat ($statId)") | ||
|
||
server.runTask( | ||
ChangeClubSetStatTask( | ||
persistenceCtx = server.persistenceCtx, | ||
player = player, | ||
itemUid = itemUid, | ||
type = type.toInt(), | ||
stat = stat | ||
) | ||
) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
game-server/src/main/kotlin/work/fking/pangya/game/packet/outbound/ClubSetReplies.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,17 @@ | ||
package work.fking.pangya.game.packet.outbound | ||
|
||
import work.fking.pangya.networking.protocol.OutboundPacket | ||
|
||
object ClubSetReplies { | ||
|
||
fun upgradeAck(type: Int, stat: Int, itemUid: Int, cost: Long): OutboundPacket { | ||
return OutboundPacket { buffer -> | ||
buffer.writeShortLE(0xa5) | ||
buffer.writeByte(type) | ||
buffer.writeByte(1) // 0 doesn't upgrade a clubset but 1 does? | ||
buffer.writeByte(stat) | ||
buffer.writeIntLE(itemUid) | ||
buffer.writeLongLE(cost) // TODO: how to calculate the upgrade cost? | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
game-server/src/main/kotlin/work/fking/pangya/game/player/Stat.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,11 @@ | ||
package work.fking.pangya.game.player | ||
|
||
enum class Stat { | ||
POWER, | ||
CONTROL, | ||
ACCURACY, | ||
SPIN, | ||
CURVE | ||
} | ||
|
||
fun statById(id: Int) = Stat.entries.find { it.ordinal == id } |
68 changes: 68 additions & 0 deletions
68
game-server/src/main/kotlin/work/fking/pangya/game/task/ChangeClubSetStatTask.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,68 @@ | ||
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.persistence.PersistenceContext | ||
import work.fking.pangya.game.player.Item | ||
import work.fking.pangya.game.player.Player | ||
import work.fking.pangya.game.player.Stat | ||
import work.fking.pangya.game.player.Stat.ACCURACY | ||
import work.fking.pangya.game.player.Stat.CONTROL | ||
import work.fking.pangya.game.player.Stat.CURVE | ||
import work.fking.pangya.game.player.Stat.POWER | ||
import work.fking.pangya.game.player.Stat.SPIN | ||
|
||
private const val TYPE_UPGRADE_CLUBSET = 1 | ||
private const val TYPE_DOWNGRADE_CLUBSET = 3 | ||
|
||
class ChangeClubSetStatTask( | ||
private val persistenceCtx: PersistenceContext, | ||
private val player: Player, | ||
private val itemUid: Int, | ||
private val type: Int, | ||
private val stat: Stat | ||
) : Runnable { | ||
|
||
|
||
override fun run() { | ||
val clubSet = player.inventory.findByUid(itemUid) ?: throw IllegalStateException("Player ${player.nickname} tried to upgrade an item it does not own") | ||
require(iffTypeFromId(clubSet.iffId) == IFF_TYPE_CLUBSET) { "Player ${player.nickname} tried to upgrade an item that is not a ClubSet" } | ||
|
||
when (type) { | ||
TYPE_UPGRADE_CLUBSET -> upgrade(clubSet) | ||
TYPE_DOWNGRADE_CLUBSET -> downgrade(clubSet) | ||
else -> throw IllegalStateException("Player ${player.nickname} tried to upgrade an item but the type is invalid ($type)") | ||
} | ||
} | ||
|
||
private fun upgrade(clubSet: Item) { | ||
val level = clubSet.stats[stat.ordinal] | ||
val cost = (level + 1) * when (stat) { | ||
POWER -> 2100 | ||
CONTROL -> 1700 | ||
ACCURACY -> 2400 | ||
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" } | ||
// TODO: verify clubset upgrade limits | ||
|
||
persistenceCtx.transactional { tx -> | ||
wallet.pangBalance -= cost | ||
clubSet.stats[stat.ordinal]++ | ||
persistenceCtx.playerRepository.saveWallet(tx, player.uid, wallet) | ||
persistenceCtx.inventoryRepository.saveItem(tx, player.uid, clubSet) | ||
} | ||
player.writeAndFlush(ClubSetReplies.upgradeAck(type, stat.ordinal, itemUid, cost.toLong())) | ||
} | ||
|
||
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" } | ||
|
||
clubSet.stats[stat.ordinal]-- | ||
persistenceCtx.inventoryRepository.saveItem(persistenceCtx.noTxContext(), player.uid, clubSet) | ||
player.writeAndFlush(ClubSetReplies.upgradeAck(type, stat.ordinal, itemUid, 0)) | ||
} | ||
} | ||
|