Advertisement
Guest User

Untitled

a guest
Nov 24th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. function attachEvents() {
  2. const kinveyBaseUrl = "https://baas.kinvey.com/appdata/";
  3. const kinveyAppKey = "kid_rJ0nPMBze";
  4. const kinveyUsername = "guest";
  5. const kinveyPassword = "guest"
  6. const kinveyAppSecret =
  7. "8a0a464e0e0b4e8681c206c6dd375d2c";
  8. const kinveyAppAuthHeaders = {
  9. 'Authorization': "Basic " + btoa(kinveyUsername + ":" + kinveyPassword),
  10. "Content-type": "application/json"
  11. };
  12.  
  13. function loadPlayers() {
  14. $.ajax({
  15. method: "GET",
  16. url: kinveyBaseUrl + kinveyAppKey + "/players",
  17. headers: kinveyAppAuthHeaders
  18. })
  19. .then(displayPlayers)
  20. .catch(displayError);
  21. }
  22. loadPlayers();
  23. function displayPlayers(data) {
  24. $("#players").empty();
  25. for(let player of data){
  26. let div = $(`<div class="player" data-id="${player._id}">
  27. <div class="row">
  28. <label>Name:</label>
  29. <label class="name">${player.name}</label>
  30. </div>
  31. <div class="row">
  32. <label>Money:</label>
  33. <label class="money">${player.money}</label>
  34. </div>
  35. <div class="row">
  36. <label>Bullets:</label>
  37. <label class="bullets">${player.bullets}</label>
  38. </div>
  39. </div>`)
  40. $('<button class="play">Play</button>').on('click', afterPlayClicked).appendTo($(div));
  41. $('<button class="delete">Delete</button>').on('click', deletePlayer).appendTo($(div));
  42.  
  43.  
  44. $(div).appendTo($("#players"))
  45. }
  46. }
  47.  
  48. $("#save").on('click', saveProgress);
  49.  
  50. function stopActions() {
  51. $("#canvas").hide();
  52. $("#save").hide();
  53. $("#reload").hide();
  54. clearInterval(canvas.intervalId);
  55. }
  56.  
  57.  
  58. $("#reload").on('click', reload);
  59.  
  60. function reload() {
  61. let playerInPlay = $('.player[inPlay=true]');
  62. let id = $(playerInPlay).attr('data-id');
  63. let name = $(playerInPlay).children().children()[1].innerText
  64. let money = $(playerInPlay).children().children()[3].innerText
  65. let bullets = $(playerInPlay).children().children()[5].innerText
  66. let obj = {
  67. name:name,
  68. money: Number(money) - 60,
  69. bullets: 6
  70. };
  71. let request = {
  72. method: "PUT",
  73. url: kinveyBaseUrl + kinveyAppKey + "/players/" + id,
  74. headers: kinveyAppAuthHeaders,
  75. data: JSON.stringify(obj)
  76. };
  77. $.ajax(request)
  78. .then(loadPlayers)
  79. .catch(displayError)
  80. }
  81.  
  82. function saveProgress() {
  83.  
  84. let playerInPlay = $('.player[inPlay=true]');
  85. if(playerInPlay.length>0){
  86. let id = $(playerInPlay).attr('data-id');
  87. let name = $(playerInPlay).children().children()[1].innerText
  88. let money = $(playerInPlay).children().children()[3].innerText
  89. let bullets = $(playerInPlay).children().children()[5].innerText
  90. let obj = {
  91. name:name,
  92. money: money,
  93. bullets: bullets
  94. };
  95. let request = {
  96. method: "PUT",
  97. url: kinveyBaseUrl + kinveyAppKey + "/players/" + id,
  98. headers: kinveyAppAuthHeaders,
  99. data: JSON.stringify(obj)
  100. }
  101. $.ajax(request)
  102. .then(stopActions)
  103. .catch(displayError)
  104. }
  105. }
  106.  
  107. $("#addPlayer").on('click', addPlayer);
  108. function addPlayer() {
  109. let name = $("#addName").val();
  110. let obj = {
  111. "name": name,
  112. "bullets": 6,
  113. "money": 500
  114. };
  115. let request = {
  116. method: "POST",
  117. url: kinveyBaseUrl + kinveyAppKey + "/players",
  118. headers: kinveyAppAuthHeaders,
  119. data: JSON.stringify(obj)
  120. };
  121. if(name!=""){
  122. $.ajax(request)
  123. .then(loadPlayers)
  124. .catch(displayError);
  125. }
  126. $("#addName").val('')
  127. }
  128.  
  129. function afterPlayClicked() {
  130. let previousPlayer = $(".player[inPlay=true]");
  131. previousPlayer.attr('inPlay', false);
  132. let playerInPlay = $(this).parent();
  133. let id = $(playerInPlay).attr("data-id");
  134. $(playerInPlay).attr('inPlay', true);
  135. saveProgress();
  136. $("#canvas").css('display', 'inline-block');
  137. saveProgress();
  138. reload();
  139. let name = $(playerInPlay).children().children()[1].innerText
  140. let money = $(playerInPlay).children().children()[3].innerText
  141. let bullets = $(playerInPlay).children().children()[5].innerText
  142. let obj = {
  143. name:name,
  144. money: Number(money),
  145. bullets: Number(bullets)
  146. };
  147. loadCanvas(obj)
  148. }
  149. function deletePlayer() {
  150. let div = $(this).parent();
  151. $(div).remove();
  152. let id = $(div).attr("data-id");
  153. let request = {
  154. method: "DELETE",
  155. url: kinveyBaseUrl + kinveyAppKey + "/players/" + id,
  156. headers: kinveyAppAuthHeaders
  157. };
  158. $.ajax(request)
  159. .then(function () {
  160. console.log('player deleted')
  161. })
  162. .catch(displayError);
  163. }
  164.  
  165. function displayError(err) {
  166. console.log(err.statusText);
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement