UI.AddCheckbox( "[HMI] Hit/Miss Ratio" );
UI.AddCheckbox( "[HMI] Reset on round start" );

var x = 30, y = 420;

var gun_fired = false;
var font_size = 4;

var other_weapons =
[
    "knife",
    "knife_t",
    "knife_karambit", 
    "knife_m9_bayonet", 
    "knife_survival_bowie", 
    "knife_butterfly", 
    "knife_flip", 
    "knife_push", 
    "knife_tactical",
    "knife_falchion", 
    "knife_gut", 
    "knife_ursus", 
    "knife_gypsy_jackknife", 
    "knife_stiletto", 
    "knife_widowmaker", 
    "knife_css", 
    "knife_cord", 
    "knife_canis", 
    "knife_outdoor", 
    "knife_skeleton", 
    "bayonet", 
    "hegrenade",
    "smokegrenade",
    "molotov",
    "incgrenade",
    "flashbang",
    "decoy",
    "taser"
];

var shots =
{
    fired: 0,
    hit: 0,
    missed: 0,
    hit_chance: 0,
    miss_chance: 0
};

function is_gun(weapon_name) {
    
    for(var i = 0; i < other_weapons.length; i++) {
        
        if(weapon_name == "weapon_" + other_weapons[i])
        {

            return false;

        }

    }

    return true;

}

function weapon_fire() {
    var player_id = Event.GetInt("userid");
    var player_weapon = Event.GetString("weapon");

    if(Entity.IsLocalPlayer(Entity.GetEntityFromUserID(player_id)) && is_gun(player_weapon)) {

        shots.fired = shots.fired + 1;
        gun_fired = true;

    }
}

function player_hurt() {
    var attacker_id = Event.GetInt("attacker");
    var attacker_weapon = Event.GetString("weapon");

    if(Entity.IsLocalPlayer(Entity.GetEntityFromUserID(attacker_id)) && is_gun(attacker_weapon) && gun_fired) {

        shots.hit = shots.hit + 1;
        gun_fired = false;

    }
}

function round_prestart() {
    if( UI.GetValue("Misc", "JAVASCRIPT", "Script Items", "[HMI] Reset on round start") ) {
        for (var key in shots){
            shots[key] = 0;
        }
    }
}

function main() {

    if(Global.GetMapName() == "" || !UI.GetValue("Misc", "JAVASCRIPT", "Script Items", "[HMI] Hit/Miss Ratio"))
        return;
    
    shots.missed = shots.fired - shots.hit;
    shots.hit_chance = ( (shots.hit / shots.fired) * 100 );
    shots.miss_chance = ( (shots.missed / shots.fired) * 100 );
    
    text_size = Render.TextSize("Total: " + shots.fired, font_size);

    Render.String(x, y, 0, "Total: " + shots.fired, [255,0,255, 255], font_size);

    Render.String(x, y + (text_size[1]-12), 0, "Hit: " + shots.hit, [255,0,255, 255], font_size);

    Render.String(x, y + (text_size[1]-12)*2, 0, "Miss: " + shots.missed, [255,0,255, 255], font_size);

    if(shots.fired > 0) {

        Render.String(x, y + (text_size[1]-12)*3, 0, "HitP: " + Math.round(shots.hit_chance), [220,20,60, 255], font_size);

        Render.String(x, y + (text_size[1]-12)*4, 0, "MisP: " + Math.round(shots.miss_chance), [220,20,60, 255], font_size);

    }

}

Global.RegisterCallback("weapon_fire", "weapon_fire");
Global.RegisterCallback("player_hurt", "player_hurt");
Global.RegisterCallback("round_prestart", "round_prestart");

Global.RegisterCallback("Draw", "main");