Skip to content

Commit 99eb1b2

Browse files
committed
refactor: add types for shop config in metadata
1 parent eeb486d commit 99eb1b2

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

src/engine/config/shop-config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ export class Shop {
9797
}
9898

9999
public open(player: Player): void {
100-
player.metadata['lastOpenedShop'] = this;
101-
player.metadata['shopCloseListener'] = player.interfaceState.closed.subscribe((whatClosed: WidgetClosedEvent) => {
100+
player.metadata.lastOpenedShop = this;
101+
player.metadata.shopCloseListener = player.interfaceState.closed.subscribe((whatClosed: WidgetClosedEvent) => {
102102
if(whatClosed && whatClosed.widget && whatClosed.widget.widgetId === widgets.shop.widgetId) {
103103
this.removePlayerFromShop(player);
104104
}
@@ -121,7 +121,7 @@ export class Shop {
121121

122122
private updateCustomers() {
123123
for (const player of this.customers) {
124-
if(player.metadata['lastOpenedShop'] === this) {
124+
if(player.metadata.lastOpenedShop === this) {
125125
player.outgoingPackets.sendUpdateAllWidgetItems(widgets.shop, this.container);
126126
} else {
127127
this.removePlayerFromShop(player);
@@ -130,9 +130,9 @@ export class Shop {
130130
}
131131

132132
private removePlayerFromShop(player: Player) {
133-
if(player.metadata['lastOpenedShop'] === this) {
134-
player.metadata['lastOpenedShop'] = undefined;
135-
player.metadata['shopCloseListener'].unsubscribe();
133+
if(player.metadata.lastOpenedShop === this) {
134+
player.metadata.lastOpenedShop = undefined;
135+
player.metadata.shopCloseListener.unsubscribe();
136136
}
137137
this.customers = this.customers.filter((c) => c !== player);
138138
}

src/engine/world/actor/player/metadata.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Subscription } from 'rxjs';
12
import { Chunk } from '@engine/world/map';
23
import { Position } from '@engine/world/position';
34

@@ -29,4 +30,18 @@ export type PlayerMetadata = {
2930
* The player's last position before teleporting.
3031
*/
3132
lastPosition: Position;
33+
34+
/**
35+
* The player's currently open shop.
36+
*
37+
* TODO (jameskmonger) This is currently an instance of `Shop`. We shouldn't be storing whole instances of classes in the metadata.
38+
*/
39+
lastOpenedShop: any;
40+
41+
/**
42+
* A subscription to the player's "widget closed" events.
43+
*
44+
* Used to remove a player from a shop when they close the shop's widget.
45+
*/
46+
shopCloseListener: Subscription;
3247
};

src/plugins/items/shopping/buy-from-shop.plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const handler: itemInteractionActionHandler = (details) => {
1919
return;
2020
}
2121

22-
const openedShop: Shop = player.metadata['lastOpenedShop'];
22+
const openedShop = player.metadata.lastOpenedShop;
2323
if(!openedShop) {
2424
return;
2525
}

src/plugins/items/shopping/item-value.plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const shopSellValueHandler: itemInteractionActionHandler = ({ player, ite
88
};
99

1010
export const shopPurchaseValueHandler: itemInteractionActionHandler = ({ player, itemDetails }) => {
11-
const openedShop: Shop = player.metadata['lastOpenedShop'];
11+
const openedShop = player.metadata.lastOpenedShop;
1212
if(!openedShop) {
1313
return;
1414
}

src/plugins/items/shopping/sell-to-shop.plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const handler: itemInteractionActionHandler = (details) => {
1212
return;
1313
}
1414

15-
const openedShop: Shop = player.metadata['lastOpenedShop'];
15+
const openedShop = player.metadata.lastOpenedShop;
1616
if(!openedShop) {
1717
return;
1818
}

0 commit comments

Comments
 (0)