{"size": 3639, "ext": "js", "max_stars_repo_path": "javascript/features/player_stats/player_stats_tracker.js", "max_stars_repo_name": "EPIC-striker/playground", "max_stars_repo_stars_event_min_datetime": "2015-10-17T12:50:07.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-01T14:55:32.000Z", "max_issues_repo_path": "javascript/features/player_stats/player_stats_tracker.js", "max_issues_repo_name": "EPIC-striker/playground", "max_issues_repo_issues_event_min_datetime": "2015-10-13T19:07:51.000Z", "max_issues_repo_issues_event_max_datetime": "2021-02-23T13:24:42.000Z", "max_forks_repo_path": "javascript/features/player_stats/player_stats_tracker.js", "max_forks_repo_name": "EPIC-striker/playground", "max_forks_repo_forks_event_min_datetime": "2015-10-27T11:18:26.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-26T14:15:07.000Z"}
{"max_stars_count": 52, "max_issues_count": 752, "max_forks_count": 58, "avg_line_length": 36.7575757576, "max_line_length": 100, "alphanum_fraction": 0.5754328112}
// Copyright 2020 Las Venturas Playground. All rights reserved.
// Use of this source code is governed by the MIT license, a copy of which can
// be found in the LICENSE file.

import { PlayerEventObserver } from 'components/events/player_event_observer.js';

// Tracks statistics and amends the PlayerStatsSupplement instances when something substantial
// changes. Observes relevant events from the DeferredEventManager to avoid a layering violation.
export class PlayerStatsTracker extends PlayerEventObserver {
    constructor() {
        super();

        provideNative(
            'GetPlayerDeathCountJS', 'i',
            PlayerStatsTracker.prototype.getPlayerDeathCount.bind(this));

        provideNative(
            'GetPlayerKillCountJS', 'i',
            PlayerStatsTracker.prototype.getPlayerKillCount.bind(this));

        server.deferredEventManager.addObserver(this);
    }

    // ---------------------------------------------------------------------------------------------

    // Called when a player has died, potentially by doing of another player.
    onPlayerDeath(player, killer, reason) {
        player.stats.enduring.deathCount++;
        player.stats.session.deathCount++;

        if (!killer)
            return;  // a death, or an invalid killer id
        
        killer.stats.enduring.killCount++;
        killer.stats.session.killCount++;
    }

    // Called when a player has reported taking damage.
    onPlayerTakeDamage(player, issuer, amount, weaponId, bodyPart) {
        player.stats.enduring.damageTaken += amount;
        player.stats.session.damageTaken += amount;

        if (issuer) {
            issuer.stats.enduring.damageGiven += amount;
            issuer.stats.session.damageGiven += amount;
        }
    }

    // Called when a player has issued a shot.
    onPlayerWeaponShot(player, weaponId, hitType, hitId, hitPosition) {
        if (hitType === /* player */ 1) {
            const victim = server.playerManager.getById(hitId);
            if (victim && victim !== player) {
                player.stats.enduring.shotsHit++;
                player.stats.session.shotsHit++;

                victim.stats.enduring.shotsTaken++;
                victim.stats.session.shotsTaken++;
            }
        } else if (hitType === /* vehicle */ 2) {
            const vehicle = server.vehicleManager.getById(hitId);
            if (vehicle && vehicle.isOccupied()) {
                player.stats.enduring.shotsHit++;
                player.stats.session.shotsHit++;
            } else {
                player.stats.enduring.shotsMissed++;
                player.stats.session.shotsMissed++;
            }
        } else {
            player.stats.enduring.shotsMissed++;
            player.stats.session.shotsMissed++;
        }
    }

    // ---------------------------------------------------------------------------------------------

    // native GetPlayerDeathCountJS(playerId);
    getPlayerDeathCount(playerId) {
        const player = server.playerManager.getById(playerId);
        return player ? player.stats.enduring.deathCount : 0;
        
    }

    // native GetPlayerKillCountJS(playerId);
    getPlayerKillCount(playerId) {
        const player = server.playerManager.getById(playerId);
        return player ? player.stats.enduring.killCount : 0;
    }

    // ---------------------------------------------------------------------------------------------

    dispose() {
        provideNative('GetPlayerDeathCountJS', 'i', () => 0);
        provideNative('GetPlayerKillCountJS', 'i', () => 0);

        server.deferredEventManager.removeObserver(this);
    }
}
