var _ = require('underscore');

var livereload = require('livereload');
var server = livereload.createServer();
server.watch(__dirname + '/public');

var express = require('express');
var app = express();

var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(express.static(__dirname + '/public'));
http.listen(8080, function() {
    console.log('listening on 8080');
});

var width = 10;
var height = 5;

var gameState = [];

for (var i = 0; i < height; i++) {
    gameState.push([]);
}

var numPlayers = 4;

for (var x = 0; x < width; x++) {
    for (var y = 0; y < height; y++) {
        gameState[y][x] = Math.floor(Math.random() * numPlayers);
    }
}

var drawGameState = function() {
    console.log(gameState);
    /*
    for (var y = 0; y < height; y++) {
        for (var x = 0; x < width; x++) {
            process.stdout.write(gameState[x][y] + ', ');
        }
        process.stdout.write('\n');
    }
    */
};
drawGameState();

var doMove = function(from, direction) {
    var temp = gameState[from.y][from.x];

    switch (direction) {
        case 'u':
            if (from.y <= 0) {
                console.log('invalid move!');
                return;
            }

            gameState[from.y][from.x] = gameState[from.y - 1][from.x];
            gameState[from.y - 1][from.x] = temp;

            var to = {x: from.x, y: from.y - 1};
            io.sockets.emit('doMove', [from, to]);
            drawGameState();
            break;
        case 'd':
            if (from.y > height - 1) {
                console.log('invalid move!');
                return;
            }

            gameState[from.y][from.x] = gameState[from.y + 1][from.x];
            gameState[from.y + 1][from.x] = temp;

            var to = {x: from.x, y: from.y + 1};
            io.sockets.emit('doMove', [from, to]);
            drawGameState();
            break;
        case 'l':
            if (from.x <= 0) {
                console.log('invalid move!');
                return;
            }

            gameState[from.y][from.x] = gameState[from.y][from.x - 1];
            gameState[from.y][from.x - 1] = temp;

            var to = {x: from.x - 1, y: from.y};
            io.sockets.emit('doMove', [from, to]);
            drawGameState();
            break;
        case 'r':
            if (from.x > width - 1) {
                console.log('invalid move!');
                return;
            }

            gameState[from.y][from.x] = gameState[from.y][from.x + 1];
            gameState[from.y][from.x + 1] = temp;

            var to = {x: from.x + 1, y: from.y};
            io.sockets.emit('doMove', [from, to]);
            drawGameState();
            break;
        default:
            console.log('invalid move received! ' + direction);
    }
};

io.on('connection', function(socket) {
    socket.emit('gameState', gameState);
});

var token = require(process.env.HOME + '/.diagram-bot-token.js');
var Bot = require('node-telegram-bot');

var bot = new Bot({
    token: token
})
.on('message', function(msg) {
    if (msg.text) {
        console.log('got TG message: ' + msg.text);
        var x = msg.text.charCodeAt(0) - 'a'.charCodeAt(0);
        var y = msg.text.charCodeAt(1) - '0'.charCodeAt(0);
        var dir = msg.text.substr(2, 3);

        doMove({x: x, y: y}, dir);
    }
});

bot.start();

@@ -1008,24 +1008,174 @@
 irection) %7B%0A
+    if (from.x %3C 0 %7C%7C from.x %3E width - 1 %7C%7C%0A        from.y %3C 0 %7C%7C from.y %3E height - 1) %7B%0A        console.log('invalid move!');%0A        return;%0A    %7D%0A%0A
     var temp
@@ -1204,16 +1204,16 @@
 rom.x%5D;%0A
-
 %0A    swi
@@ -1668,32 +1668,33 @@
     if (from.y %3E
+=
  height - 1) %7B%0A 
@@ -2485,32 +2485,33 @@
     if (from.x %3E
+=
  width - 1) %7B%0A  
@@ -3213,24 +3213,24 @@
 tion(msg) %7B%0A
-
     if (msg.
@@ -3233,24 +3233,67 @@
 msg.text) %7B%0A
+        msg.text = msg.text.toLowerCase();%0A
         cons

fix off by one