Advertisement
Guest User

Untitled

a guest
May 13th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. function attachJQuery() {
  2. var script = document.createElement('script');
  3. script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js';
  4. script.type = 'text/javascript';
  5. document.getElementsByTagName('head')[0].appendChild(script);
  6. script.src = "http://code.jquery.com/ui/1.9.2/jquery-ui.js";
  7. document.getElementsByTagName('head')[0].appendChild(script);
  8. }
  9.  
  10.  
  11.  
  12. function doPollIsGameStarted() {
  13. $.get(baseREST + "isgamestarted/" + getCookie("user"), function (data) {
  14. setTimeout(doPollIsGameStarted, 1000);
  15. if (data == 'true')
  16. window.location.replace("game.html");//break out
  17. });
  18. }
  19.  
  20. function doPollGetPlayers() {
  21. //alert("!!"+baseREST+res);
  22. $.get(baseREST + "getplayers/" + getCookie("gameid"), function (data) {
  23. var h = document.getElementById("players");
  24. h.innerHTML = "<h3>Players: " + data + "</h3>";//h.insertAdjacentHTML("beforeBegin","Players: " + data);
  25. setTimeout(doPollGetPlayers, 1000);//setInterval(function(){doPollGetPlayers()}, 1000);<-interval buggy
  26. });
  27. }
  28.  
  29. //REST CALLS
  30. //var baseREST = "http://localhost:8080/LolRestService/webresources/lolsoapaccess/"//LOCAL TEST ENV
  31. var baseREST = "http://ec2-35-165-42-120.us-west-2.compute.amazonaws.com:8080/LolRest/webresources/lolsoapaccess/";
  32. var user = getCookie("user");//lazy could refactor and remove
  33. //var gameID = getCookie("gameid");
  34.  
  35.  
  36.  
  37. //function sendReqASYNCSHOWOFF(httpmethod, resource, token, block) {
  38. // var xhttp = new XMLHttpRequest();
  39. // try {
  40. // xhttp.open(httpmethod, baseREST + resource + "/" + token + "?timestamp=" + new Date().getTime(), true);
  41. // xhttp.setRequestHeader("Content-type", "text/plain");
  42. // xhttp.send();
  43. //
  44. // xhttp.onreadystatechange = function () {
  45. // if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  46. // block();
  47. // }
  48. // };
  49. //
  50. // return xhttp.responseText;
  51. // } catch (e) {
  52. // alert("error: " + e.message);
  53. // }
  54. //}
  55. //var someFunc = function(){
  56. // //wrap post-req code from functions that invoke sendReq() and pass this code block as param
  57. //}
  58. //
  59. //sendReqASYNCSHOWOFF("GET", "someresource", "token", someFunc);
  60.  
  61. function getChampionImg() {
  62. var xhttp = new XMLHttpRequest();
  63. xhttp.open("GET", baseREST + "getchamppic" + "/" + user, false);
  64. xhttp.setRequestHeader("Content-type", "text/plain");
  65. xhttp.send();
  66.  
  67. var res = xhttp.responseText;
  68.  
  69. // res = res.replace(/ /g, "");fixed backend, ty
  70. // res = res.replace(/'/g, "");
  71. return res;
  72. }
  73.  
  74. function guessChamp() {
  75. var text = document.getElementById("answer").value;
  76. if (text == '')
  77. alert("Enter your guess in the textfield!");
  78. else {
  79. var res = sendReq("GET", "guesschamp/" + text, user);
  80. alert(res == 'true' ? "Success" : "Failure nab");
  81. }
  82.  
  83.  
  84. checkProgress();
  85.  
  86.  
  87. }
  88.  
  89.  
  90. function skipQuestion() {
  91. sendReq("GET", "skip", user);
  92.  
  93. checkProgress();
  94. }
  95.  
  96. function checkProgress() {
  97. document.getElementById("champimg").setAttribute("src", getChampionImg() + "?timestamp=" + new Date().getTime());
  98. if (sendReq("GET", "playerdoneguessing/" + getCookie("gameid"), user) == 'true'){
  99. window.location.replace("gamedone.html");
  100. //alert("doneGuessin");
  101. }else{
  102. document.getElementById("champimg").setAttribute("src", getChampionImg() + "?timestamp=" + new Date().getTime());
  103. }
  104. }
  105.  
  106. function isGameStarted() {
  107. var res = sendReq("GET", "isgamestarted", user);
  108. return res;
  109.  
  110. }
  111.  
  112.  
  113. function createGame() {
  114. var res = sendReq("GET", "creategame", user);
  115. if (res != "") {
  116. setCookie("gameid", res, 24);
  117. //joinGame(res);
  118. //startGame();
  119. window.location.replace("creategame.html");
  120. }
  121.  
  122. }
  123.  
  124. function joinGame() {
  125. var sel = document.getElementById("gameslist");
  126. var id = sel.options[sel.selectedIndex].value;
  127. var res;
  128. if (id != "")
  129. res = sendReq("GET", "joingame/" + id, user);//joingame doesnt return a bool yet.... pls fix
  130. else
  131. alert("Select a game from the list or refresh");
  132.  
  133. setCookie("gameid", id, 24);
  134. //IF JOIN SUCCESS (fix backend, currently void response..)
  135. window.location.replace("lobby.html");
  136. }
  137.  
  138. function startGame() {
  139. res = sendReq("GET", "startgame", user);
  140. //alert("Started game:" +res);
  141. window.location.replace("game.html");
  142. }
  143.  
  144. function findGames() {
  145. var res = JSON.parse(sendReq("GET", "findgames", ""));
  146. //alert(res.length);
  147. var sel = document.getElementById("gameslist");
  148. sel.options.length = 0;
  149. for (var i = 0; i < res.length; i++) {
  150. var opt = document.createElement("option");
  151. opt.innerHTML = res[i];
  152. opt.value = res[i];
  153. sel.appendChild(opt);
  154. }
  155. }
  156.  
  157. function getPlayers() {
  158. return sendReq("GET", "getplayers/" + getCookie("gameid"), "");
  159. //alert(res.length+res);
  160. }
  161.  
  162. function getGameTime() {
  163. return sendReq("GET", "gettimetaken/" + getCookie("gameid"), user);
  164. }
  165.  
  166. function getWinner() {
  167. return sendReq("GET", "getwinner/" + getCookie("gameid"), "");
  168.  
  169. }
  170.  
  171. function getAmountCorrectGuesses() {
  172. return sendReq("GET", "getscore/" + getCookie("gameid"), user);
  173. }
  174.  
  175. function login() {
  176. var user = document.getElementById("username").value;
  177. var password = document.getElementById("password").value;
  178. var res = sendReq("GET", "hentbruger/" + password, user);
  179.  
  180. if (res == "true") {
  181. alert("Login succesfull");
  182. setCookie("user", user, 24);
  183. document.getElementById("quickfix").click();
  184. //window.location.href = "manage.html";// = "C:\Users\MasterMind\workspace\LoLTest\manage.html";//.replace("manage.html");
  185. } else
  186. alert("NO ACCESS FOR PLEBS");
  187.  
  188. }
  189.  
  190.  
  191. //COOKIES
  192. function getCookie(cname) {
  193. var name = cname + "=";
  194. var decodedCookie = decodeURIComponent(document.cookie);
  195. var ca = decodedCookie.split(';');
  196. for (var i = 0; i < ca.length; i++) {
  197. var c = ca[i];
  198. while (c.charAt(0) == ' ') {
  199. c = c.substring(1);
  200. }
  201. if (c.indexOf(name) == 0) {
  202. return c.substring(name.length, c.length);
  203. }
  204. }
  205. return "";
  206. }
  207.  
  208. function setCookie(cname, cval, hours) {
  209. var date = new Date();
  210. date.setTime(date.getTime() + (hours*60*60*1000));
  211. var exp = "expires=" + date.toUTCString();
  212. document.cookie = cname + "=" + cval + ";" + exp + ";path=/";
  213. }
  214.  
  215.  
  216.  
  217. //enter = answer NOT WORKING CAN WE DO THIS
  218. //function addSpacebarListener(ele) {
  219. //var skipBtn = document.getElementById("skip_button");
  220. //var answerTextField = document.getElementById("answer");
  221. //
  222. // ele.addEventListener("keypress", function (event) {
  223. // event.preventDefault();
  224. // alert("deteceted press");
  225. // if (event.keyCode == 13)
  226. // alert("deteceted press 13");//skipBtn.click();
  227. // });
  228. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement