Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BANKING! and some small changes to harvestables #168

Merged
merged 7 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
130 changes: 126 additions & 4 deletions src/plugins/objects/bank/bank-booth-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,146 @@ import { ActionType, RunePlugin } from '@server/plugins/plugin';
import { objectIds } from '@server/world/config/object-ids';
import { widgets } 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';


export const openBankInterface: objectAction = (details) => {
details.player.activeWidget = {
widgetId: widgets.bank.screenWidget,
widgetId: widgets.bank.screenWidget.widgetId,
secondaryWidgetId: widgets.bank.tabWidget.widgetId,
type: 'SCREEN_AND_TAB',
closeOnWalk: true
};

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);

};

export const depositItem: objectAction = (details) => {
export const depositItem: itemAction = (details) => {
// Check if player might be spawning widget clientside
if (!details.player.activeWidget ||
!(details.player.activeWidget.widgetId === widgets.bank.screenWidget) ||
!(details.player.activeWidget.widgetId === widgets.bank.screenWidget.widgetId) ||
!(details.player.activeWidget.secondaryWidgetId === widgets.bank.tabWidget.widgetId)) {
return;
}

// Check if the player has the item
if (!details.player.hasItemInInventory(details.itemId)) {
return;
}

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);
let itemAmount: number = 0;
slotsWithItem.forEach((slot) => itemAmount += playerInventory.items[slot].amount);
if (countToRemove == -1 || countToRemove > itemAmount) {
countToRemove = itemAmount;
}

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


const itemToAdd: Item = {itemId: details.itemId, amount: 0};
while (countToRemove > 0 && playerInventory.has(details.itemId)) {
const invIndex = playerInventory.findIndex(details.itemId);
const invItem = playerInventory.items[invIndex];
if (countToRemove >= invItem.amount) {
itemToAdd.amount += invItem.amount;
countToRemove -= invItem.amount;
playerInventory.remove(invIndex);
} else {
itemToAdd.amount += countToRemove;
invItem.amount -= countToRemove;
countToRemove = 0;
}
}
playerBank.addStacking(itemToAdd);


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 withdrawItem: itemAction = (details) => {
// Check if player might be spawning widget clientside
if (!details.player.activeWidget ||
!(details.player.activeWidget.widgetId === widgets.bank.screenWidget.widgetId) ||
!(details.player.activeWidget.secondaryWidgetId === widgets.bank.tabWidget.widgetId)) {
return;
}
// Check if the player has the item
if (!details.player.hasItemInBank(details.itemId)) {
return;
}
let countToRemove: number;
if (details.option.endsWith('all')) {
countToRemove = -1;
} else {
countToRemove = +details.option.replace('withdraw-', '');
}

const playerBank: ItemContainer = details.player.bank;
const playerInventory: ItemContainer = details.player.inventory;
const slotWithItem: number = playerBank.findIndex(details.itemId);
const itemAmount: number = playerBank.items[slotWithItem].amount;
if (countToRemove == -1 || countToRemove > itemAmount) {
countToRemove = itemAmount;
}

if (!details.itemDetails.stackable) {
const slots = playerInventory.getOpenSlotCount();
if (slots < countToRemove) {
countToRemove = slots;
}
}

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


const itemToAdd: Item = {itemId: details.itemId, amount: 0};
while (countToRemove > 0 && playerBank.has(details.itemId)) {
const invIndex = playerBank.findIndex(details.itemId);
const invItem = playerBank.items[invIndex];
if (countToRemove >= invItem.amount) {
itemToAdd.amount += invItem.amount;
countToRemove -= invItem.amount;
playerBank.remove(invIndex);
} else {
itemToAdd.amount += countToRemove;
invItem.amount -= countToRemove;
countToRemove = 0;
}
}
playerInventory.addStacking(itemToAdd);


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 default new RunePlugin([{
type: ActionType.OBJECT_ACTION,
objectIds: objectIds.bankBooth,
Expand All @@ -34,6 +151,11 @@ export default new RunePlugin([{
}, {
type: ActionType.ITEM_ACTION,
widgets: widgets.bank.tabWidget,
options: ['deposit-1', 'deposit-5', 'deposit-10'],
options: ['deposit-1', 'deposit-5', 'deposit-10', 'deposit-all'],
action: depositItem,
}, {
type: ActionType.ITEM_ACTION,
widgets: widgets.bank.screenWidget,
options: ['withdraw-1', 'withdraw-5', 'withdraw-10', 'withdraw-all'],
action: withdrawItem,
}]);
2 changes: 1 addition & 1 deletion src/plugins/skills/woodcutting/woodcutting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const action: objectAction = (details) => {

export default new RunePlugin({
type: ActionType.OBJECT_ACTION,
options: ['chop down'],
options: ['chop down', 'chop'],
objectIds: getTreeIds(),
walkTo: true,
action
Expand Down
3 changes: 3 additions & 0 deletions src/util/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hasValueNotNull(variable: any): boolean {
return typeof variable !== 'undefined' && variable !== null;
}
1 change: 1 addition & 0 deletions src/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function runWebServer(): void {
level: p.position.level
},
inventory: p.inventory.items,
bank: p.bank.items,
equipment: p.equipment.items
};
}));
Expand Down
15 changes: 15 additions & 0 deletions src/world/actor/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export abstract class Actor {
private _runDirection: number;
private _faceDirection: number;
private readonly _inventory: ItemContainer;
private readonly _bank: ItemContainer;
public readonly skills: Skills;
private _busy: boolean;
public readonly metadata: { [key: string]: any } = {};
Expand All @@ -36,6 +37,7 @@ export abstract class Actor {
this._runDirection = -1;
this._faceDirection = 6;
this._inventory = new ItemContainer(28);
this._bank = new ItemContainer(376);
this.skills = new Skills(this);
this._busy = false;
this._combatActions = [];
Expand Down Expand Up @@ -100,13 +102,23 @@ export abstract class Actor {
this._inventory.remove(slot);
}

public removeBankItem(slot: number): void {
this._bank.remove(slot);
}

public giveItem(item: number | Item): boolean {
return this._inventory.add(item) !== null;
}
public giveBankItem(item: number | Item): boolean {
return this._bank.add(item) !== null;
}

public hasItemInInventory(item: number | Item): boolean {
return this._inventory.has(item);
}
public hasItemInBank(item: number | Item): boolean {
return this._bank.has(item);
}

public hasItemOnPerson(item: number | Item): boolean {
return this.hasItemInInventory(item);
Expand Down Expand Up @@ -286,6 +298,9 @@ export abstract class Actor {
public get inventory(): ItemContainer {
return this._inventory;
}
public get bank(): ItemContainer {
return this._bank;
}

public get busy(): boolean {
return this._busy;
Expand Down
9 changes: 9 additions & 0 deletions src/world/actor/player/action/swap-item-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ export const swapItemAction = (player: Player, fromSlot: number, toSlot: number,

inventory.swap(fromSlot, toSlot);
}
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;
}

bank.swap(fromSlot, toSlot);
}
};
5 changes: 5 additions & 0 deletions src/world/actor/player/player-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { join } from 'path';
import { logger } from '@runejs/logger';
import { Player } from './player';
import { SkillValue } from '@server/world/actor/skills';
import { hasValueNotNull } from '@server/util/data';


export interface QuestProgress {
Expand Down Expand Up @@ -58,6 +59,7 @@ export interface PlayerSave {
};
appearance: Appearance;
inventory: Item[];
bank: Item[];
equipment: Item[];
skills: SkillValue[];
settings: PlayerSettings;
Expand Down Expand Up @@ -122,6 +124,9 @@ export function savePlayerData(player: Player): boolean {
rights: player.rights.valueOf(),
appearance: player.appearance,
inventory: player.inventory.items,
bank: player.bank.items.filter((item) => {
return hasValueNotNull(item);
}),
equipment: player.equipment.items,
skills: player.skills.values,
settings: player.settings,
Expand Down
4 changes: 4 additions & 0 deletions src/world/actor/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class Player extends Actor {
public readonly actionsCancelled: Subject<ActionCancelType>;
private quadtreeKey: QuadtreeKey = null;
public savedMetadata: { [key: string]: any } = {};
public sessionMetadata: { [key: string]: any } = {};
public quests: QuestProgress[] = [];
public achievements: string[] = [];

Expand Down Expand Up @@ -152,6 +153,9 @@ export class Player extends Actor {
if(playerSave.inventory && playerSave.inventory.length !== 0) {
this.inventory.setAll(playerSave.inventory);
}
if(playerSave.bank && playerSave.bank.length !== 0) {
this.bank.setAll(playerSave.bank);
}
if(playerSave.equipment && playerSave.equipment.length !== 0) {
this.equipment.setAll(playerSave.equipment);
}
Expand Down
51 changes: 9 additions & 42 deletions src/world/config/harvestable-object.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { objectIds } from '@server/world/config/object-ids';

export interface IHarvestable {
objects: Map<number, number>;
itemId: number;
Expand Down Expand Up @@ -123,52 +125,17 @@ const RUNITE_OBJECTS: Map<number, number> = new Map<number, number>([
]);

const NORMAL_OBJECTS: Map<number, number> = new Map<number, number>([
[1276, 1342],
[1277, 1342],
[1278, 1342],
[1279, 1342],
[1280, 1342],
[1282, 1341],
[1283, 1341],
[1284, 1341],
[1285, 1341],
[1285, 1341],
[1286, 1341],
[1289, 1341],
[1290, 1341],
[1291, 1341],
[1315, 1342],
[1316, 1342],
[1318, 1342],
[1330, 1342],
[1331, 1342],
[1332, 1342],
[1365, 1342],
[1383, 1342],
[1384, 1342],
[2409, 1342],
[3033, 1342],
[3034, 1342],
[3035, 1342],
[3036, 1342],
[3881, 1342],
[3882, 1342],
[3883, 1342],
[5902, 1342],
[5903, 1342],
[5904, 1342],
[10041, 1342]
]);
...objectIds.tree.normal.map((tree) => [tree.default, tree.stump]),
...objectIds.tree.dead.map((tree) => [tree.default, tree.stump]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

] as [number, number][]);

const ACHEY_OBJECTS: Map<number, number> = new Map<number, number>([
[2023, 3371],
]);

...objectIds.tree.archey.map((tree) => [tree.default, tree.stump]),
] as [number, number][]);

const OAK_OBJECTS: Map<number, number> = new Map<number, number>([
[1281, 1342],
[3037, 1342],
]);
...objectIds.tree.oak.map((tree) => [tree.default, tree.stump]),
] as [number, number][]);


const WILLOW_OBJECTS: Map<number, number> = new Map<number, number>([
Expand Down
Loading