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

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ws.on('message', function incoming(msg) {
  65. try {
  66. var msg = JSON.parse(msg);
  67. } catch (error) {
  68. ws.send(makeMessage(events.outgoing.ERROR, 'Invalid action'));
  69. return;
  70. }
  71. try {
  72. switch (msg.action) {
  73. case events.incoming.JOIN_GAME:
  74. var player = new Player(board.players.length + 1, board.players.length === 0 ? '1' : '2', msg.data);
  75. board.addPlayer(player);
  76. break;
  77. case events.incoming.MARK:
  78. if (board.checkTurn(msg.data.playerId)) {
  79. var player = board.players.filter(function(player){
  80. return player.id == msg.data.playerId;
  81. }).pop();
  82. board.mark(msg.data.cupId);
  83. }
  84. break;
  85. case events.incoming.QUIT:
  86. board = new Board();
  87. wss.clients.forEach(function(client){
  88. client.send(makeMessage(events.outgoing.QUIT, {}));
  89. });
  90. break;
  91. }
  92. } catch (error) {
  93. ws.send(makeMessage(events.outgoing.ERROR, error.message));
  94. }
  95. });
  96. });
  97. console.log('Listening on port %d', PORT);