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.

app.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var socket = new WebSocket('ws://192.168.188.23:2667');
  2. var events = {
  3. outgoing: {
  4. JOIN_GAME: 'csJoinGame',
  5. MARK: 'csMark',
  6. QUIT: 'csQuit'
  7. },
  8. incoming: {
  9. JOIN_GAME: 'scJoinGame',
  10. MARK: 'scMark',
  11. SET_TURN: 'scSetTurn',
  12. OPPONENT_READY: 'scOpponentReady',
  13. GAME_OVER: 'scGameOver',
  14. ERROR: 'scError',
  15. QUIT: 'scQuit'
  16. }
  17. };
  18. var container = document.querySelector('#gameBoard');
  19. var startBtn = document.querySelector('#startBtn');
  20. var nameInput = document.querySelector('#nickname');
  21. var scoreBoard = [
  22. document.querySelector('#p1Score'),
  23. document.querySelector('#p2Score')
  24. ];
  25. var hero = {};
  26. var board = new Board(scoreBoard);
  27. startBtn.setAttribute('disabled', true);
  28. nameInput.setAttribute('disabled', true);
  29. nameInput.setAttribute('placeholder', 'Loading...');
  30. /**
  31. *
  32. * @param action
  33. * @param data
  34. * @returns {*}
  35. */
  36. function makeMessage(action, data){
  37. var resp = {
  38. action: action,
  39. data: data
  40. };
  41. return JSON.stringify(resp);
  42. }
  43. /**
  44. *
  45. * @param cellId
  46. */
  47. board.onMark = function(cellId){
  48. socket.send(makeMessage(events.outgoing.MARK, {playerId: hero.id, cellId: cellId}));
  49. };
  50. /**
  51. *
  52. */
  53. function start() {
  54. while (container.lastChild) {
  55. container.removeChild(container.lastChild);
  56. }
  57. if (board.players.length === 1) {
  58. scoreBoard[1].textContent = 'waiting...';
  59. }
  60. board.bindTo(container);
  61. }
  62. startBtn.addEventListener('click', function(event) {
  63. var name = nameInput.value.trim();
  64. if (name.length > 0) {
  65. hero.name = name;
  66. socket.send(makeMessage(events.outgoing.JOIN_GAME, name));
  67. }
  68. });
  69. socket.onmessage = function(event){
  70. var msg = JSON.parse(event.data);
  71. switch (msg.action) {
  72. case events.incoming.ERROR:
  73. alert('Error: ' + msg.data);
  74. break;
  75. case events.incoming.JOIN_GAME:
  76. board.addPlayer(msg.data);
  77. if (msg.data.name === hero.name) {
  78. hero = msg.data;
  79. start();
  80. }
  81. break;
  82. case events.incoming.SET_TURN:
  83. board.highlightScoreboard(msg.data.id);
  84. board.ready = true;
  85. if (msg.data.id === hero.id) {
  86. board.enableTurn();
  87. }
  88. break;
  89. case events.incoming.MARK:
  90. board.doMark(msg.data.cellId, msg.data.player.label);
  91. break;
  92. case events.incoming.GAME_OVER:
  93. if (msg.data.player) {
  94. board.doWinner(msg.data.pos);
  95. } else {
  96. board.doDraw();
  97. }
  98. socket.send(makeMessage(events.outgoing.QUIT, hero.id));
  99. break;
  100. case events.incoming.QUIT:
  101. socket.close();
  102. break;
  103. }
  104. };
  105. socket.onopen = function(event) {
  106. startBtn.removeAttribute('disabled');
  107. nameInput.removeAttribute('disabled');
  108. nameInput.removeAttribute('placeholder');
  109. nameInput.focus();
  110. };