Advertisement
Guest User

Untitled

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