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.

BoardServer.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. var EventEmitter = require('events').EventEmitter;
  2. var util = require('util');
  3. /**
  4. *
  5. * @constructor
  6. */
  7. var Board = function() {
  8. this.cups = [];
  9. this.players = [];
  10. this.currentTurn = 0;
  11. this.ready = false;
  12. this.init();
  13. };
  14. Board.events = {
  15. PLAYER_CONNECTED: 'playerConnected',
  16. GAME_READY: 'gameReady',
  17. CUP_MARKED: 'cupMarked',
  18. CHANGE_TURN: 'changeTurn',
  19. WINNER: 'winner',
  20. DRAW: 'draw'
  21. };
  22. util.inherits(Board, EventEmitter);
  23. /**
  24. *
  25. */
  26. Board.prototype.init = function() {
  27. for (var i = 0; i < 9; i++) {
  28. this.cups.push({});
  29. }
  30. };
  31. Board.prototype.disableAll = function() {
  32. this.cups.forEach(function(cup) {
  33. cup.active = false;
  34. });
  35. };
  36. Board.prototype.enableAll = function() {
  37. this.cups.forEach(function(cup) {
  38. cup.active = true;
  39. });
  40. };
  41. /**
  42. *
  43. * @param cupId
  44. */
  45. Board.prototype.mark = function(cupId) {
  46. var cup = this.cups[cupId];
  47. if (!cup) {
  48. return false;
  49. }
  50. if (this.ready && cup.active) {
  51. var player = this.players[this.currentTurn];
  52. cup.value = player.label;
  53. cup.active = false;
  54. this.emit(Board.events.CUP_MARKED, {cupId: cupId, player: player});
  55. var res = this.checkWinner();
  56. if (res.win) {
  57. this.disableAll();
  58. this.emit(Board.events.WINNER, {player: this.players[this.currentTurn], pos: res.pos});
  59. } else if (this.checkDraw()) {
  60. this.emit(Board.events.DRAW, {});
  61. } else {
  62. this.currentTurn = (this.currentTurn + 1) % 2;
  63. this.emit(Board.events.CHANGE_TURN, this.players[this.currentTurn]);
  64. }
  65. }
  66. };
  67. /**
  68. *
  69. * @param playerId
  70. * @returns {boolean}
  71. */
  72. Board.prototype.checkTurn = function(playerId) {
  73. return this.players[this.currentTurn].id == playerId;
  74. };
  75. /**
  76. *
  77. * @returns {{win: boolean, pos: Array}}
  78. */
  79. Board.prototype.checkWinner = function() {
  80. var winPosition = [
  81. [0, 1, 2],
  82. [3, 4, 5],
  83. [6, 7, 8],
  84. [0, 3, 6],
  85. [1, 4, 7],
  86. [2, 5, 8],
  87. [0, 4, 8],
  88. [6, 4, 2]
  89. ];
  90. var player = this.players[this.currentTurn];
  91. var pos = [];
  92. var win = winPosition.some(function(win) {
  93. if (this.cups[win[0]].value === player.label) {
  94. var res = this.cups[win[0]].value === this.cups[win[1]].value && this.cups[win[1]].value === this.cups[win[2]].value;
  95. if (res) {
  96. pos = win;
  97. return true;
  98. }
  99. }
  100. return false;
  101. }, this);
  102. return {
  103. win: win,
  104. pos: pos
  105. };
  106. };
  107. /**
  108. *
  109. * @returns {boolean}
  110. */
  111. Board.prototype.checkDraw = function() {
  112. return this.cups.every(function(cup) {
  113. return cup.value === this.players[0].label || cup.value === this.players[1].label;
  114. }, this);
  115. };
  116. /**
  117. *
  118. * @param player
  119. */
  120. Board.prototype.addPlayer = function(player) {
  121. if (this.players.length < 2) {
  122. var isNew = this.players.filter(function(p) {
  123. return p.id == player.id;
  124. }).length === 0;
  125. if (isNew) {
  126. this.players.push(player);
  127. this.ready = this.players.length === 2;
  128. this.emit(Board.events.PLAYER_CONNECTED, player);
  129. if (this.ready) {
  130. this.enableAll();
  131. this.emit(Board.events.GAME_READY, this.players[0]);
  132. }
  133. }
  134. }
  135. };
  136. module.exports = Board;