Jump to content

Image Generation, Data from Flatfile


Dusaro

Recommended Posts

Well, I had this working perfectly until they stopped using a mysql database and started using a flatfile.

What it should do:

Grab data from the flatfile, trim the unwanted parts and set it in an array.

With the received data display as an image.

I am very confused about how I would do this.

 

My current code:

signature.php:

<?php

//GET AND TRIM
$fileName="accounts.txt";
$fileHandle = fopen($fileName,"r");
$filecontents = fread($fileHandle,filesize($fileName));

$exploded = explode(" ",$filecontents);
$name="";
$balance="";
$accounts=array();
$num=0;
for($i=0;$i<count($exploded);$i++)
{

$line = explode("\t",$exploded[$i]);
for($a=0;$a<count($line);$a++)
{

	if(trim($line[$a])=="player")
	{
	}elseif($line[$a]=="money")
	{
	}elseif($line[$a]=="type")
	{
	}elseif(trim($line[$a])=="{")
	{
	}else
	{
		$subline = explode("\r",$line[$a]);
		for($e=0;$e<count($subline);$e++)
		{
			if(trim($subline[$e])=="}")
			{
			}
			elseif(is_numeric(trim($subline[$e])))
			{
				$balance=trim($subline[$e]);
			}
			else
			{
				$name=trim($subline[$e]);
			}
			if($name!=""&&$balance!="")
			{
				$temp=array($name=>$balance);
				$accounts[$num]=$temp;	
				$num++;
				$name="";
				$balance="";
			}
		}
	}

}


}

//Image Generation
require_once('../func.inc.php');
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache");
$font = "minecraft.ttf";
$user="";
if(isset($_GET['u']))
{
if(!empty($_GET['u']))
{
	$user = $_GET['u'];
}
}

if(isset($_GET['i']))
{
if(!empty($_GET['i']))
{
	$img = $_GET['i'];
}
else{$img='1';}
}
else{$img='1';}

if(isset($_POST['u'],$_POST['i']))
{
if(!empty($_POST['u']))
{
	$user=$_POST['u'];
}
if(!empty($_POST['i']))
{
	$img = $_POST['i'];
}
}

if(in_array($user, $accounts))
{
//Info Grabs
$money = get_balance($user);
//End Info Grabs
}
else
{
die("That is an invalid player.");
}

//Display Image

$user = ucfirst($user);

$image = imagecreatefrompng("images/".$img.".png");
$color = imagecolorallocate($image, 255,255,255);

if ($_GET['i'] >= '8')
{

ImageTTFText ($image, "8", 0, 100, 13, $color, $font,"Name:");
ImageTTFText ($image, "8", 0, 138, 13, $color, $font,$user);

ImageTTFText ($image, "8", 0, 240, 13, $color,$font,"Money:");
ImageTTFText ($image, "8", 0, 285, 13, $color,$font,"$".$money);
}

else
{
ImageTTFText ($image, "7", 0, 246, 10, $color, $font,"Spartan Universe");
ImageTTFText ($image, "12", 0, 130, 50, $color, $font,"Name:");
ImageTTFText ($image, "12", 0, 130, 96, $color,$font,"Money:");
ImageTTFText ($image, "12", 0, 185, 50, $color, $font,$user);
ImageTTFText ($image, "12", 0, 198, 96, $color,$font,"$".$money);
}	

//End Image Display

header("Content-type: image/png");
imagepng($image);
imagedestroy($image);

?>

 

accounts.txt:

arkangel011 {
type player
money 23.47
}
chemicalbacon {
type player
money 203.89
}

 

func.inc.php

<?php

function get_players()
{
	$con = mysql_connect("host","user","pass");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("minecraft",$con);
	$query = "SELECT * FROM `lb-players` ORDER BY  `lb-players`.`playername` ASC ";
	$res = mysql_query($query,$con);
	while($row = mysql_fetch_assoc($res))
	{
		echo "<option value='".$row['playername']."'>".$row['playername']."</option>";
	}

}
function is_valid_player($user)
{
	$con = mysql_connect("host","user","pass");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("minecraft",$con);
	$query = "SELECT COUNT(playername) AS count FROM `lb-players` WHERE `playername` = '".$user."'";
	$res = mysql_query($query,$con);
	$row = mysql_fetch_assoc($res);
	if($row['count']==1)
	{
		return true;
	}
	else
	{
		return false;
	}

}

function get_balance($name)
{
	$con = mysql_connect("host","user","pass");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("iconomy",$con);

	$query = "SELECT * FROM `iconomy` WHERE `iconomy`.`username` = '".$name."'";
	$res = mysql_query($query,$con);
	$row=mysql_fetch_array($res);
	//echo $row['balance'];

	mysql_close($con);
	return $row['balance'];
}
function update_economy()
{
	$con = mysql_connect("host","user","pass");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("iconomy",$con);


	$fileName="accounts.txt";
	$fileHandle = fopen($fileName,"r");
	$filecontents = fread($fileHandle,filesize($fileName));

	$exploded = explode(" ",$filecontents);
	$name="";
	$balance="";
	$accounts=array();
	$num=0;
	for($i=0;$i<count($exploded);$i++)
	{

		$line = explode("\t",$exploded[$i]);
		for($a=0;$a<count($line);$a++)
		{

			if(trim($line[$a])=="player")
			{
				//unset($line[$a]);
			}elseif($line[$a]=="money")
			{
				//unset($line[$a]);
			}
			elseif($line[$a]=="type")
			{
				//unset($line[$a]);
			}
			elseif(trim($line[$a])=="{")
			{
				//unset($line[$a]);
			}
			else
			{
				//echo $line[$a]."<br>";
				$subline = explode("\r",$line[$a]);
				for($e=0;$e<count($subline);$e++)
				{
					//echo $subline[$e]."<br>";
					if(trim($subline[$e])=="}")
					{
						//unset($subline[$e]);
					}
					elseif(is_numeric(trim($subline[$e])))
					{
						//echo $subline[$e]."<br>";
						$balance=trim($subline[$e]);
					}
					else
					{
						//echo $subline[$e]."<br>";
						$name=trim($subline[$e]);
					}
					if($name!=""&&$balance!="")
					{
						$temp=array($name=>$balance);
						$accounts[$num]=$temp;	
						$num++;
						$name="";
						$balance="";
					}
				}
			}

		}


	}

	foreach($accounts as $account=>$balance)
	{
		foreach($balance as $name=>$dollar)
		{
			$query = "UPDATE  `iconomy`.`iconomy` SET  `balance` =  '".$dollar."' WHERE  `iconomy`.`username` ='".$name."'";
			mysql_query($query,$con);
			//echo "Name: ".$name;
			//echo ": Balance: ".number_format($dollar,2);
			//echo "<br>";
		}
	}
}

?>

Link to comment
Share on other sites

Why have they gone from a database to a flat file? Seems like a bit of a backwards step!

 

Looking at this from my iPhone, so I can't really test anything but it looks like its still trying to use MySQL?

Link to comment
Share on other sites

Well, the reason is that the game's script they were using was discontinued and had to use a different one.

 

Just ignore the func.inc.php file. I wont be needing it again since that is the only thing i can remember using mysql. and i moved all the stuff i need from it to the signature.php file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.