<?php ob_start("ob_gzhandler");

require("func_template.php");
require("func_general.php");
require("config.php");

$pagetemplate=read_template("index.htm");
$raidtemplate=read_template("raiditem.htm");
$chartemplate=read_template("charitem.htm");
$litemtemplate=read_template("litemitem.htm");
$uitemtemplate=read_template("uitemitem.htm");
$chartooltiptemplate=read_template("chartooltip.htm");

validateCache();

$res=mysql_query("SELECT raidid,name,notes,timestamp,(SELECT COUNT(DISTINCT `character`) FROM transactions WHERE raid = raids.raidid) AS charcount,(SELECT COUNT(*) FROM transactions WHERE raid = raids.raidid AND value < 0) AS itemcount,(SELECT SUM(transactions.value) FROM transactions WHERE raid = raids.raidid AND value > 0) AS value FROM raids WHERE 1 ORDER BY timestamp DESC, raidid DESC LIMIT 0,10");
$raid="";
while($item=mysql_fetch_assoc($res)) {
	unset($data);
	$data['date']=date("d.m.Y",$item['timestamp']);
	$data['name']=$item['name'];
	$data['notes']=$item['notes'];
	$data['charcount']=$item['charcount'];
	$data['itemcount']=$item['itemcount'];
	$data['raidid']=$item['raidid'];
	$data['value']=displayrp($item['value']);
	$raid=$raid.parse_template($raidtemplate,$data);
}

$res=mysql_query("SELECT charid,name,class,raidcount,lastraid,lastraidid,rpin,rpout,rpin+rpout AS rpcur FROM (
    SELECT characters.charid,name,(
        SELECT classes.name FROM classes WHERE classid = class
    ) AS class,pointscache.raidcount,pointscache.lastraid,pointscache.lastraidid,pointscache.rpin,pointscache.rpout FROM characters LEFT JOIN pointscache ON characters.charid = pointscache.charid
) AS subquery ORDER BY raidcount DESC LIMIT 0,10");

$characters="";
while($item=mysql_fetch_assoc($res)) {
	unset($data);
	$data['charid']=$item['charid'];
	$data['lastraidid']=$item['lastraidid'];
	$data['date']=date("d.m.Y",$item['lastraid']);
	if($data['date']=="01.01.1970") $data['date']="";
	$data['name']=$item['name'];
	$data['class']=$item['class'];
	$data['raidcount']=$item['raidcount'];
	$data['rpin']=displayrp($item['rpin']);
	$data['rpout']=displayrp(-$item['rpout']);
	$data['rpcur']=displayrp($item['rpcur']);
	if($data['rpcur']>=0) $data['rpcurcol']="00CC33"; else $data['rpcurcol']="FF3333";
	$characters=$characters.parse_template($chartemplate,$data);
}

$res=mysql_query("SELECT firstcharacter,raid,subject,item,value,raidname,buyer,raidtimestamp,itemdata,raidcount,(
    SELECT rpin FROM pointscache WHERE charid = subquery.firstcharacter
) AS totalrpin,(
    SELECT rpout FROM pointscache WHERE charid = subquery.firstcharacter
) AS totalrpout
FROM (
    SELECT `character` AS firstcharacter,raid,subject,item,value,(
	SELECT raids.name FROM raids WHERE raidid = raid
) as raidname,(
	SELECT characters.name FROM characters WHERE charid = firstcharacter
) as buyer,(
	SELECT raids.timestamp FROM raids WHERE raidid = raid
) as raidtimestamp,(
	SELECT items.data FROM items WHERE itemid = item
) as itemdata,(
	SELECT raidcount FROM pointscache WHERE charid = firstcharacter
) AS raidcount FROM transactions WHERE isitem = 1 ORDER BY transactionid DESC LIMIT 0,10
) AS subquery");
$items="";
while($item=mysql_fetch_assoc($res)) {
	unset($data);
	$data['date']=date("d.m.Y",$item['raidtimestamp']);
	$data['raidid']=$item['raid'];
	$data['raidname']=$item['raidname'];
	$data['itemid']=$item['item'];
	$data['uniqueitemid']=($uniquenumber++)."|".$item['item'];
	$data['itemname']=$item['subject'];
	$data['buyer']=$item['buyer'];
	$data['buyerid']=$item['firstcharacter'];
	$data['itemdata']=$item['itemdata'];
	$data['value']=displayrp(-$item['value']);
	$data['uniquecharid']=($uniquenumber++);
	$data2['raidcount']=$item['raidcount'];
	$data2['rpin']=displayrp($item['totalrpin']);
	$data2['rpout']=displayrp(-$item['totalrpout']);
	$data2['rpcur']=displayrp($item['totalrpin']+$item['totalrpout']);
	if($data2['rpcur']>=0) $data2['rpcurcolor']="00CC33"; else $data2['rpcurcolor']="FF3333";
	$data['chardata']=parse_template($chartooltiptemplate,$data2);
	if($item['item']=="") $items=$items.parse_template($uitemtemplate,$data); else $items=$items.parse_template($litemtemplate,$data);
}


$bosses=array("%gar", "Gruul%", "Magtheridon", "%Grauen%", "Hydross%", "Morogrim%", "%Karathress%", "Leotheras%","Void%","%Solarian%","%\'ar","%Vashj%","%Kael%","%Winter%","%Anetheron%","%rogal","%Azgalor%","%Archimonde%","%entus","Supremus","%Akama%","%Teron%","Reli%","%blut","%Shahraz","%Illidari%","Illidan%");
$bossnames=array("Maulgar","Gruul","Magtheridon","Lurker","Hydross","Morogrim","Karathress","Leotheras","Void Reaver","Solarian","Al'ar","Lady Vashj", "Kael'thas","Winterfrost","Anetheron","Kaz'rogal","Azgalor","Archimonde","Naj'entus","Supremus","Akamas Schemen","Teron Blutschatten","Relikt der Seelen","Gurtogg Siedeblut","Mutter Shahraz","Rat der Illidari","Illidan Sturmgrimm");

for($i=0;$i<sizeof($bosses);$i++) {
	$res=mysql_query("SELECT COUNT(DISTINCT raid) FROM transactions WHERE subject LIKE '".$bosses[$i]."' AND value > 100");
	$bkcount[$i]=mysql_result($res,0);
	$bkname[$i]=$bossnames[$i];
}


$bosskilllinestemplate[1]=read_template("bosskilllines1.htm");
$bosskilllinestemplate[2]=read_template("bosskilllines2.htm");
$bosskilllinestemplate[3]=read_template("bosskilllines3.htm");
$bosskilllinestemplate[4]=read_template("bosskilllines4.htm");
$bosskilllinestemplate[5]=read_template("bosskilllines5.htm");
$bosskillblanktemplate=read_template("bosskillblank.htm");
$bosskillitemtemplate=read_template("bosskillitem.htm");

$count=0;
$bosskillstat="";

for($i=0;$i<sizeof($bosses);$i++) {
	if($bkcount[$i]>0) {
		unset($data);
		$data['bossname']=$bkname[$i];
		$fives=floor($bkcount[$i]/5);
		$rest=$bkcount[$i]%5;
		for($c=0;$c<$fives;$c++) {
			$data['lineimages'].=$bosskilllinestemplate[5];
		}
		if($rest>0) $data['lineimages'].=$bosskilllinestemplate[$rest];
		$bosskillstat.=parse_template($bosskillitemtemplate,$data);
		$count++;
	}
}

unset($data);
$data['raids']=$raid;
$data['characters']=$characters;
$data['items']=$items;
$data['bosskillstat']=$bosskillstat;

echo parse_template($pagetemplate,$data,1);

?>