Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added formatting for "color coded" text from Minecraft #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions MCServerStatus/Minecraft/Formatting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Minecraft;

class Formatting
{
private static function foregroundColorCodeToHex( $color )
{
$foregroundColor = array( '0' => "000000", '1' => "0000AA", '2' => "00AA00", '3' => "00AAAA",
'4' => "AA0000", '5' => "AA00AA", '6' => "FFAA00", '7' => "AAAAAA",
'8' => "555555", '9' => "5555FF", 'a' => "55FF55", 'b' => "55FFFF",
'c' => "FF5555", 'd' => "FF55FF", 'e' => "FFFF55", 'f' => "FFFFFF" );
return $foregroundColor[$color];
}

private static function backgoundColorCodeToHex( $color )
{
$backgroundColor= array( '0' => "000000", '1' => "00002A", '2' => "002A00", '3' => "002A2A",
'4' => "2A0000", '5' => "2A002A", '6' => "2A2A00", '7' => "2A2A2A",
'8' => "151515", '9' => "15153F", 'a' => "153F15", 'b' => "153F3F",
'c' => "3F1515", 'd' => "3F153F", 'e' => "3F3F15", 'f' => "3F3F3F" );
return $backgroundColor[$color];
}

private static function formatStyle( $style )
{
switch( $style )
{
case "k": return ""; // obfuscated
case "l": return "font-weight:bold;"; // bold
case "m": return "text-decoration:line-through"; // strikethrough
case "n": return "text-decoration:underline"; // underline
case "o": return "font-style:italic"; // italic
}
}

private static function formatStart( $fgColor, $bgColor, $style )
{
$result = "";
if ($fgColor != null) { $result .= "color: #".Formatting::foregroundColorCodeToHex($fgColor).";"; }
if ($bgColor != null) { $result .= "background-color: #".Formatting::backgoundColorCodeToHex($bgColor).";"; }
if ($style != null) { $result .= Formatting::formatStyle($style); }

return "<span style=\"".$result."\">";
}

private static function formatEnd()
{
return "</span>";
}

private static function isColorChar( $char )
{
return (($char >= '0' && $char <= '9') || ($char >= 'a' && $char <= 'f'));
}

private static function isStyleChar( $char )
{
return ($char >= 'k' && $char <= 'o');
}

// Note: does not support obfuscated style
public static function formatText( $text )
{
$fgColor = null;
$bgColor = null;
$format = null;

$parsing = false;
$previousFormatChar = null;
$inStyle = false;

$result = "";

for ($idxChar = 0; $idxChar < strlen($text); $idxChar++)
{
$char = $text[$idxChar];
if ($char == "\xA7") // Check for section sign
{
if ($inStyle)
{
// Close previos styling
$result .= Formatting::formatEnd();
$inStyle = false;
}
$previousFormatChar = null;
$parsing = true;
}
else if ($parsing)
{
$validChar = false;
if (Formatting::isColorChar($char))
{
// Valid color
if ($previousFormatChar == null)
{
$fgColor = $char;
$validChar = true;
}
else if (Formatting::isColorChar($previousFormatChar))
{
$bgColor = $char;
$validChar = true;
}
else
{
$validChar = false;
}
}
else if (Formatting::isStyleChar($char))
{
// Valid style
if ($previousFormatChar == null)
{
$format = $char;
$validChar = true;
}
else
{
$validChar = false;
}
}
else if ($char == 'r')
{
// Reset
$validChar = true;

$fgColor = null;
$bgColor = null;
$format = null;

if ($inStyle)
{
$result .= Formatting::formatEnd();
}
$inStyle = false;
}

if ($validChar)
{
$previousFormatChar = $char;
}
else
{
if ($fgColor != null || $bgColor != null || $format != null)
{
$result .= Formatting::formatStart($fgColor, $bgColor, $format);
$inStyle = true;
}
$parsing = false;
$previousFormatChar = null;
$result .= $char;
}
}
else
{
$result .= $char;
}
}
if ($inStyle)
{
// Close previos styling
$result .= Formatting::formatEnd();
$inStyle = false;
}

return $result;
}
}
4 changes: 2 additions & 2 deletions demo/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Autoloader. Use SPL in a real project.
foreach(array('Server', 'Stats', 'StatsException') as $file) {
foreach(array('Server', 'Stats', 'StatsException', 'Formatting') as $file) {
include sprintf('../MCServerStatus/Minecraft/%s.php', $file);
}

Expand Down Expand Up @@ -51,7 +51,7 @@
<span class="badge badge-important"><i class="icon-remove icon-white"></i></span>
<?php endif; ?>
</td>
<td class="motd"><?php echo $stats->motd; ?> <code><?php echo $server; ?></code></td>
<td class="motd"><?php echo \Minecraft\Formatting::formatText($stats->motd); ?> <code><?php echo $server; ?></code></td>
<td><?php printf('%u/%u', $stats->online_players, $stats->max_players); ?></td>
</tr>
<?php unset($stats); ?>
Expand Down