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.9KB

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