Skip to content

Commit

Permalink
Merge pull request #175 from Promises/banking
Browse files Browse the repository at this point in the history
Banking
  • Loading branch information
TheBlackParade authored Jun 13, 2020
2 parents 897c506 + 09ae510 commit ab72d0e
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/net/incoming-packets/item-swap-packet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { incomingPacket } from '../incoming-packet';
import { Player } from '../../world/actor/player/player';
import { swapItemAction } from '../../world/actor/player/action/swap-item-action';
import { insertItemAction, swapItemAction } from '../../world/actor/player/action/swap-item-action';
import { ByteBuffer } from '@runejs/byte-buffer';

export const itemSwapPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: ByteBuffer): void => {
Expand All @@ -18,6 +18,6 @@ export const itemSwapPacket: incomingPacket = (player: Player, packetId: number,
// Swap
swapItemAction(player, fromSlot, toSlot, { widgetId, containerId });
} else if(swapType === 1) {
// @TODO insert
insertItemAction(player, fromSlot, toSlot, { widgetId, containerId });
}
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { objectIds } from '@server/world/config/object-ids';
import { widgets } from '@server/world/config/widget';
import { widgets, widgetScripts } from '@server/world/config/widget';
import { objectAction } from '@server/world/actor/player/action/object-action';
import { ItemContainer } from '@server/world/items/item-container';
import { itemAction } from '@server/world/actor/player/action/item-action';
import { Item } from '@server/world/items/item';

import { fromNote, Item, toNote } from '@server/world/items/item';
import { buttonAction } from '@server/world/actor/player/action/button-action';
import { logger } from '@runejs/logger/dist/logger';
import { hasValueNotNull } from '@server/util/data';

const buttonIds: number[] = [
92, // as note
93, // as item
98, // swap
99, // insert
];

export const openBankInterface: objectAction = (details) => {
details.player.activeWidget = {
Expand All @@ -17,9 +26,8 @@ export const openBankInterface: objectAction = (details) => {

details.player.outgoingPackets.sendUpdateAllWidgetItems(widgets.bank.tabWidget, details.player.inventory);
details.player.outgoingPackets.sendUpdateAllWidgetItems(widgets.bank.screenWidget, details.player.bank);
details.player.outgoingPackets.updateClientConfig(304, details.player.sessionMetadata['bankRearrangeMode'] === 'insert' ? 1 : 0);
details.player.outgoingPackets.updateClientConfig(115, details.player.sessionMetadata['bankWithdrawAs'] === 'note' ? 1 : 0);

details.player.outgoingPackets.updateClientConfig(widgetScripts.bankInsertMode, details.player.settings.bankInsertMode);
details.player.outgoingPackets.updateClientConfig(widgetScripts.bankWithdrawNoteMode, details.player.settings.bankWithdrawNoteMode);
};

export const depositItem: itemAction = (details) => {
Expand All @@ -31,17 +39,26 @@ export const depositItem: itemAction = (details) => {
}

// Check if the player has the item

if (!details.player.hasItemInInventory(details.itemId)) {
return;
}


let itemIdToAdd: number = details.itemId;
const fromNoteId: number = fromNote(details.itemId);
if (fromNoteId > -1) {
itemIdToAdd = fromNoteId;
}

let countToRemove: number;
if (details.option.endsWith('all')) {
countToRemove = -1;
} else {
countToRemove = +details.option.replace('deposit-', '');
}


const playerInventory: ItemContainer = details.player.inventory;
const playerBank: ItemContainer = details.player.bank;
const slotsWithItem: number[] = playerInventory.findAll(details.itemId);
Expand All @@ -51,13 +68,13 @@ export const depositItem: itemAction = (details) => {
countToRemove = itemAmount;
}

if (!playerBank.canFit({itemId: details.itemId, amount: countToRemove}, true)) {
if (!playerBank.canFit({itemId: itemIdToAdd, amount: countToRemove}, true)) {
details.player.sendMessage('Your bank is full.');
return;
}


const itemToAdd: Item = {itemId: details.itemId, amount: 0};
const itemToAdd: Item = {itemId: itemIdToAdd, amount: 0};
while (countToRemove > 0 && playerInventory.has(details.itemId)) {
const invIndex = playerInventory.findIndex(details.itemId);
const invItem = playerInventory.items[invIndex];
Expand All @@ -71,6 +88,7 @@ export const depositItem: itemAction = (details) => {
countToRemove = 0;
}
}

playerBank.addStacking(itemToAdd);


Expand All @@ -91,6 +109,18 @@ export const withdrawItem: itemAction = (details) => {
if (!details.player.hasItemInBank(details.itemId)) {
return;
}

let itemIdToAdd: number = details.itemId;
if (details.player.settings.bankWithdrawNoteMode) {
const toNoteId: number = toNote(details.itemId);
if (toNoteId > -1) {
itemIdToAdd = toNoteId;
} else {
details.player.sendMessage('This item can not be withdrawn as a note.');
}
}


let countToRemove: number;
if (details.option.endsWith('all')) {
countToRemove = -1;
Expand All @@ -112,14 +142,13 @@ export const withdrawItem: itemAction = (details) => {
countToRemove = slots;
}
}

if (!playerInventory.canFit({itemId: details.itemId, amount: countToRemove})) {
if (!playerInventory.canFit({itemId: itemIdToAdd, amount: countToRemove}) || countToRemove === 0) {
details.player.sendMessage('Your inventory is full.');
return;
}


const itemToAdd: Item = {itemId: details.itemId, amount: 0};
const itemToAdd: Item = {itemId: itemIdToAdd, amount: 0};
while (countToRemove > 0 && playerBank.has(details.itemId)) {
const invIndex = playerBank.findIndex(details.itemId);
const invItem = playerBank.items[invIndex];
Expand All @@ -133,14 +162,34 @@ export const withdrawItem: itemAction = (details) => {
countToRemove = 0;
}
}
playerInventory.addStacking(itemToAdd);
for (let i = 0; i < itemToAdd.amount; i++) {
playerInventory.add({itemId: itemIdToAdd, amount: 1});
}


details.player.outgoingPackets.sendUpdateAllWidgetItems(widgets.bank.tabWidget, details.player.inventory);
details.player.outgoingPackets.sendUpdateAllWidgetItems(widgets.inventory, details.player.inventory);
details.player.outgoingPackets.sendUpdateAllWidgetItems(widgets.bank.screenWidget, details.player.bank);
};

export const btnAction: buttonAction = (details) => {
const {player, buttonId} = details;
player.settingChanged(buttonId);

const settingsMappings = {
92: {setting: 'bankWithdrawNoteMode', value: 1},
93: {setting: 'bankWithdrawNoteMode', value: 0},
98: {setting: 'bankInsertMode', value: 0},
99: {setting: 'bankInsertMode', value: 1},
};
if (!settingsMappings.hasOwnProperty(buttonId)) {
return;
}

const config = settingsMappings[buttonId];
player.settings[config.setting] = config.value;
};


export default new RunePlugin([{
type: ActionType.OBJECT_ACTION,
Expand All @@ -158,4 +207,4 @@ export default new RunePlugin([{
widgets: widgets.bank.screenWidget,
options: ['withdraw-1', 'withdraw-5', 'withdraw-10', 'withdraw-all'],
action: withdrawItem,
}]);
}, {type: ActionType.BUTTON, widgetId: widgets.bank.screenWidget.widgetId, buttonIds: buttonIds, action: btnAction}]);
1 change: 1 addition & 0 deletions src/plugins/player/login-update-settings-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const action: playerInitAction = (details) => {
player.outgoingPackets.updateClientConfig(widgetScripts.autoRetaliate, settings.autoRetaliateEnabled ? 0 : 1);
player.outgoingPackets.updateClientConfig(widgetScripts.attackStyle, settings.attackStyle);
player.outgoingPackets.updateClientConfig(widgetScripts.bankInsertMode, settings.bankInsertMode);
player.outgoingPackets.updateClientConfig(widgetScripts.bankWithdrawNoteMode, settings.bankWithdrawNoteMode);
};

export default new RunePlugin({ type: ActionType.PLAYER_INIT, action });
41 changes: 37 additions & 4 deletions src/world/actor/player/action/swap-item-action.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
import { Player } from '../player';
import { widgets } from '../../../config/widget';
import { logger } from '@runejs/logger/dist/logger';

export const swapItemAction = (player: Player, fromSlot: number, toSlot: number, widget: { widgetId: number, containerId: number }) => {
if(widget.widgetId === widgets.inventory.widgetId && widget.containerId === widgets.inventory.containerId) {
if (widget.widgetId === widgets.inventory.widgetId && widget.containerId === widgets.inventory.containerId) {
const inventory = player.inventory;

if(toSlot > inventory.size - 1 || fromSlot > inventory.size - 1) {
if (toSlot > inventory.size - 1 || fromSlot > inventory.size - 1) {
return;
}

inventory.swap(fromSlot, toSlot);
}
if(widget.widgetId === widgets.bank.screenWidget.widgetId && widget.containerId === widgets.bank.screenWidget.containerId) {
if (widget.widgetId === widgets.bank.screenWidget.widgetId && widget.containerId === widgets.bank.screenWidget.containerId) {
const bank = player.bank;

if(toSlot > bank.size - 1 || fromSlot > bank.size - 1) {
if (toSlot > bank.size - 1 || fromSlot > bank.size - 1) {
return;
}

bank.swap(fromSlot, toSlot);
}
};


export const insertItemAction = (player: Player, fromSlot: number, toSlot: number, widget: { widgetId: number, containerId: number }) => {
if (widget.widgetId === widgets.bank.screenWidget.widgetId && widget.containerId === widgets.bank.screenWidget.containerId) {
const bank = player.bank;

if (toSlot > bank.size - 1 || fromSlot > bank.size - 1) {
return;
}
if (fromSlot < toSlot) {
let slot = toSlot;
let current = bank.remove(fromSlot);
while (slot >= fromSlot) {
const temp = bank.remove(slot);
bank.set(slot, current);
current = temp;
slot--;
}
} else {
let slot = toSlot;
let current = bank.remove(fromSlot);
while (slot <= fromSlot) {
const temp = bank.remove(slot);
bank.set(slot, current);
current = temp;
slot++;
}
}
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.bank.screenWidget, player.bank);

}
};
1 change: 1 addition & 0 deletions src/world/actor/player/player-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class PlayerSettings {
autoRetaliateEnabled: boolean = true;
attackStyle: number = 0;
bankInsertMode: number = 0;
bankWithdrawNoteMode: number = 0;
}

export interface PlayerSave {
Expand Down
1 change: 1 addition & 0 deletions src/world/config/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const widgetScripts = {
runMode: 173,
splitPrivateChat: 287,
bankInsertMode: 304,
bankWithdrawNoteMode: 115,
acceptAid: 427,
areaEffectVolume: 872,
questPoints: 101
Expand Down
5 changes: 4 additions & 1 deletion src/world/items/item-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,15 @@ export class ItemContainer {
return slot;
}

public remove(slot: number, fireEvent: boolean = true): void {
public remove(slot: number, fireEvent: boolean = true): Item {
const item = this._items[slot];
this._items[slot] = null;

if (fireEvent) {
this._containerUpdated.next({type: 'REMOVE', slot});
}
return item;

}

public getFirstOpenSlot(): number {
Expand Down

0 comments on commit ab72d0e

Please sign in to comment.