Skip to content

Instantly share code, notes, and snippets.

@elegos
Created August 16, 2012 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elegos/3371913 to your computer and use it in GitHub Desktop.
Save elegos/3371913 to your computer and use it in GitHub Desktop.
World of Warcraft MogIt addon's link functions Lua -> PHP
<?php
class MogIt {
private $charset;
private $base;
private $maxlen;
function __construct($charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", $maxlen = 3)
{
$this->charset = $charset;
$this->base = strlen($charset);
$this->maxlen = $maxlen;
}
function toBase($num) {
$base = $this->base;
$charset = $this->charset;
$str = "";
while($num > 0)
{
$str = $charset[($num%$base)] . $str;
$num = floor($num/$base);
}
return $str;
}
function fromBase($str) {
$base = $this->base;
$charset = $this->charset;
$len = strlen($str);
$num = 0;
for($i = 0; $i < $len; $i++)
{
$num += (strpos($charset, $str[$i])) * pow($base, $len-$i - 1);
}
return $num;
}
function setToLink($set) {
$maxlen = $this->maxlen;
$link = "[MogIt:";
foreach($set as $k => $v)
{
$link .= sprintf("%0".$maxlen."s", $this->toBase($v));
}
$link .= "]";
return $link;
}
function linkToSet($link) {
$maxlen = $this->maxlen;
$set = array();
$matches = preg_match("/MogIt:([^]]+)/", $link, $items);
if(!$matches)
return $set;
$len = strlen($items[1]);
$max = floor($len/$maxlen);
for($i = 0; $i < $max; $i++)
{
$set[] = $this->fromBase(substr($items[1], $i*$maxlen, $maxlen));
}
return $set;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment