##########################################
# Log out when no potions left by Mindee #
##########################################

# Potion check interval in seconds.
potionCheckInterval = 2
# Check for potions in inventory too. False = only backpack.
checkInventory = True
# Potion ids. Add more if missing.
potions = (5561,5562,5563,5564,5565,5566,5567,2587)
# Play alarm.
playAlarm = True
alarmText = "No potions are left. Logging out!"

# Don't touch this. Must be False.
timerRunning = False

def onScriptActivation():
	RunEvent()

def onScriptDeactivation():
	global timerRunning
	timerRunning = False

def RunEvent():
	global timerRunning
	timerRunning = True
	script.RunEvent('checkPotions', potionCheckInterval * 1000)

def checkPotions():
	if not timerRunning:
		return
	
	if not script.ReceivedInventory():
		RunEvent()
		return
		
	if len(potions) < 1:
		RunEvent()
		return

	count = 0
	for id in potions:
		count += getItemCount(id)
	
	if count < 1:
		if playAlarm:
			script.Alarm(alarmText)
			script.Logout()
			return

	RunEvent()

def getItemCount(itemId):
	if checkInventory:
		return script.GetItemsCountWithInventory(itemId)
	
	return script.GetItemsCount(itemId, False)