#!/usr/bin/env node const io=require("socket.io-client"); const child_process=require("child_process"); let SERVER_URL=process.argv[3]?process.argv[3]:"http://localhost:15937"; if(process.argv.length<3||process.argv.length>4){ console.log("Usage: ./botproxy.js <./bot> [http://server-url]"); process.exit(1); } const BOT_CMD=process.argv[2]; function boardEmpty(bd){ for(const v of bd)if(v!=0)return false; return true; } function boardDetectMove(prev,cur){ for(let i=0;i{ socket.emit("my_info",(p)=>{ ME=p; console.log(`PROXY: My name: '${ME.name}'`); }); }); socket.on("err",(err)=>console.log(`ERROR: ${err}`)); socket.on("game_create",(game)=>{ console.log(`PROXY: <- game_create ${JSON.stringify(game)}`); if(game.size!=9){ console.log("PROXY: Invited for game with non-9 size!"); socket.emit("leave_game",game.id); return; } currentGame=game; botproc=child_process.spawn(BOT_CMD,{shell: true, stdio: ["pipe","pipe",process.stderr]}); let botbuf=""; botproc.stdout.on("data",(data)=>{ botbuf+=data.toString(); let idx=botbuf.indexOf("\n"); while(idx!=-1){ handleBotLine(botbuf.slice(0,idx)); botbuf=botbuf.slice(idx+1); idx=botbuf.indexOf("\n"); } }); botproc.stdout.on("end",()=>{ console.log("PROXY: Bot process exited!"); botproc.kill(); botproc=null; if(currentGame)socket.emit("leave_game",currentGame.id); socket.close(); }); }); socket.on("game_leave",(g,p)=>{ console.log(`PROXY: <- game_leave ${JSON.stringify(g)} ${JSON.stringify(p)}`); if(g.id!=currentGame.id)return; socket.emit("leave_game",g.id); if(botproc)botproc.kill(); currentGame=null; }); socket.on("game_turn",(g)=>{ console.log(`PROXY: <- game_turn ${JSON.stringify(g)}`); if(g.id!=currentGame.id)return; if(!botproc){ console.log("PROXY: game_turn while bot already died"); socket.emit("leave_game",g.id); return; } const prevBoard=currentGame.board; currentGame=g; const mv=boardDetectMove(prevBoard,currentGame.board); const towrite=mv==-1?"Start":encodeMove(mv,currentGame.size); botproc.stdin.write(towrite+"\n"); console.log(`PROXY: ->bot '${towrite}'`); }); socket.on("game_other_turn",(g)=>{ console.log(`PROXY: <- game_other_turn ${JSON.stringify(g)}`); if(g.id!=currentGame.id)return; currentGame=g; }); socket.on("game_win",(g,p)=>{ console.log(`PROXY: <- game_win ${JSON.stringify(g)} ${JSON.stringify(p)}`); if(g.id!=currentGame.id)return; if(p.id==ME.id)console.log("PROXY: We won"); else console.log("PROXY: We lost"); socket.emit("leave_game",g.id); if(botproc)botproc.kill(); socket.close(); });