From 96266bbff2480daf08abd864a1a6a3601d614caa Mon Sep 17 00:00:00 2001 From: hide_d Date: Wed, 15 Sep 2021 02:21:06 +0900 Subject: [PATCH] =?UTF-8?q?InheritAction:=20=EC=9C=A0=EB=8B=88=ED=81=AC=20?= =?UTF-8?q?=EC=9E=85=EC=B0=B0,=20=EB=9E=9C=EB=8D=A4=20=EC=95=84=EC=9D=B4?= =?UTF-8?q?=ED=85=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/func.php | 4 +- hwe/sammo/API/General/Join.php | 6 +- .../API/InheritAction/BuyRandomUnique.php | 52 ++++++++++++ .../API/InheritAction/BuySpecificUnique.php | 80 +++++++++++++++++++ hwe/sammo/BaseAPI.php | 2 +- hwe/sammo/GameConstBase.php | 2 + src/sammo/Validator.php | 11 ++- 7 files changed, 148 insertions(+), 9 deletions(-) create mode 100644 hwe/sammo/API/InheritAction/BuyRandomUnique.php create mode 100644 hwe/sammo/API/InheritAction/BuySpecificUnique.php diff --git a/hwe/func.php b/hwe/func.php index 5c89d774..cffdd747 100644 --- a/hwe/func.php +++ b/hwe/func.php @@ -1711,6 +1711,8 @@ function tryInheritUniqueItem(General $general, string $acquireType = '아이템 rollbackInheritUniqueTrial($general, $itemKey, '절차상의 오류입니다.'); continue; } + + //XXX: 정렬할 필요 없지 않나? usort($anyTrials, function ($lhsTrial, $rhsTrial) { [,, $lhsAmount] = $lhsTrial; [,, $rhsAmount] = $rhsTrial; @@ -1718,7 +1720,7 @@ function tryInheritUniqueItem(General $general, string $acquireType = '아이템 }); //공동 1등인데 본인이 있을 수도 있다. - [,, $topAmount] = $anyTrials; + [,, $topAmount] = $anyTrials[0]; if ($amount < $topAmount) { $compAmount = $topAmount / $amount; if ($compAmount > 2.0) { diff --git a/hwe/sammo/API/General/Join.php b/hwe/sammo/API/General/Join.php index 5d50164c..5e3d4527 100644 --- a/hwe/sammo/API/General/Join.php +++ b/hwe/sammo/API/General/Join.php @@ -78,12 +78,8 @@ class Join extends \sammo\BaseAPI return static::REQ_LOGIN | static::REQ_READ_ONLY; } - public function launch(?Session $session, ?\DateTimeInterface $modifiedSince, ?string $reqEtag) + public function launch(Session $session, ?\DateTimeInterface $modifiedSince, ?string $reqEtag) { - if ($session === null) { - throw "invalid session"; - } - $userID = $session->userID; $name = $this->args['name']; diff --git a/hwe/sammo/API/InheritAction/BuyRandomUnique.php b/hwe/sammo/API/InheritAction/BuyRandomUnique.php new file mode 100644 index 00000000..dc94af23 --- /dev/null +++ b/hwe/sammo/API/InheritAction/BuyRandomUnique.php @@ -0,0 +1,52 @@ +userID; + $generalID = $session->generalID; + + $general = General::createGeneralObjFromDB($generalID); + if($userID != $general->getVar('owner')){ + return '로그인 상태가 이상합니다. 다시 로그인해 주세요.'; + } + + if($general->getAuxVar('inheritRandomUnique') !== null){ + return '이미 구입 명령을 내렸습니다. 다음 턴까지 기다려주세요.'; + } + + $db = DB::db(); + $inheritStor = KVStorage::getStorage($db, "inheritance_{$userID}"); + $previousPoint = $inheritStor->getValue('previous')??0; + if($previousPoint < GameConst::$inheritItemRandomPoint){ + return '충분한 유산 포인트를 가지고 있지 않습니다.'; + } + + $general->setAuxVar('inheritRandomUnique', TimeUtil::now()); + $inheritStor->setValue('previous', $previousPoint - GameConst::$inheritItemRandomPoint); + $general->flushUpdateValues(); + return null; + } +} diff --git a/hwe/sammo/API/InheritAction/BuySpecificUnique.php b/hwe/sammo/API/InheritAction/BuySpecificUnique.php new file mode 100644 index 00000000..cd76e5e1 --- /dev/null +++ b/hwe/sammo/API/InheritAction/BuySpecificUnique.php @@ -0,0 +1,80 @@ +$amount){ + if($amount == 0){ + continue; + } + $availableItems[$itemKey] = $amount; + } + } + + $v = new Validator($this->args); + $v->rule('required', [ + 'item', + 'amount', + ]) + ->rule('integer', 'amount') + ->rule('min', GameConst::$inheritItemUniqueMinPoint) + ->rule('keyExists', $availableItems); + + if (!$v->validate()) { + return $v->errorStr(); + } + return null; + } + + public function getRequiredSessionMode(): int + { + //KVStrorage, General.aux 모두 쓰므로 lock; + return static::REQ_GAME_LOGIN; + } + + public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag) + { + $itemKey = $this->args['item']; + $amount = $this->args['amount']; + + $userID = $session->userID; + $generalID = $session->generalID; + + $general = General::createGeneralObjFromDB($generalID); + if($userID != $general->getVar('owner')){ + return '로그인 상태가 이상합니다. 다시 로그인해 주세요.'; + } + + $itemTrials = $general->getAuxVar('inheritUniqueTrial') ?? []; + if(key_exists($itemKey, $itemTrials)){ + return '이미 입찰한 아이템입니다. 다음 턴에 시도해 주세요.'; + } + + $db = DB::db(); + $inheritStor = KVStorage::getStorage($db, "inheritance_{$userID}"); + $trialStor = KVStorage::getStorage($db, "ut_{$itemKey}"); + $previousPoint = $inheritStor->getValue('previous')??0; + if($previousPoint < $amount){ + return '충분한 유산 포인트를 가지고 있지 않습니다.'; + } + + $itemTrials[$itemKey] = $amount; + $general->setAuxVar('inheritUniqueTrial', $itemTrials); + $inheritStor->setValue('previous', $previousPoint - $amount); + $trialStor->setValue("u{$userID}", [$userID, $generalID, $amount]); + $general->flushUpdateValues(); + return null; + } +} diff --git a/hwe/sammo/BaseAPI.php b/hwe/sammo/BaseAPI.php index c730b5f2..784a1193 100644 --- a/hwe/sammo/BaseAPI.php +++ b/hwe/sammo/BaseAPI.php @@ -20,7 +20,7 @@ abstract class BaseAPI abstract function validateArgs(): ?string; /** @return null|string|array */ - abstract function launch(?Session $session, ?\DateTimeInterface $modifiedSince, ?string $reqEtag); + abstract function launch(Session $session, ?\DateTimeInterface $modifiedSince, ?string $reqEtag); public function tryCache():?string{ return null; diff --git a/hwe/sammo/GameConstBase.php b/hwe/sammo/GameConstBase.php index d36689df..5f9344ce 100644 --- a/hwe/sammo/GameConstBase.php +++ b/hwe/sammo/GameConstBase.php @@ -199,6 +199,8 @@ class GameConstBase public static $inheritBornTurntimePoint = 3000; public static $inheritBornCityPoint = 1000; public static $inheritBornStatPoint = 1000; + public static $inheritItemUniqueMinPoint = 5000; + public static $inheritItemRandomPoint = 3000; public static $allItems = [ 'horse' => [ diff --git a/src/sammo/Validator.php b/src/sammo/Validator.php index dcd7fb74..5718421f 100644 --- a/src/sammo/Validator.php +++ b/src/sammo/Validator.php @@ -21,7 +21,7 @@ class Validator extends \Valitron\Validator * $rule에 함수를 넣는 대신 상속한 클래스에 추가하는 방식을 사용할 것. * * @suppress PhanUndeclaredFunctionInCallable - * + * * @param string $rule * @param array|string $fields * @return $this @@ -31,7 +31,7 @@ class Validator extends \Valitron\Validator $params = func_get_args(); return parent::rule(...$params); - + } /** @@ -98,4 +98,11 @@ class Validator extends \Valitron\Validator $width = mb_strwidth($value); return $params[0] <= $width && $width <= $params[1]; } + + protected function validateKeyExists($field, $value, $params){ + if(!is_string($value) && !is_numeric($value)){ + return false; + } + return key_exists($value, $params[0]); + } }