forked from devsam/core
유틸리티 함수들 일부 수정. 랜덤값 뽑기 함수 하나 더 추가
This commit is contained in:
+1
-1
@@ -19,7 +19,7 @@ class Json
|
||||
$value = Util::eraseNullValue($value);
|
||||
}
|
||||
|
||||
if(($flag & static::EMPTY_ARRAY_IS_DICT) && $value === []){
|
||||
if($value === [] && ($flag & static::EMPTY_ARRAY_IS_DICT)){
|
||||
$value = (object)null;
|
||||
}
|
||||
return json_encode($value, $rawFlag);
|
||||
|
||||
+4
-12
@@ -311,13 +311,9 @@ class Session
|
||||
* 로그인 유저의 전역 grade를 받아옴
|
||||
* @return int|null
|
||||
*/
|
||||
public static function getUserGrade(bool $requireLogin = false, string $exitPath = '..')
|
||||
public static function getUserGrade($actionOnError = '..')
|
||||
{
|
||||
if ($requireLogin) {
|
||||
$obj = self::requireLogin($exitPath);
|
||||
} else {
|
||||
$obj = self::getInstance();
|
||||
}
|
||||
$obj = self::requireLogin($actionOnError);
|
||||
|
||||
return $obj->userGrade;
|
||||
}
|
||||
@@ -327,13 +323,9 @@ class Session
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public static function getUserID(bool $requireLogin = false, string $exitPath = '..')
|
||||
public static function getUserID($actionOnError = '..')
|
||||
{
|
||||
if ($requireLogin) {
|
||||
$obj = self::requireLogin($exitPath);
|
||||
} else {
|
||||
$obj = self::getInstance();
|
||||
}
|
||||
$obj = self::requireLogin($actionOnError);
|
||||
|
||||
return $obj->userID;
|
||||
}
|
||||
|
||||
@@ -442,6 +442,9 @@ class Util extends \utilphp\util
|
||||
{
|
||||
$sum = 0;
|
||||
foreach ($items as $value) {
|
||||
if($value <= 0){
|
||||
continue;
|
||||
}
|
||||
$sum += $value;
|
||||
}
|
||||
|
||||
@@ -458,6 +461,36 @@ class Util extends \utilphp\util
|
||||
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()
|
||||
*
|
||||
@@ -470,6 +503,9 @@ class Util extends \utilphp\util
|
||||
return $items[array_rand($items)];
|
||||
}
|
||||
|
||||
/**
|
||||
* fqn 클래스 경로에서 클래스 이름을 받아옴
|
||||
*/
|
||||
function getClassName(string $classpath)
|
||||
{
|
||||
if ($pos = strrpos($classpath, '\\')) return substr($classpath, $pos + 1);
|
||||
|
||||
Reference in New Issue
Block a user