From 1d133cfefe08fd309b9d6bc1bee3934e653ccfea Mon Sep 17 00:00:00 2001 From: Vari Date: Wed, 28 Feb 2024 17:17:39 +0100 Subject: [PATCH] Added purchase/item endpoint. The endpoints checks if you can afford the item and have completed the required challenges when any exist. It also throws an error if you try to buy an item you already have in your inventory. --- .../Api/Player/PurchaseController.php | 91 +++++++++++++++++++ .../Api/Player/PurchaseItemRequest.php | 49 ++++++++++ .../Player/Purchase/PurchaseItemResponse.php | 17 ++++ .../Api/Player/Purchase/WalletEntry.php | 48 ++++++++++ dist/routes/deathgardenApi.php | 3 + 5 files changed, 208 insertions(+) create mode 100644 dist/app/Http/Controllers/Api/Player/PurchaseController.php create mode 100644 dist/app/Http/Requests/Api/Player/PurchaseItemRequest.php create mode 100644 dist/app/Http/Responses/Api/Player/Purchase/PurchaseItemResponse.php create mode 100644 dist/app/Http/Responses/Api/Player/Purchase/WalletEntry.php diff --git a/dist/app/Http/Controllers/Api/Player/PurchaseController.php b/dist/app/Http/Controllers/Api/Player/PurchaseController.php new file mode 100644 index 0000000..5c162b0 --- /dev/null +++ b/dist/app/Http/Controllers/Api/Player/PurchaseController.php @@ -0,0 +1,91 @@ +playerData(); + $quantityToBuy = $request->wantedQuantity - $request->oldQuantity; + + $itemToBuy = CatalogItem::find($request->objectId); + + if(!$this->hasCompletedChallengeForItem($itemToBuy, $playerData)) + return response('Not completed required challenge/s', 418); + + if( $playerData->currency_a < ($itemToBuy->default_cost_currency_a * $quantityToBuy) || + $playerData->currency_b < ($itemToBuy->default_cost_currency_b * $quantityToBuy) || + $playerData->currency_c < ($itemToBuy->default_cost_currency_c * $quantityToBuy)) + return response('Not enough Currency', 418); + + try { + $playerData->inventory()->attach($itemToBuy); + } + catch (UniqueConstraintViolationException $e) { + return response('Item already in inventory.', 418); + } + + $playerData->currency_a -= $itemToBuy->default_cost_currency_a * $quantityToBuy; + $playerData->currency_b -= $itemToBuy->default_cost_currency_b * $quantityToBuy; + $playerData->currency_c -= $itemToBuy->default_cost_currency_c * $quantityToBuy; + $playerData->save(); + + $hexItemId = Uuid::fromString($itemToBuy->id)->getHex()->toString(); + + $response = new PurchaseItemResponse( + $user->id, + $hexItemId, + $quantityToBuy, + new WalletEntry( + $playerData->currency_a, + $itemToBuy->default_cost_currency_a * $quantityToBuy, + $playerData->currency_b, + $itemToBuy->default_cost_currency_b * $quantityToBuy, + $playerData->currency_c, + $itemToBuy->default_cost_currency_c * $quantityToBuy, + ), + [$hexItemId], + ); + + return json_encode($response); + } + + protected function hasCompletedChallengeForItem(CatalogItem &$item, PlayerData &$playerData) + { + /** @var Challenge $foundChallenge */ + $foundChallenge = $item->requiredChallenges()->first(); + + if ($foundChallenge !== null) { + return $foundChallenge->getProgressForPlayer($playerData->id) >= $foundChallenge->completion_value; + } + + $characterIds = []; + $playerData->characterData()->get(['id'])->each(function (CharacterData $data) use ($characterIds) { + $characterIds[] = $data->id; + }); + + $foundChallenge = PickedChallenge::where('catalog_item_id', '=', $item->id) + ->whereIn('character_data_id', $characterIds)->first(); + + if($foundChallenge === null) + return true; + + return $foundChallenge->progress >= $foundChallenge->completion_value; + } +} diff --git a/dist/app/Http/Requests/Api/Player/PurchaseItemRequest.php b/dist/app/Http/Requests/Api/Player/PurchaseItemRequest.php new file mode 100644 index 0000000..caae9c8 --- /dev/null +++ b/dist/app/Http/Requests/Api/Player/PurchaseItemRequest.php @@ -0,0 +1,49 @@ +|string> + */ + public function rules(): array + { + return [ + 'data.objectId' => 'required|string', + 'data.oldQuantity' => 'required|int', + 'data.wantedQuantity' => 'required|int', + 'data.currencyGroup' => 'required|string', + ]; + } + + protected function passedValidation() + { + $this->objectId = Uuid::fromString($this->input('data.objectId'))->toString(); + $this->oldQuantity = $this->input('data.oldQuantity'); + $this->wantedQuantity = $this->input('data.wantedQuantity'); + $this->currencyGroup =$this->input('data.currencyGroup'); + } +} diff --git a/dist/app/Http/Responses/Api/Player/Purchase/PurchaseItemResponse.php b/dist/app/Http/Responses/Api/Player/Purchase/PurchaseItemResponse.php new file mode 100644 index 0000000..57737b6 --- /dev/null +++ b/dist/app/Http/Responses/Api/Player/Purchase/PurchaseItemResponse.php @@ -0,0 +1,17 @@ +debitCurrencyA > 0) + $result[] = [ + 'balance' => $this->currencyA, + 'currency' => 'CurrencyA', + 'currencyGroup' => 'SoftCurrencyGroup', + 'debited' => $this->debitCurrencyA + ]; + + if($this->debitCurrencyB > 0) + $result[] = [ + 'balance' => $this->currencyB, + 'currency' => 'CurrencyB', + 'currencyGroup' => 'SoftCurrencyGroup', + 'debited' => $this->debitCurrencyB + ]; + + if($this->debitCurrencyC > 0) + $result[] = [ + 'balance' => $this->currencyC, + 'currency' => 'CurrencyC', + 'currencyGroup' => 'SoftCurrencyGroup', + 'debited' => $this->debitCurrencyC + ]; + + return $result; + } +} \ No newline at end of file diff --git a/dist/routes/deathgardenApi.php b/dist/routes/deathgardenApi.php index 691b6ed..30a750e 100644 --- a/dist/routes/deathgardenApi.php +++ b/dist/routes/deathgardenApi.php @@ -7,6 +7,7 @@ use App\Http\Controllers\Api\Player\MetadataController; use App\Http\Controllers\Api\Player\ModifierCenterController; use App\Http\Controllers\Api\Player\PlayerController; +use App\Http\Controllers\Api\Player\PurchaseController; use App\Http\Controllers\Api\VersionController; use Illuminate\Support\Facades\Route; @@ -46,6 +47,8 @@ Route::post('extensions/challenges/getChallengeProgressionBatch', [ChallengeController::class, 'getProgressionBatch']); Route::post('extensions/challenges/getChallenges', [ChallengeController::class, 'getChallenges']); + Route::post('extensions/purchase/item', [PurchaseController::class, 'purchaseItem']); + Route::get('wallet/currencies', [CurrencyController::class, 'getCurrencies']); Route::get('inventories', [PlayerController::class, 'getInventory']);