<? include $DOCUMENT_ROOT."/zzzfilelist.inc"; ?>
<? if (!$dont_inc_general) { include Inc("functions_general.inc.php"); } ?>
<? include Inc("metoo.inc.php"); ?>
<? include Inc("metoo_loc.inc.php"); ?>
<?
	$fn=Inc("mapdata.csv");
	$fp = fopen ($fn, "r");
	$mapcontent = fread($fp, filesize($fn));
	fclose($fp);
	Decrunch($mapcontent);
	$mapcontentcell=$cell;

	$fn=Inc("regions.csv");
	$fp = fopen ($fn, "r");
	$content = fread($fp, filesize($fn));
	fclose($fp);
	Decrunch($content);
	$mapregions=$cell;
?>
<html>
<head><title>MeToo : Middle-Earth Tool for Overcoming Opposition</title></head>
<body>
<center>
<font size=+2><b>Middle-Earth Tool for Overcoming Opposition</b></font><br>
</center>

<hr>
<?
	if (!$goingtologin) { include Inc("testlogin.php");	}
?>
<? $where="showregions"; include Inc("checkgameaccess.inc.php"); ?>
<? $game_checked=$game; include Inc("loaddata.inc.php"); ?>
<?
	$nbupdtodate=0;
	for ($i=1; $i<=25; $i++) {
		if ($turnmosttodate[$i]==$turntodisplaymax) {
			$nbupdtodate++;
		}
	}
	# sides
	for ($i=1; $i<=10; $i++) {
		$Siding[$i]=1;
	}
	for ($i=11; $i<=20; $i++) {
		$Siding[$i]=2;
	}
	for ($i=21; $i<=25; $i++) {
		$Siding[$i]=3;
		if (($NeutralSide[$i-20]!=0) && ($NeutralSideTurn[$i-20]<=$turntodisplaymax)) {
			$Siding[$i]=$NeutralSide[$i-20];
		}
	}
	
	# armies
	for ($i=1; $i<=$nbarmies; $i++) {
		if ($army[$i]->hex!=0) {
			$p=$army[$i]->people; $nbarmy[$p]++; $armylist[$p][$nbarmy[$p]]=$i;
			$x=substr($army[$i]->hex,0,2)*1;
			$y=substr($army[$i]->hex,2,2)*1;
			$reg[$i]=$mapregions[$y][$x];
		}
	}
	for ($i=1; $i<=$nbarmies; $i++) {
		if ($army[$i]->hex!=0) {
			$regArmies[$reg[$i]][$Siding[$army[$i]->people]]++;
#			echo $army[$i]->leadername." located in ".$loc[$loctag[11]+$reg[$i]][$lang]." (".$army[$i]->hex."), ".$regArmies[$reg[$i]][$Siding[$army[$i]->people]]."<br>";
			$regArmiesStr[$reg[$i]][$Siding[$army[$i]->people]]+=$army[$i]->str_final;
			$regArmiesConst[$reg[$i]][$Siding[$army[$i]->people]]+=$army[$i]->con_final;
		}
	}
	
	for ($j=1; $j<=$NbOfRegions; $j++) {
		for ($i=1; $i<=3; $i++) {
			$TotalN[$i]+=$regArmies[$j][$i];
			$TotalC[$i]+=$regArmiesConst[$j][$i];
			$TotalS[$i]+=$regArmiesStr[$j][$i];
		}
	}
	$totc=0; $tots=0;
	for ($i=1; $i<=3; $i++) {
		$totc+=$TotalC[$i];
		$tots+=$TotalS[$i];
	}

	echo "<table width=100% border=1 cellpadding=0 cellspacing=0>";
	echo "<tr align=center bgcolor=#C0C0FF><td colspan=10><b>".$loc[$loctag[11]+16][$lang]."</td></tr>";
	echo "<tr align=right>";
	for ($i=1; $i<=10; $i++) {
		echo "<td width=10%><font size=-2><b>".($i*10)."%</td>";
	}
	echo "</tr></table>";
	echo "<table width=100% height=50 border=0 cellpadding=0 cellspacing=0>";
	echo "<tr align=center valign=middle>";
	for ($i=1; $i<=3; $i++) {
		if ($TotalC[$i]>0) {
			$quote=(int)($TotalC[$i]*50/$totc+$TotalS[$i]*50/$tots);
			echo "<td width=".$quote;
			echo "% bgcolor=".$SidesColors[$i];
			echo "><font size=-1><b>";
			echo $TotalN[$i]." ".$loc[$loctag[11]+15][$lang];
			echo "</td>";
		}
	}
	echo "</tr></table>";
	echo "<br>";

	echo "<table align=center border=1 cellspacing=0>";
	echo "<tr align=center bgcolor=#C0C0FF><td colspan=".($NbOfRegions+1)."><b>".$loc[$loctag[11]+17][$lang]."</td></tr>";
	echo "<tr align=center>";
	echo "<td width=80></td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80><b>".$loc[$loctag[11]+$j][$lang]."</td>";
	}
	echo "</tr>\n";
	
	echo "<tr align=center valign=bottom>";
	echo "<td width=80></td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80>";
		$totc=0; $tots=0;
		for ($i=1; $i<=3; $i++) {
			$totc+=$regArmiesConst[$j][$i];
			$tots+=$regArmiesStr[$j][$i];
		}
		if ($tots>0) {
			$nblin=RoundItTrue($tots/10000,1)+1;
			for ($l=1; $l<=$nblin; $l++) {
				for ($i=1; $i<=3; $i++) {
					$quote=$regArmiesConst[$j][$i]*100/$totc+$regArmiesStr[$j][$i]*100/$tots;
					$quote=RoundItTrue($quote/2,5);
					if ($quote>0) {
						$qt=$quote/5;
						for ($q=1; $q<=$qt; $q++) {
							echo "<img src='".IncL("reg_".$i.".gif")."'>";
						}
					}
				}
				echo "<br>";
			}
		}
		echo "</td>";
	}
	echo "</tr>\n";

	echo "<tr align=center>";
	echo "<td width=80><font size=-1><b>".$loc[$loctag[11]+18][$lang]."</td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80><font size=-1><b>";
		for ($i=1; $i<=3; $i++) {
			if ($regArmies[$j][$i]>0) {
				echo "<font color=".$SidesColors[$i].">".$regArmies[$j][$i]." ";
			}
		}
		echo "</td>";
	}
	echo "</tr>\n";

	echo "<tr align=center>";
	echo "<td width=80><font size=-1><b>".$loc[$loctag[11]+19][$lang]."</td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80><font size=-1>";
		for ($i=1; $i<=3; $i++) {
			if ($regArmiesStr[$j][$i]>0) {
				echo "<font color=".$SidesColors[$i].">".RoundIt($regArmiesStr[$j][$i],100)."/".RoundIt($regArmiesConst[$j][$i],100)."<br>";
			}
		}
		echo "</td>";
	}
	echo "</tr>\n";
	echo "</table>\n";
	echo "<br>";

	# pop centers
	for ($i=1; $i<=$nbtowns; $i++) {
		if ($town[$i]->hex!=0) {
			$p=$town[$i]->people; $nbtown[$p]++; $townlist[$p][$nbtown[$p]]=$i;
			$x=substr($town[$i]->hex,0,2)*1;
			$y=substr($town[$i]->hex,2,2)*1;
			$regtown[$i]=$mapregions[$y][$x];
		}
	}
	for ($i=1; $i<=$nbtowns; $i++) {
		if ($town[$i]->hex!=0) {
			$regTowns[$regtown[$i]][$Siding[$town[$i]->people]]++;
#			echo $army[$i]->leadername." located in ".$loc[$loctag[11]+$reg[$i]][$lang]." (".$army[$i]->hex."), ".$regArmies[$reg[$i]][$Siding[$army[$i]->people]]."<br>";
			$regTownSize[$regtown[$i]][$Siding[$town[$i]->people]]+=$town[$i]->size;
			if ($town[$i]->size>=3) {
				$regTownSizeRec[$regtown[$i]][$Siding[$town[$i]->people]]+=$town[$i]->size;
			}
		}
	}
	
	for ($j=1; $j<=$NbOfRegions; $j++) {
		for ($i=1; $i<=3; $i++) {
			$TotalTownN[$i]+=$regTown[$j][$i];
			$TotalTownS[$i]+=$regTownSize[$j][$i];
		}
	}
	$totc=0; $tots=0;
	for ($i=1; $i<=3; $i++) {
		$tots+=$TotalTownS[$i];
	}

	echo "<table align=center border=1 cellspacing=0>";
	echo "<tr align=center bgcolor=#C0C0FF><td colspan=".($NbOfRegions+1)."><b>".$loc[$loctag[11]+20][$lang]."</td></tr>";
	echo "<tr align=center>";
	echo "<td width=80></td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80><b>".$loc[$loctag[11]+$j][$lang]."</td>";
	}
	echo "</tr>\n";
	
	echo "<tr align=center valign=bottom>";
	echo "<td width=80></td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80>";
		$tots=0;
		for ($i=1; $i<=3; $i++) {
			$tots+=$regTownSize[$j][$i];
		}
		if ($tots>0) {
			$nblin=RoundItTrue($tots/10,1)+1;
			for ($l=1; $l<=$nblin; $l++) {
				for ($i=1; $i<=3; $i++) {
					$quote=$regTownSize[$j][$i]*100/$tots;
					$quote=RoundItTrue($quote,5);
					if ($quote>0) {
						$qt=$quote/5;
						for ($q=1; $q<=$qt; $q++) {
							echo "<img src='".IncL("reg_".$i.".gif")."'>";
						}
					}
				}
				echo "<br>";
			}
		}
		echo "</td>";
	}
	echo "</tr>\n";

	echo "<tr align=center>";
	echo "<td width=80><font size=-1><b>".$loc[$loctag[11]+21][$lang]."</td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80><font size=-1><b>";
		for ($i=1; $i<=3; $i++) {
			if ($regTowns[$j][$i]>0) {
				echo "<font color=".$SidesColors[$i].">".$regTowns[$j][$i]." ";
			}
		}
		echo "</td>";
	}
	echo "</tr>\n";

	echo "<tr align=left>";
	echo "<td width=80 align=center><font size=-1><b>".$loc[$loctag[11]+22][$lang]."</td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80><font size=-2>";
		for ($t=1; $t<=5; $t++) {
			echo $loc[$loctag[9]+$t][$lang]." ";
			for ($i=1; $i<=3; $i++) {
				$nbt[$i]=0;
			}
			for ($i=1; $i<=$nbtowns; $i++) {
				if (($regtown[$i]==$j) && ($town[$i]->size==$t)) {
					$nbt[$Siding[$town[$i]->people]]++;
				}
			}
			for ($i=1; $i<=3; $i++) {
				if ($nbt[$i]>0) {
					echo "<font color=".$SidesColors[$i].">".$nbt[$i]."</font> ";
				}
			}
			echo "<br>";
		}
		echo "</td>";
	}
	echo "</tr>\n";

	echo "<tr align=center>";
	echo "<td width=80><font size=-1><b>".$loc[$loctag[11]+23][$lang]."</td>";
	for ($j=1; $j<=$NbOfRegions; $j++) {
		echo "<td width=80><font size=-1><b>";
		for ($i=1; $i<=3; $i++) {
			if ($regTownSize[$j][$i]>0) {
				echo "<font color=".$SidesColors[$i].">".($regTownSizeRec[$j][$i]*100)."<br>";
			}
		}
		echo "</td>";
	}
	echo "</tr>\n";

	echo "</table>\n";
?>