SHARE
TWEET

Untitled

a guest Nov 16th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = require('express')();
  2. var http = require('http').createServer(app);
  3. var io = require('socket.io')(http);
  4.  
  5. var maxUsers = 7;
  6. var sockets = [];
  7. var killers = [];
  8. var police = [];
  9. var healer = [];
  10. var civilians = [];
  11. var usersOnline = 0;
  12. // Dictionary username -> socket
  13. var userSockets = {};
  14. // Dictionary username -> number of votes
  15. var userKillVotes = {};
  16. //var chosenToBeKilled = "";
  17. var killersNightKillUsernames = {};
  18. var socketsIdUsernames = {};
  19. //var killersDecision = false;
  20. var mostLikelyKilled = null;
  21. var killedThisRound = false;
  22.  
  23. app.get('/', function(req, res) {
  24.   //res.sendFile(__dirname + '/client.html');
  25. });
  26.  
  27. io.on('connection', function(socket) {
  28.   sockets.push(socket);
  29.   socket.on('disconnect', function(){
  30.     console.log('user disconnected');
  31.   });
  32.  
  33.   socket.on('enter', function(username) {
  34.     enter(socket, username);
  35.   });
  36.  
  37.   socket.on('night_kill_vote', function(username) {
  38.     nightKillVote(sockets, username);
  39.   });
  40.  
  41.   socket.on('heal', function(username) {
  42.     heal(username);
  43.   });
  44.  
  45.   socket.on('police', function(username) {
  46.     police(socket, username);
  47.   });
  48.  
  49.   socket.on('kill', function(username){
  50.     kill(username);
  51.   });
  52.  
  53.   socket.on('day_kill_vote', function(username) {
  54.     ////////////////////////
  55.     console.log('day_kill_vote' + username)
  56.     dayKillVote(socket, username);
  57.     /////////////////////////
  58.   });
  59.  
  60.   socket.on('day_kill_unvote', function(username) {
  61.     /////////////////////
  62.     console.log('day_kill_unvote' + username)
  63.     dayKillUnvote(socket, username);
  64.     ////////////////////
  65.   });
  66. });
  67.  
  68. http.listen(3000, function(){
  69.   console.log('listening on *:3000');
  70. });
  71.  
  72. function enter(socket, username) {
  73.     console.log("User Entered: " + username);
  74.     userSockets[username] = socket;
  75.     socketsIdUsernames[socket.id] = username;
  76.     usersOnline++;
  77.     if(usersOnline == maxUsers)
  78.         startGame();
  79. }
  80.  
  81. function startGame() {
  82.     var role = 0;
  83.     var usernamesToBeChosen = Object.keys(userSockets);
  84.     var rolesChosen = maxUsers;
  85.     while(rolesChosen > 0)
  86.     {
  87.         var index = Math.floor(Math.random() * rolesChosen)
  88.         var currentUsername = usernamesToBeChosen[index];
  89.         if(rolesChosen > 5) {
  90.             killers.push(currentUsername);
  91.             usernamesToBeChosen.splice(index, 1);
  92.         }
  93.         else if(rolesChosen == 5) {
  94.             police.push(currentUsername);
  95.             usernamesToBeChosen.splice(index, 1);  
  96.         }
  97.         else if(rolesChosen == 4) {
  98.             healer.push(currentUsername);
  99.             usernamesToBeChosen.splice(index, 1);
  100.         }
  101.         else {
  102.             civilians.push(currentUsername);
  103.             usernamesToBeChosen.splice(index, 1);
  104.         }
  105.         rolesChosen--;
  106.     }
  107.     var i = 0;
  108.     for(const [key, value] of Object.entries(userSockets)) {
  109.         if(killers.includes(key) == true)
  110.             role = 4;
  111.         else if(police.includes(key) == true)
  112.             role = 3;
  113.         else if(healer.includes(key) == true)
  114.             role = 2;
  115.         else if(civilians.includes(key) == true)
  116.             role = 1;
  117.         value.emit('game_start', {'role':role, 'users':Object.keys(userSockets)});
  118.     }
  119.  
  120.     setTimeout(function() {
  121.         vibrate();
  122.     }, 2000);
  123.  
  124.     if(killers.length > 0)
  125.         setTimeout(function() {
  126.             for (var i = 0; i < killers.length; i++)
  127.             {
  128.                 userSockets[killers[i]].emit('vibrate', 1);
  129.             }
  130.         }, 2000);
  131. }
  132.  
  133. function vibrate() {
  134.     Object.values(userSockets).forEach(function(value) {
  135.         value.emit("vibrate", 1);
  136.     });
  137. }
  138.  
  139. function heal(username) {
  140.     if(mostLikelyKilled == username) {
  141.         mostLikelyKilled = "";
  142.     }
  143.  
  144.     setTimeout(function() {
  145.         for (var i = 0; i < healer.length; i++)
  146.         {
  147.             userSockets[healer[i]].emit('vibrate', 1);
  148.         }
  149.     }, 2000);
  150.  
  151.     if(police.length > 0)
  152.         setTimeout(function() {
  153.             for (var i = 0; i < police.length; i++)
  154.             {
  155.                 userSockets[police[i]].emit('vibrate', 1);
  156.             }
  157.         }, 2000);
  158. }
  159.  
  160. function nightKillVote(socket, username)
  161. {
  162.     killersNightKillUsernames[socket.id] = username;
  163.  
  164.     console.log(killers)
  165.     for (var i = 0; i < killers.length; i++) {
  166.         ///////////////////////
  167.         userSockets[killers[i]].emit('highlight', {
  168.             "username": username,
  169.             "caller": socketsIdUsernames[socket.id]
  170.         });
  171.         ///////////////////////////
  172.     }
  173.  
  174.     if(Object.keys(killersNightKillUsernames).length == killers.length)
  175.     {
  176.         var tempUsername = null;
  177.         var areAllTheSame = true;
  178.         Object.values(killersNightKillUsernames).forEach(function(value)
  179.         {
  180.             if(tempUsername == null)
  181.                 tempUsername = value;
  182.             else
  183.             {
  184.                 if(tempUsername != value){
  185.                     areAllTheSame = false;
  186.                 }
  187.             }
  188.         });
  189.         if(areAllTheSame)
  190.         {
  191.             console.log("killed " + mostLikelyKilled)
  192.             mostLikelyKilled = tempUsername
  193.  
  194.             setTimeout(function() {
  195.                 for (var i = 0; i < killers.length; i++)
  196.                 {
  197.                     userSockets[killers[i]].emit('vibrate', 1);
  198.                 }
  199.             }, 2000);
  200.  
  201.             if(healer.length > 0)
  202.                 setTimeout(function() {
  203.                     for (var i = 0; i < healer.length; i++)
  204.                     {
  205.                         userSockets[healer[i]].emit('vibrate', 1);
  206.                     }
  207.                 }, 2000);
  208.         }
  209.     }
  210. }
  211.  
  212. function police(socket, username) {
  213.     socket.emit('check_result', killers.includes(username));
  214.  
  215.     setTimeout(function() {
  216.         for (var i = 0; i < police.length; i++)
  217.         {
  218.             userSockets[police[i]].emit('vibrate', 1);
  219.         }
  220.  
  221.         endNight();
  222.     }, 2000);
  223. }
  224.  
  225. function endNight() {
  226.     setTimeout(function() {
  227.         vibrate(); 
  228.     }, 2000);
  229.  
  230.     if(mostLikelyKilled == "") {
  231.         Object.values(userSockets).forEach(function(value) {
  232.             value.emit('death_report', "Noone died during the night!");
  233.         });
  234.     }
  235.     else {
  236.         Object.values(userSockets).forEach(function(value) {
  237.             value.emit('death_report', mostLikelyKilled);
  238.         });
  239.  
  240.         delete userSockets[mostLikelyKilled];
  241.         mostLikelyKilled = "";
  242.         usersOnline--;
  243.     }
  244. }
  245. /////////
  246. function dayKillVote(socket, username) {
  247. ////////
  248.     if(userKillVotes[username] == undefined) {
  249.         userKillVotes[username] = 1;
  250.     } else {
  251.         userKillVotes[username]++;
  252.         if(userKillVotes[username] > Math.floor(usersOnline / 2)) {
  253.             Object.values(userSockets).forEach(function(value) {
  254.                 value.emit('start_timer', username + " has 30 seconds to defend him/herself!");
  255.             });
  256.         }
  257.     }
  258.     ///////////////
  259.     Object.values(userSockets).forEach(function(value) {
  260.         value.emit('highlight', {
  261.             "username": username,
  262.             "caller": socketsIdUsernames[socket.id]
  263.         });
  264.     });
  265.     ////////////////
  266. }
  267.  
  268. /////////////////////
  269. function dayKillUnvote(socket, username) {
  270.     if(userKillVotes[username] != undefined) {
  271.         userKillVotes[username]--;
  272.         Object.values(userSockets).forEach(function(value) {
  273.             value.emit('unhighlight', {
  274.                 "username": username,
  275.                 "caller": socketsIdUsernames[socket.id]
  276.             });
  277.         });
  278.         if(userKillVotes[username] + 1 > Math.floor(usersOnline / 2)) {
  279.             Object.values(userSockets).forEach(function(value) {
  280.                 value.emit('stop_timer', username + " has defended him/herself!");
  281.             });
  282.         }
  283.     }
  284. }
  285. ///////////////////
  286.  
  287. function kill(username) {
  288.     if(killedThisRound == false) {
  289.         killedThisRound = true;
  290.         delete userSockets[username];
  291.         usersOnline--;
  292.         if(killers.length >= usersOnline / 2) {
  293.             Object.values(userSockets).forEach(function(value) {
  294.                 value.emit('game_over', "The mafia wins!");
  295.             });
  296.         }
  297.         else
  298.             endDay();
  299.     }
  300. }
  301.  
  302. function endDay() {
  303.     setTimeout(function() {
  304.         vibrate(); 
  305.     }, 2000);
  306.  
  307.     setTimeout(function() {
  308.         if(killers.length >= usersOnline / 2) {
  309.             Object.values(userSockets).forEach(function(value) {
  310.                 value.emit('game_over', "The mafia wins!");
  311.             });
  312.         }
  313.         else if(killers.length > 0) {
  314.             setTimeout(function() {
  315.                 vibrate();
  316.             }, 2000);
  317.         }
  318.         else
  319.         {
  320.             Object.values(userSockets).forEach(function(value) {
  321.                 value.emit('game_over', "The good guys win!");
  322.             });
  323.         }
  324.     }, 2000);
  325. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top