<?php
/*
* GuildInfo, by Kaeus
* This module helps people with guild recruitment.
*
* modifyed by Snyder for CRO guild on Crom server
* Modified by FA_Frey_XC for Corps of Xeno on Crom Server

*/
$GuildInfo = new GuildInfo($bot);

class GuildInfo extends BaseActiveModule {
	private $recruitment_file = 'D:/www/bebot/recruitment.php';
	private $who_clicked = array();
	private $spam_time = 15;// minutes that spam protection is active
/*
	constructor
*/
	function __construct(&$bot) {
		parent::__construct(&$bot, get_class($this));
		
		$this->register_command('all', 'ginfo', "ANONYMOUS");
		
		$this->bot->core("colors")->define_scheme("GI", "highlight", "yellow");
		$this->bot->core("colors")->define_scheme("GI", "normal", "white");
		$this->bot->core("colors")->define_scheme("GI", "info", "lightgreen");
		$this->bot->core("colors")->define_scheme("GI", "red", "red");
		$this->bot->core("colors")->define_scheme("GI", "blue", "blue");
/*
	define classes and colors for recruitment
*/
		$this->classes = array(
			'sin' =>		'Assasin',
			'barb' =>		'Barbarian',
			'bs' =>			'Bear Shaman',
			'conq' =>		'Conqueror',
			'dt' =>			'Dark Templar',
			'demo' =>		'Demonologist',
			'guard' =>		'Guardian',
			'hox' =>		'Herald of Xotli',
			'necro' =>		'Necromancer',
			'pom' =>		'Priest of Mitra',
			'rang' =>		'Ranger',
			'tos' =>		'Tempest of Set',
		);
/*
	you can pute more/less levels of recruitment
*/
		$this->colors = array(
			'none' =>			'#FFCC00',
			'low' =>			'#FF0000',
			'medium' =>			'#FF6600',
			'high' =>			'#009900',
			'ultrahigh' =>		'#005900',
		);
		// settings
		$i = 1;
		$opt = "";
		// make options for settings
		foreach ($this->colors as $k => $v) {$opt .= $k.";";unset($k, $v);}
		$opt = eregi_replace(";$", "", $opt);// remove last ; from options
		// make settings loop
		foreach ($this->classes as $short => $long) {
			$this->bot->core("settings")->create("GuildInfo", $short, "none", $long." requirement", $opt, false, $i);
			$i++;
		}
		unset($opt, $i);
		// help
		$this->help['description'] =					'Helps with guild recruitment.';
		$this->help['command']['ginfo'] =				'Displays guild\'s info tab.';
		// officers
		$this->officers = array(
			// first is guild leader
			'Astotherous',
			// rest are officers
			'Frennetty',
			'Beniira',
			
		);

	}
/*

*/
	function command_handler($name, $msg, $origin) {
		// there is only one command
		if (preg_match('/^ginfo/i', $msg, $info)) {
			// spam protection
			if ($this->is_not_spam($name)) {
				return $this->info($name);
			}
		}
		else {
			$this->bot->send_help($name);
		}
	}
   
/*
	guild info stuff
*/
	function info($name)	{
		// guild name
		$txt = "<font face='hyboriansmall' color='#FFFFFF'>Corps of Xeno Guild</font>\n<font color=#000000>.<br></font>";
		$txt .= "<font face='hyboriansmall' color='#FFFFFF'>http://www.xenocorp.com</font>\n<font color=#000000>.<br></font>";
		// staff
		$i = 0;
		foreach ($this->officers as $v) {
			$online = $this->find_officer($v);
			$n = $online[1];
			$online = $online[0];
			// first one is guildleader
			if (!$i) {$txt .= "##normal##Guild leader##end##\n";}
			// rest are officers
			else if ($i == 1) {$txt .= "##normal##Officers##end##\n";}
			$txt .= "##GI_highlight##".$online['content']." :: <a href='chatcmd:///tell ".$n." Hey ".$n.", i would like some more info'>".$n."</a> ( ".$v." )##end##";
			if ($v == $name) {$txt .= " - <font color='yellow'>posted recruitment info</font>";}
			$txt .= "\n";
			unset($v, $online, $n);
			$i++;
		}
		// recruitment text from local file
		// you can get one from your website if you really want...
		$data = file_exists($this->recruitment_file) ? file_get_contents($this->recruitment_file) : '';
		$txt .= $data;
		// loop to create requirement
		foreach ($this->classes as $short => $long) {
			$r = $this->bot->core("settings")->get("GuildInfo", $short);// requirement for class
			$txt .= "##normal##".$long.":##end## <font color='".$this->colors[ $r ]."'>[".$r."]</font>\n";
			unset($short, $long, $r);
		}
		unset($data);
		return $this->bot->core("tools")->make_blob("CoX Guild info", $txt);
	}
/*
	find officer
*/
	public function find_officer($name) {
		// query for officer, if hes not on any alts his main char will be shown ($name)
		$query = $this->bot->db->select("select o.nickname from #___online as o, #___alts as a where o.status_gc = '1' and ( (o.nickname = a.alt and a.main = '".$name."') or (o.nickname = a.main and a.alt = '".$name."') ) LIMIT 1");
		if (!empty($query)) {
			// just in case go with foreach loop, but there should be only one item in array
			foreach ($query as $user) {
				$name = $user[0];
				unset($user);
			}
		}
		unset($query);
		// online status on character $name
		$online = $this->bot->core("online")->get_online_state($name);
		// return data array (online/offline, character name)
		return array($online, $name);
	}
/*
	spam protection agains wankers that click dozen times on link
*/
	public function is_not_spam($name) {
		// if name is in array, check if he clicked in last $this->spam_time minutes
		if (isset($this->who_clicked[ $name ])) {
			// name is in array, check if time char last time clicked is greater than time() - $this->spam_time
			if ( $this->who_clicked[ $name ] > ( time() - $this->spam_time * 60 ) ) {
				return false;
			}
		}
		$this->who_clicked[ $name ] = time();
		return true;
	}
}
?>