import es
import usermsg
import playerlib
from xa import xa

#plugin information
info = es.AddonInfo() 
info.name     = "Most Destructive" 
info.version  = "1.0" 
info.author   = "SumGuy14"
info.basename = "xamostdestructive"

gPlayerInfo = {}
# Make a global dictionary to track things in

# register module with XA
xamostdestructive = xa.register('xamostdestructive')
xalanguage = xamostdestructive.language.getLanguage()

def unload(): 
    xamostdestructive.unregister() 

def round_start(event_var):
    gPlayerInfo.clear()
    players = playerlib.getUseridList("#all")
    for userid in players:
        gPlayerInfo[userid] = [0,0]
        ####################
        # [0] = Damage done
        # [1] = Kills
        ####################

def player_hurt(event_var):
    attacker = event_var['attacker']
    CheckPlayer(attacker) # Create a spot in memory for player if he doesnt have one
    if event_var['es_attackerteam'] != event_var['es_userteam'] and attacker != event_var['userid'] and es.exists('userid',attacker):
        gPlayerInfo[attacker][0]+=int(event_var['dmg_health'])

def player_death(event_var):
    attacker = event_var['attacker']
    CheckPlayer(attacker) # Create a spot in memory for player if he doesnt have one
    if event_var['es_attackerteam'] != event_var['es_userteam'] and attacker != event_var['userid'] and es.exists('userid',attacker):
        gPlayerInfo[attacker][1]+=1

def round_end(event_var):
    reason = event_var['reason']
    if reason != '10' and reason != '16':
        mostdestructive = '0'
        highdamage = 0
        for userid in gPlayerInfo:
            if gPlayerInfo[userid][0] > highdamage:
                mostdestructive=userid
                highdamage=gPlayerInfo[userid][0]
        if mostdestructive != '0':
            name = es.getplayername(mostdestructive)
            players = playerlib.getPlayerList("#all")
            xamostdestructive.logging.log("is most destructive", mostdestructive)
            if gPlayerInfo[mostdestructive][1] == 1:
                for player in players:
                    usermsg.hudhint(int(userid), xalanguage('most destructive', {'user': name, 'damage': gPlayerInfo[mostdestructive][0]}, player.get("lang")))
            else:
                for player in players:
                    usermsg.hudhint(int(userid), xalanguage('most destructives', {'user': name, 'damage': gPlayerInfo[mostdestructive][0], 'kills': gPlayerInfo[mostdestructive][1]}, player.get("lang")))

def player_disconnect(event_var):
    userid = event_var['userid']
    if userid in gPlayerInfo:
        del gPlayerInfo[userid]

def CheckPlayer(userid):
    if userid not in gPlayerInfo:
        gPlayerInfo[userid] = [0,0]
