Created
August 16, 2012 17:35
-
-
Save elegos/3371913 to your computer and use it in GitHub Desktop.
World of Warcraft MogIt addon's link functions Lua -> PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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