<commit_before>/**
 * Interpret, cache and merge player data.
 *
 * @author <NAME> (@bgotink)
 * @license MIT
 */

'use strict';

window.iidentity = window.iidentity || {};

(function (module, $) {
    var exports = module.data = {},

        anomalies = [ '13magnus', 'recursion', 'interitus' ],

    // unexported helper functions and classes

        getExtraDataValueName = function (str) {
            var i = str.indexOf(':');

            if (i === -1) {
                return str.trim();
            } else {
                return str.substr(0, i).trim();
            }
        },
        addToArray = function (src, dst) {
            if (!Array.isArray(src)) {
                src = [ src ];
            }

            var existing = [],
                name;

            dst.forEach(function (elem) {
                existing.push(getExtraDataValueName(elem));
            });

            src.forEach(function (elem) {
                name = getExtraDataValueName(elem);

                if (existing.indexOf(name) === -1) {
                    dst.push(elem);
                    existing.push(name);
                }
            });
        },
        merge_player = function () {
            if (arguments.length === 0) {
                return false;
            } else if (arguments.length == 1) {
                return arguments[0];
            }

            var target = arguments[0],
                src = arguments[1],
                newArguments,
                key,
                extraKey,
                tmp;

            if (typeof target.extra !== 'object') {
                target.extra = {};
            }

            for (key in src) {
                if (key === 'err') {
                    if (Array.isArray(src.err)) {
                        if ('err' in target) {
                            if (Array.isArray(target.err)) {
                                target.err = target.err.join(src.err);
                            } else {
                                target.err = src.err;
                            }
                        } else {
                            target.err = src.err;
                        }
                    }
                } else if (key === 'extra') {
                    for (extraKey in src.extra) {
                        if (extraKey in target.extra) {
                            if (Array.isArray(target.extra[extraKey])) {
                                addToArray(
                                    src.extra[extraKey],
                                    target.extra[extraKey]
                                );
                            } else {
                                tmp = [ target.extra[extraKey] ];
                                addToArray(
                                    src.extra[extraKey],
                                    tmp
                                );

                                if (tmp.length > 1) {
                                    target.extra[extraKey] = tmp;
                                }
                            }
                        } else {
                            target.extra[extraKey] = src.extra[extraKey];
                        }
                    }
                } else {
                    target[key] = src[key];
                }
            }

            newArguments = Array.prototype.slice.call(arguments, 1);
            newArguments[0] = target;
            return merge_player.apply(null, newArguments);
        },

        PlayerSource = Class.extend({
            init: function (key, query, data, players) {
                this.key = key;
                this.query = query;
                this.data = data;
                this.err = [];
                this.timestamp = +new Date();

                if ('extratags' in data) {
                    try {
                        data.extratags = JSON.parse(data.extratags);
                    } catch (e) {
                        this.err.push('Invalid JSON in extratags: ' + e.message);
                        data.extratags = {};
                    }
                } else {
                    data.extratags = {};
                }

                if ('anomaly' in data.extratags) {
                    var anomaly = data.extratags.anomaly;

                    if (anomalies.indexOf(anomaly) === -1) {
                        this.err.push('Invalid anomaly: ' + anomaly);
                        delete data.extratags.anomaly;
                    }
                }

                if ('community' in data.extratags) {
                    var community = data.extratags.community;

                    if (community.indexOf(':') === -1) {
                        this.err.push('Invalid community: "' + community + '"');
                        delete data.extratags.community;
                    }
                }

                this.setPlayers(players);
            },

            setPlayers: function (players) {
                this.players = {};
                var newPlayers = this.players;
                players.forEach(function (player) {
                    newPlayers[player.oid] = player;
                });
            },

            hasPlayer: function (oid) {
                return (oid in this.players);
            },
            getPlayer: function (oid) {
                if (!this.hasPlayer(oid)) {
                    return null;
                }

                return $.extend(
                    true,
                    {
                        faction: this.data.faction,
                        extra: this.data.extratags
                    },
                    this.players[oid]
                );
            },

            getNbPlayers: function () {
                return Object.keys(this.players).length;
            },

            getKey: function () {
                return this.key;
            },
            getTag: function () {
                return this.data.tag;
            },
            getVersion: function () {
                return this.data.lastupdated;
            },
            getFaction: function () {
                return this.data.faction;
            },

            getTimestamp: function () {
                return this.timestamp;
            },
            getUpdateInterval: function () {<commit_msg>[backend] player data is now merged correctly fix #2<commit_after>/**
 * Interpret, cache and merge player data.
 *
 * @author <NAME> (@bgotink)
 * @license MIT
 */

'use strict';

window.iidentity = window.iidentity || {};

(function (module, $) {
    var exports = module.data = {},

        anomalies = [ '13magnus', 'recursion', 'interitus' ],
        mainPlayerData = ['name', 'nickname', 'oid', 'level'],

    // unexported helper functions and classes

        getExtraDataValueName = function (str) {
            var i = str.indexOf(':');

            if (i === -1) {
                return str.trim();
            } else {
                return str.substr(0, i).trim();
            }
        },
        addToArray = function (src, dst) {
            if (!Array.isArray(src)) {
                src = [ src ];
            }

            var existing = [],
                name;

            dst.forEach(function (elem) {
                existing.push(getExtraDataValueName(elem));
            });

            src.forEach(function (elem) {
                name = getExtraDataValueName(elem);

                if (existing.indexOf(name) === -1) {
                    dst.push(elem);
                    existing.push(name);
                }
            });
        },
        merge_player = function () {
            if (arguments.length === 0) {
                return false;
            } else if (arguments.length == 1) {
                return arguments[0];
            }

            var target = arguments[0],
                src = arguments[1],
                newArguments,
                key,
                extraKey,
                tmp;

            if (typeof target.extra !== 'object') {
                target.extra = {};
            }

            for (key in src) {
                if (key === 'err') {
                    if (Array.isArray(src.err)) {
                        if ('err' in target) {
                            if (Array.isArray(target.err)) {
                                target.err = target.err.join(src.err);
                            } else {
                                target.err = src.err;
                            }
                        } else {
                            target.err = src.err;
                        }
                    }
                } else if (key === 'extra') {
                    for (extraKey in src.extra) {
                        if (extraKey in target.extra) {
                            if (Array.isArray(target.extra[extraKey])) {
                                addToArray(
                                    src.extra[extraKey],
                                    target.extra[extraKey]
                                );
                            } else {
                                tmp = [ target.extra[extraKey] ];
                                addToArray(
                                    src.extra[extraKey],
                                    tmp
                                );

                                if (tmp.length > 1) {
                                    target.extra[extraKey] = tmp;
                                }
                            }
                        } else {
                            target.extra[extraKey] = src.extra[extraKey];
                        }
                    }
                } else {
                    target[key] = src[key];
                }
            }

            newArguments = Array.prototype.slice.call(arguments, 1);
            newArguments[0] = target;
            return merge_player.apply(null, newArguments);
        },

        PlayerSource = Class.extend({
            init: function (key, query, data, players) {
                this.key = key;
                this.query = query;
                this.data = data;
                this.err = [];
                this.timestamp = +new Date();

                if ('extratags' in data) {
                    try {
                        data.extratags = JSON.parse(data.extratags);
                    } catch (e) {
                        this.err.push('Invalid JSON in extratags: ' + e.message);
                        data.extratags = {};
                    }
                } else {
                    data.extratags = {};
                }

                if ('anomaly' in data.extratags) {
                    var anomaly = data.extratags.anomaly;

                    if (anomalies.indexOf(anomaly) === -1) {
                        this.err.push('Invalid anomaly: ' + anomaly);
                        delete data.extratags.anomaly;
                    }
                }

                if ('community' in data.extratags) {
                    var community = data.extratags.community;

                    if (community.indexOf(':') === -1) {
                        this.err.push('Invalid community: "' + community + '"');
                        delete data.extratags.community;
                    }
                }

                this.setPlayers(players);
            },

            setPlayers: function (players) {
                this.players = {};
                var newPlayers = this.players;
                players.forEach(function (player) {
                    newPlayers[player.oid] = player;
                });
            },

            hasPlayer: function (oid) {
                return (oid in this.players);
            },
            getPlayer: function (oid) {
                if (!this.hasPlayer(oid)) {
                    return null;
                }

                var rawPlayer = this.players[oid],
                    player = {},
                    key;
                player.extra = {};

                for (key in rawPlayer) {
                    if (mainPlayerData.indexOf(key) !== -1) {
                        player[key] = rawPlayer[key];
                    } else {
                        player.extra[key] = rawPlayer[key];
                    }
                }

                return $.extend(
                    true,
                    {
                        faction: this.data.faction,
                        extra: this.data.extratags
                    },
                    player
                );
            },

            getNbPlayers: function () {
                return Object.keys(this.players).length;
            },

            getKey: function () {
                return this.key;
            },
            getTag: function () {
                return this.data.tag;
            },
            getVersion: function () {
                return this.data.lastupdated;
            },
            getFaction: function () {
                return this.data.faction;
            },

            getTimestamp: function () {
                return this.timestamp;
            },
            getUpdateInterval: function () {
{'fraction_non_alphanumeric': 0.10352905614751001, 'fraction_numerical': 0.0024181969319126426, 'mean_word_length': 0.829163787145819, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1937212', 'n_tokens_mistral': 3271, 'n_tokens_neox': 2960, 'n_words': 601}