-
Notifications
You must be signed in to change notification settings - Fork 83
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 #12 from rune-js/commands
Adding a player input command handling system.
- Loading branch information
Showing
7 changed files
with
110 additions
and
5 deletions.
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
83 changes: 83 additions & 0 deletions
83
src/server/world/entity/mob/player/action/input-command-action.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,83 @@ | ||
import { Player } from '../player'; | ||
import { logger } from '@runejs/logger/dist/logger'; | ||
import { world } from '../../../../../game-server'; | ||
import { throws } from 'assert'; | ||
import { interfaceIds } from '../game-interface'; | ||
|
||
type commandHandler = (player: Player, args?: string[]) => void; | ||
|
||
const commands: { [key: string]: commandHandler } = { | ||
|
||
pos: (player: Player) => { | ||
player.packetSender.chatboxMessage(`@[ ${player.position.x}, ${player.position.y}, ${player.position.level} ]`); | ||
}, | ||
|
||
move: (player: Player, args: string[]) => { | ||
if(args.length < 2 || args.length > 3) { | ||
throw `move x y [level]`; | ||
} | ||
|
||
const x: number = parseInt(args[0], 10); | ||
const y: number = parseInt(args[1], 10); | ||
let level: number = 0; | ||
|
||
if(args.length === 3) { | ||
level = parseInt(args[2]); | ||
} | ||
|
||
if(isNaN(x) || isNaN(y) || isNaN(level)) { | ||
throw `move x y [level]`; | ||
} | ||
|
||
const oldChunk = world.chunkManager.getChunkForWorldPosition(player.position); | ||
|
||
player.position.move(x, y, level); | ||
|
||
const newChunk = world.chunkManager.getChunkForWorldPosition(player.position); | ||
|
||
if(!oldChunk.equals(newChunk)) { | ||
oldChunk.removePlayer(player); | ||
newChunk.addPlayer(player); | ||
player.packetSender.sendCurrentMapRegion(); | ||
} | ||
|
||
player.updateFlags.mapRegionUpdateRequired = true; | ||
}, | ||
|
||
give: (player: Player, args: string[]) => { | ||
if(args.length !== 1) { | ||
throw `give itemId`; | ||
} | ||
|
||
const inventorySlot = player.inventory.getFirstOpenSlot(); | ||
|
||
if(inventorySlot === -1) { | ||
player.packetSender.chatboxMessage(`You don't have enough free space to do that.`); | ||
return; | ||
} | ||
|
||
const itemId: number = parseInt(args[0]); | ||
|
||
if(isNaN(itemId)) { | ||
throw `give itemId`; | ||
} | ||
|
||
const item = { itemId, amount: 1 }; | ||
player.inventory.add(item); | ||
player.packetSender.sendUpdateSingleInterfaceItem(interfaceIds.inventory, inventorySlot, item); | ||
player.packetSender.chatboxMessage(`Adding 1x ${world.itemData.get(itemId).name} to inventory.`); | ||
} | ||
|
||
}; | ||
|
||
export const inputCommandAction = (player: Player, command: string, args: string[]): void => { | ||
if(commands.hasOwnProperty(command)) { | ||
try { | ||
commands[command](player, args); | ||
} catch(invalidSyntaxError) { | ||
player.packetSender.chatboxMessage(`Invalid command syntax, try ::${invalidSyntaxError}`); | ||
} | ||
} else { | ||
logger.info(`Unhandled command ${command} with arguments ${JSON.stringify(args)}.`); | ||
} | ||
}; |
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
19 changes: 19 additions & 0 deletions
19
src/server/world/entity/mob/player/packet/impl/command-packet.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,19 @@ | ||
import { incomingPacket } from '../incoming-packet'; | ||
import { Player } from '../../player'; | ||
import { RsBuffer } from '../../../../../../net/rs-buffer'; | ||
import { inputCommandAction } from '../../action/input-command-action'; | ||
|
||
export const commandPacket: incomingPacket = (player: Player, packetId: number, packetSize: number, packet: RsBuffer): void => { | ||
const input = packet.readString(); | ||
|
||
if(!input || input.trim().length === 0) { | ||
return; | ||
} | ||
|
||
const args = input.trim().split(' '); | ||
const command = args[0]; | ||
|
||
args.splice(0, 1); | ||
|
||
inputCommandAction(player, command, args); | ||
}; |
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
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