<html>
<head>
<title>2 of these people are lieing</title>
<style>
.errorbox{
	display: none;
	background-color: rgb(200,0,0);
	color: white;
	padding: 4px;
	position: relative;
	width: 30%;
}
.adminform, .viewnamebox{
	border: 2px solid black;
	padding: 10px;
	position: relative;
	width: 30%;
}
.viewnamebox{
	display: none;
}
</style>
</head>
<body>
<?php
$password="1323";
//SET PASSWORD ABOVE
$errorlvl="";
if(file_exists("2otpal.json")==false){
	$file = fopen("2otpal.json","w");
	fwrite($file,"{\n	\"names\":[\n			\n	],\n	\"maxplayers\":\"4\"\n}");
	fclose($file);
	$output="2otpal.json did not exist, creating";
	echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}
$string = file_get_contents("2otpal.json");
$Jfile = json_decode($string, true);
if($_POST['doing']=="add"&&$_POST['Name']!=""){
	if(count($Jfile['names'])<intval($Jfile['maxplayers'])-1){
		$file=fopen("2otpal.json","w");
		fwrite($file,"{\n	\"names\":[");
		for($i=0;$i<count($Jfile['names']);$i++){
			fwrite($file,"\n			\"".$Jfile['names'][$i]."\",");
		}
		fwrite($file,"\n			\"".$_POST['Name']."\"");
		fwrite($file,"\n	],\n	\"maxplayers\":\"".$Jfile['maxplayers']."\"\n}");
		fclose($file);
	} else {
		$errorlvl="max names have been reached";
	}
}
if($_POST['doing']=="reset names"){
	if($_POST['password']==$password){
		$file = fopen("2otpal.json","w");
		fwrite($file,"{\n	\"names\":[\n			\n	],\n	\"maxplayers\":\"".$Jfile['maxplayers']."\"\n}");
		fclose($file);
	} else {
		$errorlvl="password is not correct";
	}
}
if($_POST['doing']=="set players"){
	if($_POST['password']==$password){
		$file=fopen("2otpal.json","w");
		fwrite($file,"{\n	\"names\":[");
		for($i=0;$i<count($Jfile['names']);$i++){
			if($i<count($Jfile['names'])-1){
				fwrite($file,"\n			\"".$Jfile['names'][$i]."\",");
			} else {
				fwrite($file,"\n			\"".$Jfile['names'][$i]."\"");
			}
		}
		fwrite($file,"\n	],\n	\"maxplayers\":\"".$_POST['maxplayers']."\"\n}");
	} else {
		$errorlvl="password is not correct";
	}
}
$string = file_get_contents("2otpal.json");
$Jfile = json_decode($string, true);
$_POST['password']=="";
$_POST['doing']=="";
?>

<div align="center">
<h1>this is a page for my game by tom scott, <br> two of these people are lying</h1>
<p> see his playlist <a href="https://www.youtube.com/watch?v=3UAOs9B9UH8&list=PLfx61sxf1Yz2I-c7eMRk9wBUUDCJkU7H0" target="_blank">here</a></p><br><br>
<div class="errorbox" id="errorbox"></div><br>
<form method="post" target="_self">
<input autocomplete="off" type="hidden" name="doing" value="add" />
wiki page name: <input autocomplete="off" type="text" name="Name" /><br><br>
<input autocomplete="off" type="submit" value="submit">
</form>
<br><button onclick="viewnames()">view wiki names</button><br><br>
<div id="viewnamebox" class="viewnamebox"></div>
<br><br>
<p>admin/host stuff below</p>
<form class="adminform" method="post" target="_self">
<select name="doing">
	<option value="reset names">reset names</option>
	<option value="set players">set players</option>
</select><br><br>
password: <input autocomplete="off" type="text" name="password"/><br><br>
players: (if setting players): <input autocomplete="off" type="number" max="100" name="maxplayers"/><br><br>
<input type="submit" value="submit">
</form>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
	if("<?php echo $errorlvl; ?>"!=""){
		$("#errorbox").css("display","block");
		$("#errorbox").html("<?php echo $errorlvl; ?>");
	}
	function viewnames(){
		var getnames = <?php echo json_encode($Jfile['names'], JSON_PRETTY_PRINT) ?>;
		$("#viewnamebox").css("display","block");
		text="";
		for(i=0; i<getnames.length; i++){
			text=text+getnames[i]+"<br>";
		}
		$("#viewnamebox").html(text);
	}
</script>
</body>
</html>