<commit_before>let WSServer = require('ws').Server;
let config = require('../config');
let mongoose = require('mongoose');
let Room = mongoose.model('Room');

let server = new WSServer({port: config.socketPort});

let players = new Map();
let rplayers = new Map();

server.on('connection', (client) => {
  client.on('message', handleMessage.bind(client));
  client.on('close', handleDisconnect);
  client.on('error', handleError);

  client.send('Connection received!');
});

let commands = {
  connectPlayer: function (roomId) {
    if (typeof roomId !== undefined) {
      Room.loadById(roomId, (err, room) => {
        if (!err && room) {
          let playerId = mongoose.Types.ObjectId().toString();
          players.set(playerId, this);
          rplayers.set(this, playerId);
          this.send(JSON.stringify({c: 'connected', d: playerId}));
          room.players.push(playerId);
          room.save();
        } else {
          this.send("Error");
        } 
      });
    } else {
      this.send("No room id provided");
    }
  },
  movePlayer: function(data) {
    let playerId = rplayers.get(this);
    if (data.hasOwnProperty('location') && data.hasOwnProperty('direction')) {
      Room.loadEnemyPlayer(playerId, (err, enemyId) => {
        if (!err && enemyId) {
          let client = players.get(enemyId);
          client.send(JSON.stringify({c: 'move', d: data}));
        } else {
          this.send('Fak');
        }
      });
    } else {
      this.send('No data');
    }
  },
  unrecognized: function() {
    this.send('Unrecognized command');
  }
}

function handleMessage (msg) {
  let m;
  try {
    m = JSON.parse(msg);
  } catch (ex) {
    console.error(ex);
    return;
  }
  if (m.hasOwnProperty('c')) {
    switch(m.c) {
      case 'connect':
        commands.connectPlayer.call(this, m.d);
        break;
      case 'move':
        commands.movePlayer.call(this, m.d);
        break;
      default:
        commands.unrecognized.call(this);
        break;
    }
  } else {
    console.error('No command given');
    this.send('no command');
  }
}

function handleDisconnect () {
  console.log('Player disconnected');
}

function handleError () {
  console.log('Error');
}<commit_msg>Added allowAny command<commit_after>let WSServer = require('ws').Server;
let config = require('../config');
let mongoose = require('mongoose');
let Room = mongoose.model('Room');

let server = new WSServer({port: config.socketPort});

let players = new Map();
let rplayers = new Map();

server.on('connection', (client) => {
  client.on('message', handleMessage.bind(client));
  client.on('close', handleDisconnect);
  client.on('error', handleError);

  client.send('Connection received!');
});

let commands = {
  connectPlayer: function (roomId) {
    if (typeof roomId !== undefined) {
      Room.loadById(roomId, (err, room) => {
        if (!err && room) {
          let playerId = mongoose.Types.ObjectId().toString();
          players.set(playerId, this);
          rplayers.set(this, playerId);
          this.send(JSON.stringify({c: 'connected', d: playerId}));
          room.players.push(playerId);
          room.save();
        } else {
          this.send("Error");
        } 
      });
    } else {
      this.send("No room id provided");
    }
  },
  movePlayer: function(data) {
    let playerId = rplayers.get(this);
    if (data.hasOwnProperty('location') && data.hasOwnProperty('direction')) {
      Room.loadEnemyPlayer(playerId, (err, enemyId) => {
        if (!err && enemyId) {
          let client = players.get(enemyId);
          client.send(JSON.stringify({c: 'move', d: data}));
        } else {
          this.send('Fak');
        }
      });
    } else {
      this.send('No data');
    }
  },
  stopPlayer: function(data) {
    let playerId = rplayers.get(this);
    if (data.hasOwnProperty('location')) {
      Room.loadEnemyPlayer(playerId, (err, enemyId) => {
        if (!err && enemyId) {
          let client = players.get(enemyId);
          client.send(JSON.stringify({c: 'stop', d: data}));
        } else {
          this.send(JSON.stringify({c: 'error', d: 'No enemy'}));
        }
      })
    } else {
      this.send(JSON.stringify({c: 'error', d: 'No data'}));
    }
  },
  unrecognized: function() {
    this.send('Unrecognized command');
  },
  allowAny: function(message) {
    let playerId = rplayers.get(this);
    Room.loadEnemyPlayer(playerId, (err, enemyId) => {
      if (!err && enemyId) {
        let client = players.get(enemyId);
        client.send(JSON.stringify(m));
      } else {
        this.send(JSON.stringify({c: 'error', d: 'No enemy'}));
      }
    })
  }
}

function handleMessage (msg) {
  let m;
  try {
    m = JSON.parse(msg);
  } catch (ex) {
    console.error(ex);
    return;
  }
  commands.allowAny.call(this, m);
  // if (m.hasOwnProperty('c')) {
  //   switch(m.c) {
  //     case 'connect':
  //       commands.connectPlayer.call(this, m.d);
  //       break;
  //     case 'move':
  //       commands.movePlayer.call(this, m.d);
  //       break;
  //     case 'stop':
  //       commands.stopPlayer.call(this, m.d);
  //     default:
  //       commands.unrecognized.call(this);
  //       break;
  //   }
  // } else {
  //   console.error('No command given');
  //   this.send('no command');
  // }
}

function handleDisconnect () {
  console.log('Player disconnected');
}

function handleError () {
  console.log('Error');
}
{'fraction_non_alphanumeric': 0.17546441052050762, 'fraction_numerical': 0.0, 'mean_word_length': 2.4814340588988477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3581966', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1767, 'n_words': 343}