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')

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.getUseridList("#all")
            if gPlayerInfo[mostdestructive][1] == 1:
                for userid in players:
                    usermsg.hudhint(userid,"The most destructive player is\n %s inflicting %s damage\n and killing %s player" % (name, gPlayerInfo[mostdestructive][0], gPlayerInfo[mostdestructive][1]))
            else:
                for userid in players:
                    usermsg.hudhint(userid,"The most destructive player is\n %s inflicting %s damage\n and killing %s players" % (name, gPlayerInfo[mostdestructive][0], gPlayerInfo[mostdestructive][1]))

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]
