botsArena/src/arenas/tron/js.js

47 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-06-29 11:35:10 +02:00
function createElem(type,attributes){
var elem=document.createElement(type);
for (var i in attributes)
{elem.setAttribute(i,attributes[i]);}
return elem;
}
2016-06-29 12:26:03 +02:00
function createElemNS(type,attributes){
2016-06-29 13:43:20 +02:00
//same as createElem but with ns for svg file
2016-06-29 12:20:16 +02:00
var elem=document.createElementNS("http://www.w3.org/2000/svg",type);
2016-06-29 12:14:01 +02:00
for (var i in attributes)
2016-06-29 12:26:03 +02:00
{elem.setAttributeNS(null,i,attributes[i]);}
2016-06-29 12:14:01 +02:00
return elem;
}
2016-06-29 09:30:21 +02:00
2016-06-29 13:55:23 +02:00
function applyInitMessage(req){
2016-06-29 16:30:39 +02:00
//callback function when init game request
2016-06-29 13:55:23 +02:00
if(req.readyState == 4){
if(req.status == 200) {
2016-06-29 17:00:23 +02:00
alert (req.responseText);
2016-06-29 09:30:21 +02:00
2016-06-29 13:43:20 +02:00
}else{
alert ('error ' + xhr.status);
document.getElementById('fightButton').disabled=false;
return;
}
}
}
2016-06-29 13:51:45 +02:00
function tron(bot1,bot2,xd_check){
2016-06-29 12:55:45 +02:00
//empty
while (document.getElementById('fightResult').firstChild) {
document.getElementById('fightResult').removeChild(document.getElementById('fightResult').firstChild);
}
// draw border;
2016-06-29 13:04:30 +02:00
var svg = createElemNS('svg',{'id':'map','width':'500','height':'500','viewBox':'0 0 1000 1000'});
2016-06-29 12:55:45 +02:00
var rect=createElemNS('rect',{'x':'0','y':'0','width':'1000','height':'1000','style':'stroke:#000000; fill:none;'});
svg.appendChild(rect);
2016-06-29 12:09:02 +02:00
2016-06-29 12:55:45 +02:00
document.getElementById("fightResult").appendChild(svg);
2016-06-29 13:43:20 +02:00
//ask arena to send bots init messages
2016-06-29 13:55:23 +02:00
var request = new XMLHttpRequest();
2016-06-29 13:56:10 +02:00
request.onreadystatechange = function(){applyInitMessage(request)};
2016-06-29 13:43:20 +02:00
request.open("POST", '/tron', true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send('act=initGame&bot1=' + bot1 + '&bot2=' + bot2 + '&xd_check=' + xd_check + '&fullLogs=' + document.getElementById("fullLogs").checked);
2016-06-29 10:40:08 +02:00
}