Advertisement
KaeruCT

game.php

May 28th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.72 KB | None | 0 0
  1. <?php
  2.     // By Kaeru~
  3.     require('lib/common.php');
  4.     /*if($_GET['lol']=='yes')
  5.     {
  6.         $sql->query('TRUNCATE TABLE g_opts');
  7.         $sql->query('TRUNCATE TABLE g_stages');
  8.         $sql->query('TRUNCATE TABLE g_rep');
  9.         $sql->query('INSERT INTO g_stages SET text = "Oops you fell out of the internet into real life. Now what?"');
  10.         $sql->query('INSERT INTO g_opts SET stage = 1, text = "Go back to sleep"');
  11.         $sql->query('INSERT INTO g_opts SET stage = 1, text = "Eat something"');
  12.         $sql->query('INSERT INTO g_opts SET stage = 1, text = "Go outside"');
  13.         print 'queries done';
  14.     }*/
  15.    
  16.     function branch($stage)
  17.     {
  18.         global $sql;
  19.         $tree = array();
  20.  
  21.         $opts = $sql->query('SELECT * FROM g_opts WHERE stage = '.$stage);
  22.         $stage = $sql->fetchq('SELECT * FROM g_stages WHERE id = '.$stage);
  23.        
  24.         $tree['text'] = $stage['text'];
  25.         $tree['opts'] = array();
  26.        
  27.         while($opt = $sql->fetch($opts))
  28.         {
  29.             $tree['opts'][$opt['id']] = array('opt' => $opt['text'], 'branch' => branch($opt['to']));
  30.         }
  31.  
  32.         return $tree;
  33.     }
  34.    
  35.     function loosends($stage, &$num)
  36.     {
  37.         global $sql;
  38.        
  39.         $out = '';
  40.         $opts = $sql->query('SELECT * FROM g_opts WHERE stage = '.$stage);
  41.  
  42.         while($opt = $sql->fetch($opts))
  43.         {
  44.             if(!$opt['to'])
  45.             {
  46.                 $num++;
  47.                 $out .= '<li><a href="game.php?option='.$opt['id'].'">'.$opt['text'].'</a></li>';
  48.             }
  49.             else
  50.                 $out .= loosends($opt['to'], $num);
  51.         }
  52.  
  53.         return $out;
  54.     }
  55.    
  56.     function branchp($arr)
  57.     {
  58.         $out = '<div class="tree" style="display: none;">';
  59.         $out .= '<p>'.($arr['text']?nl2br(htmlval($arr['text'])):'This part of the story isn\'t done yet!').'</p>';
  60.  
  61.         foreach($arr['opts'] as $k => $v)
  62.         {
  63.             if(is_array($v))
  64.             {
  65.                 $out .= '<div class="tog" onclick="$(this).next().slideToggle();">'.$v['opt'].'</div>';
  66.                 $out .= branchp($v['branch']);
  67.             }
  68.         }
  69.        
  70.         $out .= '</div>';
  71.         return $out;
  72.     }
  73.  
  74.     $links = '<a href="game.php?loose">Loose ends</a>&nbsp;|&nbsp;<a href="game.php">Beginning</a>';
  75.     $option = isset($_GET['option'])?intval($_GET['option']):0;
  76.     $action = isset($_POST['action'])?$_POST['action']:'';
  77.     $rep = isset($_GET['report'])?intval($_GET['report']):0;
  78.     $msg = $link = $title = '';
  79.    
  80.     if($rep&&$log)
  81.     {
  82.         if($sql->resultq('SELECT COUNT(*) FROM g_stages WHERE id = "'.$rep.'"')>0)
  83.         {
  84.             $option = $sql->resultq('SELECT id FROM g_opts WHERE `to` = "'.$rep.'"');
  85.             if($sql->resultq('SELECT COUNT(*) FROM g_rep WHERE stage = "'.$rep.'" AND user = "'.$loguser['id'].'"')>0)
  86.             {
  87.                 $msg = 'You already reported this setting!';
  88.             }
  89.             else
  90.             {
  91.                 $sql->query('REPLACE INTO g_rep SET stage = "'.$rep.'", user = "'.$loguser['id'].'"');
  92.                 $msg = 'This setting has been reported!';
  93.             }
  94.             $msg .= '<br /><a href="game.php?option='.$option.'">Go back</a>';
  95.         }
  96.         else
  97.             $msg = 'This setting doesn\'t even exist!<br /><a href="game.php">Go back</a>';
  98.     }
  99.     elseif($option!=0)
  100.     {
  101.         $option = $sql->fetchq('SELECT * FROM g_opts WHERE id = "'.$option.'"');
  102.         $link = '<a href="game.php">Go back to start</a>';
  103.     }
  104.     else
  105.         $option = array('id' => 0, 'to' => 1, 'stage' => 0, 'text' => 'The Beginning');
  106.    
  107.     if(isset($_GET['branch']))
  108.     {
  109.         $title = 'Branch';
  110.         $tree = branch(1);
  111.         $msg = '<div class="tog" onclick="$(this).next().slideDown();">Start!</div>'.branchp($tree);
  112.     }
  113.     elseif(isset($_GET['loose']))
  114.     {
  115.         $num = 0;
  116.         $msg = '<ol>'.loosends(1, $num).'</ol>';
  117.         $title = 'Loose ends ('.$num.')';
  118.     }
  119.     elseif($action)
  120.     {
  121.         if($log)
  122.         {
  123.         switch($action)
  124.         {
  125.             case 'newsetting':
  126.             $num = isset($_POST['numopts'])?intval($_POST['numopts']):2;
  127.             $optid = isset($_POST['optid'])?intval($_POST['optid']):0;
  128.            
  129.             if($num>=4) $num = 4;
  130.             if($num<1) $num = 1;
  131.            
  132.             if($optid)
  133.             {
  134.                 $opt = $sql->fetchq('SELECT * FROM g_opts WHERE id = "'.$_POST['optid'].'"');
  135.                
  136.                 $msg = '
  137.                 <form method="post" action="game.php"><input type="hidden" name="optid" value="'.$_POST['optid'].'"><input type="hidden" name="numopts" value="'.$num.'"><input type="hidden" name="action" value="submitsetting"><table>
  138.                 <tr><th colspan="2">Setting for "'.$opt['text'].'" option</th></tr>
  139.                 <tr><td width="150px"><label for="setting">Setting:</label></td><td><textarea id="setting" name="setting" cols="80" rows="5"></textarea></td></tr>';
  140.  
  141.                 for($i=1;$i<=$num;$i++)
  142.                     $msg.= '<tr><td><label for="opt'.$i.'">Option '.$i.':</label></td><td><input type="text" id="opt'.$i.'" name="opt'.$i.'"></td></tr>
  143.                     ';
  144.                
  145.                 $msg .= '<tr><td>&nbsp;</td><td><input type="submit" value="Submit"></td></tr>
  146.                 </table></form>';
  147.             }
  148.             else
  149.                 $msg = 'No option ID was specified.';
  150.                
  151.             break;
  152.            
  153.             case 'submitsetting':
  154.             $num = isset($_POST['numopts'])?intval($_POST['numopts']):2;
  155.             if($num>=4) $num = 4;
  156.             if($num<1) $num = 1;
  157.             $text = isset($_POST['setting'])?trim($_POST['setting']):'';
  158.             $optid = isset($_POST['optid'])?intval($_POST['optid']):0;
  159.             $err = array();
  160.            
  161.             if(!$text)
  162.             {
  163.                 $err[] = 'You didn\'t fill the "setting" field.';
  164.             }
  165.             if(!$optid)
  166.             {
  167.                 $err[] = 'No option ID was specified.';
  168.             }
  169.             else
  170.             {
  171.                 $opt = $sql->fetchq('SELECT * FROM g_opts WHERE id = "'.$_POST['optid'].'"');
  172.                 if($opt['to']!=0)
  173.                     $err[] = 'Someone submitted the option before you did!';
  174.             }
  175.             for($i=1;$i<=$num;$i++)
  176.             {
  177.                 if(!trim($_POST['opt'.$i]))
  178.                 {
  179.                     $err[] = 'You didn\'t fill all the options.';
  180.                     break;
  181.                 }
  182.             }
  183.            
  184.             if(!$err[0])
  185.             {
  186.                 $sql->query('INSERT INTO g_stages SET text = "'.$text.'", user = "'.$loguser['id'].'"');
  187.                 $stageid = mysql_insert_id();
  188.                 $sql->query('UPDATE g_opts SET `to` = "'.$stageid.'" WHERE id = "'.$optid.'"');
  189.                 for($i=1;$i<=$num;$i++)
  190.                     $sql->query('INSERT INTO g_opts SET stage = '.$stageid.', text = "'.$_POST['opt'.$i].'"');
  191.                
  192.                 sendirc('New option submitted by '.$loguser['name'].'! {boardurl}game.php?option='.$optid);
  193.                 $msg = 'Option submitted succesfully!<br /><a href="game.php?option='.$optid.'">Go back where you ended!</a>';
  194.             }
  195.             else
  196.             {
  197.                 $msg = 'The following error'.(sizeof($err)==1?'':'s').' occured:<ul><li>'.implode('</li><li>', $err).'</li></ul>';
  198.             }
  199.  
  200.             break;
  201.            
  202.             case 'ending':
  203.             $text = isset($_POST['ending'])?trim($_POST['ending']):'';
  204.             $optid = isset($_POST['optid'])?intval($_POST['optid']):0;
  205.            
  206.             if(!$text)
  207.             {
  208.                 $err[] = 'You didn\'t fill the "ending" field.';
  209.             }
  210.             if(!$optid)
  211.             {
  212.                 $err[] = 'No option ID was specified.';
  213.             }
  214.            
  215.             if(!$err[0])
  216.             {
  217.                 $sql->query('INSERT INTO g_stages SET text = "'.$text.'", user = "'.$loguser['id'].'"');
  218.                 $stageid = mysql_insert_id();
  219.                 $sql->query('UPDATE g_opts SET `to` = "'.$stageid.'" WHERE id = "'.$optid.'"');
  220.            
  221.                 sendirc('An ending was submitted by '.$loguser['name'].'! {boardurl}game.php?option='.$optid);
  222.                 $msg = 'Setting submitted succesfully!<br /><a href="game.php?option='.$optid.'">Go back where you ended!</a>';
  223.             }
  224.             else
  225.             {
  226.                 $msg = 'The following error'.(sizeof($err)==1?'':'s').' occured:<ul><li>'.implode('</li><li>', $err).'</li></ul>';
  227.             }
  228.  
  229.             break;
  230.         }
  231.         }
  232.         else
  233.             $msg = 'You need to be logged in to add to the game!<br /><a href="login.php">Log in.</a>';
  234.     }
  235.     elseif(!$msg)
  236.     {
  237.         if(!$option)
  238.         {
  239.             $msg = 'This option doesn\'t even exist!';
  240.         }
  241.         elseif(!$option['to'])
  242.         {
  243.             $title = $option['text'];
  244.             $msg = '<h3>This part of the story isn\'t done yet. You can either continue the story, or end it here.</h3>
  245.             <div><a href="#" onclick="$(\'#end\').slideUp(); $(\'#continue\').slideDown(); return false;">Continue</a> | <a href="#" onclick="$(\'#continue\').slideUp(); $(\'#end\').slideDown(); return false;">End</a></div>
  246.             <div id="continue" style="display: none;"><form method="post" action="game.php"><input type="hidden" name="optid" value="'.$option['id'].'"><input type="hidden" name="action" value="newsetting"><table>
  247.             <tr><th colspan="2">Continue the story</th></tr>
  248.             <tr><td width="150px"><label for="numopts">Number of options (max: 4):</label></td><td><input type="text" id="numopts" name="numopts" size="2" maxlength="1"></td></tr>
  249.             <tr><td>&nbsp;</td><td><input type="submit" value="Submit"></td></tr>
  250.             </table></form></div>
  251.             <div id="end" style="display: none;"><form method="post" action="game.php"><input type="hidden" name="optid" value="'.$option['id'].'"><input type="hidden" name="action" value="ending"><table>
  252.             <tr><th colspan="2">End it here</th></tr>
  253.             <tr><td width="150px"><label for="ending">Ending text:</label></td><td><textarea id="ending" name="ending" cols="80" rows="5"></textarea></td></tr>
  254.             <tr><td>&nbsp;</td><td><input type="submit" value="Submit"></td></tr>
  255.             </table></form></div>';
  256.         }
  257.         else
  258.         {
  259.             $setting = $sql->fetchq('SELECT * FROM g_stages WHERE id = "'.$option['to'].'"');
  260.             $opts = $sql->query('SELECT * FROM g_opts WHERE stage = "'.$option['to'].'"');
  261.             $title = $option['text'];
  262.             $l = array();
  263.             while($opt = $sql->fetch($opts))
  264.             {
  265.                 $msg .= '<li><a href="game.php?option='.$opt['id'].'">'.htmlentities($opt['text']).'</a></li>';
  266.             }
  267.            
  268.             if(!$msg) $msg = '<h2>THE END</h2><a href="game.php">Go back to the beginning</a>.';
  269.             else $msg = '<ul>'.$msg.'</ul>';
  270.         }
  271.    
  272.         $msg = '<div id="setting">'.nl2br(htmlval($setting['text'])).'</div><div id="opts">'.$msg.'</div>';
  273.  
  274.         if($option['stage'])
  275.         {
  276.             $option = $sql->resultq('SELECT id FROM g_opts WHERE `to` = "'.$option['stage'].'"');
  277.             $l[] = '<a href="game.php'.($option?'?option='.$option:'').'">Go back to previous setting</a>';
  278.         }
  279.  
  280.         if($setting['user'])
  281.         {
  282.             $user = $sql->fetchq('SELECT name,sex,power,color,id FROM users WHERE id = '.$setting['user']);
  283.             $l[] = 'Setting by '.userlink($user).'~';
  284.        
  285.         }
  286.        
  287.         if($log)
  288.             $l[] = '<a href="game.php?report='.$setting['id'].'">Report</a>';
  289.  
  290.         if($l[0])
  291.             $links .= '&nbsp;|&nbsp;'.implode('&nbsp;|&nbsp;', $l);
  292.     }
  293.    
  294.     $title = ($title?$title.' | ':'').'Choose your own adventure: Neritic';
  295. ?>
  296. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  297.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  298. <html xmlns="http://www.w3.org/1999/xhtml">
  299. <head>
  300.     <title><?=$title?></title>
  301.     <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  302.     <meta name="Author" content="Kaeru" />
  303.     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  304.     <style type="text/css">
  305.         body{font-size: 12px; font-family: tahoma; color: #000; background: #fafafa; padding: 8px;}
  306.         div#main{background: #eee; border: #000 1px solid; padding: 10px;}
  307.         div.t {background: #444; border: #666 1px solid; padding: 10px; color: #eee;}
  308.         div.t > div {padding: 10px;}
  309.         p.sm{border: 0; border-top: #999 1px dotted; font-size: .8em}
  310.         div.tree{padding: 4px; border: #aaa 1px solid; background: url('theme/fragment/tdbg3.png')}
  311.         div.tog{font-weight: bold; cursor: pointer; margin: 4px;}
  312.         input[type=submit]{background: #e6e6e6; border: #ccc 1px outset; color: #111;}
  313.         input[type=submit]:hover{background: #eee; border: #ccc 1px inset; color: #111;}
  314.         textarea, input[type=text]{background: #c6c6c6; border: #eee 1px outset; color: #111;}
  315.         a:link{color:#333; font-weight: bold; text-decoration: none;}
  316.         a:visited{color:#333; font-weight: bold; text-decoration: none;}
  317.         a:active{color:#222; text-decoration: none;}
  318.         a:hover{color:#111;text-decoration: underline;}
  319.         div a:link, div a:visited{color:#ccc; font-weight: bold; text-decoration: none;}
  320.         div a:active{color:#fff; text-decoration: none;}
  321.         div a:hover{color:#fff;text-decoration: underline;}
  322.         form{display: inline;}
  323.         table{border: #e6e6e6 1px outset; width: 100%; margin: 4px;}
  324.     </style>
  325. </head>
  326. <body>
  327. <h1><?=$title?></h1>
  328. <p class="sm"><?=$links?></p>
  329. <div id="main">
  330. <div class="t"><?=$msg?></div>
  331. </div>
  332. <div><small>By Kaeru~<br /><?=pagestats();?></small></div>
  333. </body>
  334. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement