<? include $DOCUMENT_ROOT."/zzzfilelist.inc"; ?>
<? include Inc("metootop.inc.php"); ?>
<? $where="enterorders"; include Inc("checkgameaccess.inc.php"); ?>
<center>
<?
	if ($mode=="enter") {
		$game_checked=$game;		
		include Inc("loaddata.inc.php");
		
		for ($t=1; $t<=$nbheroes; $t++) {
			$tempname=substr(strtolower($hero[$t]->name), 0, 5);
			trim($tempname);
			
			for ($a=1; $a<=count($Accents); $a++) {
				$tempname=str_replace($Accents[$a],$AccentsR[$a],$tempname);
			}
			$tempname=str_replace("-"," ",$tempname);
			$hero[$t]->handle=trim($tempname);
		}

		$entereddata = str_replace("§end§", "\r", $entereddata);
		$entereddata = trim($entereddata);
		$entereddata = str_replace("\n", "", $entereddata); //UNIX
		$entereddata = str_replace("\r\n", "§end§", $entereddata); //DOS
		$entereddata = str_replace("\r", "§end§", $entereddata); //MAC

		$tempentereddata=str_replace("§end§"," ",$entereddata);
		$pieces = explode("/", $tempentereddata);
		for ($ip=0; $ip<=count($pieces)-1; $ip++) {
			if ((is_numeric(substr($pieces[$ip], 0, 3))) && (substr($pieces[$ip], 3, 1)==" ")) {
				$searchhero=trim($pieces[$ip-1]);
				for ($t=1; $t<=$nbheroes; $t++) {
					if ($hero[$t]->handle==$searchhero) {
						$peopleorders=$hero[$t]->people;
					}
				}
			}
		}
		if ($peopleorders=="") {
			echo "<br><center><font class='rougeboldpadd'>".$loc[$loctag[2]+40][$lang]."</font>";
			die();
		}
		
		$savefn="Orders ".$game."-".$peopleorders.".csv";
		$fp = fopen ($basedir.$metoobasedir.'turn/orders/'.$savefn, "w");
		fwrite($fp, $entereddata);
		fclose($fp);
		echo "<br><font class='orangeBOLD'><b>".$loc[$loctag[2]+18][$lang]." (".$savefn.") !</b></font><br>";

		$fn=Inc("regions.csv");
		$fp = fopen ($fn, "r");
		$content = fread($fp, filesize($fn));
		fclose($fp);
		Decrunch($content);
		$mapregions=$cell;

		echo "<font class='jaunenormal'>";
		for ($t=1; $t<=$nbtowns; $t++) {
			if (($town[$t]->capital) && ($town[$t]->people==$peopleorders)) {
				$caphex=$town[$t]->hex;
			}
		}
		$text="";
		$nbherop=0;
		$piecesorders = explode("§end§", $entereddata);
		$nborders=count($piecesorders)-1;
		for ($ip=0; $ip<=$nborders; $ip++) {
			$pieces = explode("/", $piecesorders[$ip]);
			$searchhero=trim($pieces[0]);
			for ($t=1; $t<=$nbheroes; $t++) {
				if ($hero[$t]->handle==$searchhero) {
					$herop[$ip]=$t;
					
					$text.="[b]".$hero[$t]->name."[/b]";
					$text.=" at ".$hero[$t]->hex;
					if ($hero[$t]->commander) {
						$text.=" commander";
						$showherop[$ip]+=5;
					}
					if ($hero[$t]->hex==$caphex) {
						$text.=" (capitale)";
						$showherop[$ip]+=1000;
					} else {
						$x=substr($hero[$t]->hex,0,2)*1;
						$y=substr($hero[$t]->hex,2,2)*1;
						$reg[$t]=$mapregions[$y][$x];
						if ($reg[$t]!=0) {
							$text.=" (".$loc[$loctag[11]+$reg[$t]][$lang].")";
						} else {
							$text.=" ".$loc[$loctag[11]+24][$lang];
						}
						$showherop[$ip]+=$reg[$t]*10;
					}
					$text.=" ".$showherop[$ip];
					$text.="<br>";
				}
			}
		}
		$text.="<br>";
#		echo $text;

		for ($t=0; $t<=$nborders; $t++) {
			$max=-1;
			for ($t2=0; $t2<=$nborders; $t2++) {
				if ($showherop[$t2]>$max) {
					$max=$showherop[$t2];
					$k=$t2;
				}
			}
			$order_inorder[$t]=$k;
			$showherop[$k]=0;
		}

		$text="";
		for ($t=0; $t<=$nborders; $t++) {
			$t2=$herop[$order_inorder[$t]];
			$text.="[b]".$hero[$t2]->name."[/b]";
			$text.=" at ".$hero[$t2]->hex;
			if ($hero[$t2]->commander) {
				$text.=" commander";
			}
			if ($hero[$t2]->hex==$caphex) {
				$text.=" (capitale)";
			} else {
				$x=substr($hero[$t2]->hex,0,2)*1;
				$y=substr($hero[$t2]->hex,2,2)*1;
				$reg[$t2]=$mapregions[$y][$x];
				if ($reg[$t2]!=0) {
					$text.=" (".$loc[$loctag[11]+$reg[$t2]][$lang].")";
				} else {
					$text.=" ".$loc[$loctag[11]+24][$lang];
				}
			}
			$text.="<br>";
		}
#		echo $text;

		$fn=Inc("orders.csv");
		$fp = fopen ($fn, "r");
		if (filesize($fn)>0) { $ordersfile = fread($fp, filesize($fn)); }
		fclose($fp);
		Decrunch($ordersfile);
		$orders=$cell;
		$orderslin=$lin;
		
		$fn=Inc("names.csv");
		$fp = fopen ($fn, "r");
		if (filesize($fn)>0) { $namesfile = fread($fp, filesize($fn)); }
		fclose($fp);
		Decrunch($namesfile);
		$names=$cell;
		$nameslin=$lin;
		
		$fn=Inc("spells.csv");
		$fp = fopen ($fn, "r");
		$spellsfile = fread($fp, filesize($fn));
		fclose($fp);
		Decrunch($spellsfile);
		$spells=$cell;
		$spellslin=$lin;
		
		$previoushero="";
		echo "<br><hr><br><font class='jaunenormal'>";
		for ($ip=0; $ip<=$nborders; $ip++) {
			$t=$herop[$order_inorder[$ip]];
			$pieces = explode("/", $piecesorders[$order_inorder[$ip]]);
			if ($hero[$t]->name!=$previoushero) {
				$text="";
				if ($hero[$t]->hex==$caphex) {
					$currentregion="[u][b]Capitale[/b][/u]";
				} else {
					$x=substr($hero[$t]->hex,0,2)*1;
					$y=substr($hero[$t]->hex,2,2)*1;
					$reg[$t]=$mapregions[$y][$x];
					if ($reg[$t]!=0) {
						$currentregion="<br><br><br>[u][b]".$loc[$loctag[11]+$reg[$t]][$lang]."[/b][/u]";
					} else {
						$currentregion="<br><br><br>[u][b]".$loc[$loctag[11]+24][$lang]."[/b][/u]";
					}
				}
				if ($currentregion!=$currentregionmem) {
					$text.=$currentregion;
				}
				$currentregionmem=$currentregion;
				
				$text.="<br>[b]".$hero[$t]->name."[/b]";
				if ($hero[$t]->command>20) { $text.=" ".$loc[$loctag[4]+25][$lang]."&nbsp;".$hero[$t]->command; }
				if ($hero[$t]->agent>20) { $text.=" ".$loc[$loctag[4]+26][$lang]."&nbsp;".$hero[$t]->agent; }
				if ($hero[$t]->emissary>20) { $text.=" ".$loc[$loctag[4]+27][$lang]."&nbsp;".$hero[$t]->emissary; }
				if ($hero[$t]->mage>20) { $text.=" ".$loc[$loctag[4]+28][$lang]."&nbsp;".$hero[$t]->mage; }
				if (($hero[$t]->health<100) && ($hero[$t]->health>0)) { $text.=" ".$loc[$loctag[4]+29][$lang]."&nbsp;".$hero[$t]->health; }
				if ($hero[$t]->stealth>0) { $text.=" ".$loc[$loctag[4]+30][$lang]."&nbsp;".$hero[$t]->stealth; }
				if ($hero[$t]->challenge>50) { $text.=" ".$loc[$loctag[4]+31][$lang]."&nbsp;".$hero[$t]->challenge; }
				$text.=" ".$loc[$loctag[2]+44][$lang]." ".$hero[$t]->hex;
				if ($hero[$t]->hex!=$caphex) {
					for ($v=1; $v<=$nbtowns; $v++) {
						if ($town[$v]->hex==$hero[$t]->hex) {
							$text.=" (".$town[$v]->name;
							if ($town[$v]->people!=$hero[$t]->people) {
								$text.=", ".$loc[$loctag[1]+$town[$v]->people][$lang];
							}
							$text.=")";
						}
					}
				}
				if ($hero[$t]->commander) {
					for ($t2=1; $t2<=$nbarmies; $t2++) {
						if ($army[$t2]->leadername==$hero[$t]->name) {
							$text.=" (".$loc[$loctag[5]+4][$lang];
							for ($t3=1; $t3<=7; $t3++) {
								if ($army[$t2]->unitsnb[$t3]>=100) { $text.=" ".RoundIt($army[$t2]->unitsnb[$t3],100).$loc[$loctag[7]+55+$t3][$lang]; } else {
									if ($army[$t2]->unitsnb[$t3]!=0) { $text.=" ".RoundIt($army[$t2]->unitsnb[$t3],10).$loc[$loctag[7]+55+$t3][$lang]; }
								}
							}
							$text.=")";
						}
					}
				}
				$text.="<br>";
				echo $text;
			}
			
			echo substr($pieces[1], 0, 3)." ";
			$orderfound=false;
			for ($to=1; $to<=$orderslin; $to++) {
				if ($orders[$to][1]==substr($pieces[1], 0, 3)) {
					$orderfound=true;
					$temporder=$orders[$to][$lang+1];
					if (strpos($temporder, "§")) {
						$temporderpieces=explode("§", $temporder);
						$temporder="";
						for ($tp=0; $tp<=count($temporderpieces)-1; $tp++) {
							if (strpos($temporderpieces[$tp], "~")) {
								$temporderpieces2=explode("~", $temporderpieces[$tp]);
								$place=$temporderpieces2[1];
								switch ($temporderpieces2[0]) {
									case "name" :
										$temporderpieces[$tp]=$pieces[1+$place];
										$found=false;
										for ($t2=1; $t2<=$nameslin; $t2++) {
											if ($names[$t2][1]==$pieces[1+$place]) {
												$found=true;
											}
										}
										if (!strpos($pieces[1+$place], " ")) {
											$found=true;
										}
										if (!$found) {
											$nameslin++;
											$names[$nameslin][1]=$pieces[1+$place];
										}
									break;
									case "m" :
										$temporderpieces[$tp]=$pieces[1+$place];
										if ($temporderpieces[$tp]=="") { $temporderpieces[$tp]="-"; }
									break;
									case "v" :
										$temporderpieces[$tp]=$pieces[1+$place];
										$search=trim($pieces[1+$place]);
										for ($t2=1; $t2<=$nbtowns; $t2++) {
											if ($town[$t2]->hex==$search) {
												$temporderpieces[$tp].=" (".$town[$t2]->name;
												if ($town[$t2]->people!=$hero[$t]->people) {
													$temporderpieces[$tp].=", ".$loc[$loctag[1]+$town[$t2]->people][$lang];
												}
												$temporderpieces[$tp].=")";
											}
										}
									break;
									case "s" :
										$search=substr($pieces[1+$place], 0, 3);
										for ($t2=1; $t2<=$spellslin; $t2++) {
											if ($spells[$t2][1]==$search) {
												$temporderpieces[$tp]=$spells[$t2][$lang+1];
											}
										}
									break;
									case "c" :
										$search=$hero[$t]->hex;
										for ($t2=1; $t2<=$nbtowns; $t2++) {
											if ($town[$t2]->hex==$search) {
												$temporderpieces[$tp]=$town[$t2]->name;
											}
										}
									break;
									case "n" :
										$searchnation=trim(substr($pieces[1+$place], 0, 2));
										$temporderpieces[$tp]=$loc[$loctag[1]+$searchnation][$lang];
									break;
									case "a" :
										$space=strpos($pieces[1+$place], " ");
										$search=trim(substr($pieces[1+$place], 0, $space));
										$temporderpieces[$tp]=$artefactname[$search][$lang];
									break;
									case "pn" :
									case "p" :
										$charfound=false;
										$searchhero2=trim($pieces[1+$place]);
										for ($t2=1; $t2<=$nbheroes; $t2++) {
											if ($hero[$t2]->handle==$searchhero2) {
												$temporderpieces[$tp]=$hero[$t2]->name;
												if ($temporderpieces2[0]=="pn") {
													$temporderpieces[$tp].=" (".$loc[$loctag[1]+$hero[$t2]->people][$lang].")";
												}
												$charfound=true;
											}
										}
										if (!$charfound) {
												$temporderpieces[$tp]=trim($pieces[1+$place])."??";
										}
									break;
									case "move" :
										$moveorder="";
										$x=substr($hero[$t]->hex,0,2)*1;
										$y=substr($hero[$t]->hex,2,2)*1;
										for ($t2=1; $t2<=$place; $t2++) {
											if ($pieces[1+$t2]!="") {
												if ($t2!=1) {
													$moveorder.=",";
												}
												for ($msearch=1; $msearch<=7; $msearch++) {
													if ($pieces[1+$t2]==$ArmyMoves[$msearch]) {
														$moven=$msearch;
													}
												}
												if (1&$y) {
													$x+=$ArmyMoveXodd[$moven];
												} else {
													$x+=$ArmyMoveXeven[$moven];
												}
												$y+=$ArmyMoveY[$moven];
												$moveorder.=$pieces[1+$t2];
											}																	
										}
										if ($x<10) { $x="0".$x; }
										if ($y<10) { $y="0".$y; }
										$moveorder.=" (".$x.$y;
										for ($v=1; $v<=$nbtowns; $v++) {
											if ($town[$v]->hex==$x.$y) {
												$moveorder.=": ".$town[$v]->name;
												if ($town[$v]->people!=$hero[$t]->people) {
													$text.=", ".$loc[$loctag[1]+$town[$v]->people][$lang];
												}
											}
										}
										$moveorder.=")";
										$temporderpieces[$tp]=$moveorder;
									break;
									case "army" :
										$armyorder="";
										for ($t2=1; $t2<=6; $t2++) {
											if ($pieces[1+1+$t2]!="0") {
												$armyorder.=$pieces[1+1+$t2].$loc[$loctag[7]+55+$t2][$lang]." ";
											}
										}
										$temporderpieces[$tp]=$armyorder;
									break;
									case "carac" :
										$caracorder="";
										for ($t2=1; $t2<=4; $t2++) {
											if ($pieces[1+2+$t2]!="0") {
												$caracorder.=$loc[$loctag[4]+24+$t2][$lang]." ".$pieces[1+2+$t2]." ";
											}
										}
										$temporderpieces[$tp]=$caracorder;
									break;
								}
							}
							$temporder.=$temporderpieces[$tp];
						}
					}
					echo $temporder;
				}
			}
			if (!$orderfound) {
				echo " ".$loc[$loctag[2]+43][$lang];
			}
			echo "<br>";
			$previoushero=$hero[$t]->name;
		}
		echo "<br><hr><br>";
		
		
		$namessave="";
		for ($i=1; $i<=$nameslin; $i++) {
			$namessave.=$names[$i][1].";".chr(10);
		}
		$savefn="names.csv";
		$fp = fopen ($basedir.$metoobasedir.'data/'.$savefn, "w");
		fwrite($fp, $namessave);
		fclose($fp);

	}

	$basedir = $DOCUMENT_ROOT;
	$filenb=0;
	MakeList($metoobasedir.'turn/orders/');
	$ordersnb=1;
	for ($nbf=1; $nbf<=$filenb; $nbf++) {
		if (strpos($filename[$nbf], "rders ".$game."-")!=false) {
			$ordersnb++;
		}
	}

	$savefn="Orders ".$game."-?.csv";
?>
<br>
<form action="enterorders.php" method="post">
	<input type="hidden" name="mode" value="enter">
	<input type="hidden" name="game" value="<? echo $game; ?>">
	<input type="hidden" name="ordersnb" value="<? echo $ordersnb; ?>">
	<font class=jaunenormal><? echo $loc[$loctag[2]+36][$lang]; ?> :<br>
	<textarea name="entereddata" cols=100 rows=20 tabindex=1></textarea>
	<br>
<?
	echo $loc[$loctag[2]+16][$lang]." : ".$savefn."<br>";
?>
	<input type="submit" value="<? echo $loc[$loctag[2]+12][$lang]; ?>" tabindex=2>
</form>
<? include Inc("metoobottom.inc.php"); ?>