summaryrefslogtreecommitdiff
path: root/interactor
diff options
context:
space:
mode:
authortomsmeding <hallo@tomsmeding.nl>2015-10-23 18:10:19 +0200
committertomsmeding <hallo@tomsmeding.nl>2015-10-23 18:10:19 +0200
commit51bf3a48efb1b4109507bb7aaba15dd62f8b1c0f (patch)
tree77190afa90969e6abf4c0209c7d8ac4afbedeff2 /interactor
parent39901aa8410680de099e40cfb5577ca3cc4a13a3 (diff)
Bugfixing and 7x8 -> 8x9
Diffstat (limited to 'interactor')
-rw-r--r--interactor/common.js2
-rwxr-xr-xinteractor/interactor.js38
2 files changed, 32 insertions, 8 deletions
diff --git a/interactor/common.js b/interactor/common.js
index af7c462..c098b5c 100644
--- a/interactor/common.js
+++ b/interactor/common.js
@@ -1,4 +1,4 @@
-var W=7,H=8;
+var W=8,H=9;
function emptyboard(){
return new Array(H).fill(0).map(function(){
diff --git a/interactor/interactor.js b/interactor/interactor.js
index 32f8517..c31b93f 100755
--- a/interactor/interactor.js
+++ b/interactor/interactor.js
@@ -74,8 +74,24 @@ io.on("connection",function(conn){
var bd=emptyboard();
var aiplayer=0;
+ var now=new Date();
+ var logfile=fs.openSync(
+ "logs/"+
+ now.getFullYear()+
+ ("00"+now.getMonth()).slice(-2)+
+ ("00"+now.getDate()).slice(-2)+"-"+
+ ("00"+now.getHours()).slice(-2)+
+ ("00"+now.getMinutes()).slice(-2)+
+ ("00"+now.getSeconds()).slice(-2),
+ "w");
+ var log=function(s){
+ fs.writeSync(logfile,s+"\n");
+ };
+
conn.on("disconnect",function(){
console.log("Disconnect id "+id);
+ log("Ending log on dissconnect (id "+id+")");
+ fs.closeSync(logfile);
if(proc){stopproc(proc);proc=null;}
if(pingtimeout)clearTimeout(pingtimeout);
});
@@ -83,18 +99,20 @@ io.on("connection",function(conn){
if(pingtimeout)clearTimeout(pingtimeout);
pingtimeout=setTimeout(function(){
console.log("Ping timeout on id "+id+"!");
+ log("Ping timeout on id "+id+"!");
if(proc){stopproc(proc);proc=null;}
conn.disconnect(true);
},4000);
});
conn.on("usermove",function(idx){
idx=+idx;
- if(idx<0||idx>=W*H){
+ if(isNaN(idx)||idx<0||idx>=W*H){
conn.emit("alert","You sent an invalid move, index "+idx);
conn.emit("getusermove");
return;
}
var x=idx%W,y=~~(idx/W);
+ log("User: "+x+" "+y);
bd[y][x].c=1-aiplayer;
bd[y][x].n++;
bd=stabilise(bd);
@@ -102,6 +120,7 @@ io.on("connection",function(conn){
var win=checkwin(bd);
if(win!=-1){
conn.emit("win",1-aiplayer);
+ log("Win: user");
if(proc){stopproc(proc);proc=null;}
return;
}
@@ -115,6 +134,7 @@ io.on("connection",function(conn){
stdio:["pipe","pipe",process.stderr]
});
childpids.push(proc.pid);
+ log("Process started with pid "+proc.pid);
var buffer="";
proc.stdout.on("data",function(data){
var idx,line,mv,win;
@@ -123,20 +143,24 @@ io.on("connection",function(conn){
line=buffer.slice(0,idx);
buffer=buffer.slice(idx+1);
mv=line.split(" ").map(function(s){return parseInt(s,10);});
- if(mv.length!=2||isNaN(mv[0])||isNaN(mv[1])){
+ var x=mv[0],y=mv[1];
+ if(mv.length!=2||isNaN(x)||isNaN(y)||x<0||x>=W||y<0||y>=H){
console.log("Invalid move written by AI: '"+line+"'");
+ log("Invalid move written by AI: '"+line+"'");
conn.emit("alert","Invalid move written by AI: '"+line+"'");
- mv[0]=0;
- mv[1]=0;
+ x=0;
+ y=0;
}
- bd[mv[1]][mv[0]].c=aiplayer;
- bd[mv[1]][mv[0]].n++;
+ log("AI: "+x+" "+y);
+ bd[y][x].c=aiplayer;
+ bd[y][x].n++;
bd=stabilise(bd);
- conn.emit("applymove",{index:W*mv[1]+mv[0],player:aiplayer});
+ conn.emit("applymove",{index:W*y+x,player:aiplayer});
if(countballs(bd)>2){
win=checkwin(bd);
if(win!=-1){
conn.emit("win",aiplayer);
+ log("Win: AI");
if(proc){stopproc(proc);proc=null;}
return;
}