var _ = require("lodash");
var WebSocket = require('ws');


var ws = new WebSocket('ws://localhost:5110');
var gWaitInt = 0;

var gMoveSet = [
  [0, 0],
  [0, 1],
  [0, 2],
  [1, 0],
  [1, 1],
  [1, 2],
  [2, 0],
  [2, 1],
  [2, 2]
];

var gSession = "1:51:xxxx:0";
var gUid = "51";

function sendCmd(cmd, params) {
  var baseCmd = {
    session: gSession,
    cmd: cmd
  };

  var data = _.merge(baseCmd, params);
  console.log("send:", data);
  ws.send(JSON.stringify(data));
}

function waitForGame() {
  sendCmd("match.count", {name: "tictactoe"});
}

ws.on('open', function () {
  sendCmd("channel2.add", {channel: "lobby:0"});
  sendCmd("game.playing");
  gWaitInt = setInterval(waitForGame, 5000);
});

ws.on('close', function () {
  console.log("socket closed");
  clearInterval(gWaitInt);
});

ws.on('error', function (error) {
  console.log(error);
});

function makeMove(game) {
  for (var i=0; i<3; i++) {
    for (var j=0; j<3; j++) {
      if (game.state.gameBoard[i][j] === "") {
        sendCmd("game.turn", {gameId: game.oid, move: {x: i, y: j}});
        return;
      }
    }
  }
}

ws.on('message', function (message) {
  console.log('received: %s', message);
  try {
  var msg = JSON.parse(message);
  } catch (e) {
    console.error("unabel to parse message");
    return;
  }
  if (msg.event === "match.count") {
    if (msg.data.waiting > 0) {
      sendCmd("match.add", {name: "tictactoe"});
    }
  } else if (msg.event === "match.made") {
    sendCmd("game.join", {gameId: msg.data.gameId});
    sendCmd("channel2.add", {channel: "game:" + msg.data.gameId});
  } else if (msg.event === "game.info"
    || msg.event === "game.reset"
    || msg.event === "game.join") {
    if (msg.data.whoTurn === gUid) {
      setTimeout(function () {
        makeMove(msg.data);
      }, 1000);
    }
  } else if (msg.event === "game.playing") {
    _.each(msg.data, function (gameInfo, gameId) {
      sendCmd("channel2.add", {channel: "game:" + gameId});
      if(gameInfo.whoTurn === gUid) {
        sendCmd("game.get", {gameId: gameId});  // trigger a move base on gameBoard
      }
    });
  } else if (msg.event === "game.turn.next") {
    if (msg.data.whoTurn === gUid) {
      sendCmd("game.get", {gameId: msg.data.gameId});  // trigger a move base on gameBoard
    }
  }
});
@@ -587,33 +587,32 @@
 sendCmd(%22channel
-2
 .add%22, %7Bchannel:
@@ -888,38 +888,54 @@
 %7B%0A  
-for (var i=
+var i = 0;%0A  for (i = 
 0; i
-%3C
+ %3C 
 3; i
-++
+ += 1
 ) %7B%0A    
 for 
@@ -934,30 +934,48 @@
     
-for (var j=
+var j = 0;%0A    for (j = 
 0; j
-%3C
+ %3C 
 3; j
-++
+ += 1
 ) %7B%0A
@@ -1206,16 +1206,34 @@
 ssage);%0A
+  var msg = null;%0A
   try %7B%0A
@@ -1234,19 +1234,17 @@
 try %7B%0A  
-var
+ 
  msg = J
@@ -1322,16 +1322,30 @@
 message%22
+, e.toString()
 );%0A    r
@@ -1587,33 +1587,32 @@
 sendCmd(%22channel
-2
 .add%22, %7Bchannel:
@@ -1678,14 +1678,15 @@
 ame.
-info%22%0A
+get%22%0A  
     
@@ -1711,24 +1711,26 @@
 game.reset%22%0A
+  
     %7C%7C msg.e
@@ -1989,17 +1989,16 @@
 %22channel
-2
 .add%22, %7B

fix robo player 51;