유틸리티 함수들 일부 수정. 랜덤값 뽑기 함수 하나 더 추가

This commit is contained in:
2018-10-04 01:08:56 +09:00
parent 258aabbdde
commit e909b48fea
3 changed files with 41 additions and 13 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ class Json
$value = Util::eraseNullValue($value); $value = Util::eraseNullValue($value);
} }
if(($flag & static::EMPTY_ARRAY_IS_DICT) && $value === []){ if($value === [] && ($flag & static::EMPTY_ARRAY_IS_DICT)){
$value = (object)null; $value = (object)null;
} }
return json_encode($value, $rawFlag); return json_encode($value, $rawFlag);
+4 -12
View File
@@ -311,13 +311,9 @@ class Session
* 로그인 유저의 전역 grade를 받아옴 * 로그인 유저의 전역 grade를 받아옴
* @return int|null * @return int|null
*/ */
public static function getUserGrade(bool $requireLogin = false, string $exitPath = '..') public static function getUserGrade($actionOnError = '..')
{ {
if ($requireLogin) { $obj = self::requireLogin($actionOnError);
$obj = self::requireLogin($exitPath);
} else {
$obj = self::getInstance();
}
return $obj->userGrade; return $obj->userGrade;
} }
@@ -327,13 +323,9 @@ class Session
* *
* @return int|null * @return int|null
*/ */
public static function getUserID(bool $requireLogin = false, string $exitPath = '..') public static function getUserID($actionOnError = '..')
{ {
if ($requireLogin) { $obj = self::requireLogin($actionOnError);
$obj = self::requireLogin($exitPath);
} else {
$obj = self::getInstance();
}
return $obj->userID; return $obj->userID;
} }
+36
View File
@@ -442,6 +442,9 @@ class Util extends \utilphp\util
{ {
$sum = 0; $sum = 0;
foreach ($items as $value) { foreach ($items as $value) {
if($value <= 0){
continue;
}
$sum += $value; $sum += $value;
} }
@@ -458,6 +461,36 @@ class Util extends \utilphp\util
return key($items); return key($items);
} }
/**
* 각 값의 비중에 따라 랜덤한 값을 선택.
*
* @param array $items 각 수치와 비중. [값, weight] 으로 보관
*
* @return object 선택된 랜덤 값의 첫번째 값
*/
public static function choiceRandomUsingWeightPair(array $items)
{
$sum = 0;
foreach ($items as [$item, $value]) {
if($value <= 0){
continue;
}
$sum += $value;
}
$rd = self::randF()*$sum;
foreach ($items as [$item, $value]) {
if ($rd <= $value) {
return $item;
}
$rd -= $value;
}
//fallback. 이곳으로 빠지지 않음
end($items);
return $items[key($items)][0];
}
/** /**
* 배열의 아무거나 고름. Python의 random.choice() * 배열의 아무거나 고름. Python의 random.choice()
* *
@@ -470,6 +503,9 @@ class Util extends \utilphp\util
return $items[array_rand($items)]; return $items[array_rand($items)];
} }
/**
* fqn 클래스 경로에서 클래스 이름을 받아옴
*/
function getClassName(string $classpath) function getClassName(string $classpath)
{ {
if ($pos = strrpos($classpath, '\\')) return substr($classpath, $pos + 1); if ($pos = strrpos($classpath, '\\')) return substr($classpath, $pos + 1);