# Squadless Kick System

# Module: squadless_kick_v2.py
# Author: Sarc
# Version: 0.3a
# Description: Players not in a squad will be kicked from the server.
#			An automated message will issue general warnings at specified intervals.
#			Squadless player checks are done when the player spawns.

# Installation:
#	Save this script, 'squadless_kick_v2.py', in your <bf2>/admin/standard_admin directory.
#	Add 'import squadless_kick_v2' and 'squadless_kick_v2.init()' to the file '<bf2>/admin/standard_admin/__init__.py'.

import bf2
import host
import math
import random
import sys


from bf2 import g_debug

#debug flag
sks_debug = False # for verbose server console messages

#variable settings; intervals are in seconds
kickInterval = 180 # time before a squadless player is kicked
msgInterval = 90 # time between warning messages
JumpInterval = 10 # Jump Delay
warningMsg = "ADMIN: |c1000Squadless players will be kicked!" # the warning message
JumpTimerMsg = "JumpTimerEvent"
jumpmessage = "Jump Calculation Underway"
jumpmessage2 = "Jump Calculation Underway"
jumpmessage3 = "Jump Calculation Underway"
jumpmessage4 = "Jump Complete"
jumpmessage5 = "Found ^ in Jump Command"
jumpmessage6 = "BlindJump"
jumpmessage7 = "We've Jumped way beyond the Red Line!"
jumpmessage8 = "Jump Home"
WCMessage1 = "weapon change event"
WCMessage2 = "weapon change complete"
Diemessage1 = "Die......."
DieMessage2 = "Die Again"
DieMessage3 = "Die Complete"
DieMessage4 = "Die Try"

#timers
msgTimer = None

#queue lists
kickList = []

# Initialize the kick system i.e. register game hooks.
def init():
	if g_debug: print "initializing squadless_kick script"
	
	host.registerHandler('PlayerSpawn', onPlayerSpawn)
	host.registerGameStatusHandler(onGameStatusChanged)
	host.registerHandler('ChatMessage', onChatMessage, 1)
	#host.registerHandler('PlayerChangeWeapon', onPlayerChangeWeapon)




















#ADD GAIUSBALTAR

def onChatMessage(playerId, msgText, channel, flags):
	global JumpInterval, msgTimer, myvehic, newx, newy, newz

	if msgText.find("jump") <> -1:
		JumpInterval = 5
		host.rcon_invoke("game.sayAll \"" + jumpmessage + "\"")
		myplayer = bf2.playerManager.getPlayerByIndex(playerId)

 		myweapon = myplayer.getPrimaryWeapon()
		myweaponType = myweapon.templateName 


		myvehic = myplayer.getVehicle()
		mypos = myvehic.getPosition()
		host.rcon_invoke("game.sayAll \"" + jumpmessage + "\"")
		newpos = myvehic.getPosition()
		myrot = myvehic.getRotation()
		myyaw = myrot[0]
		mypitch = myrot[1]
		r = 1.0
		
		if msgText.find("^") <> -1:
			JMsplt = msgText.split("^")
			host.rcon_invoke("game.sayAll \"" + jumpmessage5 + "\"")
			host.rcon_invoke("game.sayAll \"" + JMsplt[1] + "\"")
			JDText = JMsplt[1]
			r = float(JDText)

			
		mypitch = -1.0 * mypitch  #pitch is backwards
		mypitch = math.radians(mypitch)
		myyaw = math.radians(myyaw)
		dz = math.cos(myyaw) * math.cos(mypitch) * r
		dx = math.sin(myyaw) * math.cos(mypitch) * r
		dy = math.sin(mypitch) * r
		#assume good code to here
		host.rcon_invoke("game.sayAll \"" + jumpmessage2 + "\"")
		#newpos[1] = newpos[1] + 300.0
		host.rcon_invoke("game.sayAll \"" + str(myrot[0]) + "\"")
		host.rcon_invoke("game.sayAll \"" + str(myrot[1]) + "\"")
		newx = mypos[0] + dx
		newy = mypos[1] + dy
		newz = mypos[2] + dz


		if msgText.find("jumphome") <> -1:
			host.rcon_invoke("game.sayAll \"" + jumpmessage8 + "\"")
			newx = 0.0
			newy = 0.0
			newz = 0.0
			JumpInterval = 3
			



		if msgText.find("blindjump") <> -1:
			host.rcon_invoke("game.sayAll \"" + jumpmessage6 + "\"")
			randx = random.randint(1, 2000)	
			randx = randx - 1000
			host.rcon_invoke("game.sayAll \"" + str(float(randx)) + "\"")
			randz = random.randint(1, 3000)
			randz = randz - 1000
			if randz > 2000 :
				host.rcon_invoke("game.sayAll \"" + jumpmessage7 + "\"")
			host.rcon_invoke("game.sayAll \"" + str(float(randz)) + "\"")
			newx = float(randx)
			newz = float(randz)
			JumpInterval = 1
		
		#host.rcon_invoke("game.sayAll \"" + str(dx) + "\"")
		#host.rcon_invoke("game.sayAll \"" + str(dy) + "\"")
		#host.rcon_invoke("game.sayAll \"" + str(dz) + "\"")
		host.rcon_invoke("game.sayAll \"" + jumpmessage + "\"")

		host.rcon_invoke("game.sayAll \"" + str(myweaponType) + "\"")

		msgTimer = bf2.Timer(onJumpTimer, JumpInterval, 1)

		#myvehic.setPosition((newx, newy, newz))
		host.rcon_invoke("game.sayAll \"" + jumpmessage4 + "\"")
		vehicname = myvehic.templateName
		host.rcon_invoke("game.sayAll \"" + str(vehicname) + "\"")


	if msgText.find("die") <> -1:
		f35b_objs = bf2.objectManager.getObjectsOfType('dice.hfe.world.ObjectTemplate.PlayerControlObject')
		CounterX = 0
		for obj in f35b_objs:
			if obj.templateName == 'air_f35b':
				host.rcon_invoke("game.sayAll \"" + Diemessage1 + "\"")

				try:
					host.rcon_invoke("game.sayAll \"" + Diemessage1 + "\"")

					obj.setDamage(-1)
					CounterX = CounterX + 1
				except:
					host.rcon_invoke("game.sayAll \"" + Diemessage1 + "\"")

				host.rcon_invoke("game.sayAll \"" + str(CounterX) + "\"")
				host.rcon_invoke("game.sayAll \"" + Diemessage1 + "\"")

				#f35b_pos = obj.getPosition()
				#if f35b_pos[1] > 100.0:	
					#host.rcon_invoke("game.sayAll \"" + str(f35b_pos[1]) + "\"")
					#f35bx = f35b_pos[0]
					#f35by = f35b_pos[1] 
					#f35bz = f35b_pos[2]
					#f35by = 499.0
					#obj.setPosition((f35bx, f35by, f35bz))
					#f35b_pos[1] = f35by 
					#host.rcon_invoke("game.sayAll \"" + str(f35by) + "\"")




def onJumpTimer(data):
	
	global JumpTimerMsg
	
	host.rcon_invoke("game.sayAll \"" + JumpTimerMsg + "\"")
	myvehic.setPosition((newx, newy, newz))
	host.rcon_invoke("game.sayAll \"" + str(newx) + "\"")
	host.rcon_invoke("game.sayAll \"" + str(newy) + "\"")
	host.rcon_invoke("game.sayAll \"" + str(newz) + "\"")



#def onPlayerChangeWeapon(playerObject, oldWeaponObject, newWeaponObject):

	  #host.rcon_invoke("game.sayAll \"" + WCMessage1 + "\"")
	  #host.rcon_invoke("game.sayAll \"" + newWeaponObject.templateName + "\"")
	  #host.rcon_invoke("game.sayAll \"" + WCMessage2 + "\"")

#END GAIUSBALTAR



















# On spawn, checks if the player is in a squad or commanding.
# Player is added to kick list if not in a squad or not commanding.
def onPlayerSpawn(p, vehicle):
	global sks_debug
	global kickList, kickInterval
	
	if (p.getSquadId() == 0) and not p.isCommander():
		# Add the player if not in kick list
		if kickList.count(p.index) == 0:
			kickList.append(p.index)

			# Set timer for kick
			bf2.Timer(onKickTimer, kickInterval, 1)
			
			if sks_debug: host.rcon_invoke("echo \"" + p.getName() + " was added to the kick list.\"")

		if sks_debug: host.rcon_invoke("echo \"Kick warning issued.\"")

		# Issue kick warning
		host.rcon_invoke("game.sayAll \"ADMIN: |c1000" + p.getName() + " will be kicked for not being in a squad\"")

# Sends out warning messages
def onMsgTimer(data):
	global sks_debug
	global warningMsg
	
	host.rcon_invoke("game.sayAll \"" + warningMsg + "\"")
	
	if sks_debug: host.rcon_invoke("echo \"Warning message issued.\"")

# Processes the kick list queue. The next player in queue
# is checked again and kicked if not in a squad or not commanding.
def onKickTimer(data):
	global sks_debug
	global kickList

	if sks_debug: host.rcon_invoke("echo \"Kick timer triggered.\"")
	
	# Process the next player in queue.
	p = bf2.playerManager.getPlayerByIndex(kickList.pop(0))
	
	# Player is still squadless, kick him.
	if (p.getSquadId() == 0) and not p.isCommander():
		host.rcon_invoke("echo \"" + p.getName() + " was kicked for not being in a squad\"")
		host.rcon_invoke("admin.kickPlayer " + str(p.index))

# Handles clean up and initialization between rounds.
def onGameStatusChanged(status):
	global kickList
	global msgInterval, msgTimer

	if status == bf2.GameStatus.Playing:
		host.rcon_invoke("echo \"Game Status: Playing\"")
		
		# Set the warning timer.
		msgTimer = bf2.Timer(onMsgTimer, msgInterval, 1)
		msgTimer.setRecurring(msgInterval)

	elif status == bf2.GameStatus.PreGame:
		host.rcon_invoke("echo \"Game Status: PreGame\"")
		
		# Destroy any timers
		msgTimer.destroy()
		msgTimer = None
		
		# Blank the list.
		kickList = []
	
	elif status == bf2.GameStatus.EndGame:
		host.rcon_invoke("echo \"Game Status: EndGame\"")
