diff --git a/src/kcpServer/packets/BuyResin.ts b/src/kcpServer/packets/BuyResin.ts new file mode 100644 index 0000000..e304ceb --- /dev/null +++ b/src/kcpServer/packets/BuyResin.ts @@ -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 { + 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 { + await super.response(context, data) + } +} + +let packet: BuyResinPacket +export default (() => packet = packet || new BuyResinPacket())() \ No newline at end of file diff --git a/src/kcpServer/packets/ResinChange.ts b/src/kcpServer/packets/ResinChange.ts new file mode 100644 index 0000000..3f98dec --- /dev/null +++ b/src/kcpServer/packets/ResinChange.ts @@ -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 { + 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())() \ No newline at end of file