summaryrefslogtreecommitdiff
path: root/interactor
diff options
context:
space:
mode:
Diffstat (limited to 'interactor')
-rw-r--r--interactor/index.html20
-rwxr-xr-xinteractor/interactor.js25
2 files changed, 40 insertions, 5 deletions
diff --git a/interactor/index.html b/interactor/index.html
index 4593974..46df00f 100644
--- a/interactor/index.html
+++ b/interactor/index.html
@@ -10,7 +10,7 @@ var CVSH=500;
var COLOURS=["#00F","#F00","#0CC"];
-var socket=io();
+var socket=io(location.href);
var CELLSZ=~~(CVSH/(H+1));
var CVSW=CELLSZ*(W+1);
var CELL0X=~~(CVSW/2-W/2*CELLSZ)+.5,CELL0Y=~~(CVSH/2-H/2*CELLSZ)+.5;
@@ -184,6 +184,11 @@ function getusermove(){
setstatustext("<b>Your turn!</b>");
}
+function newgameparameter(){
+ var parameter=document.getElementById("parameter").value;
+ location.href=location.pathname+"?parameter="+encodeURIComponent(parameter);
+}
+
function setstatustext(text){
var elem=document.getElementById("statustext");
elem.innerHTML=text;
@@ -192,6 +197,11 @@ function setstatustext(text){
function assert(cond){if(!cond)throw new Error("Assertion failed");}
+socket.on("ainame",function(name){
+ var span=document.getElementById("ai_name");
+ span.innerHTML="";
+ span.appendChild(document.createTextNode(name));
+});
socket.on("emptyboard",function(){
bd=emptyboard();
});
@@ -223,6 +233,10 @@ socket.on("win",function(player){
<body onload="init()">
<h3>Interactor for Chain Reaction</h3>
<canvas id="cvs"></canvas><br>
-<span id="statustext"></span>
+<span id="statustext"></span><br>
+<br>
+Current AI: <span id="ai_name"></span><br>
+Parameter setting: <input id="parameter" type="text">
+<input type="button" onclick="newgameparameter()" value="New game with this parameter">
</body>
-</html> \ No newline at end of file
+</html>
diff --git a/interactor/interactor.js b/interactor/interactor.js
index 64684ae..ae38b29 100755
--- a/interactor/interactor.js
+++ b/interactor/interactor.js
@@ -69,7 +69,26 @@ function stopproc(proc){
mkdirp("logs");
+function validparameterchar(c){
+ return 48<=c.charCodeAt(0)&&c.charCodeAt(0)<58;
+}
+
io.on("connection",function(conn){
+ // TODO: make nicer
+ var unsafe_parameter=conn.request._query.parameter;
+ var aicmd_here=aicmd;
+ if(unsafe_parameter!=null){
+ var parameter="";
+ for(var i=0;i<unsafe_parameter.length;i++){
+ if(validparameterchar(unsafe_parameter[i])){
+ parameter+=unsafe_parameter[i];
+ }
+ }
+ if(parameter!=""){
+ aicmd_here+=" "+parameter;
+ }
+ }
+
var id=uniqid();
console.log("New IO connection id "+id);
var pingtimeout=null;
@@ -91,6 +110,8 @@ io.on("connection",function(conn){
fs.writeSync(logfile,s+"\n");
};
+ conn.emit("ainame",aicmd_here);
+
conn.on("disconnect",function(){
console.log("Disconnect id "+id);
log("Ending log on disconnect (id "+id+")");
@@ -133,11 +154,11 @@ io.on("connection",function(conn){
});
- var proc=spawn("sh",["-c",aicmd],{
+ var proc=spawn("sh",["-c",aicmd_here],{
stdio:["pipe","pipe",process.stderr]
});
childpids.push(proc.pid);
- log("Process '"+aicmd+"' started with pid "+proc.pid+", initiated by ip "+conn.request.connection.remoteAddress);
+ log("Process '"+aicmd_here+"' started with pid "+proc.pid+", initiated by ip "+conn.request.connection.remoteAddress);
var buffer="";
proc.stdout.on("data",function(data){
var idx,line,mv,win;