195 lines
5.6 KiB
JavaScript
Raw Normal View History

2015-07-06 22:32:53 -04:00
//Global Variables
var coordinate;
var username;
var team;
var timer;
var type;
2015-07-07 22:32:51 -04:00
var username;
2015-07-06 22:32:53 -04:00
var playerColor;
var claimColor;
2015-07-07 22:32:51 -04:00
var turn = 0;
//Colors
var playerColors = {
"red": "#E62E2E",
"blue": "#4343D8"
};
var claimedColors = {
"red": "#FF9999",
"blue": "#9999FF"
}
2015-07-06 22:32:53 -04:00
document.getElementsByClassName('play')[0].onclick = function startGame() {
2015-07-07 02:29:19 -04:00
2015-07-07 22:32:51 -04:00
uuid4 = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, _uuid4);
};
//// OPTIMIZATION - cache callback
_uuid4 = function(cc) {
var rr = Math.random() * 16 | 0; return (cc === 'x' ? rr : (rr & 0x3 | 0x8)).toString(16);
};
username = uuid4()
2015-07-07 02:29:19 -04:00
//*********************
// TODO Get from server
//*********************
2015-07-06 22:32:53 -04:00
coordinate = [0,0];
2015-07-07 22:32:51 -04:00
team = "blue";
2015-07-07 02:29:19 -04:00
// TODO IP Handling, most likely not necessary
2015-07-06 22:32:53 -04:00
2015-07-07 22:32:51 -04:00
var login = document.getElementById("login");
login.parentNode.removeChild(login);
2015-07-06 22:32:53 -04:00
var scoreboard = document.getElementsByTagName('body')[0].appendChild(document.createElement("DIV"));
scoreboard.className = 'scoreboard';
scoreboard.appendChild(document.createTextNode(""));
scoreboard.appendChild(document.createElement("BR"));
scoreboard.appendChild(document.createTextNode(""));
updateScore();
tableCreate();
createPlayer();
2015-07-07 02:29:19 -04:00
document.onkeydown = movePlayer;
2015-07-06 22:32:53 -04:00
}
2015-07-07 22:32:51 -04:00
function serverTransfer(coordinate,team,turn,username) {
2015-07-06 22:32:53 -04:00
var move = {
coordinate: coordinate,
team: team,
2015-07-07 22:32:51 -04:00
turn: turn,
username: username
2015-07-06 22:32:53 -04:00
};
2015-07-07 02:29:19 -04:00
// For debugging
2015-07-06 22:32:53 -04:00
console.log(move);
$.ajax('http://127.0.0.1:5000/game', {
method: 'POST',
type : "POST",
data: JSON.stringify(move, null, '\t'),
dataType: "json",
contentType: 'application/json;charset=UTF-8'
})
.then(
function success(data) {
2015-07-07 02:29:19 -04:00
//*******************************
// TODO Use moves given by server
//*******************************
2015-07-07 22:32:51 -04:00
2015-07-06 22:32:53 -04:00
console.log(data);
2015-07-07 22:32:51 -04:00
for (var user in data) {
if (data.hasOwnProperty(user)) {
console.log(data[user][turn]);
var theMove = data[user][turn];
tableUpdate(theMove[1], theMove[2]);
}
}
2015-07-06 22:32:53 -04:00
},
function fail(data, status) {
alert('Request failed. Returned status of ' + status);
}
);
}
2015-07-07 02:29:19 -04:00
// Creation of Table
2015-07-06 22:32:53 -04:00
function tableCreate() {
var body = document.body
var tbl = document.createElement('table');
tbl.style.border = "1px solid black";
for(var i = 0; i < 20; i++) {
var tr = tbl.insertRow();
for(var j = 0; j < 30; j++) {
var td = tr.insertCell();
}
}
body.appendChild(tbl);
table = document.getElementsByTagName('table')[0];
}
2015-07-07 22:32:51 -04:00
function tableUpdate (coordinate, team) {
current = table.rows[coordinate[0]].cells[coordinate[1]];
current.className = "player ";
current.className += team;
current.style.backgroundColor = playerColors[team]
}
2015-07-07 02:29:19 -04:00
// Creation of Player
2015-07-06 22:32:53 -04:00
function createPlayer() {
2015-07-08 00:35:31 -04:00
table.rows[coordinate[0]].cells[coordinate[1]].style.backgroundColor = playerColors[team];
2015-07-06 22:32:53 -04:00
table.rows[coordinate[0]].cells[coordinate[1]].className = "player ";
}
function movement(x,y) {
timer =
setTimeout(function() {
try {
if (table.rows[coordinate[0] + y].cells[coordinate[1] + x].className.includes(team)) {
2015-07-08 00:35:31 -04:00
table.rows[coordinate[0]].cells[coordinate[1]].style.backgroundColor = claimedColors[team];
2015-07-06 22:32:53 -04:00
document.getElementsByClassName('player')[0].className = "";
spectatorMode();
}
else {
table.rows[coordinate[0]].cells[coordinate[1]].className = table.rows[coordinate[0]].cells[coordinate[1]].className.replace("player ", "");
table.rows[coordinate[0] + y].cells[coordinate[1] + x].className = "player ";
table.rows[coordinate[0] + y].cells[coordinate[1] + x].className += team;
2015-07-08 00:35:31 -04:00
table.rows[coordinate[0] + y].cells[coordinate[1] + x].style.backgroundColor = playerColors[team];
table.rows[coordinate[0]].cells[coordinate[1]].style.backgroundColor = claimedColors[team];
2015-07-06 22:32:53 -04:00
coordinate = [coordinate[0] + y, coordinate[1] + x];
2015-07-07 02:29:19 -04:00
updateScore();
2015-07-07 22:32:51 -04:00
serverTransfer(coordinate,team,turn,username);
2015-07-06 22:32:53 -04:00
turn = turn + 1;
movement(x,y);
}
}
catch(err) {
}
}, 100);
}
function movePlayer(e) {
e = e || window.event;
if(e.keyCode === 38 && type != "up") {
type = "up";
clearTimeout(timer);
movement(0,-1);
} else if(e.keyCode === 40 && type != "down") {
type = "down";
clearTimeout(timer);
movement(0,1);
}
else if(e.keyCode === 37 && type != "left") {
type = "left"
clearTimeout(timer);
movement(-1,0);
}
else if(e.keyCode === 39 && type != "right") {
type = "right"
clearTimeout(timer);
movement(1,0);
}
}
function spectatorMode() {
2015-07-07 02:29:19 -04:00
//***************************
// TODO Finish Spectator Mode
//***************************
2015-07-06 22:32:53 -04:00
coordinate = null;
}
2015-07-07 02:29:19 -04:00
2015-07-06 22:32:53 -04:00
function updateScore() {
2015-07-07 02:29:19 -04:00
var score = [
document.getElementsByClassName('red').length,
document.getElementsByClassName('blue').length
2015-07-06 22:32:53 -04:00
];
document.getElementsByClassName('scoreboard')[0].childNodes[0].nodeValue = "Red: " + score[0];
document.getElementsByClassName('scoreboard')[0].childNodes[2].nodeValue = "Blue: " + score[1];
}