## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005

import UnitStatisticsUtils
import UnitStatisticsTools
import CvStatisticsScreen
from UnitStatisticsDefines import *


# globals
###################################################

gg = UnitStatisticsTools.UnitStatisticsTools()
g_UnitStatistics = "placeHolder"
#gg = gc.getGame()
#g_UnitStatistics = ModOptionTypes.MODOPTION_UNIT_STATISTICS

objUnitStatisticsUtils = UnitStatisticsUtils.UnitStatisticsUtils()
objUnitStatisticsTools = UnitStatisticsTools.UnitStatisticsTools()



class CvUnitStatisticsEventManager:
	def __init__(self, eventManager):
	
		#################### ON EVENT MAP ######################
		self.EventKeyDown=6
		self.EventKeyUp=7
		self.eventManager = eventManager

		eventManager.addEventHandler("kbdEvent",self.onKbdEvent)
		eventManager.addEventHandler("combatResult",self.onCombatResult)
		eventManager.addEventHandler("unitBuilt",self.onUnitBuilt)
		eventManager.addEventHandler("unitMove",self.onUnitMove)
		eventManager.addEventHandler("unitSetXY",self.onUnitSetXY)
		eventManager.addEventHandler("unitCreated",self.onUnitCreated)
		eventManager.addEventHandler("unitPromoted",self.onUnitPromoted)
		eventManager.addEventHandler("unitLost",self.onUnitLost)
		eventManager.addEventHandler("unitKilled",self.onUnitKilled)
		eventManager.addEventHandler("goodyReceived",self.onGoodyReceived)
		eventManager.addEventHandler("BeginPlayerTurn",self.onBeginPlayerTurn)
		eventManager.addEventHandler("OnLoad",self.onLoadGame)
		eventManager.addEventHandler("GameStart",self.onGameStart)
 		eventManager.setEventHandler("combatHit",self.onCombatHit)
		eventManager.setEventHandler("airIntercept", self.onAirIntercept)
		eventManager.setEventHandler("combatBegin",self.onCombatBegin)
		eventManager.setEventHandler("combatWithdrawal",self.onCombatWithdrawal)
		eventManager.setEventHandler("collateralHit", self.onCollateralHit)
		eventManager.setEventHandler("airStrikeHit", self.onAirStrikeHit)
		eventManager.setEventHandler("unitUpgraded",self.onUnitUpgraded)
#		eventManager.addEventHandler("cityAcquired",self.onCityAcquired)

	# Displays the unit statistics window if a unit is selected and the letter 'u' was pressed.
	def onKbdEvent(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		eventType,key,mx,my,px,py = argsList		
		theKey=int(key)

		# Try to get the selected unit
		objUnit = CyInterface().getSelectionUnit(0)

		if (eventType == self.EventKeyDown and theKey == int(InputTypes.KB_U)):
			statisticsScreen = CvStatisticsScreen.CvStatisticsScreen()
			if (objUnit != None and (objUnit.getOwner() == gc.getGame().getActivePlayer() or g_bShowAllPlayers)):
				statisticsScreen.startScreen(objUnit, "unit")
				return 1
			else:
				statisticsScreen.startScreen(objUnit, "player")
				return 1

		return 0

	# Initializes UnitStats if there is something wrong with the game (if it hasn't been started
	# with UnitStats running for example).
	def onLoadGame(self, argsList):
		
		if(sdObjectExists("UnitStats", gc.getActivePlayer()) == False):
			objUnitStatisticsUtils.onLoadGame()

	def onGameStart(self, argsList):
		objUnitStatisticsUtils.onGameStart()
			
	# Logs the unit combat results		
	def onCombatResult(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		pWinner,pLoser = argsList
		# If we got a valid unit log the unit location.
		if(pWinner != None and (pWinner.getOwner() == gc.getGame().getActivePlayer() or (g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()))):
			objUnitStatisticsUtils.logCombatResults(pWinner,pLoser)
		if(pLoser != None and (pLoser.getOwner() == gc.getGame().getActivePlayer() or (g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()))):
			objUnitStatisticsUtils.logCombatLoss(pWinner,pLoser)

	
	# Begins logging the unit information
	def onUnitBuilt(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		city = argsList[0]
		unit = argsList[1]
		iPlayerID = city.getOwner()

		# If we got a valid unit and city then begin logging the unit.

		if(unit != None and city != None and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or unit.getOwner() == gc.getGame().getActivePlayer()) and (unit.getUnitCombatType() != -1 or g_bTrackNonCombatants)):
			objUnitStatisticsUtils.beginLoggingUnit(city.getName(), unit, iPlayerID)
		
		
	def onUnitMove(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		pPlot,pUnit = argsList
		
		# If we got a valid unit log the unit move.

		if(g_bTrackMovement and pUnit != None and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pUnit.getOwner() == gc.getGame().getActivePlayer()) and (pUnit.getUnitCombatType() != -1 or g_bTrackNonCombatants)):
			objUnitStatisticsUtils.logUnitMove(pUnit)
	

	def onUnitSetXY(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return
			
		pPlot,pUnit = argsList

		# If we got a valid unit log the unit location.

		if(g_bTrackMovement and pUnit != None and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pUnit.getOwner() == gc.getGame().getActivePlayer()) and (pUnit.getUnitCombatType() != -1 or g_bTrackNonCombatants)):
			objUnitStatisticsUtils.logUnitLocation(pUnit)


	# Logs the unit created event information
	def onUnitCreated(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		pUnit = argsList[0]
		iPlayerID = pUnit.getOwner()
		
		# If we got a valid unit log the unit creation.

		if(pUnit != None and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pUnit.getOwner() == gc.getGame().getActivePlayer()) and (pUnit.getUnitCombatType() != -1 or g_bTrackNonCombatants)):
			objUnitStatisticsUtils.logUnitCreation(pUnit, iPlayerID)

		# For non-combat-units, create a dummy-file in order to avoid problems with the high score calculation
		elif(pUnit != None and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pUnit.getOwner() == gc.getGame().getActivePlayer()) and pUnit.getUnitCombatType() == -1):
			objUnitStatisticsUtils.setupUnitStats(pUnit)
			SdToolKitAdvanced.sdObjectSetVal("UnitStats", pUnit, STARTTURN, 6000)

		
	def onUnitLost(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		pUnit = argsList[0]
		if(pUnit != None and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pUnit.getOwner() == gc.getGame().getActivePlayer()) and (pUnit.getUnitCombatType() != -1 or g_bTrackNonCombatants)):
			objUnitStatisticsUtils.onUnitLost(argsList)


	# Logs the unit promoted event information				
	def onUnitPromoted(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		pUnit, iPromotion = argsList

		# If we got a valid unit log the unit promotion.

		if(g_bTrackUnitPromotions and pUnit != None and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pUnit.getOwner() == gc.getGame().getActivePlayer()) and (pUnit.getUnitCombatType() != -1 or g_bTrackNonCombatants)):
			objUnitStatisticsUtils.logUnitPromotion(pUnit, iPromotion)


	# Logs the goody received event information
	def onGoodyReceived(self, argsList):
		'Goody received'

		if not gg.isModOption(g_UnitStatistics):
			return

		iPlayer, pPlot, pUnit, iGoodyType = argsList

		# If we got a valid unit log the unit promotion.

		if(g_bTrackGoodyReceived and pUnit != None and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pUnit.getOwner() == gc.getGame().getActivePlayer()) and (pUnit.getUnitCombatType() != -1 or g_bTrackNonCombatants)):
			objUnitStatisticsUtils.logUnitGoodyReceived(argsList)

		
	def onCombatHit(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		genericArgs = argsList[0][0]
		attackerUnit = genericArgs[0]
		defenderUnit = genericArgs[1]
		iIsAttacker = genericArgs[2]
		iDamage = genericArgs[3]
		
		# If we got a valid unit log the unit damage.

		
		if((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer())):
			objUnitStatisticsUtils.onCombatHit(argsList, 0)
		elif(attackerUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onCombatHit(argsList, 1)			
		elif(defenderUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onCombatHit(argsList, 2)

	def onAirIntercept(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		genericArgs = argsList[0][0]
		attackerUnit = genericArgs[0]
		interceptorUnit = genericArgs[1]
		iIsAttacker = genericArgs[2]
		iDamage = genericArgs[3]
		
		# If we got a valid unit log the unit damage.

		
		if((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer())):
			objUnitStatisticsUtils.onAirIntercept(argsList, 0)
		elif(attackerUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onAirIntercept(argsList, 1)			
		elif(interceptorUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onAirIntercept(argsList, 2)

	def onCollateralHit(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		genericArgs = argsList[0][0]
		attackerUnit = genericArgs[0]
		defenderUnit = genericArgs[1]
		iDamage = genericArgs[2]
		
		# If we got a valid unit log the unit damage.

		
		if((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer())):
			objUnitStatisticsUtils.onCollateralHit(argsList, 0)
		elif(attackerUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onCollateralHit(argsList, 1)			
		elif(defenderUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onCollateralHit(argsList, 2)
			
	def onAirStrikeHit(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		genericArgs = argsList[0][0]
		attackerUnit = genericArgs[0]
		defenderUnit = genericArgs[1]
		iDamage = genericArgs[2]
		
		# If we got a valid unit log the unit damage.

		
		if((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer())):
			objUnitStatisticsUtils.onAirStrikeHit(argsList, 0)
		elif(attackerUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onAirStrikeHit(argsList, 1)			
		elif(defenderUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onAirStrikeHit(argsList, 2)

	def onCombatBegin(self, argsList):
	
		if not gg.isModOption(g_UnitStatistics):
			return
	
		genericArgs = argsList[0][0]
		attackerUnit = genericArgs[0]
		defenderUnit = genericArgs[1]
		iDefenderOdds = genericArgs[2]
		
		# If we got a valid unit log the combat odds.
		if(((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or attackerUnit.getOwner() == gc.getGame().getActivePlayer() or defenderUnit.getOwner() == gc.getGame().getActivePlayer())):
			objUnitStatisticsUtils.onCombatBegin(argsList)
	
	def onCombatWithdrawal(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		genericArgs = argsList[0][0]
		attackerUnit = genericArgs[0]
		defenderUnit = genericArgs[1]
		
		# If we got a valid unit log the withdrawal
		
		if((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer())):
			objUnitStatisticsUtils.onCombatWithdrawal(argsList, 0)
		elif(attackerUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onCombatWithdrawal(argsList, 1)			
		elif(defenderUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.onCombatWithdrawal(argsList, 2)

	def onUnitKilled(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		pUnit = argsList[0]
		if((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pUnit.getOwner() == gc.getGame().getActivePlayer()):
			for i in range(10):
				objUnitStatisticsTools.checkHighScoresCurrentUnit([DAMAGETYPE[i]], pUnit)

	def onUnitUpgraded(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		genericArgs = argsList[0][0]
		pOldUnit = genericArgs[0]
		pNewUnit = genericArgs[1]
		
		if((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or pOldUnit.getOwner() == gc.getGame().getActivePlayer() or pNewUnit.getOwner() == gc.getGame().getActivePlayer()):
			objUnitStatisticsUtils.logUnitUpgraded(argsList)

## 	Not working.
#	def onCityAcquired(self, argsList):
#		'City Acquired'
#
#		if not gg.isModOption(g_UnitStatistics):
#			return
#
#		owner,playerType,city,bConquest,bTrade = argsList
#		print "city acquired test1"
#
#		if(bConquest and (playerType == gc.getGame().getActivePlayer() or (g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()))):
#			print "city acquired test2"
#			objUnitStatisticsUtils.logCityCapture(argsList)
##

	def onBeginPlayerTurn(self, argsList):

		if not gg.isModOption(g_UnitStatistics):
			return

		iGameTurn, iPlayer = argsList
		if(g_bTrackTurnInformation and ((g_bTrackAllPlayers or gc.getGame().isGameMultiPlayer()) or iPlayer == gc.getGame().getActivePlayer())):
			objUnitStatisticsUtils.logBeginPlayerTurn(argsList)