Permalink
Browse files

Separated command parser

  • Loading branch information...
1 parent 6a52ee3 commit b2fee66c345797888f11087c5c05469e0df5aa4d @bpierre committed Apr 29, 2012
Showing with 123 additions and 35 deletions.
  1. +39 −0 lib/command-parser.js
  2. +10 −35 lib/game.js
  3. +74 −0 test/command-parser.js
View
39 lib/command-parser.js
@@ -0,0 +1,39 @@
+var commands = {};
+
+commands['move'] = {
+ regex: /^move +(N|E|S|W|NORTH|EAST|SOUTH|WEST)\W?$/i,
+ cleanValue: function(commandStr) {
+ var val = commandStr.match(this.regex);
+ return val[1].slice(0,1).toUpperCase();
+ }
+};
+
+commands['attack'] = {
+ regex: /^attack +(([a-zA-Z])([1-9][0-9]?)|([1-9][0-9]?)([a-zA-Z]))\W?$/i,
+ cleanValue: function(commandStr) {
+ var val = commandStr.match(this.regex);
+ if (val[4] && val[5]) {
+ return [val[5].toUpperCase(), val[4]];
+ }
+ return [val[2].toUpperCase(), val[3]];
+ }
+};
+
+function commandParser(commandStr) {
+ var command;
+ for (var name in commands) {
+ if (commands.hasOwnProperty(name) && commands[name].regex.test(commandStr)) {
+ command = name;
+ break;
+ }
+ }
+ if (!command) {
+ return false;
+ }
+ return {
+ action: command,
+ value: commands[name].cleanValue(commandStr)
+ };
+};
+
+module.exports = commandParser;
View
45 lib/game.js
@@ -4,11 +4,8 @@ var util = require('util'),
Player = require('./player'),
Terrain = require('./terrain'),
Data = require('./data').Data,
+ commandParser = require('./command-parser'),
Game,
- commands = {
- move: /^move (N|E|S|W)/,
- cast: /^cast ([a-zA-Z])([1-9][0-9]?)/
- },
DEFAULT_SETTINGS = {
width: 13,
height: 19,
@@ -81,19 +78,12 @@ Game.prototype.movePlayer = function(player, direction) {
x = player.position[0],
y = player.position[1],
isOccupied;
+
switch (direction) {
- case 'N':
- y--;
- break;
- case 'E':
- x++;
- break;
- case 'S':
- y++;
- break;
- case 'W':
- x--;
- break;
+ case 'N': y--; break;
+ case 'E': x++; break;
+ case 'S': y++; break;
+ case 'W': x--; break;
}
isOccupied = this.terrain.isOccupied(x, y);
@@ -107,7 +97,7 @@ Game.prototype.movePlayer = function(player, direction) {
this.emit('update');
return true;
-}
+};
Game.prototype.castPlayer = function(player, col, line) {
var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
@@ -131,7 +121,7 @@ Game.prototype.castPlayer = function(player, col, line) {
other.health -= 5;
this.emit('update');
return true;
-}
+};
Game.prototype.getPlayerById = function(id) {
if (this.player1.email === id) {
@@ -160,30 +150,15 @@ Game.prototype.command = function(player, commandStr) {
return false;
}
- for (var i in commands) {
- if (commands.hasOwnProperty(i) && commands[i].test(commandStr)) {
- command = i;
- break;
- }
- }
+ command = commandParser(commandStr);
if (!command) {
console.log('[COMMAND DOES NOT EXISTS] %s: %s', player.email, commandStr);
return false;
}
- commandValue = commandStr.match(commands[command]);
-
console.log('[COMMAND] %s: %s', player.email, commandStr);
-
- switch (command) {
- case 'move':
- player.waitActions.move = commandValue[1];
- break;
- case 'cast':
- player.waitActions.cast = [commandValue[1], commandValue[2]];
- break;
- }
+ player.waitActions[command.action] = command.value;
return true;
};
View
74 test/command-parser.js
@@ -0,0 +1,74 @@
+var assert = require('assert'),
+ commandParser = require('../lib/command-parser');
+
+describe('command-parser', function(){
+
+ it('should parse a move command', function() {
+ var res,
+ values = {
+ 'N': {
+ 'eq': [ 'move n', 'move N', 'move NORTH', 'move north ', 'move nOrTh' ],
+ 'neq': [ 'move NORTHE' ]
+ },
+ 'E': {
+ 'eq': [ 'move e', 'move E', 'move EAST', 'MoVe east', 'move eAsT' ],
+ 'neq': [ 'move eeast' ]
+ },
+ 'S': {
+ 'eq': [ 'move s', 'move S', 'move SOUTH', 'MOVE south', 'move sOuTh' ],
+ 'neq': [ 'moove s' ]
+ },
+ 'W': {
+ 'eq': [ 'move w', 'move W', 'move WEST', 'move west', 'move wEsT', 'move west' ],
+ 'neq': [ 'movve west']
+ },
+ };
+
+ for (var direction in values) {
+ if (values.hasOwnProperty(direction)) {
+ // Valid
+ for (var i=0; i < values[direction].eq.length; i++) {
+ res = commandParser(values[direction].eq[i]);
+ assert.equal(res.action, 'move');
+ assert.equal(res.value, direction);
+ }
+ // Not valid
+ for (var i=0; i < values[direction].neq.length; i++) {
+ res = commandParser(values[direction].neq[i]);
+ assert.equal(res, false);
+ }
+ }
+ }
+ });
+
+ it('should parse an attack command', function() {
+ var res,
+ valid = [
+ ['ATTACK D20', 'D', '20'],
+ ['attack 28D', 'D', '28'],
+ ['attack d28', 'D', '28']
+ ],
+ notValid = [
+ 'attack n',
+ 'atack d3',
+ 'attack 1',
+ 'attack 1d1',
+ 'attack d',
+ 'attack d1d'
+ ];
+
+ // Valid
+ for (var i=0; i < valid.length; i++) {
+ res = commandParser(valid[i][0]);
+ assert.equal(res.action, 'attack');
+ assert.equal(res.value[0], valid[i][1]);
+ assert.equal(res.value[1], valid[i][2]);
+ }
+ // Not valid
+ for (var i=0; i < notValid.length; i++) {
+ res = commandParser(notValid[i]);
+ assert.equal(res, false);
+ }
+ });
+
+});

0 comments on commit b2fee66

Please sign in to comment.