<commit_before>/* global define */ define([
    'react',
    'cartoon-battle/util',
    'views/Kongregate/Authorization'
], function (React, util, Authorization) {

    var e = React.createElement, PropTypes = React.PropTypes;

    var localStorage = window.localStorage || {};

    function get_stored_credentials() {
        if ('user_id' in localStorage) {
            return {
                user_id: localStorage['user_id'],
                password: localStorage['password']
            }
        }

        return null;
    }

    var Island = React.createClass({
        displayName: "Island",

        propTypes: {
            name: PropTypes.string.isRequired,
            users: PropTypes.arrayOf(PropTypes.object).isRequired,
            onUserAdded: PropTypes.func
        },

        getInitialState: function () {
            return {
                users: []
            }
        },

        populateUsers: function (user) {
            this.props.onUserAdded && this.props.onUserAdded(user);

            this.setState(function (prevState) {
                return {
                    users: prevState.users.concat([user])
                }
            });
        },

        componentDidMount: function () {
            var sessionStorage = window.sessionStorage || {}, island = this.props.name;

            var populateUsers = this.populateUsers, addUser = function (user) {
                populateUsers({
                    id: user.user_id,
                    name: user.name,
                    island: island,
                    guild: user.guild.name,
                    level: user.pvp_data.level,
                    legendaries: user.cards_by_rarity[4],
                    win_count:  user.pvp_data.win_count,
                    sfc_points: user.pvp_data.rating
                });
            };

            this.props.users.map(function (user) {
                return user.user_id;
            }).forEach(function (user_id) {
                var key = "user_profile_" + user_id;

                if (sessionStorage[key]) {
                    return addUser(JSON.parse(sessionStorage[key]));
                }

                util.get_user_info(user_id, function (response) {
                    var user = response.user_profile_info;

                    sessionStorage[key] = JSON.stringify(user);

                    return addUser(user);
                }/* credentials are implied */)
            });
        },

        renderUsersProgress: function () {
            var loaded = this.state.users.length;
            if (loaded === this.props.users.length) {
                return loaded;
            }

            return loaded + " (of " + this.props.users.length + ")";
        },

        countAverageStat: function (key, prec) {
            prec = Math.pow(10, prec || 0);

            var map = function (user) {
                return user[key];
            };

            var reduce = function (sum, value) {
                return parseInt(sum) + parseInt(value);
            };

            return this.state.users.length
                ? Math.round(this.state.users.map(map).reduce(reduce) / this.state.users.length * prec) / prec
                : 'N/A';
        },

        render: function () {
            return e('tr', {},
                e('td', {}, this.props.name),
                e('td', {}, this.renderUsersProgress()),
                e('td', {}, this.countAverageStat("level", 2)),
                e('td', {}, this.countAverageStat("win_count")),
                e('td', {}, this.countAverageStat("legendaries")),
                e('td', {}, this.countAverageStat("sfc_points"))
            );<commit_msg>avoid „NaN” reports<commit_after>/* global define */ define([
    'react',
    'cartoon-battle/util',
    'views/Kongregate/Authorization'
], function (React, util, Authorization) {

    var e = React.createElement, PropTypes = React.PropTypes;

    var localStorage = window.localStorage || {};

    function get_stored_credentials() {
        if ('user_id' in localStorage) {
            return {
                user_id: localStorage['user_id'],
                password: localStorage['password']
            }
        }

        return null;
    }

    var Island = React.createClass({
        displayName: "Island",

        propTypes: {
            name: PropTypes.string.isRequired,
            users: PropTypes.arrayOf(PropTypes.object).isRequired,
            onUserAdded: PropTypes.func
        },

        getInitialState: function () {
            return {
                users: []
            }
        },

        populateUsers: function (user) {
            this.props.onUserAdded && this.props.onUserAdded(user);

            this.setState(function (prevState) {
                return {
                    users: prevState.users.concat([user])
                }
            });
        },

        componentDidMount: function () {
            var sessionStorage = window.sessionStorage || {}, island = this.props.name;

            var populateUsers = this.populateUsers, addUser = function (user) {
                populateUsers({
                    id: user.user_id,
                    name: user.name,
                    island: island,
                    guild: user.guild.name,
                    level: user.pvp_data.level,
                    legendaries: user.cards_by_rarity[4],
                    win_count:  user.pvp_data.win_count,
                    sfc_points: user.pvp_data.rating
                });
            };

            this.props.users.map(function (user) {
                return user.user_id;
            }).forEach(function (user_id) {
                var key = "user_profile_" + user_id;

                if (sessionStorage[key]) {
                    return addUser(JSON.parse(sessionStorage[key]));
                }

                util.get_user_info(user_id, function (response) {
                    var user = response.user_profile_info;

                    sessionStorage[key] = JSON.stringify(user);

                    return addUser(user);
                }/* credentials are implied */)
            });
        },

        renderUsersProgress: function () {
            var loaded = this.state.users.length;
            if (loaded === this.props.users.length) {
                return loaded;
            }

            return loaded + " (of " + this.props.users.length + ")";
        },

        countAverageStat: function (key, prec) {
            prec = Math.pow(10, prec || 0);

            var map = function (user) {
                return user[key];
            };

            var reduce = function (sum, value) {
                return (parseInt(sum) || 0) + (parseInt(value) || 0);
            };

            return this.state.users.length
                ? Math.round(this.state.users.map(map).reduce(reduce) / this.state.users.length * prec) / prec
                : 'N/A';
        },

        render: function () {
            return e('tr', {},
                e('td', {}, this.props.name),
                e('td', {}, this.renderUsersProgress()),
                e('td', {}, this.countAverageStat("level", 2)),
                e('td', {}, this.countAverageStat("win_count")),
                e('td', {}, this.countAverageStat("legendaries")),
                e('td', {}, this.countAverageStat("sfc_points"))
            );
{'fraction_non_alphanumeric': 0.12172031376791993, 'fraction_numerical': 0.0016229375169055991, 'mean_word_length': 1.4285714285714286, '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': '6798116', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1878, 'n_words': 354}