#!/usr/bin/php
<?php
  $dir = "/home/duke/aa/servers/YOURDIRECTORY/var/"; //the directory location of the file
  $file = "won_matches.txt";         //the file you will rotate
  
     // Your border colors. Add/remove lines as you please.
    // Add a color multiple times to make it appear more often, or only once to make it rare.
   // Example: 1/16 (6.25%); 3/16 (18.75%); 8/16 (50%)
          $color_border = array("0x444488", // 1.blue
                               "0x444488", // 2.blue again
                              "0x444488", // 3.blue once more
                             "0x888888", // 4.gray
                            "0x888888", // 5.gray again
                           "0x888888", // 6.gray once more
                          "0x884444", // 7.red
                         "0x884444", // 8.red again
                        "0x448844", // 9.green
                       "0x448844", // 10.green again
                      "0x448888", // 11.cyan border+purple text
                     "0x888844", // 12.yellow
                    "0xaaaaaa", // 13.gray (inverted)
                   "0x8888bb", // 14.blue (inverted)
                  "0xbb8888", // 15.red (inverted)
                "0x88bb88"); // 16.green (inverted)
                                             
          // Your text colors - same amount and order as the border colors, otherwise wtf broken.
            $color_text = array("0x8888bb", // 1.blue
                               "0x8888bb", // 2.blue again
                              "0x8888bb", // 3.blue once more
                             "0xdddddd", // 4.gray
                            "0xdddddd", // 5.gray again
                           "0xdddddd", // 6.gray once more
                          "0xbb8888", // 7.red
                         "0xbb8888", // 8.red again
                        "0x88bb88", // 9.green
                       "0x88bb88", // 10.green again
                      "0x8888bb", // 11.cyan border+purple text
                     "0xbbbb88", // 12.yellow
                    "0x888888", // 13.gray (inverted)
                   "0x444488", // 14.blue (inverted)
                  "0x884444", // 15.red (inverted)
                "0x448844"); // 16.green (inverted)         
         
  
$trigger = "NEW_MATCH";

$file = $dir.$file;
while(!feof(STDIN))
{
  $line = rtrim(fgets(STDIN));
  $part = explode(" ", $line);
  switch($part[0])
  {
    case "INVALID_COMMAND":
      if($part[1] == "/leaderboard" || $part[1] == "/ladder")
      {
        pm($part[2],getBoard());
      }
      break;
    case $trigger:
      console_message(getBoard());
      break;
  }
}

function getBoard()
{
  $fp = fopen($file, 'r');
  
  $colornumber = mt_rand(0, count($color_border)-1);  // randomizer by Titanoboa,
  $color_1 = $color_border[$colornumber];            // anyone may use or modify
  $color_2 = $color_text[$colornumber];             // as they please.  \(_)/

  $board  = "{$color_1}***********************************************************" . '\\n';   
  $board .= "{$color_1}***************    {$color_2}Top 10 Match Winners   {$color_1}*****************" . '\\n';
  $board .= "{$color_1}***********************************************************" . '\\n';
  $board .= "{$color_1}**   {$color_2}RANK   {$color_1}***          {$color_2}PLAYER           {$color_1}***    {$color_2}WINS    {$color_1}**" . '\\n';
  $board .= "{$color_1}***********************************************************" . '\\n';
  
  for ($i = 0; $i < 10; $i++)
  {
    $log = fgets($fp);
    
    if (feof($fp))
      break;
    
    list($wins, $player) = preg_split('/\s+/', $log);
    
    $rank = $i + 1;
    $space_rank = 6 - strlen($rank);
    $space_wins = 7 - strlen($wins);
    $space_player = 23 - strlen( stripcslashes($player) );
    
    $board .= "{$color_1}**    {$color_2}{$rank}" . str_repeat(" ", $space_rank) . "{$color_1}***    {$color_2}" . stripslashes($player) . str_repeat(" ", $space_player) . "{$color_1}***     {$color_2}{$wins}" . str_repeat(" ", $space_wins) . "{$color_1}**" . '\\n';
  }
  
  $board .= "{$color_1}***********************************************************";
  
  fclose($fp);
  
  return $board;
}

function console_message($message)
{
  echo "CONSOLE_MESSAGE {$message}" . "\n";
}

function pm($p,$msg)
{
  $msg = str_replace(["\\","\n","\""],["\\\\","\\\n","\\\""],$msg);
  //note: the color is a workaround for the script potentially interpreting a number name as an ID. The server filters the color.
  print("PLAYER_MESSAGE 0xRESETT$p \"$msg\"\n");
}

?>