<commit_before>const app = require('express')();
const server = require('http').Server(app); // eslint-disable-line
const io = require('socket.io')(server);
const uuid = require('uuid');

const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config.js');

const port = 3000;

app.post('/api/game/new', (req, res) => {
  console.log(req.body);

  setTimeout(() => {
    res.json({
      gameId: uuid.v4(),
    });
  }, 1000);
});

const compiler = webpack(config);
app.use(webpackMiddleware(compiler, {
  hot: true,
  filename: config.output.filename,
  publicPath: config.output.publicPath,
  stats: {
    colors: true,
  },
}));
app.use(webpackHotMiddleware(compiler));

server.listen(port, 'localhost', (error) => {
  io.on('connection', (socket) => {
    socket.on('action', (payload) => {
      let newAction = payload.action;
      console.log('********');
      console.log('Payload BEFORE', newAction);

      if (payload.action.target || payload.action.source) {
        newAction = Object.assign({}, newAction, {
          source: payload.action.source === 'PLAYER' ? 'OPPONENT' : 'PLAYER',
          target: payload.action.target === 'PLAYER' ? 'OPPONENT' : 'PLAYER',
        });
      }

      if (payload.action.card) {
        console.log('--------------');
        console.log('typeof card', typeof payload.action.card);
        console.log('--------------');
      }

      console.log('Payload AFTER', newAction);
      console.log('********');<commit_msg>Add newGame emitting from server<commit_after>const app = require('express')();
const server = require('http').Server(app); // eslint-disable-line
const io = require('socket.io')(server);
const uuid = require('uuid');

const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config.js');

function newGame(opponentName, isStarting) {
  return {
    opponentName,
    isStarting,
  };
}

const port = 3000;

app.post('/api/game/new', (req, res) => {
  console.log(req.body);

  setTimeout(() => {
    res.json({
      gameId: uuid.v4(),
    });
  }, 1000);
});

const compiler = webpack(config);
app.use(webpackMiddleware(compiler, {
  hot: true,
  filename: config.output.filename,
  publicPath: config.output.publicPath,
  stats: {
    colors: true,
  },
}));
app.use(webpackHotMiddleware(compiler));

const clients = [];

server.listen(port, 'localhost', (error) => {
  io.on('connection', (socket) => {
    clients.push(socket.id);

    console.log('Has more than two players connected?', io.engine.clientsCount > 1);
    if (io.engine.clientsCount > 1) {
      const playerOneStarts = Math.random() >= 0.5;
      const [playerOne, playerTwo] = clients;

      io.in(playerOne).emit('newGame', newGame('GuardianBanana', playerOneStarts));
      io.in(playerTwo).emit('newGame', newGame('Boyd', !playerOneStarts));
    }

    socket.on('action', (payload) => {
      let newAction = payload.action;
      console.log('********');
      console.log('Payload BEFORE', newAction);

      if (payload.action.target || payload.action.source) {
        newAction = Object.assign({}, newAction, {
          source: payload.action.source === 'PLAYER' ? 'OPPONENT' : 'PLAYER',
          target: payload.action.target === 'PLAYER' ? 'OPPONENT' : 'PLAYER',
        });
      }

      if (payload.action.card) {
        console.log('--------------');
        console.log('typeof card', typeof payload.action.card);
        console.log('--------------');
      }

      console.log('Payload AFTER', newAction);
      console.log('********');
{'fraction_non_alphanumeric': 0.19751454257006876, 'fraction_numerical': 0.005817028027498678, 'mean_word_length': 3.3086560364464694, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '4847795', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1159, 'n_words': 223}