Skip to content

Commit

Permalink
Merge pull request #20 from deanblackborough/main
Browse files Browse the repository at this point in the history
Game detail page
  • Loading branch information
deanblackborough authored Jul 26, 2022
2 parents caad663 + 29a7e0c commit 980b188
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 15 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

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.9.0] - [2022-07-26]
### Added
- Added a game detail page, for now, no extra data or statistics.

### Changed
- More details added to the player scores table at the bottom of the score sheet.
- Start game redirects to the game detail page, no need to wait for API cache to be invalidated.

### Fixed
- The `complete` and `add players` buttons should only display when a game is incomplete.

## [0.8.0] - [2022-07-24]
### Added
- Confetti, who doesn't like confetti?
Expand Down
7 changes: 7 additions & 0 deletions app/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abstract class Action

protected array $validation_errors = [];

protected string $game_id;

public function getValidationErrors(): array
{
return $this->validation_errors;
Expand All @@ -23,4 +25,9 @@ public function getMessage(): string
{
return $this->message;
}

public function getGameId(): string
{
return $this->game_id;
}
}
8 changes: 5 additions & 3 deletions app/Actions/Game/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,25 @@ public function __invoke(Service $api, string $resource_type_id, string $resourc

$config = Config::get('app.config');

$this->game_id = $create_game_response['content']['id'];

foreach ($input['players'] as $player) {
$api->addPlayerToGame(
$resource_type_id,
$resource_id,
$create_game_response['content']['id'],
$this->game_id,
$player
);

try {
$token = new ShareToken();
$token->token = Str::uuid();
$token->game_id = $create_game_response['content']['id'];
$token->game_id = $this->game_id;
$token->player_id = $player;
$token->parameters = json_encode([
'resource_type_id' => $resource_type_id,
'resource_id' => $resource_id,
'game_id' => $create_game_response['content']['id'],
'game_id' => $this->game_id,
'player_id' => $player,
'owner_bearer' => request()->cookie($config['cookie_bearer'])
], JSON_THROW_ON_ERROR);
Expand Down
50 changes: 47 additions & 3 deletions app/Http/Controllers/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Actions\Game\Complete;
use App\Actions\Game\Create;
use App\Actions\Game\Score;
use App\Models\ShareToken;
use Illuminate\Http\Request;

/**
Expand All @@ -28,6 +29,45 @@ public function index(Request $request)
);
}

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

$game = $this->api->getGame(
$this->resource_type_id,
$this->resource_id,
$game_id,
['include-players' => 1]
);

if ($game['status'] !== 200) {
abort(404, 'Unable to find the game');
}

$game_scores = [];
$game_score_sheets_response = $this->api->getGameScoreSheets(
$this->resource_type_id,
$this->resource_id,
$game['content']['id']
);

if ($game_score_sheets_response['status'] === 200) {
foreach ($game_score_sheets_response['content'] as $score_sheet) {
$game_scores[$game['content']['id']][$score_sheet['key']] = $score_sheet['value']['score']['total'];
}
}

return view(
'game',
[
'game' => $game['content'],
'game_scores' => $game_scores,
'share_tokens' => (new ShareToken())->getShareTokens(),
]
);

}

public function newGame(Request $request)
{
$this->boostrap($request);
Expand Down Expand Up @@ -58,7 +98,7 @@ public function newGameProcess(Request $request)
);

if ($result === 201) {
return redirect()->route('home');
return redirect()->route('game.show', ['game_id' => $action->getGameId()]);
}

if ($result === 422) {
Expand Down Expand Up @@ -182,12 +222,16 @@ public function playerScores(Request $request, string $game_id)
foreach ($players_response['content'] as $player) {
$scores[$player['category']['id']] = [
'name' => $player['category']['name'],
'score' => 0
'upper' => 0,
'lower' => 0,
'total' => 0
];
}

foreach ($game_score_sheets_response['content'] as $score_sheet) {
$scores[$score_sheet['key']]['score'] = $score_sheet['value']['score']['total'];
$scores[$score_sheet['key']]['upper'] = $score_sheet['value']['score']['upper'] + $score_sheet['value']['score']['bonus'];
$scores[$score_sheet['key']]['lower'] = $score_sheet['value']['score']['lower'];
$scores[$score_sheet['key']]['total'] = $score_sheet['value']['score']['total'];
}

return view(
Expand Down
8 changes: 6 additions & 2 deletions app/Http/Controllers/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ public function playerScores(Request $request, string $token)
foreach ($players_response['content'] as $player) {
$scores[$player['category']['id']] = [
'name' => $player['category']['name'],
'score' => 0
'upper' => 0,
'lower' => 0,
'total' => 0
];
}

foreach ($game_score_sheets_response['content'] as $score_sheet) {
$scores[$score_sheet['key']]['score'] = $score_sheet['value']['score']['total'];
$scores[$score_sheet['key']]['upper'] = $score_sheet['value']['score']['upper'] + $score_sheet['value']['score']['bonus'];
$scores[$score_sheet['key']]['lower'] = $score_sheet['value']['score']['lower'];
$scores[$score_sheet['key']]['total'] = $score_sheet['value']['score']['total'];
}

return view(
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.8.0',
'release_date' => '24th July 2022'
'version' => '0.9.0',
'release_date' => '26th July 2022'
];
82 changes: 82 additions & 0 deletions resources/views/game.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Yahtzee Game Score by Costs to Expect">
<meta name="author" content="Dean Blackborough">
<title>Yahtzee Game Scorer: Game</title>
<link rel="icon" sizes="48x48" href="{{ asset('images/favicon.ico') }}">
<link rel="icon" type="image/png" sizes="192x192" href="{{ asset('images/favicon.png') }}">
<link href="{{ asset('css/theme.css') }}" rel="stylesheet" />
</head>
<body>
<div class="col-lg-8 mx-auto p-3 py-md-5">
<x-layout.header />

<nav class="nav nav-fill my-4 border-bottom border-top">
<a class="nav-link" href="{{ route('home') }}">Home</a>
<a class="nav-link active" href="{{ route('games') }}">Games</a>
<a class="nav-link" href="{{ route('players') }}">Players</a>
<a class="nav-link" href="{{ route('sign-out') }}">Sign-out</a>
</nav>

<main>

<ul class="list-unstyled">
<li class="mb-3">
<span>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-controller" viewBox="0 0 16 16">
<path d="M11.5 6.027a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm2.5-.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm-6.5-3h1v1h1v1h-1v1h-1v-1h-1v-1h1v-1z"/>
<path d="M3.051 3.26a.5.5 0 0 1 .354-.613l1.932-.518a.5.5 0 0 1 .62.39c.655-.079 1.35-.117 2.043-.117.72 0 1.443.041 2.12.126a.5.5 0 0 1 .622-.399l1.932.518a.5.5 0 0 1 .306.729c.14.09.266.19.373.297.408.408.78 1.05 1.095 1.772.32.733.599 1.591.805 2.466.206.875.34 1.78.364 2.606.024.816-.059 1.602-.328 2.21a1.42 1.42 0 0 1-1.445.83c-.636-.067-1.115-.394-1.513-.773-.245-.232-.496-.526-.739-.808-.126-.148-.25-.292-.368-.423-.728-.804-1.597-1.527-3.224-1.527-1.627 0-2.496.723-3.224 1.527-.119.131-.242.275-.368.423-.243.282-.494.575-.739.808-.398.38-.877.706-1.513.773a1.42 1.42 0 0 1-1.445-.83c-.27-.608-.352-1.395-.329-2.21.024-.826.16-1.73.365-2.606.206-.875.486-1.733.805-2.466.315-.722.687-1.364 1.094-1.772a2.34 2.34 0 0 1 .433-.335.504.504 0 0 1-.028-.079zm2.036.412c-.877.185-1.469.443-1.733.708-.276.276-.587.783-.885 1.465a13.748 13.748 0 0 0-.748 2.295 12.351 12.351 0 0 0-.339 2.406c-.022.755.062 1.368.243 1.776a.42.42 0 0 0 .426.24c.327-.034.61-.199.929-.502.212-.202.4-.423.615-.674.133-.156.276-.323.44-.504C4.861 9.969 5.978 9.027 8 9.027s3.139.942 3.965 1.855c.164.181.307.348.44.504.214.251.403.472.615.674.318.303.601.468.929.503a.42.42 0 0 0 .426-.241c.18-.408.265-1.02.243-1.776a12.354 12.354 0 0 0-.339-2.406 13.753 13.753 0 0 0-.748-2.295c-.298-.682-.61-1.19-.885-1.465-.264-.265-.856-.523-1.733-.708-.85-.179-1.877-.27-2.913-.27-1.036 0-2.063.091-2.913.27z"/>
</svg>
Overview
</span>

@if (array_key_exists('collection', $game['players']))
<ul class="list-unstyled mb-2">

@foreach ($game['players']['collection'] as $__player)
<li>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-person-fill" viewBox="0 0 16 16">
<path d="M3 14s-1 0-1-1 1-4 6-4 6 3 6 4-1 1-1 1H3zm5-6a3 3 0 1 0 0-6 3 3 0 0 0 0 6z"/>
</svg>
{{ $__player['name'] }}

@if (
array_key_exists($game['id'], $game_scores) &&
array_key_exists($__player['id'], $game_scores[$game['id']])
)
({{ $game_scores[$game['id']][$__player['id']] }} pts)
@else
(0 pts)
@endif

<a href="{{ route('game.score-sheet', ['game_id' => $game['id'], 'player_id' => $__player['id']]) }}">[Score sheet]</a>

@if (array_key_exists($game['id'], $share_tokens) && array_key_exists($__player['id'], $share_tokens[$game['id']]))
<a href="{{ route('public.score-sheet', ['token' => $share_tokens[$game['id']][$__player['id']]]) }}">[Share]</a>
@endif
</li>
@endforeach
</ul>
@endif

@if ($game['complete'] !== 1)
<a href="{{ route('game.add-players.view', ['game_id' => $game['id']]) }}" class="btn btn-sm btn-primary">Add Players</a>
<form action="{{ route('game.complete', ['game_id' => $game['id']]) }}" method="POST" class="d-inline">@csrf <button type="submit" class="btn btn-sm btn-primary">Complete</button></form>
@endif
</li>
</ul>
</main>
<footer class="pt-4 my-4 text-muted border-top text-center">
Created by <a href="https://twitter.com/DBlackborough">Dean Blackborough</a><br />
powered by the <a href="https://api.costs-to-expect.com">Costs to Expect API</a>

<div class="mt-3 small">
v{{ $config['version'] }} - Released {{ $config['release_date'] }}
</div>
</footer>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion resources/views/games.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<path d="M11.5 6.027a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm2.5-.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm-6.5-3h1v1h1v1h-1v1h-1v-1h-1v-1h1v-1z"/>
<path d="M3.051 3.26a.5.5 0 0 1 .354-.613l1.932-.518a.5.5 0 0 1 .62.39c.655-.079 1.35-.117 2.043-.117.72 0 1.443.041 2.12.126a.5.5 0 0 1 .622-.399l1.932.518a.5.5 0 0 1 .306.729c.14.09.266.19.373.297.408.408.78 1.05 1.095 1.772.32.733.599 1.591.805 2.466.206.875.34 1.78.364 2.606.024.816-.059 1.602-.328 2.21a1.42 1.42 0 0 1-1.445.83c-.636-.067-1.115-.394-1.513-.773-.245-.232-.496-.526-.739-.808-.126-.148-.25-.292-.368-.423-.728-.804-1.597-1.527-3.224-1.527-1.627 0-2.496.723-3.224 1.527-.119.131-.242.275-.368.423-.243.282-.494.575-.739.808-.398.38-.877.706-1.513.773a1.42 1.42 0 0 1-1.445-.83c-.27-.608-.352-1.395-.329-2.21.024-.826.16-1.73.365-2.606.206-.875.486-1.733.805-2.466.315-.722.687-1.364 1.094-1.772a2.34 2.34 0 0 1 .433-.335.504.504 0 0 1-.028-.079zm2.036.412c-.877.185-1.469.443-1.733.708-.276.276-.587.783-.885 1.465a13.748 13.748 0 0 0-.748 2.295 12.351 12.351 0 0 0-.339 2.406c-.022.755.062 1.368.243 1.776a.42.42 0 0 0 .426.24c.327-.034.61-.199.929-.502.212-.202.4-.423.615-.674.133-.156.276-.323.44-.504C4.861 9.969 5.978 9.027 8 9.027s3.139.942 3.965 1.855c.164.181.307.348.44.504.214.251.403.472.615.674.318.303.601.468.929.503a.42.42 0 0 0 .426-.241c.18-.408.265-1.02.243-1.776a12.354 12.354 0 0 0-.339-2.406 13.753 13.753 0 0 0-.748-2.295c-.298-.682-.61-1.19-.885-1.465-.264-.265-.856-.523-1.733-.708-.85-.179-1.877-.27-2.913-.27-1.036 0-2.063.091-2.913.27z"/>
</svg>
<a href="#" class="ps-2">
<a href="{{ route('game.show', ['game_id' => $__game['id']]) }}" class="ps-2">
Overview
</a>
</span>
Expand Down
6 changes: 4 additions & 2 deletions resources/views/home.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<path d="M11.5 6.027a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm2.5-.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm-6.5-3h1v1h1v1h-1v1h-1v-1h-1v-1h1v-1z"/>
<path d="M3.051 3.26a.5.5 0 0 1 .354-.613l1.932-.518a.5.5 0 0 1 .62.39c.655-.079 1.35-.117 2.043-.117.72 0 1.443.041 2.12.126a.5.5 0 0 1 .622-.399l1.932.518a.5.5 0 0 1 .306.729c.14.09.266.19.373.297.408.408.78 1.05 1.095 1.772.32.733.599 1.591.805 2.466.206.875.34 1.78.364 2.606.024.816-.059 1.602-.328 2.21a1.42 1.42 0 0 1-1.445.83c-.636-.067-1.115-.394-1.513-.773-.245-.232-.496-.526-.739-.808-.126-.148-.25-.292-.368-.423-.728-.804-1.597-1.527-3.224-1.527-1.627 0-2.496.723-3.224 1.527-.119.131-.242.275-.368.423-.243.282-.494.575-.739.808-.398.38-.877.706-1.513.773a1.42 1.42 0 0 1-1.445-.83c-.27-.608-.352-1.395-.329-2.21.024-.826.16-1.73.365-2.606.206-.875.486-1.733.805-2.466.315-.722.687-1.364 1.094-1.772a2.34 2.34 0 0 1 .433-.335.504.504 0 0 1-.028-.079zm2.036.412c-.877.185-1.469.443-1.733.708-.276.276-.587.783-.885 1.465a13.748 13.748 0 0 0-.748 2.295 12.351 12.351 0 0 0-.339 2.406c-.022.755.062 1.368.243 1.776a.42.42 0 0 0 .426.24c.327-.034.61-.199.929-.502.212-.202.4-.423.615-.674.133-.156.276-.323.44-.504C4.861 9.969 5.978 9.027 8 9.027s3.139.942 3.965 1.855c.164.181.307.348.44.504.214.251.403.472.615.674.318.303.601.468.929.503a.42.42 0 0 0 .426-.241c.18-.408.265-1.02.243-1.776a12.354 12.354 0 0 0-.339-2.406 13.753 13.753 0 0 0-.748-2.295c-.298-.682-.61-1.19-.885-1.465-.264-.265-.856-.523-1.733-.708-.85-.179-1.877-.27-2.913-.27-1.036 0-2.063.091-2.913.27z"/>
</svg>
<a href="#" class="ps-2">
<a href="{{ route('game.show', ['game_id' => $__open_game['id']]) }}" class="ps-2">
Overview
</a>
</span>
Expand Down Expand Up @@ -69,7 +69,9 @@
@endif

<a href="{{ route('game.add-players.view', ['game_id' => $__open_game['id']]) }}" class="btn btn-sm btn-primary">Add Players</a>
@if ($__open_game['complete'] !== 1)
<form action="{{ route('game.complete', ['game_id' => $__open_game['id']]) }}" method="POST" class="d-inline">@csrf <button type="submit" class="btn btn-sm btn-primary">Complete</button></form>
@endif
</li>
@endforeach
</ul>
Expand Down Expand Up @@ -97,7 +99,7 @@
<path d="M11.5 6.027a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm2.5-.5a.5.5 0 1 1-1 0 .5.5 0 0 1 1 0zm-1.5 1.5a.5.5 0 1 0 0-1 .5.5 0 0 0 0 1zm-6.5-3h1v1h1v1h-1v1h-1v-1h-1v-1h1v-1z"/>
<path d="M3.051 3.26a.5.5 0 0 1 .354-.613l1.932-.518a.5.5 0 0 1 .62.39c.655-.079 1.35-.117 2.043-.117.72 0 1.443.041 2.12.126a.5.5 0 0 1 .622-.399l1.932.518a.5.5 0 0 1 .306.729c.14.09.266.19.373.297.408.408.78 1.05 1.095 1.772.32.733.599 1.591.805 2.466.206.875.34 1.78.364 2.606.024.816-.059 1.602-.328 2.21a1.42 1.42 0 0 1-1.445.83c-.636-.067-1.115-.394-1.513-.773-.245-.232-.496-.526-.739-.808-.126-.148-.25-.292-.368-.423-.728-.804-1.597-1.527-3.224-1.527-1.627 0-2.496.723-3.224 1.527-.119.131-.242.275-.368.423-.243.282-.494.575-.739.808-.398.38-.877.706-1.513.773a1.42 1.42 0 0 1-1.445-.83c-.27-.608-.352-1.395-.329-2.21.024-.826.16-1.73.365-2.606.206-.875.486-1.733.805-2.466.315-.722.687-1.364 1.094-1.772a2.34 2.34 0 0 1 .433-.335.504.504 0 0 1-.028-.079zm2.036.412c-.877.185-1.469.443-1.733.708-.276.276-.587.783-.885 1.465a13.748 13.748 0 0 0-.748 2.295 12.351 12.351 0 0 0-.339 2.406c-.022.755.062 1.368.243 1.776a.42.42 0 0 0 .426.24c.327-.034.61-.199.929-.502.212-.202.4-.423.615-.674.133-.156.276-.323.44-.504C4.861 9.969 5.978 9.027 8 9.027s3.139.942 3.965 1.855c.164.181.307.348.44.504.214.251.403.472.615.674.318.303.601.468.929.503a.42.42 0 0 0 .426-.241c.18-.408.265-1.02.243-1.776a12.354 12.354 0 0 0-.339-2.406 13.753 13.753 0 0 0-.748-2.295c-.298-.682-.61-1.19-.885-1.465-.264-.265-.856-.523-1.733-.708-.85-.179-1.877-.27-2.913-.27-1.036 0-2.063.091-2.913.27z"/>
</svg>
<a href="#" class="ps-2">
<a href="{{ route('game.show', ['game_id' => $__closed_game['id']]) }}" class="ps-2">
Overview
</a>
</span>
Expand Down
8 changes: 6 additions & 2 deletions resources/views/player-scores.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Score</th>
<th scope="col">Upper</th>
<th scope="col">Lower</th>
<th scope="col">Total</th>
</tr>
</thead>
<tbody class="table-group-divider">
@foreach($scores as $__score)
<tr>
<th scope="row">{{ $__score['name'] }}</th>
<td>{{ $__score['score'] }}</td>
<td scope="row">{{ $__score['upper'] }}</td>
<td scope="row">{{ $__score['lower'] }}</td>
<th scope="row">{{ $__score['total'] }}</th>
</tr>
@endforeach
</tbody>
Expand Down
Loading

0 comments on commit 980b188

Please sign in to comment.