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

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