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.

server.js 3.3KB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var WebSocketServer = require('ws').Server;
  2. var Board = require('./BoardServer');
  3. var Player = require('./Player');
  4. var PORT = 2667;
  5. var wss = new WebSocketServer({port: PORT});
  6. var board = new Board();
  7. var events = {
  8. incoming: {
  9. JOIN_GAME: 'csJoinGame',
  10. MARK: 'csMark',
  11. QUIT: 'csQuit'
  12. },
  13. outgoing: {
  14. JOIN_GAME: 'scJoinGame',
  15. MARK: 'scMark',
  16. SET_TURN: 'scSetTurn',
  17. OPPONENT_READY: 'scOpponentReady',
  18. GAME_OVER: 'scGameOver',
  19. ERROR: 'scError',
  20. QUIT: 'scQuit'
  21. }
  22. };
  23. /**
  24. *
  25. * @param action
  26. * @param data
  27. * @returns {*}
  28. */
  29. function makeMessage(action, data) {
  30. var resp = {
  31. action: action,
  32. data: data
  33. };
  34. return JSON.stringify(resp);
  35. }
  36. wss.on('connection', function connection(ws) {
  37. board.on(Board.events.PLAYER_CONNECTED, function(player) {
  38. wss.clients.forEach(function(client) {
  39. board.players.forEach(function(player) {
  40. client.send(makeMessage(events.outgoing.JOIN_GAME, player));
  41. });
  42. });
  43. });
  44. board.on(Board.events.GAME_READY, function(player) {
  45. wss.clients.forEach(function(client) {
  46. client.send(makeMessage(events.outgoing.SET_TURN, player));
  47. });
  48. });
  49. board.on(Board.events.CUP_MARKED, function(event) {
  50. wss.clients.forEach(function(client) {
  51. client.send(makeMessage(events.outgoing.MARK, event));
  52. });
  53. });
  54. board.on(Board.events.CHANGE_TURN, function(player) {
  55. wss.clients.forEach(function(client) {
  56. client.send(makeMessage(events.outgoing.SET_TURN, player));
  57. });
  58. });
  59. board.on(Board.events.WINNER, function(event) {
  60. wss.clients.forEach(function(client) {
  61. client.send(makeMessage(events.outgoing.GAME_OVER, event));
  62. });
  63. });
  64. board.on(Board.events.DRAW, function(event) {
  65. wss.clients.forEach(function(client) {
  66. client.send(makeMessage(events.outgoing.GAME_OVER, event));
  67. });
  68. });
  69. ws.on('message', function incoming(msg) {
  70. try {
  71. var msg = JSON.parse(msg);
  72. } catch (error) {
  73. ws.send(makeMessage(events.outgoing.ERROR, 'Invalid action'));
  74. return;
  75. }
  76. try {
  77. switch (msg.action) {
  78. case events.incoming.JOIN_GAME:
  79. var player = new Player(board.players.length + 1, board.players.length === 0 ? '1' : '2', msg.data);
  80. board.addPlayer(player);
  81. break;
  82. case events.incoming.MARK:
  83. if (board.checkTurn(msg.data.playerId)) {
  84. var player = board.players.filter(function(player){
  85. return player.id == msg.data.playerId;
  86. }).pop();
  87. board.mark(msg.data.cupId);
  88. }
  89. break;
  90. case events.incoming.QUIT:
  91. board = new Board();
  92. wss.clients.forEach(function(client){
  93. client.send(makeMessage(events.outgoing.QUIT, {}));
  94. });
  95. break;
  96. }
  97. } catch (error) {
  98. ws.send(makeMessage(events.outgoing.ERROR, error.message));
  99. }
  100. });
  101. });
  102. console.log('Listening on port %d', PORT);