Skip to content

Commit

Permalink
Merge pull request #280 from rune-js/fix/shop-sync
Browse files Browse the repository at this point in the history
sync shop between connected clients
  • Loading branch information
Tynarus authored Mar 24, 2021
2 parents 5c551db + b3e1b94 commit 2666a3d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
32 changes: 31 additions & 1 deletion src/game-engine/config/shop-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ItemContainer } from '@engine/world/items/item-container';
import { ContainerUpdateEvent, ItemContainer } from '@engine/world/items/item-container';
import { findItem, loadConfigurationFiles, widgets } from '@engine/config/index';
import { Player } from '@engine/world/actor/player/player';
import { ItemDetails } from '@engine/config/item-config';
import { WidgetClosedEvent } from '@engine/world/actor/player/interface-state';
import { Subscription } from 'rxjs';


export type ShopStock = [ [ string, number ] ];
Expand All @@ -18,6 +20,8 @@ export class Shop {
public sellRate: number;
public buyRate: number;
public rateModifier: number;
private customers: Player[];
private containerSubscription: Subscription;

public constructor(key: string, name: string, generalStore: boolean, stock: ShopStock, sellRate: number, buyRate: number, modifier: number) {
this.key = key;
Expand All @@ -30,6 +34,8 @@ export class Shop {
this.rateModifier = modifier;
this.originalStock = stock;
this.container = new ItemContainer(40);
this.containerSubscription = this.container.containerUpdated.subscribe((_update: ContainerUpdateEvent) => this.updateCustomers());
this.customers = [];
this.resetShopStock();
}

Expand Down Expand Up @@ -91,6 +97,12 @@ export class Shop {

public open(player: Player): void {
player.metadata['lastOpenedShop'] = this;
player.metadata['shopCloseListener'] = player.interfaceState.closed.subscribe((whatClosed: WidgetClosedEvent) => {
if(whatClosed && whatClosed.widget && whatClosed.widget.widgetId === widgets.shop.widgetId) {
this.removePlayerFromShop(player);
}
});

player.outgoingPackets.updateWidgetString(widgets.shop.widgetId, widgets.shop.title, this.name);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.shop, this.container);
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.shopPlayerInventory, player.inventory);
Expand All @@ -103,8 +115,26 @@ export class Shop {
slot: 'tabarea',
multi: true
});
this.customers.push(player);
}

private updateCustomers() {
for (const player of this.customers) {
if(player.metadata['lastOpenedShop'] === this) {
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.shop, this.container);
} else {
this.removePlayerFromShop(player);
}
}
}

private removePlayerFromShop(player: Player) {
if(player.metadata['lastOpenedShop'] === this) {
player.metadata['lastOpenedShop'] = undefined;
player.metadata['shopCloseListener'].unsubscribe();
}
this.customers = this.customers.filter((c) => c !== player);
}
}

export interface ShopConfiguration {
Expand Down
3 changes: 1 addition & 2 deletions src/game-engine/world/actor/player/interface-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Widget {

}

interface WidgetClosedEvent {
export interface WidgetClosedEvent {
widget: Widget;
data?: number;
}
Expand Down Expand Up @@ -129,7 +129,6 @@ export class InterfaceState {
public closeWidget(slot: GameInterfaceSlot, data?: number): void;
public closeWidget(i: GameInterfaceSlot | number, data?: number): void {
let widget: Widget | null;

if(typeof i === 'number') {
widget = this.findWidget(i);
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/npcs/lumbridge/shopkeeper.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { npcInteractionActionHandler } from '@engine/world/action/npc-interactio
import { findShop } from '@engine/config';


const action: npcInteractionActionHandler = ({ player }) =>
const action: npcInteractionActionHandler = ({ player }) => {
findShop('rs:lumbridge_general_store')?.open(player);

}
export default {
pluginId: 'rs:lumbridge_general_store',
hooks: [
Expand Down

0 comments on commit 2666a3d

Please sign in to comment.