#organiser.py
#by VladVP

from commands import add, admin, name

on_by_default = True

@name('to')
@admin
def toggleorganisation(connection):
	global on_by_default
	if on_by_default is True:
		on_by_default = False
		connection.protocol.send_chat("Team organisation disabled!")
	else:
		on_by_default = True
		connection.protocol.send_chat("Team organisation enabled!")

add(toggleorganisation)

def apply_script(protocol, connection, config):
	class orgConnection(connection):
		def spawn(self):
			if on_by_default:
				protocol = self.protocol
				name = self.name
				
				blue = config.get("team_preset", []).get("blue", [])
				green = config.get("team_preset", []).get("green", [])
				blueclan = config.get("team_preset", []).get("blueclan", [])
				greenclan = config.get("team_preset", []).get("greenclan", [])
				
				if   name in blue:
					self.team = protocol.blue_team
				elif name in green:
					self.team = protocol.green_team
				else:
					for clan in blueclan:
						if name.find(clan) + 1:
							self.team = protocol.blue_team
							return connection.spawn(self)
					for clan in greenclan:
						if name.find(clan) + 1:
							self.team = protocol.green_team
							return connection.spawn(self)
					self.team = protocol.spectator_team
			return connection.spawn(self)
	return protocol, orgConnection