雷同于腾讯QQ等级,采用方式:5进制
class user_level extends user {
//根据经验获取等级
public function _get_level() {
if($this->_id() < 1) {
return false;
}
//获取用户信息
$userinfo = $this->_cookie();
$exp = $userinfo['exp'];
$exp = $exp >=0 ? $exp : 0;
//等级数据
return $this->level_row($exp);
}
/*+---------------------------------公共的-------------------------------+*/
/**
* 10进制转换5进制
*
*/
private function num_10to5($num, $bin = 5) {
$arr=array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F');
$bytelen=ceil(16/$bin); //获得如果是$bin进制,一个字节的长度
$aOutChar=array();
$num=intval($num);
if($num===0){
return 0;
}
while($num>0)
{
$t=$arr[$num%$bin].$t;
$num=floor($num/$bin);
}
$tlen=strlen($t);
if($tlen%$bytelen!=0)
{
$pad_len=$bytelen-$tlen%$bytelen;
$t=str_pad("",$pad_len,"0",STR_PAD_LEFT).$t; //不足一个字节长度,自动前面补充0
}
return $t;
}
/**
* 拆分ICON 0010
*
* @param mixed $icon
*/
private function get_icon($icon) {
$len = strlen($icon);
//皇冠
$ico[] = (int)substr($icon, 0, $len - 3);
//太阳
$ico[] = (int)$icon[$len - 3];
//月亮
$ico[] = (int)$icon[$len - 2];
//星星
$ico[] = (int)$icon[$len - 1];
return $ico;
}
/**
* 根据经验查找等级信息
*
* @param int $exp 经验值
* @return array
*/
private function level_row($exp) {
$exp = $exp >=0 ? $exp : 0;
if($exp <= 500){
$base = 0;
$level = $base + $this->level($exp , 100);
$diff = ($level - $base) * 100 + 1 - ($exp - 0);
} else if($exp > 500 && $exp <= 2000) {
$base = 5;
$level = $base + $this->level($exp - 500 , 300);
$diff = ($level - $base) * 300 + 1 - ($exp - 500);
} else {
$base = 10;
$level = $base + $this->level($exp - 2000 , 400);
$diff = ($level -$base ) * 400 + 1 - ($exp - 2000);
}
//获得图标
$ico = $this->get_icon($this->num_10to5($level));
return array('level' => $level, 'diff' => $diff , 'ico' => $ico);
}
/**
* 计算等级
*
* @param int $exp 经验
* @param int $size 间隔
*/
private function level($exp , $size){
$level = $exp % $size;
if($level < 1) {
$level = $exp/$size;
} else {
$level = ceil($exp/$size);
}
return $level >= 1 ? $level : 1;
}
}
class user_level extends user {
//根据经验获取等级
public function _get_level() {
if($this->_id() < 1) {
return false;
}
//获取用户信息
$userinfo = $this->_cookie();
$exp = $userinfo['exp'];
$exp = $exp >=0 ? $exp : 0;
//等级数据
return $this->level_row($exp);
}
/*+---------------------------------公共的-------------------------------+*/
/**
* 10进制转换5进制
*
*/
private function num_10to5($num, $bin = 5) {
$arr=array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F');
$bytelen=ceil(16/$bin); //获得如果是$bin进制,一个字节的长度
$aOutChar=array();
$num=intval($num);
if($num===0){
return 0;
}
while($num>0)
{
$t=$arr[$num%$bin].$t;
$num=floor($num/$bin);
}
$tlen=strlen($t);
if($tlen%$bytelen!=0)
{
$pad_len=$bytelen-$tlen%$bytelen;
$t=str_pad("",$pad_len,"0",STR_PAD_LEFT).$t; //不足一个字节长度,自动前面补充0
}
return $t;
}
/**
* 拆分ICON 0010
*
* @param mixed $icon
*/
private function get_icon($icon) {
$len = strlen($icon);
//皇冠
$ico[] = (int)substr($icon, 0, $len - 3);
//太阳
$ico[] = (int)$icon[$len - 3];
//月亮
$ico[] = (int)$icon[$len - 2];
//星星
$ico[] = (int)$icon[$len - 1];
return $ico;
}
/**
* 根据经验查找等级信息
*
* @param int $exp 经验值
* @return array
*/
private function level_row($exp) {
$exp = $exp >=0 ? $exp : 0;
if($exp <= 500){
$base = 0;
$level = $base + $this->level($exp , 100);
$diff = ($level - $base) * 100 + 1 - ($exp - 0);
} else if($exp > 500 && $exp <= 2000) {
$base = 5;
$level = $base + $this->level($exp - 500 , 300);
$diff = ($level - $base) * 300 + 1 - ($exp - 500);
} else {
$base = 10;
$level = $base + $this->level($exp - 2000 , 400);
$diff = ($level -$base ) * 400 + 1 - ($exp - 2000);
}
//获得图标
$ico = $this->get_icon($this->num_10to5($level));
return array('level' => $level, 'diff' => $diff , 'ico' => $ico);
}
/**
* 计算等级
*
* @param int $exp 经验
* @param int $size 间隔
*/
private function level($exp , $size){
$level = $exp % $size;
if($level < 1) {
$level = $exp/$size;
} else {
$level = ceil($exp/$size);
}
return $level >= 1 ? $level : 1;
}
}