363 lines
11 KiB
JavaScript
Raw Normal View History

// Global Variables
var playerCoordinate;
2015-07-06 22:32:53 -04:00
var username;
var playerTeam;
2015-07-06 22:32:53 -04:00
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;
var spectatorChoose = 1;
2015-07-10 02:42:56 -04:00
var spectatedUser;
var numberOfPlayers;
var canMove = false;
// Colors
2016-01-16 21:36:07 -05:00
var playerColors = {
2015-07-07 22:32:51 -04:00
"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 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);
};
2015-08-30 22:41:36 -04:00
2015-07-10 02:42:56 -04:00
username = uuid4();
spectatedUser = username;
getInitial();
2015-08-30 18:54:47 -04:00
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);
// Create scoreboard before table to prevent css glitching
createScoreboard();
createTable();
// Update score before creating player so scoreboard starts at 0
2015-07-06 22:32:53 -04:00
updateScore();
waitForPlayers();
2016-01-16 22:20:59 -05:00
2015-07-06 22:32:53 -04:00
}
2015-08-30 18:54:47 -04:00
function getInitial() {
$.ajax({
url: 'http://127.0.0.1:5000/pregame',
2015-08-30 18:54:47 -04:00
type: 'GET',
2015-08-30 22:36:11 -04:00
async: false,
2015-08-30 18:54:47 -04:00
// data: '',
success: function(data) {
//called when successful
2015-08-30 22:36:11 -04:00
playerCoordinate = data["coordinate"];
playerTeam = data["team"];
2015-08-30 18:54:47 -04:00
},
error: function(e) {
//called when there is an error
2015-08-30 22:41:36 -04:00
//1(e.message);
2015-08-30 18:54:47 -04:00
}
});
}
2015-08-30 22:36:11 -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
};
// Sending Data
2015-07-06 22:32:53 -04:00
$.ajax('http://127.0.0.1:5000/game', {
method: 'POST',
type : "POST",
data: JSON.stringify(move, null, '\t'),
2015-08-30 22:36:11 -04:00
async: false,
2015-07-06 22:32:53 -04:00
dataType: "json",
contentType: 'application/json;charset=UTF-8'
})
2015-07-10 01:09:27 -04:00
// Server Response
2015-07-06 22:32:53 -04:00
.then(
function success(data) {
2015-07-07 22:32:51 -04:00
for (var user in data) {
2015-07-10 02:42:56 -04:00
if (data.hasOwnProperty(user)
&& (user != username)
&& (data[user].length > turn)
&& (data[user][turn][2] != "spectator")
) {
if ((data[user].length > data[spectatedUser].length)
&& data[spectatedUser][turn][2] === "spectator") {
spectatedUser = user;
}
2015-07-07 22:32:51 -04:00
var theMove = data[user][turn];
2015-07-10 02:49:43 -04:00
updateTable(user, theMove[1], theMove[2]);
2015-07-10 02:42:56 -04:00
if (theMove[2] != "spectator") {
var oldMove = data[user][turn - 1];
updateOldTable(oldMove[1], oldMove[2]);
}
2015-07-07 22:32:51 -04:00
}
}
2015-07-06 22:32:53 -04:00
},
function fail(data, status) {
alert('Request failed. Returned status of ' + status);
}
);
}
function waitForPlayers(uuid4) {
timer = setTimeout(function() {
// Sending "I'm here."
2016-01-16 22:20:59 -05:00
$.ajax('http://127.0.0.1:5000/pregame', {
method: 'POST',
type : "POST",
data: JSON.stringify(uuid4, null, '\t'),
async: false,
dataType: "json",
contentType: 'application/json;charset=UTF-8'
success: function(data) {
2016-01-16 22:20:59 -05:00
numberOfPlayers = data;
if (numberOfPlayers == '10') {
countdown();
// try catch delete table for visuals later.
}
},
error: function(e) {
//called when there is an error
//(e.message);
}
})
.then(
2016-01-16 22:20:59 -05:00
// Repeat this function until 10 players are here.
waitForPlayers();
);
}, 3000);
}
function countdown() {
2016-01-16 22:20:59 -05:00
timer = setTimeout(function() {
$.ajax({
url: 'http://127.0.0.1:5000/pregame',
type: 'GET',
async: false,
success: function(data) {
timeLeft = data;
//MAKE VISUAL STUFF HERE
if(timeLeft == 0) {
createPlayer();
autoScroll('start');
document.onkeydown = movePlayer;
}
countdown();
},
error: function(e) {
//called when there is an error
//(e.message);
}
})
}, 750); //Prevent too many requests
}
2016-01-16 22:20:59 -05:00
// CREATION
// Creation of Table
function createTable() {
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 < 20; j++) {
var td = tr.insertCell();
}
}
body.appendChild(tbl);
table = document.getElementsByTagName('table')[0];
}
2015-07-08 03:08:33 -04:00
// Creation of Scoreboard
function createScoreboard() {
2015-07-08 03:08:33 -04:00
var scoreboard = document.getElementsByTagName('body')[0].appendChild(document.createElement("DIV"));
scoreboard.className = 'scoreboard';
var redScore = scoreboard.appendChild(document.createElement("SPAN"));
redScore.id = "redscore";
var redNumber = scoreboard.appendChild(document.createElement("SPAN"));
redNumber.id = "rednumber";
redScore.appendChild(document.createTextNode("___"));
redNumber.appendChild(document.createTextNode(""));
scoreboard.appendChild(document.createElement("BR"));
var blueScore = scoreboard.appendChild(document.createElement("SPAN"));
blueScore.id = "bluescore";
var blueNumber = scoreboard.appendChild(document.createElement("SPAN"));
blueNumber.id = "bluenumber";
blueScore.appendChild(document.createTextNode("___"));
blueNumber.appendChild(document.createTextNode(""));
}
// Creation of Player
2015-07-06 22:32:53 -04:00
function createPlayer() {
startSquare = table.rows[playerCoordinate[0]].cells[playerCoordinate[1]];
startSquare.style.backgroundColor = playerColors[playerTeam];
startSquare.className = "player " + playerTeam;
startSquare.id = username;
2015-07-06 22:32:53 -04:00
}
// UPDATING
2015-07-07 22:32:51 -04:00
2015-07-10 02:49:43 -04:00
function updateTable(username, coordinate, team) {
otherPlayer = table.rows[coordinate[0]].cells[coordinate[1]];
otherPlayer.style.backgroundColor = playerColors[team];
2015-07-10 02:49:43 -04:00
otherPlayer.className = "player " + team;
otherPlayer.id = username;
2015-07-08 00:51:25 -04:00
}
2015-07-07 02:29:19 -04:00
function updateOldTable(coordinate, team) {
otherPlayer = table.rows[coordinate[0]].cells[coordinate[1]];
otherPlayer.style.backgroundColor = claimedColors[team];
otherPlayer.className = otherPlayer.className.replace("player ", "");
2015-07-10 02:49:43 -04:00
otherPlayer.id = ""
}
2015-07-06 22:32:53 -04:00
function updateScore() {
var score = [
document.getElementsByClassName('red').length,
document.getElementsByClassName('blue').length
];
document.getElementById('rednumber').childNodes[0].nodeValue = " : " + score[0];
document.getElementById('bluenumber').childNodes[0].nodeValue = " : " + score[1];
2015-07-06 22:32:53 -04:00
}
// PLAYER HANDLING
2015-07-06 22:32:53 -04:00
function movement(x,y) {
if (playerTeam != "spectator") {
2015-07-10 03:33:44 -04:00
try {
previousSquare = table.rows[playerCoordinate[0]].cells[playerCoordinate[1]];
nextSquare = table.rows[playerCoordinate[0] + y].cells[playerCoordinate[1] + x];
} catch(err) {
//Hitting top/down
killPlayer(playerCoordinate, playerTeam);
}
}
2015-07-06 22:32:53 -04:00
timer =
setTimeout(function() {
try {
if (nextSquare == undefined
|| nextSquare.className.includes('player')
|| playerTeam === "spectator"
|| nextSquare.className.includes(playerTeam)) {
killPlayer(playerCoordinate, playerTeam);
2015-07-06 22:32:53 -04:00
}
else {
// Changing new square
nextSquare.style.backgroundColor = playerColors[playerTeam];
nextSquare.className = "player " + playerTeam;
nextSquare.id = username;
// Resetting old square
previousSquare.style.backgroundColor = claimedColors[playerTeam];
previousSquare.className = previousSquare.className.replace("player ", "");
previousSquare.id = "";
// Recursive actions
playerCoordinate = [playerCoordinate[0] + y, playerCoordinate[1] + x];
2015-07-07 02:29:19 -04:00
updateScore();
serverTransfer(playerCoordinate,playerTeam,turn,username);
2015-07-06 22:32:53 -04:00
}
turn = turn + 1;
autoScroll('spectator');
movement(x,y);
2015-07-06 22:32:53 -04:00
}
catch(err) {
2015-07-10 03:33:44 -04:00
// Hitting left/right
killPlayer(playerCoordinate, playerTeam);
2015-07-06 22:32:53 -04:00
}
}, 100);
}
function movePlayer(e) {
if (canMove) {
e = e || window.event;
2015-07-06 22:32:53 -04:00
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);
}
}
2015-07-06 22:32:53 -04:00
}
function killPlayer(coordinate, team) {
if (playerTeam != "spectator") {
deathSquare = table.rows[coordinate[0]].cells[coordinate[1]];
deathSquare.style.backgroundColor = claimedColors[team];
deathSquare.className = deathSquare.className.replace("player ", "");
deathSquare.id = "";
}
// Spectator mode first to set team to spectator
spectatorMode();
serverTransfer(coordinate,team,turn,username);
}
2015-07-06 22:32:53 -04:00
function spectatorMode() {
playerCoordinate = null;
playerTeam = 'spectator';
document.getElementsByClassName('spectator')[0].style.opacity = 0.7;
spectatorType = 'spectatorFull';
spectatorFull();
document.getElementsByClassName('spectator')[0].onclick = function changeText() {
spectatorChoose = spectatorChoose * (-1);
if(spectatorChoose == -1) {
document.getElementsByClassName('spectator')[0].childNodes[0].childNodes[0].nodeValue = "Following";
spectatorType = 'spectatorFollow';
document.getElementsByTagName('body')[0].style.overflow = "hidden";
} else {
document.getElementsByClassName('spectator')[0].childNodes[0].childNodes[0].nodeValue = "Full View";
spectatorType = 'spectatorFull';
spectatorFull();
}
}
}
function spectatorFull() {
for(var i = 0; i < document.getElementsByTagName('td').length;i++) {
document.getElementsByTagName('body')[0].style.overflow = "auto";
document.getElementsByTagName('td')[i].style.minWidth = "75px";
document.getElementsByTagName('td')[i].style.height = "75px";
}
2015-07-06 22:32:53 -04:00
}
2015-07-10 03:30:06 -04:00
function autoScroll(type) {
center = [
window.innerHeight / -2,
window.innerWidth / -2
2015-07-06 22:32:53 -04:00
];
if(type == 'start') {
2015-07-10 03:30:06 -04:00
$('body').scrollTo(document.getElementById(username), 0, {offset: {top: center[0] , left: center[1]} });
} else if (type == "spectator") {
2015-07-10 02:42:56 -04:00
$('body').scrollTo(document.getElementById(spectatedUser), 100, {offset: {top: center[0] , left: center[1]} });
} else {
alert("Broken?")
// $('body').scrollTo(document.getElementById(username), 100, {offset: {top: center[0] , left: center[1]} });
2015-07-10 02:42:56 -04:00
}
}