refac: brandonwamboldt/utilphp 의존 코드 수정

- 실제 의존성 제거는 이후에에
This commit is contained in:
2025-10-07 19:26:22 +09:00
parent 24bc8985d0
commit 42fac46062
28 changed files with 142 additions and 36 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ $queryMap = [
]; ];
if ($reqQueryType === null || !key_exists($reqQueryType, $queryMap)) { if ($reqQueryType === null || !key_exists($reqQueryType, $queryMap)) {
$reqQueryType = Util::array_first_key($queryMap); $reqQueryType = array_key_first($queryMap);
} }
//로그인 검사 //로그인 검사
+1 -1
View File
@@ -559,7 +559,7 @@ function checkStatistic()
$nationHist = ''; $nationHist = '';
foreach (GameConst::$availableNationType as $nationType) { foreach (GameConst::$availableNationType as $nationType) {
if (!Util::array_get($nationHists[$nationType])) { if (!($nationHists[$nationType] ?? null)) {
$nationHists[$nationType] = '-'; $nationHists[$nationType] = '-';
} }
$nationHist .= getNationType($nationType) . "({$nationHists[$nationType]}), "; $nationHist .= getNationType($nationType) . "({$nationHists[$nationType]}), ";
+2 -2
View File
@@ -138,7 +138,7 @@ function JSCitiesBasedOnDistance(int $cityNo, int $maxDistance = 1): array{
$distanceList = searchDistance($cityNo, $maxDistance, true); $distanceList = searchDistance($cityNo, $maxDistance, true);
$result = []; $result = [];
for ($dist = 1; $dist <= $maxDistance; $dist++) { for ($dist = 1; $dist <= $maxDistance; $dist++) {
$result[$dist] = Util::array_get($distanceList[$dist], []); $result[$dist] = $distanceList[$dist] ?? [];
} }
return $result; return $result;
} }
@@ -152,7 +152,7 @@ function printCitiesBasedOnDistance(int $cityNo, int $maxDistance = 1): string
for ($dist = 1; $dist <= $maxDistance; $dist++) { for ($dist = 1; $dist <= $maxDistance; $dist++) {
$cityList = array_map(function ($cityID) { $cityList = array_map(function ($cityID) {
return CityConst::byID($cityID)->name; return CityConst::byID($cityID)->name;
}, Util::array_get($distanceList[$dist], [])); }, $distanceList[$dist] ?? []);
$cityStr = join(', ', $cityList); $cityStr = join(', ', $cityList);
+1 -1
View File
@@ -526,7 +526,7 @@ function DeleteConflict($nation)
function getConquerNation($city): int function getConquerNation($city): int
{ {
$conflict = Json::decode($city['conflict']); $conflict = Json::decode($city['conflict']);
return Util::array_first_key($conflict); return array_key_first($conflict);
} }
function ConquerCity(array $admin, General $general, array $city, array $defenderCityGeneralList) function ConquerCity(array $admin, General $general, array $city, array $defenderCityGeneralList)
@@ -33,7 +33,7 @@ class event_전투특기_공성 extends \sammo\BaseItem{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_SIEGE; $myArmType = 'dex'.GameUnitConst::T_SIEGE;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -28,7 +28,7 @@ class event_전투특기_궁병 extends \sammo\BaseItem{
if($statName === 'warAvoidRatio'){ if($statName === 'warAvoidRatio'){
return $value + 0.2; return $value + 0.2;
} }
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_ARCHER; $myArmType = 'dex'.GameUnitConst::T_ARCHER;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -28,7 +28,7 @@ class event_전투특기_귀병 extends \sammo\BaseItem{
if($statName === 'warMagicSuccessProb'){ if($statName === 'warMagicSuccessProb'){
return $value + 0.2; return $value + 0.2;
} }
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_WIZARD; $myArmType = 'dex'.GameUnitConst::T_WIZARD;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -32,7 +32,7 @@ class event_전투특기_기병 extends \sammo\BaseItem{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_CAVALRY; $myArmType = 'dex'.GameUnitConst::T_CAVALRY;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -33,7 +33,7 @@ class event_전투특기_보병 extends \sammo\BaseItem{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_FOOTMAN; $myArmType = 'dex'.GameUnitConst::T_FOOTMAN;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -35,7 +35,7 @@ class che_event_공성 extends \sammo\BaseSpecial{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_SIEGE; $myArmType = 'dex'.GameUnitConst::T_SIEGE;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -31,7 +31,7 @@ class che_event_궁병 extends \sammo\BaseSpecial{
if($statName === 'warAvoidRatio'){ if($statName === 'warAvoidRatio'){
return $value + 0.2; return $value + 0.2;
} }
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_ARCHER; $myArmType = 'dex'.GameUnitConst::T_ARCHER;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -30,7 +30,7 @@ class che_event_귀병 extends \sammo\BaseSpecial{
if($statName === 'warMagicSuccessProb'){ if($statName === 'warMagicSuccessProb'){
return $value + 0.2; return $value + 0.2;
} }
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_WIZARD; $myArmType = 'dex'.GameUnitConst::T_WIZARD;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -35,7 +35,7 @@ class che_event_기병 extends \sammo\BaseSpecial{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_CAVALRY; $myArmType = 'dex'.GameUnitConst::T_CAVALRY;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -35,7 +35,7 @@ class che_event_보병 extends \sammo\BaseSpecial{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_FOOTMAN; $myArmType = 'dex'.GameUnitConst::T_FOOTMAN;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
+1 -1
View File
@@ -35,7 +35,7 @@ class che_공성 extends \sammo\BaseSpecial{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_SIEGE; $myArmType = 'dex'.GameUnitConst::T_SIEGE;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
+1 -1
View File
@@ -31,7 +31,7 @@ class che_궁병 extends \sammo\BaseSpecial{
if($statName === 'warAvoidRatio'){ if($statName === 'warAvoidRatio'){
return $value + 0.2; return $value + 0.2;
} }
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_ARCHER; $myArmType = 'dex'.GameUnitConst::T_ARCHER;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
+1 -1
View File
@@ -30,7 +30,7 @@ class che_귀병 extends \sammo\BaseSpecial{
if($statName === 'warMagicSuccessProb'){ if($statName === 'warMagicSuccessProb'){
return $value + 0.2; return $value + 0.2;
} }
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_WIZARD; $myArmType = 'dex'.GameUnitConst::T_WIZARD;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
+1 -1
View File
@@ -35,7 +35,7 @@ class che_기병 extends \sammo\BaseSpecial{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_CAVALRY; $myArmType = 'dex'.GameUnitConst::T_CAVALRY;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
+1 -1
View File
@@ -35,7 +35,7 @@ class che_보병 extends \sammo\BaseSpecial{
} }
public function onCalcStat(General $general, string $statName, $value, $aux=null){ public function onCalcStat(General $general, string $statName, $value, $aux=null){
if(\sammo\Util::starts_with($statName, 'dex')){ if(str_starts_with($statName, 'dex')){
$myArmType = 'dex'.GameUnitConst::T_FOOTMAN; $myArmType = 'dex'.GameUnitConst::T_FOOTMAN;
$opposeArmType = 'dex'.$aux['opposeType']->armType;; $opposeArmType = 'dex'.$aux['opposeType']->armType;;
if($aux['isAttacker'] && $opposeArmType === $statName){ if($aux['isAttacker'] && $opposeArmType === $statName){
@@ -208,7 +208,7 @@ class che_랜덤임관 extends Command\GeneralCommand
foreach ($rawGeneralsCnt as $nation) { foreach ($rawGeneralsCnt as $nation) {
$calcCnt = $nation['warpower'] + $nation['develpower']; $calcCnt = $nation['warpower'] + $nation['develpower'];
if ($general->getNPCType() < 2 && Util::starts_with($nation['name'], 'ⓤ')) { if ($general->getNPCType() < 2 && str_starts_with($nation['name'], 'ⓤ')) {
$calcCnt *= 100; $calcCnt *= 100;
} }
+1 -1
View File
@@ -164,7 +164,7 @@ class che_출병 extends Command\GeneralCommand
$currDist = 999; $currDist = 999;
$minDist = Util::array_first_key($distanceList); $minDist = array_key_first($distanceList);
do { do {
//1: 최단 거리 도시 중 공격 대상이 있는가 확인 //1: 최단 거리 도시 중 공격 대상이 있는가 확인
//2: 최단 거리 + 1 도시 중 공격 대상이 있는가 확인 //2: 최단 거리 + 1 도시 중 공격 대상이 있는가 확인
+1 -1
View File
@@ -127,7 +127,7 @@ class ChangeCity extends \sammo\Event\Action{
return DB::db()->sqleval('ROUND(%b * %d, 0)', $keyMax, $value/100); return DB::db()->sqleval('ROUND(%b * %d, 0)', $keyMax, $value/100);
} }
if(Util::ends_with($key, '_max') && preg_match(self::REGEXP_MATH, $value, $matches)){ if(str_ends_with($key, '_max') && preg_match(self::REGEXP_MATH, $value, $matches)){
//key가 아예 최대치에 대한 값이면 연산은 결과값이 0보다만 높게. //key가 아예 최대치에 대한 값이면 연산은 결과값이 0보다만 높게.
$op = $matches[1]; $op = $matches[1];
$value = $matches[2]; $value = $matches[2];
+1 -1
View File
@@ -2084,7 +2084,7 @@ class GeneralAI
} }
} }
$finalCityID = Util::array_first_key($cityScoreList); $finalCityID = array_key_first($cityScoreList);
$dist = $distanceList[$capital][$finalCityID]; $dist = $distanceList[$capital][$finalCityID];
$targetCityID = $finalCityID; $targetCityID = $finalCityID;
if ($dist > 1) { if ($dist > 1) {
+1 -1
View File
@@ -62,7 +62,7 @@ abstract class TriggerCaller{
return $this; return $this;
} }
$lastKey = Util::array_last_key($this->triggerListByPriority); $lastKey = array_key_last($this->triggerListByPriority);
if($lastKey < $priority){ if($lastKey < $priority){
$this->triggerListByPriority[$priority] = [$uniqueID=>$trigger]; $this->triggerListByPriority[$priority] = [$uniqueID=>$trigger];
return $this; return $this;
+2 -2
View File
@@ -174,7 +174,7 @@ function tryNpmInstall()
tryComposerInstall(); tryComposerInstall();
if (tryNpmInstall()) { if (tryNpmInstall()) {
genJS(Util::array_last_key(ServConfig::getServerList())); genJS(array_key_last(ServConfig::getServerList()));
} }
$session = Session::requireLogin(null)->setReadOnly(); $session = Session::requireLogin(null)->setReadOnly();
@@ -227,7 +227,7 @@ if (!$allowFullUpdate || !$target) {
$target = $request['target']; $target = $request['target'];
} }
$baseServerName = Util::array_last_key(ServConfig::getServerList()); $baseServerName = array_key_last(ServConfig::getServerList());
if (!$target && $server != $baseServerName) { if (!$target && $server != $baseServerName) {
Json::die([ Json::die([
+4 -4
View File
@@ -109,16 +109,16 @@ if($expires < $nowDate){
$access_token = $result['access_token']; $access_token = $result['access_token'];
$expires = TimeUtil::nowAddSeconds($result['expires_in']); $expires = TimeUtil::nowAddSeconds($result['expires_in']);
if(isset($result['refresh_token'])){ if(isset($result['refresh_token'])){
$refresh_token = Util::array_get($result['refresh_token']); $refresh_token = $result['refresh_token'] ?? null;
$refresh_token_expires = TimeUtil::nowAddSeconds($result['refresh_token_expires_in']); $refresh_token_expires = TimeUtil::nowAddSeconds($result['refresh_token_expires_in']);
} }
} }
$signupResult = $restAPI->signup(); $signupResult = $restAPI->signup();
$kakaoID = Util::array_get($signupResult['id']); $kakaoID = $signupResult['id'] ?? null;
if(!$kakaoID && Util::array_get($signupResult['msg'])!='already registered'){ if(!$kakaoID && ($signupResult['msg'] ?? null) !='already registered'){
Json::die([ Json::die([
'result'=>false, 'result'=>false,
'reason'=>'카카오 로그인 과정 중 앱 연결 절차를 실패했습니다'.Json::encode($signupResult) 'reason'=>'카카오 로그인 과정 중 앱 연결 절차를 실패했습니다'.Json::encode($signupResult)
@@ -126,7 +126,7 @@ if(!$kakaoID && Util::array_get($signupResult['msg'])!='already registered'){
} }
$me = $restAPI->meWithEmail(); $me = $restAPI->meWithEmail();
$me['code'] = Util::array_get($me['code'], 0); $me['code'] = $me['code'] ?? 0;
$kakao_account = $me['kakao_account']??[]; $kakao_account = $me['kakao_account']??[];
if ($me['code']< 0) { if ($me['code']< 0) {
$restAPI->unlink(); $restAPI->unlink();
+4 -4
View File
@@ -88,9 +88,9 @@ class Setting
$tail = new FileTail($this->versionFile); $tail = new FileTail($this->versionFile);
$version = 'noVersionJson'; $version = 'noVersionJson';
foreach ($tail->smart(5, 100, true) as $line) { foreach ($tail->smart(5, 100, true) as $line) {
if (Util::starts_with($line, '//{')) { if (str_starts_with($line, '//{')) {
$version = Json::decode(substr($line, 2)); $version = Json::decode(substr($line, 2));
$version = Util::array_get($version['version'], 'noVersionValue'); $version = $version['version'] ?? 'noVersionValue';
break; break;
} }
} }
@@ -106,8 +106,8 @@ class Setting
$templates = new \League\Plates\Engine(__DIR__.'/templates'); $templates = new \League\Plates\Engine(__DIR__.'/templates');
//TODO: .htaccess가 서버 오픈에도 사용될 수 있으니 별도의 방법이 필요함 //TODO: .htaccess가 서버 오픈에도 사용될 수 있으니 별도의 방법이 필요함
$allow_ip = Util::get_client_ip(false); $allow_ip = Util::get_client_ip(false);
if (Util::starts_with($allow_ip, '192.168.') || if (str_starts_with($allow_ip, '192.168.') ||
Util::starts_with($allow_ip, '10.')) { str_starts_with($allow_ip, '10.')) {
//172.16~172.31은 코딩하기 귀찮으니까 안할거다 //172.16~172.31은 코딩하기 귀찮으니까 안할거다
$allow_ip = Util::get_client_ip(true); $allow_ip = Util::get_client_ip(true);
} }
+107 -1
View File
@@ -3,7 +3,7 @@ namespace sammo;
use Iterator; use Iterator;
class Util extends \utilphp\util class Util
{ {
/** /**
@@ -889,4 +889,110 @@ class Util extends \utilphp\util
} }
return join('|', $result); return join('|', $result);
} }
/**
* http://github.com/brandonwamboldt/utilphp/ 의 구버전 코드에서 이식함
* 일부 코드는 수정
* 해당 라이선스: MIT
*/
/**
* Returns the first element in an array.
* PHP 8.5부터 array_first 사용 가능
*
* @param array $array
* @return mixed
*/
public static function array_first(array $array)
{
return $array[array_key_first($array)];
}
/**
* Returns the last element in an array.
* PHP 8.5부터 array_last 사용 가능
*
* @param array $array
* @return mixed
*/
public static function array_last(array $array)
{
return $array[array_key_last($array)];
}
/**
* Flatten a multi-dimensional array into a one dimensional array.
*
* Contributed by Theodore R. Smith of PHP Experts, Inc. <http://www.phpexperts.pro/>
*
* @param array $array The array to flatten
* @param boolean $preserve_keys Whether or not to preserve array keys.
* Keys from deeply nested arrays will
* overwrite keys from shallowy nested arrays
* @return array
*/
public static function array_flatten(array $array, $preserve_keys = true)
{
$flattened = array();
array_walk_recursive($array, function($value, $key) use (&$flattened, $preserve_keys) {
if ($preserve_keys && !is_int($key)) {
$flattened[$key] = $value;
} else {
$flattened[] = $value;
}
});
return $flattened;
}
/**
* Returns the IP address of the client.
*
* @param boolean $trust_proxy_headers Whether or not to trust the
* proxy headers HTTP_CLIENT_IP
* and HTTP_X_FORWARDED_FOR. ONLY
* use if your server is behind a
* proxy that sets these values
* @return string
*/
public static function get_client_ip($trust_proxy_headers = false)
{
if (!$trust_proxy_headers) {
return $_SERVER['REMOTE_ADDR'];
}
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* Access an array index, retrieving the value stored there if it
* exists or a default if it does not. This function allows you to
* concisely access an index which may or may not exist without
* raising a warning.
*
* @deprecated PHP 7.0 이상에서는 null 병합 연산자 사용을 권장
*
* @param array $var Array value to access
* @param mixed $default Default value to return if the key is not
* present in the array
* @return mixed
*/
public static function array_get(&$var, $default = null)
{
if (isset($var)) {
return $var;
}
return $default;
}
}; };