<commit_before>const express = require('express')
const shortid = require('shortid');
const app = express()
const server = require("http").Server(app)
const io = require("socket.io")(server)
const fs = require('fs')

var port = process.env.PORT || 5000

app.use(express.static(__dirname + "/app"))


server.listen(port)

console.log("http server listening on %d", port)

var players = {}
var cards = JSON.parse(fs.readFileSync(__dirname + '/app/json/cards.json', 'utf8'));

app.get("*", function(req, res) {
  res.sendFile(__dirname + "/app/index.html")
})

io.on("connection", function(socket) {
  console.log("Player " + socket.id + " connected");
  
  // create room and join it
  socket.on("create_room", function(data) {
    var roomId = shortid.generate()
    data.roomId = roomId;
    socket.join(roomId)
    io.nsps['/'].adapter.rooms[roomId].gameState=0;
    addPlayer({data, socket}, true)
    console.log("NEW ROOM WITH ID " + roomId)
  })

  socket.on("join_room", function(data) {
    socket.join(data.roomId)
    addPlayer({data, socket}, false)
    console.log("JOINED ROOM WITH ID " + data.roomId)
  })

  socket.on('disconnect', function() {
  	setTimeout(function(){deletePlayer(socket.id)}, 3000);
  	console.log("Player " + socket.id + " disconnected");
  })

  socket.on('delete_player', function() {
  	deletePlayer(socket.id)
  })

  socket.on('fetch_players', function(data){
	  var playersInSameRoom = getPlayersInRoom(data.room)
	  io.to(data.room).emit("display_players", {players: playersInSameRoom})
  })

  socket.on('reconnect_player', function(data){
    socket.join(data.newPlayer.room)
    delete players[data.oldPlayer]
    players[data.newPlayer.id]=data.newPlayer
  })

  socket.on('get_first_load', function(data){
    var playersInSameRoom = getPlayersInRoom(data.room)
    if(playersInSameRoom.length == 0){
      io.nsps['/'].adapter.rooms[data.room].gameState = 0
    }
    socket.emit('first_load',{players: playersInSameRoom, gameState: io.nsps['/'].adapter.rooms[data.room].gameState})
  })

  socket.on('sync_room_gamestate', function(data){
    io.nsps['/'].adapter.rooms[data.room].gameState=data.gameState
    io.to(data.room).emit("sync_gamestate", {gameState: io.nsps['/'].adapter.rooms[data.room].gameState})
  })

  socket.on('give_whitecards', function(data){
    var deck = io.nsps['/'].adapter.rooms[data.room].whiteCards ? io.nsps['/'].adapter.rooms[data.room].whiteCards : getNewDeck("whiteCards",data.room)
    //for(var playerId in getPlayersInRoom(data.room)){  
      var playerCards = new Array()
      for(var i = 0; i < data.amount; i++){<commit_msg>Fixed crash if reloading when alone in lobby<commit_after>const express = require('express')
const shortid = require('shortid');
const app = express()
const server = require("http").Server(app)
const io = require("socket.io")(server)
const fs = require('fs')

var port = process.env.PORT || 5000

app.use(express.static(__dirname + "/app"))


server.listen(port)

console.log("http server listening on %d", port)

var players = {}
var cards = JSON.parse(fs.readFileSync(__dirname + '/app/json/cards.json', 'utf8'));

app.get("*", function(req, res) {
  res.sendFile(__dirname + "/app/index.html")
})

io.on("connection", function(socket) {
  console.log("Player " + socket.id + " connected");
  
  // create room and join it
  socket.on("create_room", function(data) {
    var roomId = shortid.generate()
    data.roomId = roomId;
    socket.join(roomId)
    io.nsps['/'].adapter.rooms[roomId].gameState=0;
    addPlayer({data, socket}, true)
    console.log("NEW ROOM WITH ID " + roomId)
  })

  socket.on("join_room", function(data) {
    socket.join(data.roomId)
    addPlayer({data, socket}, false)
    console.log("JOINED ROOM WITH ID " + data.roomId)
  })

  socket.on('disconnect', function() {
  	setTimeout(function(){deletePlayer(socket.id)}, 3000);
  	console.log("Player " + socket.id + " disconnected");
  })

  socket.on('delete_player', function() {
  	deletePlayer(socket.id)
  })

  socket.on('fetch_players', function(data){
	  var playersInSameRoom = getPlayersInRoom(data.room)
	  io.to(data.room).emit("display_players", {players: playersInSameRoom})
  })

  socket.on('reconnect_player', function(data){
    socket.join(data.newPlayer.room)
    delete players[data.oldPlayer]
    players[data.newPlayer.id]=data.newPlayer
  })

  socket.on('get_first_load', function(data){
    var playersInSameRoom = getPlayersInRoom(data.room)
    if(!io.nsps['/'].adapter.rooms[data.room]){
      socket.join(data.room)
      io.nsps['/'].adapter.rooms[data.room].gameState = 0
    }
    socket.emit('first_load',{players: playersInSameRoom, gameState: io.nsps['/'].adapter.rooms[data.room].gameState})
  })

  socket.on('sync_room_gamestate', function(data){
    io.nsps['/'].adapter.rooms[data.room].gameState=data.gameState
    io.to(data.room).emit("sync_gamestate", {gameState: io.nsps['/'].adapter.rooms[data.room].gameState})
  })

  socket.on('give_whitecards', function(data){
    var deck = io.nsps['/'].adapter.rooms[data.room].whiteCards ? io.nsps['/'].adapter.rooms[data.room].whiteCards : getNewDeck("whiteCards",data.room)
    //for(var playerId in getPlayersInRoom(data.room)){  
      var playerCards = new Array()
      for(var i = 0; i < data.amount; i++){
{'fraction_non_alphanumeric': 0.1768258161917343, 'fraction_numerical': 0.004717871296471032, 'mean_word_length': 4.380710659898477, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1107314', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1800, 'n_words': 296}