<commit_before>'use strict';

var db = require('./database');
var uuid = require('uuid');


var Client = function(socket) {
    this.id = uuid.v4();
    this.game = null;
    this.position = null;
    this.socket = socket;
    this.bindEvents();
};

Client.prototype = {
    bindEvents: function() {
        this.socket.on('join', this.onJoin.bind(this));
        this.socket.on('host', this.onDisconnect.bind(this));
        this.socket.on('disconnect', this.onDisconnect.bind(this));
        this.socket.on('gesture', this.onGesture.bind(this));
        this.socket.on('status', this.onStatus.bind(this));
    },
    onHost: function(data) {
        var self;
        this.game = data.game;
        db.getOrCreate(this.game, function() {
            self.socket.join(self.game);
        });
    },
    onJoin: function(data) {
        var self = this;
        self.game = data.game;
        db.getOrCreate(self.game, function(details) {
            if (details.left == null) {
                details.left = self.id;
                self.position = 'left';
                self.socket.join(self.game);
            } else if (details.right == null) {
                details.right = self.id;
                self.position = 'right';
                self.socket.join(self.game);
            } else {
                return self.socket.emit('fail', {
                    'reason': 'Room full'
                });
            }
            db.conn.set(self.game, JSON.stringify(details));
            self.socket.emit('join', {
                id: self.id,
                position: self.position,
                health: 100,
                mana: 100
            });
        });
    },
    onDisconnect: function() {
        var self = this;
        db.getOrCreate(self.game, function(details) {
            if (details.left === self.id) {
                details.left = null;
            } else if (details.right === self.id) {
                details.right = null;
            }<commit_msg>allow client to reuse id<commit_after>'use strict';

var db = require('./database');
var uuid = require('uuid');


var Client = function(socket) {
    this.id = uuid.v4();
    this.game = null;
    this.position = null;
    this.socket = socket;
    this.bindEvents();
};

Client.prototype = {
    bindEvents: function() {
        this.socket.on('join', this.onJoin.bind(this));
        this.socket.on('host', this.onDisconnect.bind(this));
        this.socket.on('disconnect', this.onDisconnect.bind(this));
        this.socket.on('gesture', this.onGesture.bind(this));
        this.socket.on('status', this.onStatus.bind(this));
    },
    onHost: function(data) {
        var self;
        this.game = data.game;
        db.getOrCreate(this.game, function() {
            self.socket.join(self.game);
        });
    },
    onJoin: function(data) {
        var self = this;
        if (data.id) {
            self.id = data.id;
        }
        self.game = data.game;
        db.getOrCreate(self.game, function(details) {
            if (details.left == null || details.left === self.id) {
                details.left = self.id;
                self.position = 'left';
                self.socket.join(self.game);
            } else if (details.right == null || details.right === self.id) {
                details.right = self.id;
                self.position = 'right';
                self.socket.join(self.game);
            } else {
                return self.socket.emit('fail', {
                    'reason': 'Room full'
                });
            }
            db.conn.set(self.game, JSON.stringify(details));
            self.socket.emit('join', {
                id: self.id,
                position: self.position,
                health: 100,
                mana: 100
            });
        });
    },
    onDisconnect: function() {
        var self = this;
        db.getOrCreate(self.game, function(details) {
            if (details.left === self.id) {
                details.left = null;
            } else if (details.right === self.id) {
                details.right = null;
            }
{'fraction_non_alphanumeric': 0.14650145772594753, 'fraction_numerical': 0.003401360544217687, 'mean_word_length': 1.571517801374141, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5860560', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1177, 'n_words': 218}