{"size": 1451, "ext": "js", "max_stars_repo_path": "game/connection.js", "max_stars_repo_name": "webbestmaster/canned-fish-back", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "game/connection.js", "max_issues_repo_name": "webbestmaster/canned-fish-back", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "game/connection.js", "max_forks_repo_name": "webbestmaster/canned-fish-back", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.6567164179, "max_line_length": 73, "alphanum_fraction": 0.5940730531}
const MainModel = require('main-model');

const attr = {
    connection: 'connection',
    user: 'user',
    unit: 'unit'
};

/**
 *
 * @param {object} data - connection initialized data
 *      @param {object} data.connection - "native" socket.io connection
 */
class ConnectionWrapper extends MainModel {
    constructor(data) {
        super(data);

        console.log('connection wrapper created');

        const connectionWrapper = this;

        connectionWrapper.createUser();
    }

    getConnection() {
        return this.get(attr.connection);
    }

    createUser() {
        const connectionWrapper = this;
        const connection = connectionWrapper.getConnection();

        connectionWrapper.set(attr.user, {
            id: 'user-id_' + Math.random()
        });

        connection.emit('create-user', connectionWrapper.get(attr.user));

        console.log('user created');
    }

    destroy() {
        const connectionWrapper = this;
        const unit = connectionWrapper.get(attr.unit);

        if (unit) {
            connectionWrapper.get(attr.unit).destroy();
        } else {
            console.warn('connection destroyed: unit is not exist');
        }
        super.destroy();

        console.log('connection destroyed');
    }

/*
    onXY(data) {
        const connectionWrapper = this;
        const unit = connectionWrapper.get('unit');

        unit.set(data);
    }
*/
}

module.exports = ConnectionWrapper;
