$databaseLink = mysql_connect("localhost","root","password");
mysql_select_db("database", $databaseLink);
$sql = "select * from avastar where ip_address = '".$_SERVER['REMOTE_ADDR']."'";
$result = mysql_query($sql,$databaseLink);
if (mysql_num_rows($result) == 0) {
$revealOrder = array(1,2,3,4,5,6,7,8,9);
shuffle($revealOrder);
$counter = 1;
$star_image = random_file("starImages/");
$sql = "insert into avastar (ip_address,reveal_order,counter,star_image) values (";
$sql .= "'".$_SERVER['REMOTE_ADDR']."', ";
$sql .= "'".implode(",",$revealOrder)."', ";
$sql .= "'".$counter."', ";
$sql .= "'".$star_image."')";
$result = mysql_query($sql,$databaseLink);
} else {
$record = mysql_fetch_object($result);
if ($record->counter >= 9) {
$revealOrder = array(1,2,3,4,5,6,7,8,9);
shuffle($revealOrder);
$counter = 1;
$star_image = random_file("starImages/");
$sql = "update avastar ";
$sql .= "set reveal_order = '".implode(",",$revealOrder)."', ";
$sql .= "counter = '".$counter."', ";
$sql .= "star_image = '".$star_image."' ";
$sql .= "where ip_address = '".$_SERVER['REMOTE_ADDR']."'";
$result = mysql_query($sql,$databaseLink);
} else {
$sql = "update avastar ";
$sql .= "set counter = counter+1 ";
$sql .= "where ip_address = '".$_SERVER['REMOTE_ADDR']."'";
$result = mysql_query($sql,$databaseLink);
$revealOrder = explode(",",$record->reveal_order);
$counter = 1+$record->counter;
$star_image = $record->star_image;
}
}
header ("Content-type: image/jpg");
$blankImage = imageCreateFromJpeg("blank.jpg");
$starImage = imageCreateFromJpeg($star_image);
$section['1'] = array( 0, 0, 0, 0,21,21);
$section['2'] = array( 0,22, 0,22,21,20);
$section['3'] = array( 0,43, 0,43,21,21);
$section['4'] = array(22, 0,22, 0,20,21);
$section['5'] = array(22,22,22,22,20,20);
$section['6'] = array(22,43,22,43,20,21);
$section['7'] = array(43, 0,43, 0,21,21);
$section['8'] = array(43,22,43,22,21,20);
$section['9'] = array(43,43,43,43,21,21);
for($x=0;$x<$counter;$x++) {
imagecopy($blankImage,$starImage,$section[$revealOrder[$x]][0],$section[$revealOrder[$x]][1],$section[$revealOrder[$x]][2],$section[$revealOrder[$x]][3],$section[$revealOrder[$x]][4],$section[$revealOrder[$x]][5]);
}
imageJPEG($blankImage);
function random_file($dir) {
if ($dh = opendir($dir)) {
while ($file = readdir($dh)) {
if ($file != "." and $file != "..") {
$files[] = $file;
}
}
closedir($dh);
}
shuffle($files);
return $dir.$files[0];
}
and
CREATE TABLE `avastar` (
`avastar_id` INT NOT NULL ,
`ip_address` VARCHAR( 32 ) NOT NULL ,
`reveal_order` VARCHAR( 32 ) NOT NULL ,
`counter` INT NOT NULL ,
`star_image` VARCHAR( 32 ) NOT NULL
);
I'm sure it could be tidier. But I'm busy too on something else to neaten it up.