Advertisement
jargon

mk11 TILES.php

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