Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Board.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. *
  3. * @constructor
  4. */
  5. var root = document.documentElement;
  6. var ball = document.getElementById("ball");
  7. var Board = function(scoreBoard) {
  8. this.cups = [];
  9. this.dom = document.getElementById('cont');
  10. this.dom.addEventListener('keypress', this.mark.bind(this));
  11. this.players = [];
  12. this.currentTurn = 0;
  13. this.ready = false;
  14. this.scoreBoard = scoreBoard;
  15. this.init();
  16. };
  17. Board.prototype.onMark = function(cupId){};
  18. /**
  19. *
  20. */
  21. Board.prototype.init = function() {
  22. for (var i = 1; i <= 10; i++) {
  23. var cupname = "cup" + i;
  24. var cup = document.getElementById(cupname);
  25. cup.setAttribute('marked', 'false');
  26. cup.setAttribute('data-intent', 'gamecup');
  27. this.cups.push(cup);
  28. }
  29. };
  30. /**
  31. *
  32. * @param container
  33. */
  34. Board.prototype.disableAll = function() {
  35. this.cups.forEach(function(cup) {
  36. //cup.classList.add('notActive');
  37. });
  38. };
  39. Board.prototype.enableAll = function() {
  40. alert('enableAll');
  41. this.cups.forEach(function(cup) {
  42. //cup.classList.remove('notActive');
  43. cup.setAttribute('marked', 'false');
  44. });
  45. };
  46. Board.prototype.enableTurn = function() {
  47. this.cups.forEach(function(cup) {
  48. if (cup.getAttribute('marked') === 'false') {
  49. //cup.classList.remove('notActive');
  50. cup.setAttribute('active', 'true');
  51. }
  52. });
  53. };
  54. Board.prototype.highlightcups = function() {
  55. var cups = this.cups;
  56. alert('highlightcups');
  57. cups.forEach(function(cup) {
  58. cup.setAttribute('marked', 'true');
  59. });
  60. };
  61. Board.prototype.lowlightcups = function() {
  62. alert('lowlightcups');
  63. var cups = this.cups;
  64. cups.forEach(function(cup) {
  65. //cup.classList.add('colorWhite');
  66. });
  67. };
  68. Board.prototype.getCup = function(code) {
  69. ball.classList.add("Shoot");
  70. switch (code)
  71. {
  72. case 'q':
  73. case 'Q':
  74. root.style.setProperty('--top325', "-515"+ "px");
  75. ball.style.top = "-515px";
  76. ball.style.left = "-155px";
  77. return "cup1";
  78. break;
  79. case 'w':
  80. case 'W':
  81. root.style.setProperty('--top325', "-515"+ "px");
  82. ball.style.top = "-515px";
  83. ball.style.left = "-50px";
  84. return "cup2";
  85. //removeCup("2");j
  86. break;
  87. case 'e':
  88. case 'E':
  89. root.style.setProperty('--top325', "-515"+ "px");
  90. ball.style.top = "-515px";
  91. ball.style.left = "50px";
  92. return "cup3";
  93. //removeCup("3");
  94. break;
  95. case 'r':
  96. case 'R':
  97. root.style.setProperty('--top325', "-515"+ "px");
  98. ball.style.top = "-515px";
  99. ball.style.left = "150px";
  100. return "cup4";
  101. //removeCup("4");
  102. break;
  103. case 'a':
  104. case 'A':
  105. root.style.setProperty('--top325', "-430"+ "px");
  106. ball.style.top = "-430px";
  107. ball.style.left = "-100px";
  108. return "cup5";
  109. //removeCup("5");
  110. break;
  111. case 's':
  112. case 'S':
  113. root.style.setProperty('--top325', "-430"+ "px");
  114. ball.style.top = "-430px";
  115. ball.style.left = "0px";
  116. return "cup6";
  117. //removeCup("6");
  118. break;
  119. case 'd':
  120. case 'D':
  121. root.style.setProperty('--top325', "-430"+ "px");
  122. ball.style.top = "-430px";
  123. ball.style.left = "100px";
  124. //removeCup("7");
  125. return "cup7";
  126. break;
  127. case 'y':
  128. case 'Y':
  129. root.style.setProperty('--top325', "-340"+ "px");
  130. ball.style.top = "-340px"
  131. ball.style.left = "-50px";
  132. //removeCup("8");
  133. return "cup8";
  134. break;
  135. case 'x':
  136. case 'X':
  137. root.style.setProperty('--top325', "-340"+ "px");
  138. ball.style.top = "-340px"
  139. ball.style.left = "50px";
  140. //removeCup("9");
  141. return "cup9";
  142. break;
  143. case 'c':
  144. case 'C':
  145. root.style.setProperty('--top325', "-250"+ "px");
  146. ball.style.top = "-250px"
  147. ball.style.left = "0px";
  148. //removeCup("10");
  149. return "cup10";
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. /**
  156. *
  157. * @param event
  158. */
  159. Board.prototype.mark = function(event) {
  160. if (this.ready) {
  161. var target = document.getElementById(this.getCup(event.key));
  162. if (target.getAttribute('data-intent') === 'gamecup' && target.getAttribute('active') === 'true') {
  163. this.onMark(this.cups.indexOf(target));
  164. this.disableAll();
  165. }
  166. }
  167. };
  168. /**
  169. *
  170. * @param cupId
  171. * @param label
  172. */
  173. Board.prototype.doMark = function(cupId, label) {
  174. var cup = this.cups[cupId];
  175. cup.textContent = label;
  176. //cup.classList.add('notActive');
  177. // removeCup
  178. setTimeout(this.cups[cupId].classList.add("fadeAway") ,1000);
  179. cup.setAttribute('marked', 'true');
  180. setTimeout(function() {
  181. ball.style.top = "0px";
  182. ball.style.left = "0px";
  183. }, 1000);
  184. };
  185. /**
  186. *
  187. * @param pos
  188. */
  189. Board.prototype.doWinner = function() {
  190. this.disableAll();
  191. this.highlightcups();
  192. };
  193. /**
  194. *
  195. */
  196. Board.prototype.highlightScoreboard = function(playerId) {
  197. this.scoreBoard.forEach(function(score) {
  198. score.classList.remove('active');
  199. });
  200. this.scoreBoard.forEach(function(board){
  201. if (board.getAttribute('playerId') == playerId) {
  202. board.classList.add('active');
  203. }
  204. });
  205. };
  206. /**
  207. *
  208. * @returns {{win: boolean, pos: Array}}
  209. */
  210. Board.prototype.checkWinner = function() {
  211. var counter=0;
  212. var win = false;
  213. this.cups.forEach(function(cup) {
  214. if (cup.getAttribute('marked') === 'true') {
  215. counter++;
  216. }
  217. });
  218. if(counter === 10) {
  219. win = true;
  220. }
  221. /*
  222. var winPosition = [
  223. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  224. ];
  225. var player = this.players[this.currentTurn];
  226. var pos = [];
  227. var win = winPosition.some(function(win) {
  228. if (this.cups[win[0]].textContent === player.label) {
  229. var res = this.cups[win[0]].textContent === this.cups[win[1]].textContent && this.cups[win[1]].textContent === this.cups[win[2]].textContent;
  230. if (res) {
  231. pos = win;
  232. return true;
  233. }
  234. }
  235. return false;
  236. }, this);*/
  237. return {
  238. win: win,
  239. pos: pos
  240. };
  241. };
  242. /**
  243. *
  244. * @param player
  245. */
  246. Board.prototype.addPlayer = function(player) {
  247. if (this.players.length < 2) {
  248. var isNew = this.players.filter(function(p) {
  249. return p.id == player.id;
  250. }).length === 0;
  251. if (isNew) {
  252. this.players.push(player);
  253. if (this.players.length === 1) {
  254. this.scoreBoard[this.players.length - 1].textContent = player.label + ' ' + player.name;
  255. } else {
  256. this.scoreBoard[this.players.length - 1].textContent = player.name + ' ' + player.label;
  257. }
  258. this.scoreBoard[this.players.length - 1].setAttribute('playerId', player.id);
  259. }
  260. }
  261. };