Créé dans le cadre du projet de fin d'année de la promo 2018 de CIR2 de l'ISEN Brest/Rennes, le Burger Quizz est une adaptation numérique du jeu télévisé éponyme, plus précisément d'une épreuve spécifique de ce jeu : le "Sel ou Poivre".

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var io = require('socket.io'); // Chargement du module pour mettre en place les websockets
  2. var http = require('http');
  3. var fs = require('fs'), cfgFilePath = '';
  4. var httpHost = 'localhost', httpPath = '/burger-quizz/web/api/';
  5. // Lecture du fichier de configuration
  6. if(process.argv.length > 2) {
  7. cfgFilePath = process.argv[2];
  8. } else {
  9. cfgFilePath = '../../params.cfg';
  10. }
  11. var params = fs.readFileSync(cfgFilePath).toString();
  12. var httpHost = params.match(/http_host: (.+)/)[0];
  13. var httpPath = params.match(/http_path: (.+)/)[0];
  14. console.log("Serveur initialisé sur l'URL "+httpHost+httpPath);
  15. var json;
  16. // Variables
  17. var server; // Le socket
  18. var lobby = [];
  19. var games = [];
  20. // Gestion des evenements
  21. // Attend l'évènement "connection"
  22. // Le client génère cet évènement lorsque la connexion est établie avec le serveur (voir l'établissement de connexion côté client)
  23. // En cas de connexion appel à la fonctione onSocketConnection
  24. // Un paramètre est envoyé en paramètre de l'évènement "connection" : ce paramètre représente le client
  25. var setEventHandlers = function() {
  26. server.sockets.on("connection", onSocketConnection);
  27. };
  28. // Fonction prenant en paramètre le client (voir ci-dessus)
  29. // Réception ou envoi d'évènement à partir de cet objet : client
  30. function onSocketConnection(client) {
  31. // Attente de l'évènement "new"
  32. // Dans cet exemple l'évènement "new" est envoyé avec un paramètre "pseudo"
  33. client.on('nouveau', function(pseudo) {
  34. // Log pour debug
  35. console.log('Nouveau joueur : '+ pseudo +' !');
  36. // Envoi d'un message au client
  37. //client.emit('message', 'bien reçu !!');
  38. if(lobby.length > 0) {
  39. games.push({joueur1: lobby[0], joueur2: {login: pseudo, socket: client, over: false, score: 0}, idGame: games.length, json: ''});
  40. games[games.length-1].joueur1.socket.emit("game", [games[games.length-1].joueur2.login, games[games.length-1].idGame]);
  41. games[games.length-1].joueur2.socket.emit("game", [games[games.length-1].joueur1.login, games[games.length-1].idGame]);
  42. lobby = [];
  43. } else {
  44. lobby.push({login: pseudo, socket: client, over: false, score: 0});
  45. }
  46. // Envoi d'un message aux autres clients connectés
  47. //client.broadcast.emit('autres', pseudo);
  48. });
  49. client.on('error', function(err) {
  50. console.log(err);
  51. });
  52. client.on('start', function(gameID) {
  53. http.get("http://"+httpHost+httpPath"/api/", function(res) {
  54. var data = "";
  55. res.on("data", function(returned) {
  56. data += returned;
  57. })
  58. res.on("error", function(err) {
  59. console.log(err);
  60. });
  61. res.on("end", function() {
  62. if(!games[gameID].json) {
  63. games[gameID].json = JSON.parse(data.toString());
  64. }
  65. client.emit('questions', games[gameID].json);
  66. })
  67. });
  68. });
  69. client.on('findugame', function(options) {
  70. if(games[options[0]].joueur1.socket.id === client.id) {
  71. games[options[0]].joueur1.over = true;
  72. games[options[0]].joueur1.score = options[1];
  73. console.log("Joueur 1 ("+games[options[0]].joueur1.login+") a fini.");
  74. } else if(games[options[0]].joueur2.socket.id === client.id) {
  75. games[options[0]].joueur2.over = true;
  76. games[options[0]].joueur2.score = options[1];
  77. console.log("Joueur 2 ("+games[options[0]].joueur2.login+") a fini.");
  78. }
  79. if(games[options[0]].joueur1.over && games[options[0]].joueur2.over) {
  80. console.log("Partie terminée.");
  81. games[options[0]].joueur1.socket.emit('end', games[options[0]].joueur2.score);
  82. games[options[0]].joueur2.socket.emit('end', games[options[0]].joueur1.score);
  83. games.splice(options[0], 1);
  84. console.log(games);
  85. }
  86. });
  87. client.on('disconnect', function() {
  88. games.forEach(function(row) {
  89. if(row.joueur1.socket.id === client.id) {
  90. console.log("Joueur déconnecté ("+row.joueur1.login+") ; socket id : "+client.id);
  91. row.joueur2.socket.emit('lolheded');
  92. } else if(row.joueur2.socket.id === client.id) {
  93. console.log("Joueur déconnecté ("+row.joueur2.login+") ; socket id : "+client.id);
  94. row.joueur1.socket.emit('lolheded');
  95. }
  96. });
  97. });
  98. client.on('nextQuestion', function(isRight) {
  99. games.forEach(function(row) {
  100. if(row.joueur1.socket.id === client.id) {
  101. row.joueur2.socket.emit('qpass', !isRight);
  102. } else if(row.joueur2.socket.id === client.id) {
  103. row.joueur1.socket.emit('qpass', !isRight);
  104. }
  105. });
  106. })
  107. };
  108. // Initialisation
  109. function init() {
  110. // Le server temps réel écoute sur le port 8000
  111. server = io.listen(8000);
  112. // Gestion des évènements
  113. setEventHandlers();
  114. };
  115. // Lance l'initialisation
  116. init();