Code Time

Feel free to email me if you have questions.


<?php
//-------------COOKIE WORK-------------------
include('testCookie.php');

if (isset(
$_COOKIE[testCookie])) 
    { 
$action = (isset($_COOKIE[username])) ? "saves.php" "login.php"; } 
    
//if a cookie is available and we're logged in go straight to saving it. 
    //If we're not logged in but we have cookies, throw everything to the login page which will then put it 
    //in a cookie and wait for you to create an account and/or login.
    //if no cookies, go to the cookieless page.
else { $action "cookieless.php"$noCookie " (Cookies Needed)"; }

//-----------END COOKIE WORK-------------------

//-----get the counter
$counter fread(fopen("counter.dat",'r'),filesize("counter.dat")+1) + 1;

//------set the counter
fwrite(fopen("counter.dat",'w'),$counter);

//-----create an array of all items from each file.
$locations file("locations.dat");
$genres file("genres.dat");
$characters file("characters.dat");
$actions file("actions.dat");
$charmods file("charmods.dat");

//----grab a random but unique subset of each array of items using my unique functions.
$randCharsAndMods uniqueChar(rand(2,4),$characters,$charmods);
$randLocation unique(rand(1,4),$locations);
$randGenre uniqueGenre(rand(1,3),$genres);
$randAction unique(rand(1,3),$actions);
$randPlot "$randCharsAndMods $randAction<br />in this $randGenre<br />set in $randLocation.";


echo 
'<LINK REL=StyleSheet HREF="plotStyle.css" TYPE="text/css">';
$counterH number_format($counter);

echo <<<LAYOUT
<body id="engine">
<center>
<form><input class="plotButton" type="submit" value="Boil Another Plot" /></form>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<form method="POST" action="$action" target="_top">
<input class="plotButton" type="submit" value="Save This Plot$noCookie" />
<input type="hidden" name="subid" value="$counter">
<textarea style="display:none" name="subplot">$randPlot</textarea>
</form>

<table class='scriptcover' border=0><tr><td>
<div style="position:relative;height:100%;width:100%;">
<div class='plotbox'><b><center>BOILED PLOT #$counterH</center></b><br /><br />
$randPlot</div>
<div class='credits'>Code by Brad Tibbils<br />Concept by Michael Avolio</div>
</div>
</center>
LAYOUT;

//---------------------NOTHING BUT FUNCTIONS BELOW THIS LINE----------------------------

function uniqueGenre($num,$array)
{
    
$list unique($num,$array);
    global 
$listMover;
    
$dynGenres $listMover;
    
$outputlist '';
    foreach(
$dynGenres as $dynGenre)
    {
        
$outputlist .= $dynGenre;
        if(
$dynGenre != $dynGenres[$num 1])
        {
        
$outputlist .= " / ";
        }
    }
    return 
$outputlist;
}





function 
uniqueChar($num,$chars,$mods)
{
    
$list unique($num,$chars); //This returns a unique list of characters for later.
     
global $listMover;
    
$dynChars $listMover//after we call unique() we can grab the dynamic list of characters created.
    
$outputlist '';
    foreach(
$dynChars as $dynChar)
    {
        
$modlist unique(rand(1,3),$mods);
        if(
$num == 2)
        {
            if(
$dynChars[$num-1]==$dynChar){$outputlist .= " and " $dynChar " who has " $modlist "<br />";}
            else{
$outputlist .= $dynChar " who has " $modlist "<br />";}
        }
        else
        {
            if(
$dynChars[$num-1]==$dynChar){$outputlist .= " and " $dynChar " who has " $modlist "<br />";}
            else{
$outputlist .= $dynChar " who has " $modlist ", " "<br />";}
        }
    }
    return 
ucfirst($outputlist); //return the list of characters with their char mods.
}




//----BEST. FUNCTION. EVER. WRITTEN. (by Brad)
function unique($randCount,$theArray){
for(
$i=1$i<=$randCount$i++) //create an array with random amount chosen with unique items in each slot.
{
    
$randset[] = '';
    Do{ 
//randomly pick an item and compare it to the already established set, 
        //if it's the same as any of them loop back up to create another. If not, add it to the array.
        
$forward true;
        
$picked trim($theArray[rand(0,count($theArray)-1)]);
        foreach(
$randset as $randitem)
        {
            if(
$picked == $randitem)
            {
$forward false;break;}
        }
    }While(
$forward==false);
    
$randset[$i-1] = $picked;
}

global 
$listMover;
$listMover $randset//grab the list of items in case it's the uniqueChar function that called this function.
//-----above is to make a unique set, below is just grammatical formatting.

switch ($randCount){
case 
1:
    return 
$randset[0]; break;
case 
2:
    return 
$randset[0] . " and " $randset[1]; break;
case (
$randCount >= 3):
    
$retval '';
    foreach(
$randset as $randitem)
    {
        if(
$randset[$randCount-1]==$randitem){$retval .= "and " $randitem;}
        else{
$retval .= $randitem ", ";}
    }
    return 
$retval;
    break;
}
}


?>