Skip to content

Commit

Permalink
Add simple BuyResin Packet
Browse files Browse the repository at this point in the history
always first time to buy resin
  • Loading branch information
Sycamore0 committed Oct 5, 2024
1 parent 4d17634 commit c3259c8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/kcpServer/packets/BuyResin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Packet, { PacketInterface, PacketContext } from '#/packet'
import { RetcodeEnum } from '@/types/proto/enum'
import ResinChange from './ResinChange'

export interface BuyResinReq { }

export interface BuyResinRsp {
retcode: RetcodeEnum
curValue?: number
}

class BuyResinPacket extends Packet implements PacketInterface {
constructor() {
super('BuyResin')
}

async request(context: PacketContext, data: BuyResinReq): Promise<void> {
const { player } = context
let curValue = player.resin

// always first time
// Cost primogem [50, 100, 100, 150, 200, 200]
// Limit 6 times (official server)
if (player.primogem >= 50) {
player.addPrimogem(-50)
player.addResin(60)

await ResinChange.sendNotify(context)
await this.response(context, {
retcode: RetcodeEnum.RET_SUCC,
curValue: player.resin + 60
})
} else {
await this.response(context, {
retcode: RetcodeEnum.RET_RESIN_GAIN_FAILED,
curValue: player.resin
})
}


}

async response(context: PacketContext, data: BuyResinRsp): Promise<void> {
await super.response(context, data)
}
}

let packet: BuyResinPacket
export default (() => packet = packet || new BuyResinPacket())()
28 changes: 28 additions & 0 deletions src/kcpServer/packets/ResinChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Packet, { PacketInterface, PacketContext } from '#/packet'

export interface ResinChangeNotify {
nextAddTimestamp: number
curBuyCount: number
curValue: number
}

class ResinChangePacket extends Packet implements PacketInterface {
constructor() {
super('ResinChange')
}

async sendNotify(context: PacketContext): Promise<void> {
const { player } = context

const notifyData: ResinChangeNotify = {
nextAddTimestamp: Date.now(),
curBuyCount: 0,
curValue: player.resin + 60
}

await super.sendNotify(context, notifyData)
}
}

let packet: ResinChangePacket
export default (() => packet = packet || new ResinChangePacket())()

0 comments on commit c3259c8

Please sign in to comment.