-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from nicknick-io/implement-shops
Extremely simple implementation of shops.
- Loading branch information
Showing
12 changed files
with
272 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,11 @@ | |
x: 3222 | ||
y: 3220 | ||
radius: 4 | ||
- npcId: 520 | ||
x: 3211 | ||
y: 3247 | ||
radius: 1 | ||
- npcId: 519 | ||
x: 3230 | ||
y: 3203 | ||
radius: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
- identification: LUMBRIDGE_GENERAL_STORE | ||
name: Lumbridge General Store | ||
items: | ||
- id: 1931 | ||
text: Pot | ||
price: 1 | ||
amountInStock: 5 | ||
- id: 1935 | ||
text: Jug | ||
price: 1 | ||
amountInStock: 2 | ||
- id: 5603 | ||
text: Shears | ||
price: 1 | ||
amountInStock: 2 | ||
- id: 1925 | ||
text: Bucket | ||
price: 2 | ||
amountInStock: 3 | ||
- id: 1887 | ||
text: Cake tin | ||
price: 13 | ||
amountInStock: 2 | ||
- id: 590 | ||
text: Tinderbox | ||
price: 1 | ||
amountInStock: 2 | ||
- id: 1755 | ||
text: Chisel | ||
price: 1 | ||
amountInStock: 2 | ||
- id: 952 | ||
text: Spade | ||
price: 3 | ||
amountInStock: 5 | ||
- id: 2347 | ||
text: Hammer | ||
price: 1 | ||
amountInStock: 5 | ||
- identification: BOBS_AXES | ||
name: Bob's Brilliant Axes | ||
items: | ||
- id: 1265 | ||
text: Bronze pickaxe | ||
price: 1 | ||
amountInStock: 5 | ||
- id: 1351 | ||
text: Bronze axe | ||
price: 16 | ||
amountInStock: 10 | ||
- id: 1349 | ||
text: Iron axe | ||
price: 56 | ||
amountInStock: 5 | ||
- id: 1353 | ||
text: Steel axe | ||
price: 200 | ||
amountInStock: 3 | ||
- id: 1363 | ||
text: Iron battleaxe | ||
price: 182 | ||
amountInStock: 5 | ||
- id: 1365 | ||
text: Steel battleaxe | ||
price: 650 | ||
amountInStock: 2 | ||
- id: 1369 | ||
text: Mithril battleaxe | ||
price: 1690 | ||
amountInStock: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { npcAction, NpcActionPlugin } from '@server/world/mob/player/action/npc-action'; | ||
import { openShop } from '@server/world/mob/player/action/shop-action'; | ||
|
||
const action: npcAction = (details) => { | ||
const { player, npc } = details; | ||
openShop(details.player, 'BOBS_AXES'); | ||
}; | ||
|
||
export default { npcIds: 519, options: 'trade', walkTo: true, action } as NpcActionPlugin; |
10 changes: 10 additions & 0 deletions
10
src/plugins/npc/lumbridge/general-store/shopkeeper-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { npcAction, NpcActionPlugin } from '@server/world/mob/player/action/npc-action'; | ||
import { openShop } from '@server/world/mob/player/action/shop-action'; | ||
|
||
|
||
const action: npcAction = (details) => { | ||
const { player, npc } = details; | ||
openShop(details.player, 'LUMBRIDGE_GENERAL_STORE'); | ||
}; | ||
|
||
export default { npcIds: 520, options: 'trade', walkTo: true, action } as NpcActionPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { logger } from '@runejs/logger/dist/logger'; | ||
import { JSON_SCHEMA, safeLoad } from 'js-yaml'; | ||
import { readFileSync } from 'fs'; | ||
|
||
export interface Shop { | ||
identification: string; | ||
name: string; | ||
interfaceId: number; | ||
items: ShopItems[]; | ||
} | ||
|
||
interface ShopItems { | ||
id: number; | ||
name: string; | ||
amountInStock: number; | ||
price: number; | ||
} | ||
|
||
export function parseShops(): Shop[] { | ||
try { | ||
logger.info('Parsing shops...'); | ||
|
||
const shops = safeLoad(readFileSync('data/config/shops.yaml', 'utf8'), { schema: JSON_SCHEMA }) as Shop[]; | ||
|
||
if(!shops || shops.length === 0) { | ||
throw 'Unable to read shops.'; | ||
} | ||
|
||
logger.info(`${shops.length} shops found.`); | ||
|
||
return shops; | ||
} catch(error) { | ||
logger.error('Error parsing shops: ' + error); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Player } from '@server/world/mob/player/player'; | ||
import { gameCache } from '@server/game-server'; | ||
import { widgetIds } from '@server/world/mob/player/widget'; | ||
|
||
export const buyItemAction = (player: Player, itemId: number, amount: number, slot: number, interfaceId: number) => { | ||
|
||
const purchasedItem = gameCache.itemDefinitions.get(itemId); | ||
const coinsInInventoryIndex = player.inventory.findIndex(995); | ||
const amountInStack = player.inventory.amountInStack(coinsInInventoryIndex); | ||
const amountLeftAfterPurchase = amountInStack - (purchasedItem.value * amount); | ||
|
||
// Take the money. | ||
player.inventory.set(player.inventory.findIndex(itemId), { itemId, amount: amount}); | ||
player.inventory.set(coinsInInventoryIndex, {itemId: 995, amount: amountLeftAfterPurchase}); | ||
|
||
// Add the purchased item(s) to the inventory. | ||
if (amount > 1) { | ||
for (let i = 0; i < amount; i++) { | ||
player.inventory.add(itemId); | ||
} | ||
} | ||
|
||
if(amount === 1) { | ||
player.inventory.add(itemId); | ||
} | ||
|
||
// Update the inventory items. | ||
player.packetSender.sendUpdateAllWidgetItems(widgetIds.inventory, player.inventory); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { world } from '@server/game-server'; | ||
import { Player } from '@server/world/mob/player/player'; | ||
import { logger } from '@runejs/logger/dist/logger'; | ||
import { Shop } from '@server/world/config/shops'; | ||
|
||
function findShop(identification: string): Shop { | ||
for(let i = 0; i <= world.shops.length; i++) { | ||
if(world.shops[i].identification === identification) return world.shops[i]; | ||
} | ||
return undefined; | ||
} | ||
|
||
export function openShop(player: Player, identification: string, closeOnWalk: boolean = true): void { | ||
try { | ||
const openedShop = findShop(identification); | ||
if(openedShop === undefined) { | ||
throw `Unable to find the shop with identification of: ${identification}`; | ||
} | ||
player.packetSender.updateWidgetString(3901, openedShop.name); | ||
for(let i = 0; i < 30; i++) { | ||
if(openedShop.items.length <= i) { | ||
player.packetSender.sendUpdateSingleWidgetItem(3900, i, null); | ||
} else { | ||
player.packetSender.sendUpdateSingleWidgetItem(3900, i, { | ||
itemId: openedShop.items[i].id, amount: openedShop.items[i].amountInStock | ||
}); | ||
} | ||
} | ||
for(let i = 0; i < openedShop.items.length; i++) { | ||
player.packetSender.sendUpdateSingleWidgetItem(3900, i, { | ||
itemId: openedShop.items[i].id, amount: openedShop.items[i].amountInStock | ||
}); | ||
} | ||
player.activeWidget = { | ||
widgetId: 3824, | ||
type: 'SCREEN', | ||
closeOnWalk: closeOnWalk | ||
}; | ||
} catch (error) { | ||
logger.error(`Error opening shop ${identification}: ` + error); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { incomingPacket } from '@server/world/mob/player/packet/incoming-packet'; | ||
import { Player } from '@server/world/mob/player/player'; | ||
import { RsBuffer } from '@server/net/rs-buffer'; | ||
import { gameCache } from '@server/game-server'; | ||
import { buyItemAction } from '@server/world/mob/player/action/buy-item-action'; | ||
|
||
export const buyItemPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => { | ||
|
||
if(packetId === 177) { | ||
const slot = packet.readNegativeOffsetShortBE(); | ||
const itemId = packet.readShortLE(); | ||
const interfaceId = packet.readShortLE(); | ||
|
||
if(player.inventory.findItemIndex({itemId: 995, amount: gameCache.itemDefinitions.get(itemId).value}) === undefined) { | ||
player.packetSender.chatboxMessage(`You don't have enough coins.`); | ||
return; | ||
} | ||
|
||
buyItemAction(player, itemId, 1, slot, interfaceId); | ||
} | ||
|
||
if(packetId === 91) { | ||
const itemId = packet.readShortLE(); | ||
const slot = packet.readNegativeOffsetShortLE(); | ||
const interfaceId = packet.readShortBE(); | ||
|
||
if(player.inventory.findItemIndex({itemId: 995, amount: gameCache.itemDefinitions.get(itemId).value * 5}) === undefined) { | ||
player.packetSender.chatboxMessage(`You don't have enough coins.`); | ||
return; | ||
} | ||
|
||
buyItemAction(player, itemId, 5, slot, interfaceId); | ||
} | ||
|
||
if(packetId === 231) { | ||
const interfaceId = packet.readNegativeOffsetShortLE(); | ||
const slot = packet.readShortLE(); | ||
const itemId = packet.readShortBE(); | ||
|
||
if(player.inventory.findItemIndex({itemId: 995, amount: gameCache.itemDefinitions.get(itemId).value * 10}) === undefined) { | ||
player.packetSender.chatboxMessage(`You don't have enough coins.`); | ||
return; | ||
} | ||
|
||
buyItemAction(player, itemId, 10, slot, interfaceId); | ||
} | ||
|
||
return; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters