Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachJQuery() {
- var script = document.createElement('script');
- script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js';
- script.type = 'text/javascript';
- document.getElementsByTagName('head')[0].appendChild(script);
- script.src = "http://code.jquery.com/ui/1.9.2/jquery-ui.js";
- document.getElementsByTagName('head')[0].appendChild(script);
- }
- function doPollIsGameStarted() {
- $.get(baseREST + "isgamestarted/" + getCookie("user"), function (data) {
- setTimeout(doPollIsGameStarted, 1000);
- if (data == 'true')
- window.location.replace("game.html");//break out
- });
- }
- function doPollGetPlayers() {
- //alert("!!"+baseREST+res);
- $.get(baseREST + "getplayers/" + getCookie("gameid"), function (data) {
- var h = document.getElementById("players");
- h.innerHTML = "<h3>Players: " + data + "</h3>";//h.insertAdjacentHTML("beforeBegin","Players: " + data);
- setTimeout(doPollGetPlayers, 1000);//setInterval(function(){doPollGetPlayers()}, 1000);<-interval buggy
- });
- }
- //REST CALLS
- //var baseREST = "http://localhost:8080/LolRestService/webresources/lolsoapaccess/"//LOCAL TEST ENV
- var baseREST = "http://ec2-35-165-42-120.us-west-2.compute.amazonaws.com:8080/LolRest/webresources/lolsoapaccess/";
- var user = getCookie("user");//lazy could refactor and remove
- //var gameID = getCookie("gameid");
- function sendReq(httpmethod, resource, token) {//get me that token already.... (token == username...)
- var xhttp = new XMLHttpRequest();
- //alert("REQ!"+baseREST+resource+token+"STOP!");
- try {
- xhttp.open(httpmethod, baseREST + resource + "/" + token + "?timestamp=" + new Date().getTime(), false);
- xhttp.setRequestHeader("Content-type", "text/plain");
- xhttp.send();
- return xhttp.responseText;
- } catch (e) {
- alert("error: " + e.message);
- }
- }
- function getChampionImg() {
- var xhttp = new XMLHttpRequest();
- xhttp.open("GET", baseREST + "getchamppic" + "/" + user, false);
- xhttp.setRequestHeader("Content-type", "text/plain");
- xhttp.send();
- var res = xhttp.responseText;
- // res = res.replace(/ /g, "");fixed backend, ty
- // res = res.replace(/'/g, "");
- return res;
- }
- function guessChamp() {
- var text = document.getElementById("answer").value;
- if (text == '')
- alert("Enter your guess in the textfield!");
- else {
- var res = sendReq("GET", "guesschamp/" + text, user);
- if (res == 'true') {
- //$("champimg").attr("src",getChampionImg()+"?timestamp=" + new Date().getTime());
- //$("champimg").attr("srcset",getChampionImg()+"?timestamp=" + new Date().getTime());
- //document.getElementById("test").innerHTML = "<img id ='champimg' src='"+getChampionImg()+"'></img>"
- document.getElementById("champimg").setAttribute("src", getChampionImg() + "?timestamp=" + new Date().getTime());
- }
- alert(res == 'true' ? "Success" : "Failure nab");
- }
- window.location.reload(true);
- }
- function skipQuestion() {
- sendReq("GET", "skip", user);
- document.getElementById("champimg").setAttribute("src", getChampionImg() + "?timestamp=" + new Date().getTime());
- if (isDoneGuessing() == 'true'){
- window.location.replace("gamedone.html");
- //alert("doneGuessin");
- }else{
- window.location.reload(true);//force refresh..
- }
- }
- function isDoneGuessing() {
- var res = sendReq("GET", "playerdoneguessing/" + getCookie("gameid"), user);
- return res;
- }
- function isGameStarted() {
- var res = sendReq("GET", "isgamestarted", user);
- return res;
- }
- function createGame() {
- var res = sendReq("GET", "creategame", user);
- if (res != "") {
- setCookie("gameid", res, 24);
- //joinGame(res);
- //startGame();
- window.location.replace("creategame.html");
- }
- }
- function joinGame() {
- var sel = document.getElementById("gameslist");
- var id = sel.options[sel.selectedIndex].value;
- var res;
- if (id != "")
- res = sendReq("GET", "joingame/" + id, user);//joingame doesnt return a bool yet.... pls fix
- else
- alert("Select a game from the list or refresh");
- setCookie("gameid", id, 24);
- //IF JOIN SUCCESS (fix backend, currently void response..)
- window.location.replace("lobby.html");
- }
- function startGame() {
- res = sendReq("GET", "startgame", user);
- //alert("Started game:" +res);
- window.location.replace("game.html");
- }
- function findGames() {
- var res = JSON.parse(sendReq("GET", "findgames", ""));
- //alert(res.length);
- var sel = document.getElementById("gameslist");
- sel.options.length = 0;
- for (var i = 0; i < res.length; i++) {
- var opt = document.createElement("option");
- opt.innerHTML = res[i];
- opt.value = res[i];
- sel.appendChild(opt);
- }
- }
- function getPlayers() {
- return sendReq("GET", "getplayers/" + getCookie("gameid"), "");
- //alert(res.length+res);
- }
- function getGameTime() {
- return sendReq("GET", "gettimetaken/" + getCookie("gameid"), user);
- }
- function getWinner() {
- return sendReq("GET", "getwinner/" + getCookie("gameid"), "");
- }
- function getAmountCorrectGuesses() {
- return sendReq("GET", "getscore/" + getCookie("gameid"), user);
- }
- function login() {
- var xhttp = new XMLHttpRequest();
- xhttp.onreadystatechange = function () {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
- // testing
- }
- };
- var user = document.getElementById("username").value;
- var password = document.getElementById("password").value;
- var res = sendReq("GET", "hentbruger/" + password, user);
- if (res == "true") {
- alert("Login succesfull");
- setCookie("user", user, 24);
- document.getElementById("quickfix").click();
- //window.location.href = "manage.html";// = "C:\Users\MasterMind\workspace\LoLTest\manage.html";//.replace("manage.html");
- } else
- alert("NO ACCESS FOR PLEBS");
- }
- //COOKIES
- function getCookie(cname) {
- var name = cname + "=";
- var decodedCookie = decodeURIComponent(document.cookie);
- var ca = decodedCookie.split(';');
- for (var i = 0; i < ca.length; i++) {
- var c = ca[i];
- while (c.charAt(0) == ' ') {
- c = c.substring(1);
- }
- if (c.indexOf(name) == 0) {
- return c.substring(name.length, c.length);
- }
- }
- return "";
- }
- function setCookie(cname, cval, hours) {
- var date = new Date();
- date.setTime(date.getTime() + (hours*60*60*1000));
- var exp = "expires=" + date.toUTCString();
- document.cookie = cname + "=" + cval + ";" + exp + ";path=/";
- }
- //enter = answer NOT WORKING CAN WE DO THIS
- function addSpacebarListener(ele) {
- //var skipBtn = document.getElementById("skip_button");
- //var answerTextField = document.getElementById("answer");
- ele.addEventListener("keypress", function (event) {
- event.preventDefault();
- alert("deteceted press");
- if (event.keyCode == 13)
- alert("deteceted press 13");//skipBtn.click();
- });
- }
- //alert("You answered: "+ retrieveNameText());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement