Skip to content

Commit

Permalink
Merge pull request #27 from deanblackborough/main
Browse files Browse the repository at this point in the history
Delete open game
  • Loading branch information
deanblackborough authored Aug 1, 2022
2 parents 2994369 + 21cbe9e commit 08d8546
Show file tree
Hide file tree
Showing 22 changed files with 447 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

The complete changelog for the Costs to Expect REST API, our changelog follows the format defined at https://keepachangelog.com/en/1.0.0/

## [0.15.0] - [2022-08-01]
### Added
- Open games can be deleted.
### Changed
- Adjusted the layout of open games ready for even more buttons.
- Updated the Bootstrap theme, added 'danger'.
- Adjusted a couple of messages relating to the upper bonus.

## [0.14.0] - [2022-07-31]
### Added
- Added a message about your bonus!
Expand Down
105 changes: 105 additions & 0 deletions app/Actions/Game/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);

namespace App\Actions\Game;

use App\Actions\Action;
use App\Api\Service;
use App\Models\ShareToken;

/**
* @author Dean Blackborough <[email protected]>
* @copyright Dean Blackborough (Costs to Expect) 2018-2022
* https://github.com/costs-to-expect/yahtzee/blob/main/LICENSE
*/
class Delete extends Action
{
public function __invoke(
Service $api,
string $resource_type_id,
string $resource_id,
string $game_id
): int
{
$game_response = $api->getGame(
$resource_type_id,
$resource_id,
$game_id,
['include-players' => true]
);
if ($game_response['status'] !== 200) {
abort(404, 'Unable to find the game');
}

try {

// Delete the score sheets that exist
// Possible there are not score sheets for some players
$score_sheets_response = $api->getGameScoreSheets(
$resource_type_id,
$resource_id,
$game_id
);

if ($score_sheets_response['status'] !== 200) {
throw new \RuntimeException('Unable to fetch the score sheets for the game');
}

foreach ($score_sheets_response['content'] as $score_sheet) {
$response = $api->deletePlayerScoreSheet(
$resource_type_id,
$resource_id,
$game_id,
$score_sheet['key']
);

if ($response['status'] !== 204) {
throw new \RuntimeException('Unable to delete score sheet for player with id ' . $score_sheet['key'] .
', error ' . $response['content']);
}
}

$assigned_players_response = $api->getAssignedGamePlayers(
$resource_type_id,
$resource_id,
$game_id
);

if ($assigned_players_response['status'] !== 200) {
throw new \RuntimeException('Unable to fetch the assigned game players');
}

foreach($assigned_players_response['content'] as $player) {

$response = $api->deleteAssignedGamePlayer(
$resource_type_id,
$resource_id,
$game_id,
$player['id']
);

if ($response['status'] !== 204) {
throw new \RuntimeException('Unable to delete player with id ' . $player['id'] . ', error ' .
$response['content']);
}
}

$response = $api->deleteGame(
$resource_type_id,
$resource_id,
$game_id
);

if ($response['status'] !== 204) {
throw new \RuntimeException('Unable to delete game with id ' . $game_id);
}

ShareToken::query()->where('game_id', $game_id)->delete();

} catch (\Exception $e) {
abort(500, $e->getMessage());
}

return 204;
}
}
16 changes: 16 additions & 0 deletions app/Api/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public function __construct(?string $bearer = null)
}
}

public function delete(string $uri): array
{
$response = $this->client->delete($this->baseUri() . $uri);

return match ($response->status()) {
204 => [
'status' => 204,
'content' => null,
],
default => [
'status' => $response->status(),
'content' => $response->json('message')
],
};
}

public function get(string $uri): array
{
$response = $this->client->get($this->baseUri() . $uri);
Expand Down
43 changes: 43 additions & 0 deletions app/Api/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,49 @@ public function createResourceType(): array
);
}

#[ArrayShape(['status' => "integer", 'content' => "array", 'fields' => "array"])]
public function deleteAssignedGamePlayer(
string $resource_type_id,
string $resource_id,
string $game_id,
string $player_id
): array
{
$uri = Uri::assignedGamePlayer(
$resource_type_id,
$resource_id,
$game_id,
$player_id
);

return $this->http->delete($uri['uri']);
}

#[ArrayShape(['status' => "integer", 'content' => "array"])]
public function deleteGame(
string $resource_type_id,
string $resource_id,
string $game_id
): array
{
$uri = Uri::game($resource_type_id, $resource_id, $game_id);

return $this->http->delete($uri['uri']);
}

#[ArrayShape(['status' => "integer", 'content' => "array"])]
public function deletePlayerScoreSheet(
string $resource_type_id,
string $resource_id,
string $game_id,
string $player_id
): array
{
$uri = Uri::playerScoreSheet($resource_type_id, $resource_id, $game_id, $player_id);

return $this->http->delete($uri['uri']);
}

#[ArrayShape(['status' => "integer", 'content' => "array"])]
public function getGame(
string $resource_type_id,
Expand Down
18 changes: 18 additions & 0 deletions app/Api/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ public static function game(string $resource_type_id, string $resource_id, strin
];
}

#[ArrayShape(['uri' => "string", 'name' => "string"])]
public static function assignedGamePlayer(
string $resource_type_id,
string $resource_id,
string $game_id,
string $player_id
): array
{
$uri = '/' . self::VERSION . '/resource-types/' . $resource_type_id .
'/resources/' . $resource_id . '/items/' . $game_id . '/categories/' .
$player_id;

return [
'uri' => $uri,
'name' => 'Game player'
];
}

#[ArrayShape(['uri' => "string", 'name' => "string"])]
public static function assignedGamePlayers(string $resource_type_id, string $resource_id, string $game_id, array $parameters = []): array
{
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected function fetchPlayerScores(
protected function playerBonusMessage(string $game_id, string $player_id, array $upper_section)
{
if (count($upper_section) === 0) {
$message = 'Looking good, you haven\'t messed up yet, only because you haven\'t done anything!';
$message = 'Looking good, you haven\'t messed up yet!';
return $this->playerBonusView($game_id, $player_id, $message);
}

Expand Down Expand Up @@ -232,12 +232,12 @@ protected function playerBonusMessage(string $game_id, string $player_id, array
}

if ($threes_total === 63) {
$message = 'Looking good, you haven\'t messed up yet, you just need three of everything left!';
$message = 'Looking good, you haven\'t messed up yet, three of everything left will do!';
return $this->playerBonusView($game_id, $player_id, $message);
}
if ($threes_total > 63) {
if ((count($dice_scored) + count($dice_scratched)) === 5) {
$message = 'You can still easily get the bonus, three of the last please!';
$message = 'You can still easily get the bonus, three of the last dice please!';
return $this->playerBonusView($game_id, $player_id, $message);
}
$message = 'You can still easily get the bonus';
Expand All @@ -249,20 +249,20 @@ protected function playerBonusMessage(string $game_id, string $player_id, array
}
if ($fours_total > 63) {
if ((count($dice_scored) + count($dice_scratched)) === 5) {
$message = 'You can still get the bonus, four of the last please!';
$message = 'You can still get the bonus, four of the last dice please!';
return $this->playerBonusView($game_id, $player_id, $message);
}

$message = 'You can still get the bonus, you need four of something';
return $this->playerBonusView($game_id, $player_id, $message);
}
if ($fours_total < 63) {
$message = 'Scoring four of everything won\'t help!';
$message = 'Scoring four of everything won\'t help you!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($threes_total < 63) {
$message = 'Scoring three of everything won\'t help!';
$message = 'Scoring three of everything won\'t help you!';
return $this->playerBonusView($game_id, $player_id, $message);
}
}
Expand Down
24 changes: 24 additions & 0 deletions app/Http/Controllers/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Actions\Game\AddPlayers;
use App\Actions\Game\Complete;
use App\Actions\Game\Create;
use App\Actions\Game\Delete;
use App\Models\ShareToken;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -324,6 +325,29 @@ public function completeAndPlayAgain(Request $request, string $game_id)
abort(500, 'Unable to complete the game, returned status code: ' . $result['status']);
}

public function deleteGame(Request $request, string $game_id)
{
$this->boostrap($request);

$action = new Delete();
try {
$result = $action(
$this->api,
$this->resource_type_id,
$this->resource_id,
$game_id
);

if ($result === 204) {
return redirect()->route('home');
}
} catch (\Exception $e) {
abort(500, $e->getMessage());
}

abort(500, 'Unable to delete the game, unknown error');
}

public function playerBonus(Request $request, string $game_id, string $player_id)
{
$this->boostrap($request);
Expand Down
4 changes: 2 additions & 2 deletions config/app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
'item_subtype_id' => env('ITEM_SUBTYPE_ID'),
'cookie_user' => env('SESSION_NAME_USER'),
'cookie_bearer' => env('SESSION_NAME_BEARER'),
'version' => '0.14.0',
'release_date' => '31st July 2022'
'version' => '0.15.0',
'release_date' => '1st August 2022'
];
Loading

0 comments on commit 08d8546

Please sign in to comment.