<? 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"); ?>
<html>
<head><title>MeToo : Middle-Earth Tool for Overcoming Opposition</title></head>
<?
	if (!$goingtologin) { include Inc("testlogin.php");	}
?>
<? $where="input_data_enter"; include Inc("checkgameaccess.inc.php"); ?>
<? include Inc("loaddata.inc.php"); ?>
<?
	if ($action!="") {
		$game=$game_checked;
		$fnsave="Data ".$game.".csv";
		$fnsave = $basedir.$metoobasedir.'turn/data/'.$fnsave;

		if (file_exists($fnsave)) {
/*			$fp = fopen ($fnsave, "r");
			$data = fread($fp, filesize($fnsave));
			fclose($fp);
			Decrunch($data);
			$data=$cell;
			$datalin=$lin;
			$datacol=$col;*/
		
/*			for ($j=1; $j<=$lin; $j++) {
				$move_player[$j]=$cell[$j][1];
				$move_type[$j]=$cell[$j][2];
				$move_people[$j]=$cell[$j][3];
				$move_details[$j]=$cell[$j][4];
				for ($i=5; $i<=$col; $i++) {
					$move_turn[$j][$i-4]=$cell[$j][$i];
				}
			}*/
		} else {
			for ($i=1; $i<=5; $i++) {
				$datalin++;
				$data[$datalin][1]=$turntodisplay;
				$data[$datalin][2]="neutral";
				$data[$datalin][3]=$i;
				$data[$datalin][4]=0;
				$data[$datalin][5]=0;
			}
		}

		$datalin++;
		$data[$datalin][1]=$turntodisplay;
		switch ($action) {
			case "addtown" :
			case "modifytown" :
				if ($action=="addtown") {
					$data[$datalin][2]="townadded";
				} else {
					$data[$datalin][2]="townmodified";
				}
				$townname=str_replace("\'", "`", $townname);
				$townname=str_replace("'", "`", $townname);
				$data[$datalin][3]=$townname;
				$data[$datalin][4]=$hex;
				$data[$datalin][5]=$people;
				$data[$datalin][6]=$size;
				$data[$datalin][7]=$fortif;
				$data[$datalin][8]=$loyalty;
				$data[$datalin][9]=$dock;
				$data[$datalin][10]=$hidden;
				$datacol=max($datacol, 10);
			break;
			case "destroytown" :
				$data[$datalin][2]="towndestroyed";
				$data[$datalin][3]=$hex;
				$data[$datalin][4]=$people;
				$datacol=max($datacol, 4);
			break;
			case "addarmy" :
			case "modifyarmy" :
				if ($action=="addarmy") {
					$data[$datalin][2]="armyadded";
				} else {
					$data[$datalin][2]="armymodified";
				}
				$data[$datalin][3]=$people;
				$commandername=str_replace("\'", "`", $commandername);
				$commandername=str_replace("'", "`", $commandername);
				$data[$datalin][4]=$commandername;
				$data[$datalin][5]=$hex;
				$data[$datalin][6]=$morale;
				for ($i=1; $i<=7; $i++) {
					$data[$datalin][6+$i]=$unitnb[$i];
				}
				$data[$datalin][6+7+1]=0;
				for ($i=1; $i<=6; $i++) {
					$data[$datalin][6+7+1+$i]=$training[$i];
				}
				for ($i=1; $i<=6; $i++) {
					$data[$datalin][6+7+1+6+$i]=$weapons[$i];
				}
				for ($i=1; $i<=6; $i++) {
					$data[$datalin][6+7+1+6+6+$i]=$armors[$i];
				}
				$data[$datalin][6+7+1+6+6+6+1]=$commanderlvl;
				$data[$datalin][6+7+1+6+6+6+2]=$offmagic;
				$data[$datalin][6+7+1+6+6+6+3]=$defmagic;
				$data[$datalin][6+7+1+6+6+6+4]=$intel;
				$datacol=max($datacol, 6+7+1+6+6+6+4);
			break;
			case "modifyracearmy" :
				$data[$datalin][2]="armyracemodified";
				$data[$datalin][3]=$people;
				$commandername=str_replace("\'", "`", $commandername);
				$commandername=str_replace("'", "`", $commandername);
				$data[$datalin][4]=$commandername;
				$data[$datalin][5]=$hex;
				$data[$datalin][6]=$commanderlvl;
				$data[$datalin][7]=$offmagic;
				$data[$datalin][8]=$defmagic;
				$data[$datalin][9]=$formerpeople;
				$data[$datalin][10]=$formercommandername;
				$datacol=max($datacol, 10);
			break;
			case "movearmy" :
				for ($i=1; $i<=$nbarmies; $i++) {
					if (($army[$i]->hex==$hex) && ($army[$i]->people==$people) && ($army[$i]->leadername==$leader)) {
						$data[$datalin][2]="armymoved";
						$data[$datalin][3]=$people;
						$data[$datalin][4]=$leader;
						$data[$datalin][5]=$newhex;
						$data[$datalin][6]=$hex;
						$datacol=max($datacol, 6);
					}
				}
			break;
			case "destroyarmy" :
				for ($i=1; $i<=$nbarmies; $i++) {
					if (($army[$i]->hex==$hex) && ($army[$i]->people==$people) && ($army[$i]->leadername==$leader)) {
						$data[$datalin][2]="armydestroyed";
						$data[$datalin][3]=$people;
						$data[$datalin][4]=$leader;
						$data[$datalin][5]=$hex;
						$datacol=max($datacol, 5);
					}
				}
			break;
			case "addhero" :
				$data[$datalin][2]="heroadded";
				$data[$datalin][3]=$people;
				$name=str_replace("\'", "`", $name);
				$name=str_replace("'", "`", $name);
				$data[$datalin][4]=$name;
				$data[$datalin][5]=$hex;
				$datacol=max($datacol, 5);
			break;
			case "movehero" :
				for ($i=1; $i<=$nbheroes; $i++) {
					if (($hero[$i]->hex==$hex) && ($hero[$i]->name==$name)) {
						$data[$datalin][2]="heromoved";
						$data[$datalin][3]=$people;
						$data[$datalin][4]=$name;
						$data[$datalin][5]=$newhex;
						$data[$datalin][6]=$hex;
						$datacol=max($datacol, 6);
					}
				}
			break;
			case "destroyhero" :
				for ($i=1; $i<=$nbheroes; $i++) {
					if (($hero[$i]->hex==$hex) && ($hero[$i]->name==$name)) {
						$data[$datalin][2]="herodestroyed";
						$data[$datalin][3]=$people;
						$data[$datalin][4]=$name;
						$data[$datalin][5]=$hex;
						$datacol=max($datacol, 5);
					}
				}
			break;
			case "addartefact" :
				$data[$datalin][2]="artefactadded";
				$data[$datalin][3]=$num;
				$data[$datalin][4]=$hex;
				$datacol=max($datacol, 4);
			break;
			case "moveartefact" :
				for ($i=1; $i<=$nbartefact; $i++) {
					if (($artefact[$i]->hex==$hex) && ($artefact[$i]->num==$num)) {
						$data[$datalin][2]="artefactmoved";
						$data[$datalin][3]=$num;
						$data[$datalin][4]=$newhex;
						$data[$datalin][5]=$hex;
						$datacol=max($datacol, 5);
					}
				}
			break;
			case "destroyartefact" :
				for ($i=1; $i<=$nbartefact; $i++) {
					if (($artefact[$i]->hex==$hex) && ($artefact[$i]->num==$num)) {
						$data[$datalin][2]="artefactdestroyed";
						$data[$datalin][3]=$num;
						$data[$datalin][4]=$hex;
						$datacol=max($datacol, 4);
					}
				}
			break;
			case "addmark" :
				$data[$datalin][2]="markadded";
				$data[$datalin][3]=$num;
				$data[$datalin][4]=$hex;
				$datacol=max($datacol, 4);
			break;
			case "destroymark" :
				for ($i=1; $i<=$nbmark; $i++) {
					if (($mark[$i]->hex==$hex) && ($mark[$i]->num==$num)) {
						$data[$datalin][2]="markdestroyed";
						$data[$datalin][3]=$num;
						$data[$datalin][4]=$hex;
						$datacol=max($datacol, 4);
					}
				}
			break;
		}
		
		$savefile="";
		for ($i=1; $i<=$datalin; $i++) {
			if (($data[$i][1]>"") || ($data[$i][1]=="0")) {
				for ($j=1; $j<=$datacol; $j++) {
					if ($j!=1) {
						$savefile.=";";
					}
					$savefile.=$data[$i][$j];
				}
				$savefile.=chr(10);
			}
		}
		
		$fp = fopen ($fnsave, "w");
		fwrite($fp, $savefile);
		fclose($fp);
		
		$idt=explode(".", $id);
		$hex="";
		if ($idt[0]<10) { $hex.="0"; } $hex.=$idt[0];
		if ($idt[1]<10) { $hex.="0"; } $hex.=$idt[1];
		
		echo "<center><font size=-1><b>".$loc[$loctag[4]+15][$lang]." ".$hex."</b></font><hr>";
		
		echo "<b>".$loc[$loctag[2]+18][$lang];
		echo "<form action='input_data.inc.php' method='post'>";
		echo "<td width=150><center>";
		echo "<input type='hidden' name='id' value='$id'>";
		echo "<input type='hidden' name='game_checked' value='$game'>";
		echo "<input type='submit' value='".$loc[$loctag[9]+45][$lang]."'>";
		echo "</form>";
	}
?>
<? include Inc("metoobottom.inc.php"); ?>