Advertisement
jargon

Roe Collector Card Game (mk45CARDS)

Sep 30th, 2021
1,315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.79 KB | None | 0 0
  1. <?php
  2.  
  3. $nen = nen_ini();
  4.  
  5. function nen_ini()
  6. {  
  7.     $nen['me'] = me_shorthand($_SERVER['SCRIPT_NAME']);
  8.  
  9.     // assign domain root
  10.     $nen['domain']='Roe XAMPP Builder';
  11.    
  12.     // assign host mode (local)
  13.     $nen[ 'host' ] = 'local';
  14.    
  15.     // assign host mode (online)
  16.     //$nen[ 'host' ] = 'online';
  17.    
  18.     // switch http host vars
  19.     switch( $_SERVER[ 'HTTP_HOST' ] )
  20.     {
  21.        
  22.     case 'localhost':
  23.         // domain local
  24.         $nen[ 'root' ] = $_SERVER[ 'DOCUMENT_ROOT' ] . '/' . $nen[ 'domain' ] . '/';
  25.         break;
  26.  
  27.     default:
  28.         // domain host
  29.         $nen[ 'root' ] = $_SERVER[ 'DOCUMENT_ROOT' ] . '/';
  30.         break;     
  31.     }
  32.    
  33.     return $nen;
  34. }
  35.  
  36. // configure cascading stylesheet
  37. function nen_css()
  38. {
  39.     $nen = nen_ini();
  40.     // load stylesheet into memory 
  41.     return file_get_contents( $nen[ 'root' ]. 'css/stylesheet.css');
  42. }
  43.  
  44. $nen[ 'src' ] = 'R11-SRC';
  45. $nen[ 'emojis' ] = 'emojis/';
  46.  
  47. // include Tim Keal's token library
  48. function trk_strippath( $filename = '' )
  49. {
  50.     trk_stripchars( $filename, '\\', '/', $file );
  51.     trk_tok( $file, '/', $tok );
  52.     return strtoupper( $tok[ count( $tok ) - 1 ] );
  53. }
  54.  
  55. function trk_fastfiles( $path = '', $Exts = array(), &$files = array() )
  56. {
  57.     $nen = nen_ini();
  58.  
  59.     $files[ null ] = $path;
  60.    
  61.     if ( is_dir( $nen['root'] . '/' . $path . '/' ) )
  62.     {
  63.         if ( $handle = opendir( $nen['root'] . '/' . $files[ null ] . '/' ) )
  64.         {
  65.             while ( false !== ( $entry = readdir( $handle ) ) )
  66.             {
  67.                 if( is_file( $nen['root'] . '/' . $files[ null ] . '/' . $entry ) )
  68.                 {
  69.                     if ( ( $entry !== '.' ) && ( $entry !== '..' ) )
  70.                     {
  71.                         foreach( $Exts as $Ext )
  72.                         {
  73.                             if ( substr( $entry, -strlen( '.'.$Ext ), strlen( '.'.$Ext ) ) === '.'.$Ext )
  74.                             {
  75.                                 $files[ $entry ] = null;
  76.                             }
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.        
  83.         closedir($handle);
  84.     }
  85.     jensort($files);
  86. }
  87.  
  88. function trk_cleancfg( &$config = array() )
  89. {
  90.     $config_temp = $config;
  91.     $config = array();
  92.    
  93.     foreach( $config_temp as $k => $v )
  94.     {
  95.         if( is_integer( $k ) )
  96.         {
  97.             if( is_array( $v ) )
  98.             {
  99.                 foreach( $v as $k2 => $v2 )
  100.                 {
  101.                     $config[ $k2 ] = $v2;
  102.                 }
  103.             }
  104.             elseif( is_string( $v ) )
  105.             {
  106.                 $config[ $v ] = '';
  107.             }
  108.         }
  109.     }
  110.        
  111.     unset( $config_temp );
  112. }
  113.  
  114. function trk_fastini( $cfgfile = '', &$ini = array() )
  115. {
  116.     trk_loadcfg( $cfgfile, $config );
  117.     trk_cleancfg( $config );
  118.  
  119.     $ini = array();
  120.     foreach( $config as $k => $empty )
  121.     {
  122.         $ini[ $k ] = jpp_build( $k );
  123.     }
  124. }
  125.  
  126. function trk_loadcfg($filename = '', &$config = array() )
  127. {
  128.     $nen = nen_ini();
  129.    
  130.     $crlf = "\r\n";
  131.     $comma = ",";
  132.     $colon = ":";
  133.     $quot = "\"";
  134.    
  135.     $buffer = '';
  136.    
  137.     if( file_exists($nen['root'].$filename ) ? is_file($nen['root'].$filename) : false )
  138.     {
  139.         trk_fastload( $filename, $buffer );
  140.     }
  141.     else
  142.     {
  143.         $buffer = '';
  144.     }
  145.    
  146.     trk_fastcfg( $buffer, $config );
  147. }
  148.  
  149. function trk_fastcfg( $buffer = '', &$config = array() )
  150. {
  151.     $config = array();
  152.    
  153.     $crlf   =   "\r\n";
  154.     $colon  =   ":";
  155.     $comma  =   ",";
  156.     $quot   =   "\"";
  157.  
  158.     $ct = 0;
  159.     if( strlen( $buffer ) === 0 )
  160.     {
  161.         return;
  162.     }
  163.     elseif ( strpos( $buffer, $crlf ) !== false )
  164.     {  
  165.         trk_tok( $buffer, $crlf, $lines );
  166.         unset( $lines[ 0 ] );
  167.        
  168.         foreach($lines as $line)
  169.         {
  170.             trk_fastcfg( $line, $config[] );
  171.         }
  172.        
  173.         return;
  174.     }
  175.     elseif ( strpos( $buffer, $colon ) !== false )
  176.     {
  177.         $prefix = substr( $buffer, 0, strpos( $buffer, $colon ) );
  178.         $suffix = substr( $buffer, strpos( $buffer, $colon ) + 1 );
  179.        
  180.         $config[ $prefix ] = $suffix;
  181.        
  182.         return;
  183.     }
  184.     elseif ( strpos( $buffer, $comma ) !== false )
  185.     {  
  186.         trk_tok( $buffer, $comma, $parts );
  187.         unset( $parts[ 0 ] );
  188.                
  189.         foreach($parts as $part)
  190.         {
  191.             $config[] = $part;
  192.         }
  193.        
  194.         return;
  195.     }
  196.     else
  197.     {
  198.         $config = $buffer;
  199.         return;
  200.     }
  201.        
  202. }
  203.  
  204. function trk_fastload( $filename = '', &$buffer = '')
  205. {
  206.     if( is_file( $filename ) )
  207.     {
  208.         $buffer = file_get_contents($filename);
  209.     }
  210.     else
  211.     {
  212.         $buffer = '';
  213.     }
  214. }
  215.  
  216.  
  217. function trk_tok( $subject = '', $delimiter = ',', &$tok = array() )
  218. {
  219.     $tok = explode( $delimiter, $delimiter.$subject );
  220.     $tok[0] = $subject;
  221. }
  222.  
  223. function trk_implode( $tok = array(), $delimiter = ',', &$buffer = '' )
  224. {
  225.     if( isset( $tok[0] ) )
  226.     {
  227.         unset( $tok[0] );
  228.     }
  229.     $buffer = implode( $delimiter, $tok);
  230. }
  231.  
  232. function trk_stripchars ($subject = '', $ws = ' ',$replace = '', &$buffer = '' )
  233. {
  234.     $buffer = str_replace( $ws, $replace, $subject );
  235. }
  236.  
  237. function trk_striptok ( $subject='', $delimiter=',', $skip = '', &$buffer = '')
  238. {
  239.     $tok=array();
  240.     trk_tok( $subject, $delimiter, $tok() );
  241.     $token_index = 0; $buffer_index = 0;
  242.    
  243.     if( count( tok ) < 1 )
  244.     {
  245.         return;
  246.     }
  247.    
  248.     $buffer = '';
  249.    
  250.     foreach( $tok as $tok_index => $tok_dat)
  251.     {
  252.         if( !( $tok[ $token_index ] === 0 ) )
  253.         {
  254.             if( !( $tok[ $token_index ] === $skip ) )
  255.             {
  256.                 if( strlen( $buffer ) > 0 )
  257.                 {
  258.                     $buffer .= $delimiter;
  259.                 }
  260.                 $buffer .= $tok[ token_index ];
  261.             }
  262.         }
  263.     }
  264.  
  265. }
  266.  
  267. function trk_debug( $color = 'red', $subject = '' )
  268. {
  269.     return '<span style="color:'.$color.';">'.$subject.'</span>';
  270. }
  271.  
  272. function trk_fastlog( $caption, $filename )
  273. {
  274.     return '<h1>'.htmlentities( strtoupper( $caption ) ).':'.trk_fastdump( jpp_build( $filename ) ).'</h1>';
  275. }
  276.  
  277. function trk_styles( $designation = '' )
  278. {
  279.     $ret = '';
  280.    
  281.     switch ($designation)
  282.     {
  283.     case 'array':
  284.         $ret = 'red';
  285.         break;
  286.     case 'string':
  287.         $ret = 'orange';
  288.         break;
  289.     case 'integer':
  290.         $ret = 'orange';
  291.         break;
  292.     case 'float':
  293.         $ret = 'orange';
  294.         break;
  295.     case 'bool':   
  296.         $ret = 'orange';
  297.         break;
  298.     case 'object': 
  299.         $ret = 'brown';
  300.         break;
  301.     case 'null':   
  302.         $ret = 'yellow';
  303.         break;
  304.     case 'unknown':
  305.         $ret = 'purple';
  306.         break;
  307.     default:
  308.         $ret = '';
  309.         break;
  310.     }
  311.     return $ret;
  312. }
  313.  
  314. // include Tim Keal's memory trace library
  315. function trk_emoji( $buffer )
  316. {
  317.     $nen = nen_ini();
  318.  
  319.     return '<img src="./emojis/stop-sign.png" style="width:50px;height:50px;">'. strtoupper( $buffer );
  320. }
  321.  
  322. function trk_echo2($header = '', $buffer = '')
  323. {
  324.     echo '<h1>' . trk_emoji($header) . '[' . trk_fastdump($buffer) . ']</h1>';
  325. }
  326.  
  327. function trk_fastdump( $subject = array() )
  328. {
  329.     $ret = '';
  330.    
  331.     switch( true )
  332.     {
  333.     case is_array( $subject ):
  334.         $type = 'array';
  335.         break;
  336.     case is_string( $subject ):
  337.         $type = 'string';
  338.         break;
  339.     case is_integer( $subject ):
  340.         $type = 'integer';
  341.         break;
  342.     case is_float( $subject ):
  343.         $type = 'float';
  344.         break;
  345.     case is_bool( $subject ):
  346.         $type = 'bool';
  347.         break;
  348.     case is_object( $subject ):
  349.         $type = 'object';
  350.         break;
  351.     case is_null( $subject ):
  352.         $type = 'null';
  353.         break;
  354.     default:
  355.         $type = 'unknown';
  356.         break;
  357.     }
  358.    
  359.     switch( $type )
  360.     {
  361.     case 'array':
  362.        
  363.         $ret.=
  364.         trk_debug('red','array{');
  365.         $ct=0;
  366.         foreach( $subject as $key => $value )
  367.         {
  368.             $ct++;
  369.             if( $ct > 0 )
  370.             {
  371.                 $ret.= trk_debug( trk_styles( 'array' ), '[' ).trk_fastdump($key).trk_debug( trk_styles( 'array' ), ']');
  372.  
  373.                 $ret.= trk_debug( trk_styles( 'array' ), ' => ');
  374.                
  375.                 $ret.= trk_fastdump( $value );
  376.                
  377.                 if( $ct < count( $subject ) )
  378.                 {  
  379.                     $ret.= trk_debug( trk_styles( 'array' ), ',');
  380.                 }
  381.             }
  382.         }
  383.         $ret.= trk_debug('red','}');
  384.  
  385.         break;
  386.    
  387.     case 'string':
  388.         $ret.=
  389.             'string('.strlen($subject).')'.
  390.             ' '.
  391.             '"'.htmlentities($subject).'"';
  392.         break;
  393.     case 'integer':
  394.         $ret.=
  395.             trk_debug( trk_styles( $type ), $type.'[').
  396.             htmlentities($subject).
  397.             trk_debug( trk_styles( $type ),']');
  398.         break;
  399.     case 'float':
  400.         $ret.=
  401.             trk_debug( trk_styles( $type ), $type.'[').
  402.             htmlentities($subject).
  403.             trk_debug( trk_styles( $type ),']');
  404.         break;
  405.     case 'bool':
  406.         $ret.=
  407.             trk_debug( trk_styles( $type ), $type.'[').
  408.             htmlentities($subject===true?'True':'False').
  409.             trk_debug( trk_styles( $type ),']');
  410.         break;
  411.  
  412.     case 'null':
  413.         $ret.=
  414.             trk_debug( trk_styles( $type ), $type.'[').
  415.             htmlentities('NULL').
  416.             trk_debug( trk_styles( $type ),']');
  417.         break;
  418.     case 'object':
  419.     case 'unknown':
  420.     default:
  421.  
  422.         $ret.=
  423.             trk_debug( trk_styles( $type ),$type.'[').
  424.             htmlentities( strtoupper( $type ) ).
  425.             trk_debug( trk_styles( $type ),']');
  426.         break;     
  427.     }
  428.     return $ret;
  429. }
  430.  
  431. // include Tim Keal's table of contents library
  432.  
  433. function fasttoc( $path = '', $ext = array() )
  434. {
  435.     $nen = nen_ini();
  436.  
  437.     if( is_string( $ext ) )
  438.     {
  439.         $ext = array( 0 => $ext );
  440.     }
  441.    
  442.     $buffer = ( is_dir( $nen['root'] . $path ) ? array_diff( scandir( $nen['root'] . $path, SCANDIR_SORT_ASCENDING ), array('..', '.' ) ) : array() );
  443.    
  444.     $toc = array();
  445.    
  446.     foreach( $buffer as $entry )
  447.     {
  448.         $hit = 0;
  449.         foreach( $ext as $test )
  450.         {
  451.        
  452.             if( strtolower( substr( $entry, -strlen( $test ), strlen( $test ) ) ) === strtolower( $test ) )
  453.             {
  454.                 $hit++;
  455.                 break;
  456.             }
  457.         }
  458.         if( $hit > 0 )
  459.         {
  460.             $toc[ count( $toc ) ] = $entry;
  461.         }
  462.     }
  463.    
  464.     /*
  465.     trk_echo2('toc',$toc);
  466.     */
  467.    
  468.     return $toc;
  469. }
  470.  
  471. function me_shorthand()
  472. {
  473.     $pattern = '/(mk)(11|45|71)([A-Z]+)(\.php)/';
  474.     if( preg_match($pattern, $_SERVER['SCRIPT_NAME'], $matches) )
  475.     {
  476.         return array( 'mk' => $matches[ 2 ], 'me'=> $matches[ 3 ] );
  477.     }
  478.     else
  479.     {
  480.         return null;
  481.     }
  482. }
  483.  
  484. // top of page (title)
  485. echo '<html><head><title>Roe XAMPP Builder (mk'.
  486. ( !is_null( $nen['me'] ) ? ' '. $nen['me']['mk'] : '').
  487. ( !is_null( $nen['me'] ) ? ' '. $nen['me']['me'] : '').
  488. ')</title><style>'. nen_css() . '</style></head><body>';
  489.  
  490. // copyright header
  491. echo '<table style="width:100%;text-align:top;"><tr><th colspan="2">
  492. <span style="alignment:center center;font-size:170%;">Roe Collector Card Game</span><br><span style="alignment:center center;font-size:120%;">Copyright 1997, 2021 Tim Keal</span><br><span style="alignment:center center;font-size:100%;">mk'. $nen['me']['mk']. $nen['me']['me']. '</span></th></tr>';
  493.  
  494. // perform regex upon data from table of contents
  495. //if( !is_null( $nen['me'] ) ){
  496.     //trk_echo2('result', regex_toc( $nen['me']['mk'], $nen['me']['me'] ));
  497. //}
  498.  
  499. // bottom of page
  500. echo '</body></html>';
  501.    
  502. function regex_toc()
  503. {
  504.     $nen = nen_ini();
  505.    
  506.     // table of contents filename pattern
  507.     $toc_index = 0;
  508.     $toc_pattern = '/cards\.dat/';
  509.    
  510.     $toc = fasttoc( 'cards src data', array( '.dat' ) );
  511.  
  512.     $toc_result = '';
  513.  
  514.     $card_replace = "$0\n";
  515.    
  516.     $toc_lines = array();
  517.    
  518.     $result = '';
  519.    
  520.     // loop through toc
  521.     while(
  522.         ($toc_index > count( $toc ) - 1)?false:preg_match(
  523.         $toc_pattern,
  524.         $toc[ $toc_index ],
  525.         $toc_matches
  526.         )
  527.     )
  528.     {      
  529.         // echo current toc filename
  530.         echo '<h3>'. htmlentities( $toc[ $toc_index ] ). '</h3>';
  531.                
  532.         // load current document by toc index into page memory
  533.         $page = file_get_contents( $nen[ 'root' ]. 'cards src data/'. $toc[ $toc_index ] );
  534.        
  535.         // echo currently loaded document
  536.         echo str_replace( "\r\n", '<br>'."\r\n", htmlentities( $page ) );
  537.        
  538.         $result .= regex_cards( $page );
  539.        
  540.         // increment one table of contents file higher
  541.         $toc_index++;      
  542.        
  543.     }
  544.    
  545.     return $result;
  546.    
  547. }
  548. function regex_cards( $page = '' )
  549. {
  550.     $nen = nen_ini();
  551.     $result = '';
  552.    
  553.     //load card regular expression
  554.    
  555.     $card_pattern ='/CASE "(?<cardname>_{4}|[a-z][a-z0-9_]{3})"(?:\s*+(?!CASE).*\n)*/m';
  556.     $card_pattern2 = file_get_contents( $nen[ 'root' ]. 'pcre/card data.pattern.pcre' );
  557.     if( $card_pattern !== $card_pattern2 )
  558.     {
  559.         $card_pattern = $card_pattern2;
  560.     }
  561.     unset( $card_pattern2 );
  562.    
  563.     // assign replacement (unused)
  564.     $card_replace = "$0\n";
  565.    
  566.     // create desination folder if it does not exist
  567.     if( !is_dir( $nen[ 'root' ]. 'cards out data' ) )
  568.     {
  569.         mkdir ( $nen[ 'root' ]. 'cards out data' );
  570.     }
  571.    
  572.     // perform current page data regex
  573.    
  574.     if(
  575.         preg_match_all(
  576.         $card_pattern,
  577.         $page,
  578.         $card_matches
  579.         )
  580.     )  
  581.     {
  582.         //foreach($card_matches as $card_key => $card_value )
  583.         //{
  584.             //trk_echo2( 'card', array( 'cardname' => $card_matches[ 'cardname' ], 'card_value' => $card_matches[ 0 ] ) );
  585.            
  586.             // save creation card collection to file
  587.             file_put_contents( $nen[ 'root' ]. 'cards out data/'. $card_matches[ 'cardname' ][ 0 ]. '.dat', $card_matches[ 0 ][ 0 ] );
  588.         //}
  589.        
  590.     }
  591.            
  592.     return $result;
  593.    
  594. }
  595.  
  596. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement