import { createServer, provideServerModules, RpgPlayer, RpgMap } from '@rpgjs/server'
import { Item } from '@rpgjs/database'
// Define shop items as classes (optional, can also be objects)
@Item({
name: 'Shop Potion',
description: 'A potion sold in shops',
price: 200,
hpValue: 100,
consumable: true
})
export class ShopPotion {
onUse(player: RpgPlayer) {
player.hp += 100;
player.showText('HP restored!');
}
}
@Item({
name: 'Iron Sword',
description: 'A basic sword',
price: 500
})
export class IronSword {
onEquip(player: RpgPlayer, equip: boolean) {
if (equip) {
player.showText('Iron Sword equipped!');
}
}
}
export function ShopEvent() {
return {
name: "SHOP-1",
onInit() {
this.setGraphic("shopkeeper");
},
async onAction(player: RpgPlayer) {
const map = player.getCurrentMap();
// Option 1: Use pre-registered items from database
await player.showText('Welcome to my shop!');
try {
player.buyItem('ShopPotion', 1);
player.showText('Thank you for your purchase!');
} catch (err) {
player.showText('Not enough gold!');
}
// Option 2: Create dynamic items on the fly
const dynamicItem = {
id: 'limited-edition-potion',
name: 'Limited Edition Potion',
description: 'A rare potion',
price: 500,
hpValue: 200,
consumable: true,
onUse(player) {
player.hp += 200;
player.showText('Super HP restored!');
}
};
// Register in database first
map.addInDatabase('limited-edition-potion', dynamicItem);
// Then add to player
player.addItem('limited-edition-potion', 1);
}
};
}
export default createServer({
providers: [
provideServerModules([
{
database: {
ShopPotion, // Pre-registered items
IronSword
},
player: {
// ... your player hooks
},
maps: [
{
id: "town",
events: [{ event: ShopEvent() }]
}
]
}
])
]
});