Skip to content

Commit

Permalink
Merge pull request #2 from Sycamore0/development
Browse files Browse the repository at this point in the history
Add some useless packet
  • Loading branch information
Sycamore0 authored Oct 5, 2024
2 parents a3c3a91 + 673eaf9 commit 4d17634
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/kcpServer/game/gameData/data/CombineData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Loader from '$/gameData/loader'
import CombineDataList, { CombineData } from '@/types/gameData/CombineData'

class CombineDataLoader extends Loader {
declare data: CombineDataList

constructor() {
super('CombineData')
}

async getData(): Promise<CombineDataList> {
return super.getData()
}

async getCombineData(combineId: number): Promise<CombineData[]> {
return (await this.getCombineList()).filter(data => data.CombineId === combineId) || []
}

async getCombineList(): Promise<CombineData[]> {
return (await this.getData())
}
}

let loader: CombineDataLoader
export default (() => loader = loader || new CombineDataLoader())()
2 changes: 2 additions & 0 deletions src/kcpServer/game/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ActivityScheduleInfo from '#/packets/ActivityScheduleInfo'
import AllWidgetData from '#/packets/AllWidgetData'
import AvatarData from '#/packets/AvatarData'
import AvatarSatiationData from '#/packets/AvatarSatiationData'
import CombineData from '#/packets/CombineData'
import CoopData from '#/packets/CoopData'
import DoSetPlayerBornData from '#/packets/DoSetPlayerBornData'
import FinishedParentQuest from '#/packets/FinishedParentQuest'
Expand Down Expand Up @@ -182,6 +183,7 @@ export default class Game {
await ActivityScheduleInfo.sendNotify(context)
await activityManager.sendAllActivityInfo(context)

await CombineData.sendNotify(context)
await PlayerData.sendNotify(context)
await OpenStateUpdate.sendNotify(context)
await StoreWeightLimit.sendNotify(context)
Expand Down
66 changes: 66 additions & 0 deletions src/kcpServer/packets/BuyGoods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Packet, { PacketInterface, PacketContext } from '#/packet'
import { ShopGoods } from '@/types/proto'
import { RetcodeEnum } from '@/types/proto/enum'
import Material from '$/material'

export interface BuyGoodsReq {
buyCount: number
shopType: number
goods: ShopGoods
}

export interface BuyGoodsRsp {
retcode: RetcodeEnum
buyCount: number
shopType: number
goods: ShopGoods
goodsList?: ShopGoods[]
}

class BuyGoodsPacket extends Packet implements PacketInterface {
constructor() {
super('BuyGoods')
}

async request(context: PacketContext, data: BuyGoodsReq): Promise<void> {
const { game, player } = context
const { buyCount, shopType, goods } = data

const goodsList = await game.shopManager.exportGoodsList(shopType, player)

// cost
if (goods.scoin !== undefined || 0) {
player.addMora(-goods.scoin)
} else if (goods.hcoin !== undefined || 0) {
player.addPrimogem(-goods.hcoin)
} else if (goods.mcoin !== undefined || 0) {
player.addGenesisCrystal(-goods.mcoin)
} else {
const costItemList = goods.costItemList
costItemList.forEach(costItem => {
if (costItem && costItem.itemId !== undefined && costItem.count !== undefined) {
player.inventory.remove(costItem.itemId, costItem.count);
}
});
}

const { itemId: goodItemId, count: goodItemCount } = goods.goodsItem
const goodItem = await Material.create(player, goodItemId, goodItemCount * buyCount)
player.inventory.add(goodItem)

await this.response(context, {
retcode: RetcodeEnum.RET_SUCC,
buyCount: buyCount,
shopType: shopType,
goods: goods,
goodsList
})
}

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

let packet: BuyGoodsPacket
export default (() => packet = packet || new BuyGoodsPacket())()
82 changes: 82 additions & 0 deletions src/kcpServer/packets/Combine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Packet, { PacketInterface, PacketContext } from '#/packet'
import CombineData from '$/gameData/data/CombineData'
import Material from '$/material'
import { ItemParam } from '@/types/proto/ItemParam'
import { RetcodeEnum } from '@/types/proto/enum'

export interface CombineReq {
combineId: number
combineCount: number
avatarGuid?: string
}

export interface CombineRsp {
retcode: RetcodeEnum
combineId: number
combineCount: number
avatarGuid?: string
costItemList: ItemParam[]
resultItemList: ItemParam[]
totalReturnItemList?: ItemParam[]
totalRandomItemList?: ItemParam[]
totalExtraItemList?: ItemParam[]
}

class CombinePacket extends Packet implements PacketInterface {
constructor() {
super('Combine')
}

async request(context: PacketContext, data: CombineReq): Promise<void> {
const { player } = context
const { combineId, combineCount, avatarGuid } = data

const combineData = (await CombineData.getCombineData(combineId)) || []
const resultItemId = combineData.find(data => data.ResultItemId)?.ResultItemId || 0
const resultItemCount = combineData.find(data => data.ResultItemCount)?.ResultItemCount || 0
const scoinCost = combineData.find(data => data.ScoinCost)?.ScoinCost || 0
const costItemsList = combineData.find(data => data.MaterialItems)?.MaterialItems || []
const costItemId = costItemsList.find(data => data.Id)?.Id || 0
const costItemCount = costItemsList.find(data => data.Count)?.Count || 0

// Combine
costItemsList.forEach(costItem => {
if (costItem && costItem.Id !== undefined && costItem.Count !== undefined) {
player.inventory.remove(costItem.Id, costItem.Count * combineCount)
// cost resin
if (costItem.Id === 106) {
player.addResin(-costItem.Count * combineCount)
}
}
});
player.addMora(-scoinCost * combineCount) // cost scoin
const resultItem = await Material.create(player, resultItemId, combineCount)
player.inventory.add(resultItem)

await this.response(context, {
retcode: RetcodeEnum.RET_SUCC,
combineId: combineId,
combineCount: combineCount,
avatarGuid: avatarGuid,
costItemList: [{
itemId: costItemId,
count: costItemCount * combineCount
}],
resultItemList: [{
itemId: resultItemId,
count: resultItemCount
}],

totalReturnItemList: [],
totalRandomItemList: [],
totalExtraItemList: []
})
}

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

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

export interface CombineDataNotify {
combineIdList: number[]
}

class CombineDataPacket extends Packet implements PacketInterface {
constructor() {
super('CombineData')
}

async sendNotify(context: PacketContext): Promise<void> {
// Get all combineId
const combineIdList = (await CombineData.getCombineList()).map(data => data.CombineId)
const notifyData: CombineDataNotify = {
combineIdList
}

await super.sendNotify(context, notifyData)
}
}

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

export interface McoinExchangeHcoinReq {
hcoin: number
mcoinCost: number
}

export interface McoinExchangeHcoinRsp {
retcode: RetcodeEnum
hcoin: number
mcoinCost: number
}

export interface McoinExchangeHcoinNotify { }

class McoinExchangeHcoinPacket extends Packet implements PacketInterface {
constructor() {
super('McoinExchangeHcoin')
}

async request(context: PacketContext, data: McoinExchangeHcoinReq): Promise<void> {
const { player } = context
const { hcoin, mcoinCost } = data

player.addGenesisCrystal(-mcoinCost)
player.addPrimogem(hcoin)

await this.response(context, {
retcode: RetcodeEnum.RET_SUCC,
hcoin: hcoin,
mcoinCost: mcoinCost
})
}

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

let packet: McoinExchangeHcoinPacket
export default (() => packet = packet || new McoinExchangeHcoinPacket())()
3 changes: 3 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AbilityData from '$/gameData/data/AbilityData'
import AvatarData from '$/gameData/data/AvatarData'
import CombineData from '$/gameData/data/CombineData'
import DungeonData from '$/gameData/data/DungeonData'
import GadgetData from '$/gameData/data/GadgetData'
import GrowCurveData from '$/gameData/data/GrowCurveData'
Expand Down Expand Up @@ -347,6 +348,8 @@ export default class Server {
logger.debug('message.cache.debug.ability')
await AvatarData.getData()
logger.debug('message.cache.debug.avatar')
await CombineData.getData()
logger.debug('message.cache.debug.combine')
await DungeonData.getData()
logger.debug('message.cache.debug.dungeon')
await GadgetData.getData()
Expand Down
23 changes: 23 additions & 0 deletions src/types/gameData/CombineData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface CombineData {
CombineId: number
IsDefaultShow?: boolean
CombineType: number
ResultItemId: number
ResultItemCount: number
MaterialItems: {
Id: number
Count: number
}[]

PlayerLevel?: number
ScoinCost?: number
SubCombineType?: number
RandomItems?: {
Count?: number
}[]
RecipeType?: string
}

type CombineDataList = CombineData[]

export default CombineDataList

0 comments on commit 4d17634

Please sign in to comment.