Location: PHPKode > projects > Blackbeard's Revenge > chargen.php
<?php
session_start(); 
header("Cache-control: private");

include("headers.php");
require_once "common.php";

if($_POST["firsttime"])
{	
	generateStats();
	$_SESSION['playerlevel'] = 1;
	$_SESSION['turns']       = 20;
	$_SESSION['retries'] = 3;
	Display();
	$_POST["firsttime"] = false;
}

elseif($_POST["submit"] == "Reroll")
{
	$_SESSION['retries'] = $_SESSION['retries'] - 1;
	if($_SESSION['retries'] > 0)
		generateStats();
	else $_SESSION['retries'] = 0;

	Display();
	
}
else if($_POST["submit"] == "Accept")
{

	if(checkPassword())
	{
		processForm();

		$sql = "INSERT INTO players 
						(name,
						password,
						sex,
						level,
						gold,
						shipname,
						experience,
						age,
						charisma,
						gunnery,
						sailing,
						maps,
						country,
						lastip,
						lastlogin,
						turns
					) VALUES (					
						'".$_SESSION['playername']."',
						'".$_POST["password"]."',
						'".$_SESSION['mygender']."',
						".$_SESSION['playerlevel'].",
						0,
						'".$_SESSION['myshipname']."',
						".$_SESSION['experience'].",
						".$_SESSION['myage'].",
						".$_SESSION['mycharisma'].",
						".$_SESSION['mygunnery'].",
						".$_SESSION['mysailing'].",
						0,
						".$_SESSION['mycountry'].",
						'".$_SERVER['REMOTE_ADDR']."',
						'".date("Y-m-d H:i:s",strtotime("-1 day"))."',
						".$_SESSION['turns']."
					)";

			if($DEBUG) echo $sql;
			db_query($sql) or die(db_error(LINK));
			if (db_affected_rows(LINK)<=0){
				output("`\$Error`^: Your account was not created for an unknown reason, please try again. ");
			}
			else
			{

				$query = "SELECT * FROM players WHERE players.name = '". $_SESSION['playername'] ."'";
				$result = db_query($query);
				$row = db_fetch_assoc($result);

				$playernum = $row["uid"];
				$_SESSION['uid'] = $row["uid"];

				$sql = "INSERT INTO ships 
						(ownerid,
						classname,
						shiptype,
						cannon,
						armour,
						manuver,
						hull,
						cargo,
						value,
						crew
					) VALUES (					
						$playernum,
						'".$_SESSION['myshipclass']."',	
						".$_SESSION['playershipnum'].",
						".$_SESSION['mycannons'].",
						".$_SESSION['myarmor'].",						
						".$_SESSION['myshipmanuver'].",
						".$_SESSION['myshiphull'].",
						0,
						".$_SESSION['myshipvalue'].",
						".$_SESSION['myshipcrew'].")";
					if($DEBUG) echo $sql;
					db_query($sql) or die(db_error(LINK));
					if (db_affected_rows(LINK)<=0){
						output("`\$Error`^: Your account was not created for an unknown reason, please try again. ");
					}
			}
	}
	else
	{
		Display();
	}
	echo "<script>if (top.location != location) {top.location.href = document.location.href;}</script>";
	echo "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=index.php?module=2\" target=_top\">";
}
else if($_POST["submit"] == "Quit")
{
	echo "<script>if (top.location != location) {top.location.href = document.location.href;}</script>";
	echo "<META HTTP-EQUIV=\"refresh\" content=\"1;URL=index.html\" target=_top\">";
}
else 
{
	print "Error generating page";
}

function checkPassword()
{
	if($_POST["password"] == "" || $_POST["name"] == "")
		return false;
	if($_POST["password"] == $_POST["confirm"])
		return true;
	else
		return false;
}

function generateStats()
{
	$_SESSION['myage']      = mt_rand(18, 27);
		if($_SESSION['myage'] > 21 && $_SESSION['myage'] <= 25) $_SESSION['bonus'] = 10;
		else if($_SESSION['myage'] > 25 && $_SESSION['myage'] <= 27) $_SESSION['bonus'] = 15;
		else $_SESSION['bonus'] = 5;
	$_SESSION['mycharisma']  = mt_rand(5, 25);
	$_SESSION['mygunnery']   = mt_rand(5, 25);
	$_SESSION['mysailing']   = mt_rand(5, 25);
	
	
}

function processForm()
{
	//player stats/defaults (in database at some point)
	//Player variables

	$_SESSION['playername'] = $_POST["name"];
	$_SESSION['realname'] = "Test Man";
	
	if($_POST["playersex"] == 2) $_SESSION['mygender']   = "f";
	else  $_SESSION['mygender']   = "m";
	
	$_SESSION['mycountry']  = $_POST["playershipcountry"];

	processNation();
	
	//add bonuses
	if($_POST["corestats"] == "charisma")
		$_SESSION['mycharisma']  = $_SESSION['mycharisma'] + $_SESSION['bonus'];
	else if($_POST["corestats"] == "gunnery")
		$_SESSION['mygunnery']   = $_SESSION['mygunnery']  + $_SESSION['bonus'];
	else if($_POST["corestats"] == "sailing")
		$_SESSION['mysailing']   = $_SESSION['mysailing']  + $_SESSION['bonus'];
	else {
		$iBonus = round(($_SESSION['bonus'] / 3));
		$_SESSION['mycharisma']  = $_SESSION['mycharisma'] + $iBonus;
		$_SESSION['mygunnery']   = $_SESSION['mygunnery']  + $iBonus;
		$_SESSION['mysailing']   = $_SESSION['mysailing']  + $iBonus;
		}

	$_SESSION['experience'] = 0;
}

function processNation()
{	
	//Process starting gold per nationality
	//English
	if($_SESSION['mycountry'] == 1) {
		$_SESSION['playershipnum'] = 10;
		$_SESSION['myshipname']    = "HMS Defiant";
		$_SESSION['playergold']    = 60;
		$_SESSION['playermaps']    = 1;}
	//Spanish
	else if($_SESSION['mycountry'] == 2) {
		$_SESSION['playershipnum'] = 9;
		$_SESSION['myshipname']    = "Santa Maria";
		$_SESSION['playergold']    = 600;
		$_SESSION['playermaps']    = 4;}
	//French
	else if($_SESSION['mycountry'] == 3) {		
		$_SESSION['playershipnum'] = 5;
		$_SESSION['myshipname']    = "Aventure";
		$_SESSION['playergold']    = 50;
		$_SESSION['playermaps']    = 2;}
	//Dutch
	else {		
		$_SESSION['playershipnum'] = 4;
		$_SESSION['myshipname']    = "Rondtrekken";
		$_SESSION['playergold']    = 250;
		$_SESSION['playermaps']    = 32;}


	$query = "SELECT * FROM shiptype WHERE id = ".$_SESSION['playershipnum']." ";
				$result = db_query($query);	
				$row = db_fetch_assoc($result);

	//ship info	 
	$_SESSION['myshipclass']   = $row["class"];
	$_SESSION['myshiphull']    = $row["maxhull"];
	$_SESSION['myshipmaxhull'] = $row["maxhull"];
	$_SESSION['mycannons']     = $row["basecannon"];
	$_SESSION['myarmor']       = $row["basearmour"];
	$_SESSION['myshipmanuver'] = $row["basemanuver"]; 
	$_SESSION['myshipcrew']	   = $row["mincrew"];
	$_SESSION['myshipvalue']   = $row["basevalue"];
	$_SESSION['turns'] = 10;
	}

function Display()
{
print "<div align=center>";
print "<form action=\"chargen.php\" method=\"POST\">";
print "<table class=\"menu\" border=0>";
print "	<tr><td class=\"menuheader\" colspan=3>Character creation</td></tr>";
print "	<tr><td colspan=3><div align=center><table class=\"data\" border=0>";
print "	<tr><td class=\"header\" colspan=3>Name</td></tr>";
print "	<tr><td></td><td class=\"body\"><input type=\"text\" name=\"name\" class=\"login\" value=\"".$_POST["name"]."\"></td> <td> </td></tr>";
print "	<tr><td class=\"header\" colspan=3>Age</td></tr>";
print "	<tr><td></td><td class=\"body\">". $_SESSION['myage'] ."</td> <td> </td></tr>";
print "	<tr><td colspan=3 class=\"header\">Stats</td></tr>";
print "	<tr><td class=\"header2\">Bonus</td><td class=\"header2\" style=\"text-align:center\">". $_SESSION['bonus'] ."</td><td class=\"header2\">Improve</td>";
print "	<tr><td class=\"body\">Charisma</td><td class=\"body\">". $_SESSION['mycharisma'] ."</td><td align=\"center\"><input type=\"radio\" name=\"corestats\" value=\"charisma\"></td></tr>";
print "	<tr><td class=\"body\">Gunnery Skill</td><td class=\"body\">". $_SESSION['mygunnery'] ."</td><td align=\"center\"><input type=\"radio\" name=\"corestats\" value=\"gunnery\"></td></tr>";
print "	<tr><td class=\"body\">Sailing Skill</td><td class=\"body\">". $_SESSION['mysailing'] ."</td><td align=\"center\"><input type=\"radio\" name=\"corestats\" value=\"sailing\"></td></tr>";
print "	<tr></tr>";
print "	<tr><td class=\"header\">Nationality</td><td class=\"header\"></td><td class=\"header\">Sex</td></tr>";
print "	<tr><td>";
print "	<SELECT NAME=\"playershipcountry\">";
print "	<OPTION VALUE=\"1\" SELECTED>English</OPTION>";
print "	<OPTION VALUE=\"2\">Spanish</OPTION>";
print "	<OPTION VALUE=\"3\">French</OPTION>";
print "	<OPTION VALUE=\"4\">Dutch</OPTION>";
print "	</SELECT></td><td> </td>";
print "	<td>";
print "	<SELECT NAME=\"playersex\">";
print "	<OPTION VALUE=\"1\" SELECTED>Male</OPTION>";
print "	<OPTION VALUE=\"2\">Female</OPTION>	";
print "	</SELECT></td>";
print "	</tr></div>";
print "	</table></td></tr>";

print "	<tr><td align=\"center\" colspan=3><div align=center><table class=\"data\" style=\"width:150\">";
print "	<tr><td class=\"header2\">Retries:</td><td class=\"data\" style=\"text-align:center\">".$_SESSION['retries']."</td></tr>";
print "</table></div></td></tr>";


print "	<tr><td align=\"center\" colspan=3><div align=center><table class=\"data\" style=\"width:150\">";
print "	<tr><td class=\"header\">Password</td><td class=\"header\">Confirm</td></tr>";
print "	<tr><td class=\"body\"><input type=\"password\" name=\"password\" class=\"login\" value=\"".$_POST["password"]."\"></td> ";
print "	<td class=\"body\"><input type=\"password\" name=\"confirm\" class=\"login\" value=\"".$_POST["confirm"]."\"></td></tr>";
print "</table></div></td></tr>";
print "<tr><td><input class=\"button\" type=\"Submit\" name=\"submit\" value=\"Reroll\" ";
if($_SESSION['retries'] < 1)
		echo "disabled";
else
		echo " ";
echo "></td>";
print "<td><input class=\"button\" type=\"Submit\" name=\"submit\" value=\"Accept\"></td>";
print "<td><input class=\"button\" type=\"Submit\" name=\"submit\" value=\"Quit\"></td>";
print "</tr>";
print "</table>";
print "</form>";
print "</div>";


}
Return current item: Blackbeard's Revenge