Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 1 addition & 38 deletions src/net/incoming-packet-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,6 @@ const packets: { [key: number]: incomingPacket } = {
30: objectInteractionPacket,
164: objectInteractionPacket,
183: objectInteractionPacket,

/*19: interfaceClickPacket,
140: cameraTurnPacket,

79: buttonClickPacket,
226: dialogueInteractionPacket,

112: npcInteractionPacket,
13: npcInteractionPacket,
42: npcInteractionPacket,
8: npcInteractionPacket,
67: npcInteractionPacket,

181: objectInteractionPacket,
241: objectInteractionPacket,
50: objectInteractionPacket,
136: objectInteractionPacket,
55: objectInteractionPacket,

28: walkPacket,
213: walkPacket,
247: walkPacket,

163: characterDesignPacket,

24: itemEquipPacket,
3: itemOption1Packet,
123: itemSwapPacket,
4: dropItemPacket,
1: itemOnItemPacket,

49: chatPacket,
56: commandPacket,

177: buyItemPacket,
91: buyItemPacket,
231: buyItemPacket*/
};

export function handlePacket(player: Player, packetId: number, packetSize: number, buffer: Buffer): void {
Expand All @@ -105,5 +68,5 @@ export function handlePacket(player: Player, packetId: number, packetSize: numbe
new Promise(resolve => {
packetFunction(player, packetId, packetSize, new RsBuffer(buffer));
resolve();
});
}).catch(error => logger.error(`Error handling inbound packet: ${error}`));
}
49 changes: 0 additions & 49 deletions src/net/incoming-packets/buy-item-packet.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/plugins/items/shopping/item-value-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const shopSellValueAction: itemAction = (details) => {
return;
}

player.outgoingPackets.chatboxMessage(`${item.name}: currently costs ${item.value} coins.`);
const itemValue = item.value || 1;

player.outgoingPackets.chatboxMessage(`${item.name}: currently costs ${itemValue} coins.`);
};

export const shopPurchaseValueAction: itemAction = (details) => {
Expand Down
101 changes: 101 additions & 0 deletions src/plugins/items/shopping/sell-to-shop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { getItemFromContainer, itemAction } from '@server/world/actor/player/action/item-action';
import { widgets } from '@server/world/config/widget';
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { Shop, shopItemContainer } from '@server/world/config/shops';
import { world } from '@server/game-server';
import { ItemContainer } from '@server/world/items/item-container';
import { itemIds } from '@server/world/config/item-ids';

export const action: itemAction = (details) => {
const { player, itemId, itemSlot, widgetId, containerId, option } = details;

if(!player.activeWidget || player.activeWidget.widgetId !== widgets.shop.widgetId) {
console.log('no widget');
return;
}

const openedShop: Shop = player.metadata['lastOpenedShop'];
if(!openedShop) {
console.log('no shop');
return;
}

const inventory = player.inventory;
const inventoryItem = getItemFromContainer(itemId, itemSlot, inventory);

if(!inventoryItem) {
// The specified item was not found in the specified slot.
console.log('no item');
return;
}

const sellAmounts = {
'sell-1': 1,
'sell-5': 5,
'sell-10': 10
};
let sellAmount = sellAmounts[option];
const itemDetails = world.itemData.get(itemId);
const shopContainer = shopItemContainer(openedShop);
const shopSpaces = shopContainer.items.filter(item => item === null);

const shopItemIndex = shopContainer.items.findIndex(item => item !== null && item.itemId === itemId);
if(shopItemIndex === -1 && shopSpaces.length === 0) {
player.outgoingPackets.chatboxMessage(`There isn't enough space in the shop.`);
return;
}

const shopItem = shopContainer.items[shopItemIndex];

if(itemDetails.stackable) {
if(inventoryItem.amount < sellAmount) {
inventory.remove(itemSlot);
sellAmount = inventoryItem.amount;
} else {
inventory.set(itemSlot, { itemId, amount: inventoryItem.amount - sellAmount });
}
} else {
const foundItems = inventory.items.map((item, i) => item !== null && item.itemId === itemId ? i : null).filter(i => i !== null);
if(foundItems.length < sellAmount) {
sellAmount = foundItems.length;
}

for(let i = 0; i < sellAmount; i++) {
inventory.remove(foundItems[i]);
}
}

const itemValue = itemDetails.value || 0;

if(!shopItem) {
shopContainer.set(shopContainer.getFirstOpenSlot(), { itemId, amount: sellAmount });
openedShop.items.push({ amountInStock: sellAmount, id: itemId, name: itemDetails.name, price: itemValue });
} else {
shopItem.amount += sellAmount;
openedShop.items[shopItemIndex].amountInStock += sellAmount;
}

const sellPrice = sellAmount * itemValue; // @TODO player inventory item devaluation/saturation
if(sellPrice > 0) {
let coinsIndex = player.hasCoins(1);

if(coinsIndex === -1) {
coinsIndex = inventory.getFirstOpenSlot();
inventory.set(coinsIndex, {itemId: itemIds.coins, amount: sellPrice});
} else {
inventory.items[coinsIndex].amount += sellPrice;
}
}

player.outgoingPackets.sendUpdateAllWidgetItems(widgets.shop, shopContainer);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.shopPlayerInventory, inventory);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.inventory, inventory);
};

export default new RunePlugin({
type: ActionType.ITEM_ACTION,
widgets: widgets.shopPlayerInventory,
options: [ 'sell-1', 'sell-5', 'sell-10' ],
action,
cancelOtherActions: false
});
49 changes: 0 additions & 49 deletions src/world/actor/player/action/buy-item-action.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/world/actor/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ export class Player extends Actor {
}

public hasCoins(amount: number): number {
return this.inventory.items.findIndex(item => item && item.itemId === itemIds.coins && item.amount >= amount);
return this.inventory.items
.findIndex(item => item !== null && item.itemId === itemIds.coins && item.amount >= amount);
}

public removeItem(slot: number): void {
Expand Down