AtkinsSJ

WikiVenture Original Source Code

Jan 12th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.23 KB | None | 0 0
  1. <?php
  2.     //Super-session-magic-thing
  3.     session_start();
  4.     if (!isset($_SESSION['inventory']))
  5.     {
  6.         $_SESSION['inventory'] = array();
  7.     }
  8.     asort($_SESSION['inventory']);
  9.    
  10.     if (!isset($_SESSION['visited']))
  11.     {
  12.         $_SESSION['visited'] = array();
  13.     }
  14.    
  15.     if ( !isset($_SESSION['cash']) )
  16.     {
  17.         $_SESSION['cash'] = 0;
  18.     }
  19.  
  20.     if ( !isset($_SESSION['score']) )
  21.     {
  22.         $_SESSION['score'] = 0;
  23.     }
  24.    
  25.     if (isset($_GET['say']))
  26.     {
  27.         $sayid = $_GET['say'];
  28.     }
  29.     else
  30.     {
  31.         $sayid = '0';
  32.     }
  33.    
  34.     $talklist = array();
  35.     $showinputbox = false;
  36.    
  37.     $headerextra = '<style type="text/css">
  38.     div.textadventure
  39.     {
  40.         background-color: #333333;
  41.         color: #ffffff;
  42.         font-family: monospace;
  43.         padding: 8px;
  44.     }
  45.    
  46.     div.textadventure a
  47.     {
  48.         color: #ffffff;
  49.         text-decoration: none;
  50.     }
  51.    
  52.     div.textadventure a:hover
  53.     {
  54.         color: #333333;
  55.         background-color: #ffffff;
  56.         text-decoration: none;
  57.     }
  58.     </style>';
  59.     require("header.php");
  60.    
  61.     echo '<div class="main">';
  62.     textSectionStart();
  63.     echo '<h2>Wikiventure (Naff Working Title)</h2>';
  64.    
  65.     if ( !isset($_GET['p']) )
  66.     {
  67.         $page = 'Index';
  68.     }
  69.     else
  70.     {
  71.         $page = $_GET['p'];
  72.     }
  73.    
  74.     $curlUrl = 'http://www.minild12.sophiehoulden.com/api.php?action=query&format=xml&titles=Adventure/'.$page.'&prop=revisions&rvprop=content&list=allimages';
  75.    
  76.     // create curl resource
  77.     $ch = curl_init();
  78.  
  79.     // set url
  80.     curl_setopt($ch, CURLOPT_URL, $curlUrl);
  81.  
  82.     //return the transfer as a string
  83.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  84.  
  85.     // $output contains the output string
  86.     $curlstring = curl_exec($ch);
  87.    
  88.     $xml = new SimpleXMLElement($curlstring);
  89.    
  90.     // close curl resource to free up system resources
  91.     curl_close($ch);
  92.    
  93.     //print_r($xml);
  94.    
  95.     if( !(bool)$xml->query->pages->page['pageid'] )
  96.     {
  97.         echo '<div class="textadventure">
  98.             <b>Oops! Page does not exist!</b><br/>
  99.             If you like, you can create it <a href="http://www.minild12.sophiehoulden.com/index.php?title=Adventure/'.$page.'&action=edit">here.</a>
  100.         </div>';
  101.     }
  102.     else
  103.     {
  104.         $output = $xml->query->pages->page->revisions->rev;
  105.         $useitem = '';
  106.        
  107.         if (isset($_GET['get']))
  108.         {
  109.             if ( !in_array( $_GET['get'], $_SESSION['inventory'] ) )
  110.             {
  111.                 $output = 'You pick-up the '.$_GET['get'].'.<br/><br/>' . $output;
  112.                 $_SESSION['inventory'][] = $_GET['get'];
  113.             }
  114.         }
  115.         elseif (isset($_GET['use']))
  116.         {
  117.             if ( in_array( $_GET['use'], $_SESSION['inventory'] ) )
  118.             {
  119.                 $output = 'You use your '.$_GET['use'].'.<br/><br/>' . $output;
  120.                 $useitem = $_GET['use'];
  121.             }
  122.         }
  123.        
  124.         //Location link repolacements
  125.         $link_before = array('[[Adventure/', '|', ']]');
  126.         $link_after = array('&gt; <a href="minild12.php?p=', '">', '</a>');
  127.        
  128.         $offset = 0;
  129.         $linkslist = array();
  130.         while ( substr_count( $output, '[[') > 0 )
  131.         {
  132.             $startpos = strpos($output, '[[');
  133.             $endpos = strpos($output, ']]', $startpos);
  134.             $midpos = strpos($output, ':', $startpos);
  135.             //Make-sure it gets the correct ]]
  136.             $obcount = substr_count($output, '[[', $startpos+2, $endpos-$startpos-2);
  137.             for ( $i = 0; $i < $obcount; $i++ )
  138.             {
  139.                 $temppos = $endpos;
  140.                 $endpos = strpos( $output, ']]', $endpos+2 );
  141.                 $i -= ( substr_count( substr($output,$temppos,$endpos-$temppos), '[[') );
  142.             }
  143.            
  144.             if ( ($midpos >= $endpos) || ($midpos <= $startpos) ) //No : in it
  145.             {
  146.                 $temp = substr( $output, $startpos, $endpos - $startpos + 2);
  147.                 $linkslist[] = str_replace( $link_before, $link_after, $temp );
  148.                 $output = substr_replace( $output, '', $startpos, $endpos - $startpos + 2 );
  149.             }
  150.             else //Otherwise, parse the code!
  151.             {
  152.                 $secondhalf = substr($output, $midpos+1, $endpos-$midpos-1);
  153.                 switch( substr( $output, $startpos+2, $midpos - $startpos -2) )
  154.                 {
  155.                 //SHOW IMAGE
  156.                 case 'File':
  157.                     foreach ($xml->query->allimages->img as $img)
  158.                     {
  159.                         if ( $img['name'] == $secondhalf )
  160.                         {
  161.                             $imageurl = $img['url'];
  162.                         }
  163.                     }
  164.                     $output = str_replace("[[File:$secondhalf]]", "<img src='$imageurl'/>",$output);
  165.                     break;
  166.                    
  167.                 //GET ITEM
  168.                 case 'GET':
  169.                     $output = str_replace("[[GET:$secondhalf]]", '',$output);
  170.                     if ( !in_array($secondhalf, $_SESSION['inventory']) )
  171.                     {
  172.                         $itemshere[] = "$secondhalf";
  173.                     }
  174.                     break;
  175.                    
  176.                 case 'IFHAVE':
  177.                     $splitpos = strpos($secondhalf, '|');
  178.                     $itemname = substr($secondhalf, 0, $splitpos);
  179.                     $haveitemtext = substr($secondhalf, $splitpos+1);
  180.                     if ( in_array($itemname, $_SESSION['inventory']) )
  181.                     {
  182.                         $output = substr_replace( $output, $haveitemtext, $startpos, $endpos-$startpos+2);
  183.                     }
  184.                     else
  185.                     {
  186.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  187.                     }
  188.                     break;
  189.                    
  190.                 case 'IFNHAVE':
  191.                     $splitpos = strpos($secondhalf, '|');
  192.                     $itemname = substr($secondhalf, 0, $splitpos);
  193.                     $haveitemtext = substr($secondhalf, $splitpos+1);
  194.                     if ( !in_array($itemname, $_SESSION['inventory']) )
  195.                     {
  196.                         $output = substr_replace( $output, $haveitemtext, $startpos, $endpos-$startpos+2);
  197.                     }
  198.                     else
  199.                     {
  200.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  201.                     }
  202.                     break;
  203.                    
  204.                 case 'LOSE':
  205.                     if ( $secondhalf == '*ALL' )
  206.                     {
  207.                         $output = substr_replace( $output, 'You lose all your items!', $startpos, $endpos-$startpos+2);
  208.                         unset($_SESSION['inventory']);
  209.                         $_SESSION['inventory'] = array();
  210.                     }
  211.                     elseif ( in_array($secondhalf, $_SESSION['inventory']) )
  212.                     {
  213.                         $output = substr_replace( $output, 'You lose your ' . $secondhalf, $startpos, $endpos-$startpos+2);
  214.                         unset($_SESSION['inventory'][array_search($secondhalf,$_SESSION['inventory'])]);
  215.                         $_SESSION['inventory'] = array_values($_SESSION['inventory']);
  216.                     }
  217.                     else
  218.                     {
  219.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  220.                     }
  221.                     break;
  222.                    
  223.                 case 'USE':
  224.                     $splitpos = strpos($secondhalf, '|');
  225.                     if ( $splitpos != 0 )
  226.                     {
  227.                         $itemname = substr($secondhalf, 0, $splitpos);
  228.                         $useitemtext = substr($secondhalf, $splitpos+1);
  229.                     }
  230.                     else
  231.                     {
  232.                         $itemname = $secondhalf;
  233.                         $useitemtext = '';
  234.                     }
  235.                    
  236.                     if ( $useitem == $itemname )
  237.                     {
  238.                         $output = substr_replace( $output, $useitemtext, $startpos, $endpos-$startpos+2);
  239.                     }
  240.                     else
  241.                     {
  242.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  243.                     }
  244.                     break;
  245.                
  246.                 case 'NUSE':
  247.                     $splitpos = strpos($secondhalf, '|');
  248.                     if ( $splitpos != 0 )
  249.                     {
  250.                         $itemname = substr($secondhalf, 0, $splitpos);
  251.                         $useitemtext = substr($secondhalf, $splitpos+1);
  252.                     }
  253.                     else
  254.                     {
  255.                         $itemname = $secondhalf;
  256.                         $useitemtext = '';
  257.                     }
  258.                    
  259.                     if ( $useitem != $itemname )
  260.                     {
  261.                         $output = substr_replace( $output, $useitemtext, $startpos, $endpos-$startpos+2);
  262.                     }
  263.                     else
  264.                     {
  265.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  266.                     }
  267.                     break;
  268.                    
  269.                 //VISITING
  270.                 case 'IFVISITED':
  271.                     $splitpos = strpos($secondhalf, '|');
  272.                     $placename = substr($secondhalf, 0, $splitpos);
  273.                     if ( trim($placename) == '' ) { $placename = $page; }
  274.                     $visitedtext = substr($secondhalf, $splitpos+1);
  275.                     if ( in_array($placename, $_SESSION['visited']) )
  276.                     {
  277.                         $output = substr_replace( $output, $visitedtext, $startpos, $endpos-$startpos+2);
  278.                     }
  279.                     else
  280.                     {
  281.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  282.                     }
  283.                     break;
  284.                
  285.                 case 'IFNVISITED':
  286.                     $splitpos = strpos($secondhalf, '|');
  287.                     $placename = substr($secondhalf, 0, $splitpos);
  288.                     if ( trim($placename) == '' ) { $placename = $page; }
  289.                     $visitedtext = substr($secondhalf, $splitpos+1);
  290.                     if ( !in_array($placename, $_SESSION['visited']) )
  291.                     {
  292.                         $output = substr_replace( $output, $visitedtext, $startpos, $endpos-$startpos+2);
  293.                     }
  294.                     else
  295.                     {
  296.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  297.                     }
  298.                     break;
  299.                    
  300.                 //CONVERSATIONS
  301.                 case 'SPEECH':
  302.                     $splitpos = strpos($secondhalf, '|');
  303.                     $speechid = substr($secondhalf, 0, $splitpos);
  304.                     $speechtext = substr($secondhalf, $splitpos+1);
  305.                     if ( (int)$sayid == (int)$speechid )
  306.                     {
  307.                         $output = substr_replace( $output, $speechtext, $startpos, $endpos-$startpos+2);
  308.                     }
  309.                     else
  310.                     {
  311.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  312.                     }
  313.                     break;
  314.                
  315.                 case 'TALK':
  316.                     $commapos = strpos($secondhalf, ',');
  317.                     $splitpos = strpos($secondhalf, '|');
  318.                     $talkid = substr($secondhalf, 0, $commapos); //The id of speech to show
  319.                     $speechid = substr($secondhalf, $commapos+1, $splitpos-$commapos-1);//The speech id to display this
  320.                     $speechtext = substr($secondhalf, $splitpos+1); //What to say
  321.                     if ( $talkid == $sayid ) //Just said this
  322.                     {
  323.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  324.                         $output = 'You: ' . $speechtext . '<br/><br/>' . $output;
  325.                     }
  326.                     elseif ( ($speechid == $sayid) || (trim($speechid) == '') )
  327.                     { //Display this speech option.
  328.                         $talklist[] = '> <a href="minild12.php?p='.$page.'&say='.$talkid.'">Say "'.$speechtext.'"</a>';
  329.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  330.                     }
  331.                     else
  332.                     {
  333.                         //Remove the tag
  334.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  335.                     }
  336.                     break;
  337.                
  338.                 //CASH
  339.                 case 'IFCASH':
  340.                     $commapos = strpos($secondhalf, ',');
  341.                     $splitpos = strpos($secondhalf, '|');
  342.                     $cashlow = substr($secondhalf, 0, $commapos); //Cash lower bound
  343.                     $cashhigh = substr($secondhalf, $commapos+1, $splitpos-$commapos-1); //Cash upper bound
  344.                     $cashtext = substr($secondhalf, $splitpos+1); //What to say
  345.                     if ( (($_SESSION['cash'] >= (int)$cashlow) || (trim($cashlow) == '')) &&
  346.                          (($_SESSION['cash'] <= (int)$cashhigh) || (trim($cashhigh) == '')) ) //cash is between bounds
  347.                     {
  348.                         $output = substr_replace( $output, $cashtext, $startpos, $endpos-$startpos+2);
  349.                     }
  350.                     else
  351.                     {
  352.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  353.                     }
  354.                     break;
  355.                
  356.                 case 'IFNCASH':
  357.                     $commapos = strpos($secondhalf, ',');
  358.                     $splitpos = strpos($secondhalf, '|');
  359.                     $cashlow = substr($secondhalf, 0, $commapos); //Cash lower bound
  360.                     $cashhigh = substr($secondhalf, $commapos+1, $splitpos-$commapos-1); //Cash upper bound
  361.                     $cashtext = substr($secondhalf, $splitpos+1); //What to say
  362.                     if ( (($_SESSION['cash'] < (int)$cashlow) && !(trim($cashlow) == '')) ||
  363.                          (($_SESSION['cash'] > (int)$cashhigh) && !(trim($cashhigh) == '')) ) //cash is between bounds
  364.                     {
  365.                         $output = substr_replace( $output, $cashtext, $startpos, $endpos-$startpos+2);
  366.                     }
  367.                     else
  368.                     {
  369.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  370.                     }
  371.                     break;
  372.                
  373.                 case 'GIVECASH':
  374.                     $_SESSION['cash'] += (int)$secondhalf; //Simple as that! Hopefully.
  375.                     break;
  376.                
  377.                 //SCORE
  378.                 case 'IFSCORE':
  379.                     $commapos = strpos($secondhalf, ',');
  380.                     $splitpos = strpos($secondhalf, '|');
  381.                     $cashlow = substr($secondhalf, 0, $commapos); //Cash lower bound
  382.                     $cashhigh = substr($secondhalf, $commapos+1, $splitpos-$commapos-1); //Cash upper bound
  383.                     $cashtext = substr($secondhalf, $splitpos+1); //What to say
  384.                     if ( (($_SESSION['score'] >= (int)$cashlow) || (trim($cashlow) == '')) &&
  385.                          (($_SESSION['score'] <= (int)$cashhigh) || (trim($cashhigh) == '')) ) //cash is between bounds
  386.                     {
  387.                         $output = substr_replace( $output, $cashtext, $startpos, $endpos-$startpos+2);
  388.                     }
  389.                     else
  390.                     {
  391.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  392.                     }
  393.                     break;
  394.                    
  395.                 case 'IFNSCORE':
  396.                     $commapos = strpos($secondhalf, ',');
  397.                     $splitpos = strpos($secondhalf, '|');
  398.                     $cashlow = substr($secondhalf, 0, $commapos); //Cash lower bound
  399.                     $cashhigh = substr($secondhalf, $commapos+1, $splitpos-$commapos-1); //Cash upper bound
  400.                     $cashtext = substr($secondhalf, $splitpos+1); //What to say
  401.                     if ( (($_SESSION['score'] < (int)$cashlow) && !(trim($cashlow) == '')) ||
  402.                          (($_SESSION['score'] > (int)$cashhigh) && !(trim($cashhigh) == '')) ) //cash is between bounds
  403.                     {
  404.                         $output = substr_replace( $output, $cashtext, $startpos, $endpos-$startpos+2);
  405.                     }
  406.                     else
  407.                     {
  408.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  409.                     }
  410.                     break;
  411.                
  412.                 case 'GIVEPOINTS':
  413.                     $_SESSION['score'] += (int)$secondhalf; //Simple as that! Hopefully.
  414.                     break;
  415.                
  416.                 //INPUT
  417.                 case 'IFINPUT':
  418.                     $showinputbox = true;
  419.                     $splitpos = strpos($secondhalf, '|');
  420.                     $inputexpected = substr($secondhalf, 0, $splitpos);
  421.                     $inputtext = substr($secondhalf, $splitpos+1);
  422.                     if ( strtoupper(trim($_GET['i'])) == strtoupper(trim($inputexpected)) )
  423.                     {
  424.                         $output = substr_replace( $output, $inputtext, $startpos, $endpos-$startpos+2);
  425.                     }
  426.                     else
  427.                     {
  428.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  429.                     }
  430.                     break;
  431.                    
  432.                 case 'IFNINPUT':
  433.                     $showinputbox = true;
  434.                     $splitpos = strpos($secondhalf, '|');
  435.                     $inputexpected = substr($secondhalf, 0, $splitpos);
  436.                     $inputtext = substr($secondhalf, $splitpos+1);
  437.                     if ( strtoupper(trim($_GET['i'])) != strtoupper(trim($inputexpected)) )
  438.                     {
  439.                         $output = substr_replace( $output, $inputtext, $startpos, $endpos-$startpos+2);
  440.                     }
  441.                     else
  442.                     {
  443.                         $output = substr_replace( $output, '', $startpos, $endpos-$startpos+2);
  444.                     }
  445.                     break;
  446.                    
  447.                 //DEFAULT
  448.                 default:
  449.                     $output = substr_replace( $output, '', $startpos, $endpos - $startpos + 2 );
  450.                 }
  451.             }
  452.         }
  453.        
  454.         $output = str_replace( "\n", '<br/>', trim($output) );
  455.  
  456.         //Add a list of items here.
  457.         if ( count($itemshere) != 0 )
  458.         {
  459.             $itemsherelist = 'You see the following items here: ';
  460.             $firstitem = true;
  461.             foreach ( $itemshere as $anitem )
  462.             {
  463.                 if ( $firstitem )
  464.                 {
  465.                     $itemsherelist .= $anitem;
  466.                     $firstitem = false;
  467.                 }
  468.                 else
  469.                 {
  470.                     $itemsherelist .= ', '.$anitem;
  471.                 }
  472.             }
  473.         }
  474.         $output .= '<br/><br/>';
  475.         //Speech links
  476.         if ( count($talklist) > 0 )
  477.         {
  478.             foreach ( $talklist as $anitem )
  479.             {
  480.                 array_unshift($linkslist, '<br/>'.$anitem);
  481.             }
  482.             $output .= $itemsherelist . '<br/><br/>';
  483.         }
  484.         //Add 'get this' links
  485.         if ( count($itemshere) > 0 )
  486.         {
  487.             foreach ( $itemshere as $anitem )
  488.             {
  489.                 array_unshift($linkslist, '<br/>> <a href="minild12.php?p='.$page.'&get='.urlencode($anitem).'">'.
  490.                                     'Pick up the '.$anitem.'</a>');
  491.             }
  492.             $output .= $itemsherelist . '<br/><br/>';
  493.         }
  494.         foreach ( $linkslist as $link )
  495.         {
  496.             $output .= $link.'<br/>';
  497.         }
  498.  
  499.         //Parse bold and italics
  500.         $output = str_replace_every_other("'''", '<b>', $output);
  501.         $output = str_replace("'''",'</b>',$output);
  502.         $output = str_replace_every_other("''", '<i>', $output);
  503.         $output = str_replace("''",'</i>',$output);
  504.        
  505.         //Output it!
  506.         echo '<div class="textadventure">'.$output.'
  507.         <br/>';
  508.        
  509.         if ( $showinputbox == true )
  510.         {
  511.             echo '<form action="minild12.php" method="get">
  512.             Input something: <input type="text" name="i" /> (Press enter)
  513.             <input type="hidden" name="p" value="'.$page.'" />
  514.             </form>';
  515.         }
  516.        
  517.         echo 'Inventory (Click to use): ';
  518.         $firstitem = true;
  519.         foreach($_SESSION['inventory'] as $invitem)
  520.         {
  521.             if ( $firstitem )
  522.             {
  523.                 echo '<a href="minild12.php?p='.$page.'&use='.$invitem.'">'.$invitem.'</a>';
  524.                 $firstitem = false;
  525.             }
  526.             else
  527.             {
  528.                 echo ', <a href="minild12.php?p='.$page.'&use='.$invitem.'">'.$invitem.'</a>';
  529.             }
  530.         }
  531.         if ($firstitem) {echo 'Not a sausage!';}
  532.         echo '<br/>Cash: ' . $_SESSION['cash'] . ' Score: ' . $_SESSION['score'];
  533.         echo '</div>';
  534.     }
  535.        
  536.     echo '<p>
  537.     <a href="http://www.minild12.sophiehoulden.com/index.php?title=Adventure/'.$page.'&action=edit">Edit this page!</a><br/>
  538.             Feel free to add pages to the game <a href="http://www.minild12.sophiehoulden.com/index.php?title=Adventure">here!</a>
  539.         </p>';
  540.     textSectionEnd();
  541.     echo '<br />';
  542.     include("footer.php");
  543.     echo '</div>';
  544.        
  545.     if (!in_array($page, $_SESSION['visited']))
  546.     {
  547.         $_SESSION['visited'][] = $page;
  548.     }
  549. ?>
  550. </body>
  551. </html>
Advertisement
Add Comment
Please, Sign In to add comment