import es
import gamethread
import random

import wcs
from wcs import wcs

itemName = 'Mole'

wcs.Item(itemName).registerItem('special', 10000, "Teleport to the enemy base and look like one of them.")    
        
def player_spawn(ev):
    if wcs.Command(ev['userid'])['Mole']:
        wcs.Command(ev['userid'])['Mole'] = 0
        time = int(es.ServerVar('mp_freezetime')) + 5
        es.tell(ev['userid'], '#multi', '#greenMole #default- #lightgreenYou will become a mole in %s seconds.' % time)
        gamethread.delayed(time, Mole, (ev['userid'], 'info_player_%sterrorist' % ('' if ev['es_userteam'] == '3' else 'counter') ) )
        
def Mole(userid, entitylist):
    x, y, z = map(float, es.getindexprop(random.choice(es.createentitylist(entitylist) ), 'CBaseEntity.m_vecorigin').split(','))
    if wcs.isOb:
        es.setpos(userid, x, y, z)
    else:
        es.server.queuecmd('es_xsetpos %s %s %s %s' % (userid, x, y, z) )
    wcs.Command(userid).Model('player/%s'%(random.choice(['ct_gign','ct_gsg9','ct_sas','ct_urban']) if es.getplayerteam(userid) == 2 else random.choice(['t_arctic','t_phoenix','t_guerilla','t_leet']) ) )
        
def item_bought(ev):
    if ev['item'] == itemName:
        es.tell(ev['userid'], '#multi', '#greenMole #default- #lightgreenYou will become a mole next round.')
        wcs.Command(ev['userid'])['Mole'] = 1        
