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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. ('checkTurn');
  74. return this.players[this.currentTurn].id == playerId;
  75. };
  76. /**
  77. *
  78. * @returns {{win: boolean, pos: Array}}
  79. */
  80. Board.prototype.checkWinner = function() {
  81. var winPosition = [
  82. [0, 1, 2],
  83. [3, 4, 5],
  84. [6, 7, 8],
  85. [0, 3, 6],
  86. [1, 4, 7],
  87. [2, 5, 8],
  88. [0, 4, 8],
  89. [6, 4, 2]
  90. ];
  91. var player = this.players[this.currentTurn];
  92. var pos = [];
  93. var win = winPosition.some(function(win) {
  94. if (this.cups[win[0]].value === player.label) {
  95. var res = this.cups[win[0]].value === this.cups[win[1]].value && this.cups[win[1]].value === this.cups[win[2]].value;
  96. if (res) {
  97. pos = win;
  98. return true;
  99. }
  100. }
  101. return false;
  102. }, this);
  103. return {
  104. win: win,
  105. pos: pos
  106. };
  107. };
  108. /**
  109. *
  110. * @returns {boolean}
  111. */
  112. Board.prototype.checkDraw = function() {
  113. return this.cups.every(function(cup) {
  114. return cup.value === this.players[0].label || cup.value === this.players[1].label;
  115. }, this);
  116. };
  117. /**
  118. *
  119. * @param player
  120. */
  121. Board.prototype.addPlayer = function(player) {
  122. if (this.players.length < 2) {
  123. var isNew = this.players.filter(function(p) {
  124. return p.id == player.id;
  125. }).length === 0;
  126. if (isNew) {
  127. this.players.push(player);
  128. this.ready = this.players.length === 2;
  129. this.emit(Board.events.PLAYER_CONNECTED, player);
  130. if (this.ready) {
  131. this.enableAll();
  132. this.emit(Board.events.GAME_READY, this.players[0]);
  133. }
  134. }
  135. }
  136. };
  137. module.exports = Board;