Page 1 of 1

Anyone know PHP (or be willing to learn?)

Posted: Fri Aug 08, 2008 1:54 pm
by The Mindset
I know a few of you do/did computer science degrees here. PHP is a very simple language, with probably the best documentation other than MS's .NET stuff. If you know C/C++ you can probably pick up PHP in about an hour. The syntax is very standard, php.net has excellent primers on using code and I can help you with any queries you may have.

I'm not asking for advanced programming skills. Anything you cannot do (either because you don't understand my code or because you're not familiar with the methods I'm using - this especially applies to session security and database maintainence) - I can do.

So I'm making an appeal to any coders out there with some free time. I've included some code below that's part of the mapping code. If you can explain what it means in plain terms you might be able to help me!

Code: Select all

// Set this to false if the public can view this page.
$private_page = false;
include ('common/config.php');
include ('common/sessions.php');

// BEGIN MAIN CODE

	$map = new Map_Management();
	$gd = new GD_Management();
	
	// Get random system
	if ($_SESSION['logged_in'] == true) {
		if ($_GET['x'] || $_GET['y']) {
			$x = $_GET['x'];
			$y = $_GET['y'];
			$smarty->assign('x', $x);
			$smarty->assign('y', $y);
		}
		else {
			$system = $map->random_system($nation_id);
			$x = $system['x'];
			$y = $system['y'];	
			$smarty->assign('x', $x);
			$smarty->assign('y', $y);
		}
	}
	else {
		if ($_GET['x'] || $_GET['y']) {
			$x = $_GET['x'];
			$y = $_GET['y'];
			$smarty->assign('x', $x);
			$smarty->assign('y', $y);
		}
		else {
			$x = 500;
			$y = 500;
			$smarty->assign('x', $x);
			$smarty->assign('y', $y);
		}
	}
	if ($_GET['s']) {
		$x = $_GET['x'];
		$y = $_GET['y'];
		$system_id = $_GET['s'];
		$smarty->assign('system', 'true');
		$smarty->assign('y', $y);
		$smarty->assign('s', $s);
		$smarty->assign('x', $x);
	}
	// Variables
	
	switch ($_GET['mode']) {
		case 'img':
			$tiledimensions = 40;
			$mapdimension = 16;
			$src_img = "templates/map/blank.png";
			$image = imagecreatefrompng($src_img);
			
			// Colours
			$white = imagecolorallocate($image, 255, 255, 255);
			$influence = imagecreatefrompng("templates/map/influence.png");
						
			// Get map data
			$x = $_GET['x'];
			$y = $_GET['y'];
			$tile_array = $map->tile_array($x - 8, $y - 8, (16 + $x), (16 + $y));
			if (empty($tile_array)) {
				$tile_array[0] = 0;
			}
			
			foreach($tile_array as $coord_data) {
				$coords = $gd->convert_coords($x - 8, $y - 8, $coord_data['x'], $coord_data['y']);
				
				$sx = $coords['x'];
				$sy = $coords['y'];
				imagecolortransparent($influence,imagecolorat($influence,0,0));
				imagecopymerge($image, $influence, $sx - 20, $sy - 20, 0, 0, 80, 80, 35);
			}
			
			foreach($tile_array as $coord_data) {
				$coords = $gd->convert_coords($x - 8, $y - 8, $coord_data['x'], $coord_data['y']);
				
				$sx = $coords['x'];
				$sy = $coords['y'];
				
				if (substr($coord_data['type'], 0, 3) == "sun") {
					$type = $gd->sun_type($coord_data['type']);
					imagecolortransparent($type,imagecolorat($type,0,0));
				}
				else {
					$type = imagecreatefrompng("templates/map/".$coord_data['type'].".png");
					imagecolortransparent($type,imagecolorat($type,0,0));
				}
				imagecopymerge($image, $type, $sx, $sy, 0, 0, 40, 40, 100);
			} 

			
			header('Content-type: image/png');
			imagejpeg($image);
		break;
		
		case 'system':
			$tiledimensions = 40;
			$mapdimension = 16;
			$src_img = "templates/map/blank.png";
			$image = imagecreatefrompng($src_img);
			imagealphablending($image,true);
			imageantialias($image,true);
			$system_id = $_GET['s'];
			
			// Colours
			$white = imagecolorallocate($image, 64, 64, 64);
						
			// Get map data
			if (!$_GET['x'] || !$_GET['y']) {
				$x = 500;
				$y = 500;
			}
			else {
				$x = $_GET['x'];
				$y = $_GET['y'];
			}
			$tile_array = $map->tile_array_system($x - 8, $y - 8, (16 + $x), (16 + $y), $system_id);
			if (empty($tile_array)) {
				$tile_array[0] = 0;
			}
			
			
			$sun_x = 300;
			$sun_y = 300;
			
			
			foreach($tile_array as $coord_data) {
				$coords = $gd->convert_coords($x - 8, $y - 8, $coord_data['x'], $coord_data['y']);
				
				$sx = $coords['x'];
				$sy = $coords['y'];
				
				if (substr($coord_data['type'], 0, 3) != "sun") {
					// Find distance between planet and center of map
					$distance = ($gd->distance($sun_x, $sun_y, $sx + 20.0, $sy + 20.0))*2;
					$direction = $gd->direction($sun_x, $sun_y, $sx + 20.0, $sy + 20.0, $distance);
					
					if ($distance <= 250) {
						$ellipse = 2;
					}
					else {
						$ellipse = $sx/8;
					}
					
					rotatedellipse($image, $sun_x, $sun_y, $distance, $distance - $ellipse, $direction['dir'], $white);
				}
			}
			
			foreach($tile_array as $coord_data) {
				$coords = $gd->convert_coords($x - 8, $x - 8, $coord_data['x'], $coord_data['y']);
				
				$sx = $coords['x'];
				$sy = $coords['y'];
				
				if (substr($coord_data['type'], 0, 3) == "sun") {
					$type = $gd->sun_type($coord_data['type']);
					imagecolortransparent($type,imagecolorat($type,0,0));
				}
				else {
					$type = imagecreatefrompng("templates/map/".$coord_data['type'].".png");
					imagecolortransparent($type,imagecolorat($type,0,0));
				}
				
				imagecopymerge($image, $type, $sx, $sy, 0, 0, 40, 40, 100);
				
			} 
			
			header('Content-type: image/png');
			imagejpeg($image);
		break;
	}
	
	if (!$_GET['s']) {
		$tile_array = $map->tile_array($x - 8, $y - 8, (16 + $x), (16 + $y));
	} 
	else {
		if (!$_GET['x'] || !$_GET['y']) {
			$x = 500;
			$y = 500;
		}
		else {
			$x = $_GET['x'];
			$y = $_GET['y'];
		}
		$tile_array = $map->tile_array_system($x - 8, $y - 8, (16 + $x), (16 + $y), $_GET['s']);
	}
	foreach($tile_array as $coord_data) {
				$coords = $gd->convert_coords($x - 8, $y - 8, $coord_data['x'], $coord_data['y']);
				
				$tx = $coords['x'];
				$ty = $coords['y'];
				
				$bx = $tx + 40;
				$by = $ty + 40;
				
				if (!$_GET['s']) {
					$system_name = $map->name($coord_data['coord_id']);
					$owner = $nation->full_name($coord_data['owner']);
					$system_id = $coord_data['coord_id'];
					
					$areamap = $areamap . "<AREA
   HREF='map.php?s=$system_id' SHAPE=RECT COORDS='$tx,$ty,$bx,$by' onmouseover=\"Tip('<b>$system_name</b><br/>$owner<br/>Population: population', ABOVE, true, OFFSETY, 20, WIDTH, 200, TEXTALIGN, 'left', FADEIN, 100, FADEOUT, 100, PADDING, 8)\">";
   				}
				else {
					$system_name = $map->name($_GET['s']);
					$owner = $nation->full_name($coord_data['owner']);
					$population = number_format($coord_data['population']);

					$areamap = $areamap . "<AREA
   HREF='#' SHAPE=RECT COORDS='$tx,$ty,$bx,$by' onmouseover=\"Tip('<strong>$owner</strong><br/>$system_name<br/>Population: $population', ABOVE, true, OFFSETY, 20, WIDTH, 200, TEXTALIGN, 'left', FADEIN, 100, FADEOUT, 100, PADDING, 8)\">";
				}
	}
	$smarty->assign('areamap', $areamap);
	$smarty->display('game_map.tpl');
	
// END MAIN CODE

Re: Anyone know PHP (or be willing to learn?)

Posted: Fri Aug 08, 2008 6:45 pm
by The Cerberus Alliance
Well, for starters, it looks like the map decides on a nation's location only when the owner of that nation decides to look at the map.

The map view is centered on the user's nation, or on the location (500,500) if the user isn't logged in.

That's just what I think I could figure out from the first 30 or so lines of code.

I'll try and look at more of it later. I just have a bit of work to do right now.

Re: Anyone know PHP (or be willing to learn?)

Posted: Wed Aug 13, 2008 1:37 am
by Siesatia
I'll be willing to take up some PHP if it'll help.

Re: Anyone know PHP (or be willing to learn?)

Posted: Wed Aug 13, 2008 5:10 am
by The Mindset
Can you demonstrate usage of PHP, particularly relating to database access and data processing? Do you know how to use SVN?