# Random plugin for Deathrun Mod v 1.5
# Visit our DeathRun Server 188.134.21.171:27015
# Visit our site www.tk-portal.ru

import es, gamethread, random, playerlib, spe

def round_end(event_var):
    if es.getplayercount() > 1:
        es.msg('#green', 'Choose a new terrorist...')
        es.centermsg('Choose a new terrorist...')
        gamethread.delayed(2, randomplayer)

def randomplayer():
    uid = es.getcmduserid()
    terlist = playerlib.getUseridList('#t')  
    if es.getplayercount(3):
        target = random.choice(playerlib.getUseridList('#ct'))
        es.msg('#multi', '#green%s #lightgreenis a new terrorist!'%es.getplayername(target))
        es.centermsg('%s is a new terrorist!'%es.getplayername(target))
        spe.switchTeam(target, 2)
    for ter in terlist:
        spe.switchTeam(ter,3)
    if es.getplayerteam(uid) != 1:
        es.setplayerprop(uid, "CBasePlayer.m_lifeState", 0)

def player_disconnect(event_var):
    if not es.getplayercount(2):
        if es.getplayercount() > 1:
            target = random.choice(playerlib.getUseridList('#ct'))
            spe.switchTeam(target, 2)

def player_spawn(event_var):
    userid = int(event_var['userid'])
    if es.getplayerteam(userid) != 1:
        es.setplayerprop(userid, "CBasePlayer.m_lifeState", 512)