Skip to content

Commit

Permalink
Merge pull request #39 from deanblackborough/main
Browse files Browse the repository at this point in the history
Log scores
  • Loading branch information
deanblackborough authored Aug 14, 2022
2 parents 5205f21 + 4e1fd4c commit 07c14cd
Show file tree
Hide file tree
Showing 24 changed files with 241 additions and 33 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/

## [1.05.0] - [2020-08-13]
### Added
- Added logging, scoring actions are logged in the API, the data will be used later for multiple features.
### Changed
- Correct the title in the HTML.
- Removed the `canonical` header.
- Added our Twitter handle to the footer.
### Fixed
- Scratching Yahtzee disables the Yahtzee bonus inputs.
- The minimum score for Three of a kind, Four of a kind and Chance is 5, not 6.

## [1.04.0] - [2022-08-10]
### Added
- Added a registration email, sent after the first step of account creation, allows continuation of account creation (create password).
Expand Down
39 changes: 39 additions & 0 deletions app/Actions/Game/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

namespace App\Actions\Game;

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

/**
* @author Dean Blackborough <[email protected]>
* @copyright Dean Blackborough (Costs to Expect) 2018-2022
* https://github.com/costs-to-expect/yahtzee/blob/main/LICENSE
*/
class Log extends Action
{
public function __invoke(
Service $api,
string $resource_type_id,
string $resource_id,
string $game_id,
string $message,
array $parameters = []
): int
{
$create_message_response = $api->createGameLogMessage(
$resource_type_id,
$resource_id,
$game_id,
$message,
$parameters
);

if ($create_message_response['status'] === 201) {
return 201;
}

return $create_message_response['status'];
}
}
20 changes: 20 additions & 0 deletions app/Api/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ public function createGame(
);
}

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

return $this->http->post(
$uri['uri'],
[
'message' => $message,
'parameters' => json_encode($parameters, JSON_THROW_ON_ERROR)
]
);
}

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

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

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

#[ArrayShape(['uri' => "string", 'name' => "string"])]
public static function assignedGamePlayer(
string $resource_type_id,
Expand Down
51 changes: 50 additions & 1 deletion app/Http/Controllers/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Actions\Game\Create;
use App\Actions\Game\Delete;
use App\Actions\Game\DeletePlayer;
use App\Actions\Game\Log;
use App\Models\ShareToken;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -536,6 +537,25 @@ public function scoreUpper(Request $request)
$score_sheet['score']['bonus'] = $score_bonus;
$score_sheet['score']['total'] = $score_sheet['score']['lower'] + $score_upper + $score_bonus;

$log_action = new Log();
$log_action_result = $log_action(
$this->api,
$this->resource_type_id,
$this->resource_id,
$request->input('game_id'),
'Scored ' . $request->input('score') . ' in their ' . ucfirst($request->input('dice')),
[
'player' => $request->input('player_id'),
'section' => 'upper',
'dice' => $request->input('dice'),
'score' => $request->input('score'),
]
);

if ($log_action_result !== 201) {
// @todo - Log an error
}

return $this->score(
$this->api,
$this->resource_type_id,
Expand All @@ -557,13 +577,16 @@ public function scoreLower(Request $request)
$request->input('player_id')
);

$combo = $request->input('combo');
$score = $request->input('score');

if ($score_sheet['status'] !== 200) {
return response()->json(['message' => 'Unable to fetch your score sheet'], $score_sheet['status']);
}

$score_sheet = $score_sheet['content']['value'];

$score_sheet['lower-section'][$request->input('combo')] = $request->input('score');
$score_sheet['lower-section'][$combo] = $score;
$score_lower = 0;
foreach ($score_sheet['lower-section'] as $value) {
$score_lower += $value;
Expand All @@ -572,6 +595,32 @@ public function scoreLower(Request $request)
$score_sheet['score']['lower'] = $score_lower;
$score_sheet['score']['total'] = $score_sheet['score']['upper'] + $score_sheet['score']['bonus'] + $score_lower;

$message = match ($combo) {
'three_of_a_kind', 'four_of_a_kind', 'chance' => 'Scored ' . $score . ' in ' . ucfirst(
str_replace('_', '', $combo)
),
default => 'Scored their ' . ucfirst(str_replace('_', ' ', $combo)) . ', scoring ' . $score,
};

$log_action = new Log();
$log_action_result = $log_action(
$this->api,
$this->resource_type_id,
$this->resource_id,
$request->input('game_id'),
$message,
[
'player' => $request->input('player_id'),
'section' => 'lower',
'combo' => $request->input('combo'),
'score' => $request->input('score'),
]
);

if ($log_action_result !== 201) {
// @todo - Log an error
}

return $this->score(
$this->api,
$this->resource_type_id,
Expand Down
51 changes: 50 additions & 1 deletion app/Http/Controllers/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace App\Http\Controllers;

use App\Actions\Game\Log;
use App\Api\Service;
use App\Models\ShareToken;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -163,6 +164,25 @@ public function scoreUpper(Request $request, $token)
$score_sheet['score']['bonus'] = $score_bonus;
$score_sheet['score']['total'] = $score_sheet['score']['lower'] + $score_upper + $score_bonus;

$log_action = new Log();
$log_action_result = $log_action(
$api,
$parameters['resource_type_id'],
$parameters['resource_id'],
$parameters['game_id'],
'Scored ' . $request->input('score') . ' in their ' . ucfirst($request->input('dice')),
[
'player' => $parameters['player_id'],
'section' => 'upper',
'dice' => $request->input('dice'),
'score' => $request->input('score'),
]
);

if ($log_action_result !== 201) {
// @todo - Log an error
}

return $this->score(
$api,
$parameters['resource_type_id'],
Expand All @@ -181,7 +201,10 @@ public function scoreLower(Request $request, $token)

$score_sheet = $this->getScoreSheet($api, $parameters);

$score_sheet['lower-section'][$request->input('combo')] = $request->input('score');
$combo = $request->input('combo');
$score = $request->input('score');

$score_sheet['lower-section'][$combo] = $score;
$score_lower = 0;
foreach ($score_sheet['lower-section'] as $value) {
$score_lower += $value;
Expand All @@ -190,6 +213,32 @@ public function scoreLower(Request $request, $token)
$score_sheet['score']['lower'] = $score_lower;
$score_sheet['score']['total'] = $score_sheet['score']['upper'] + $score_sheet['score']['bonus'] + $score_lower;

$message = match ($combo) {
'three_of_a_kind', 'four_of_a_kind', 'chance' => 'Scored ' . $score . ' in ' . ucfirst(
str_replace('_', '', $combo)
),
default => 'Scored their ' . ucfirst(str_replace('_', ' ', $combo)) . ', scoring ' . $score,
};

$log_action = new Log();
$log_action_result = $log_action(
$api,
$parameters['resource_type_id'],
$parameters['resource_id'],
$parameters['game_id'],
$message,
[
'player' => $parameters['player_id'],
'section' => 'lower',
'combo' => $request->input('combo'),
'score' => $request->input('score'),
]
);

if ($log_action_result !== 201) {
// @todo - Log an error
}

return $this->score(
$api,
$parameters['resource_type_id'],
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' => '1.04.0',
'release_date' => '10th August 2022'
'version' => '1.05.0',
'release_date' => '14th August 2022'
];
15 changes: 15 additions & 0 deletions public/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ export function disable_yahtzee_bonus_if_game_over(turns) {
display_selected_toast('done');
}
}

export function disable_yahtzee_bonus() {
let bonus_one = document.querySelector('input[type="checkbox"]#yahtzee_bonus_one.active');
if (bonus_one !== null) {
bonus_one.disabled = true;
}
let bonus_two = document.querySelector('input[type="checkbox"]#yahtzee_bonus_two.active');
if (bonus_two !== null) {
bonus_two.disabled = true;
}
let bonus_three = document.querySelector('input[type="checkbox"]#yahtzee_bonus_three.active');
if (bonus_three !== null) {
bonus_three.disabled = true;
}
}
8 changes: 6 additions & 2 deletions public/js/score-sheet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { display_selected_toast, disable_yahtzee_bonus_if_game_over } from './functions.js';
import {display_selected_toast, disable_yahtzee_bonus_if_game_over, disable_yahtzee_bonus} from './functions.js';

(function (axios) {
'use strict'
Expand Down Expand Up @@ -150,7 +150,7 @@ import { display_selected_toast, disable_yahtzee_bonus_if_game_over } from './fu
let score_lower_combination = function(element, show_toast = 'none') {

let score = parseInt(element.value);
if (score >= 6 && score <= 30) {
if (score >= 5 && score <= 30) {

clearTimeout(timeout);

Expand Down Expand Up @@ -288,6 +288,10 @@ import { display_selected_toast, disable_yahtzee_bonus_if_game_over } from './fu

disable_yahtzee_bonus_if_game_over(response.data.turns);

if (payload.combo === 'yahtzee') {
disable_yahtzee_bonus();
}

display_selected_toast(show_toast);

document.querySelectorAll('p.' + element.id.toString().replace('scratch_', '') + '_dice svg').forEach(dice =>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/add-players-to-game.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<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="description" content="Yahtzee Game Scorer by Costs to Expect">
<meta name="author" content="Dean Blackborough">
<title>Yahtzee Game Scorer: Add Players to Game</title>
<link rel="icon" sizes="48x48" href="{{ asset('images/favicon.ico') }}">
Expand Down
6 changes: 6 additions & 0 deletions resources/views/components/footer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<div class="col-12 col-md">
<small class="d-block mb-3 text-muted">&copy; 2022</small>
<small class="d-block mb-3 text-muted">v{{ $version }} - {{ $release_date }}</small>
<small class="d-block mb-3 text-muted">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-twitter" viewBox="0 0 16 16">
<path d="M5.026 15c6.038 0 9.341-5.003 9.341-9.334 0-.14 0-.282-.006-.422A6.685 6.685 0 0 0 16 3.542a6.658 6.658 0 0 1-1.889.518 3.301 3.301 0 0 0 1.447-1.817 6.533 6.533 0 0 1-2.087.793A3.286 3.286 0 0 0 7.875 6.03a9.325 9.325 0 0 1-6.767-3.429 3.289 3.289 0 0 0 1.018 4.382A3.323 3.323 0 0 1 .64 6.575v.045a3.288 3.288 0 0 0 2.632 3.218 3.203 3.203 0 0 1-.865.115 3.23 3.23 0 0 1-.614-.057 3.283 3.283 0 0 0 3.067 2.277A6.588 6.588 0 0 1 .78 13.58a6.32 6.32 0 0 1-.78-.045A9.344 9.344 0 0 0 5.026 15z"/>
</svg>
<a href="https://twitter.com/YahtzeeScorer">Follow</a> us on Twitter &copy;
</small>
</div>
<div class="col-6 col-md">
<h5>Powered by our API</h5>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/create-password.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<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="description" content="Yahtzee Game Scorer by Costs to Expect">
<meta name="author" content="Dean Blackborough">
<title>Yahtzee Game Scorer: Register</title>
<link rel="icon" sizes="48x48" href="{{ asset('images/favicon.ico') }}">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/game.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<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="description" content="Yahtzee Game Scorer 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') }}">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/games.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<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="description" content="Yahtzee Game Scorer by Costs to Expect">
<meta name="author" content="Dean Blackborough">
<title>Yahtzee Game Scorer: Games</title>
<link rel="icon" sizes="48x48" href="{{ asset('images/favicon.ico') }}">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/home.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<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="description" content="Yahtzee Game Scorer by Costs to Expect">
<meta name="author" content="Dean Blackborough">
<title>Yahtzee Game Scorer: Home</title>
<link rel="icon" sizes="48x48" href="{{ asset('images/favicon.ico') }}">
Expand Down
3 changes: 1 addition & 2 deletions resources/views/landing.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
<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="description" content="Yahtzee Game Scorer by Costs to Expect">
<meta name="author" content="Dean Blackborough">
<title>Yahtzee: Game Scorer</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"/>
<link rel="canonical" href="https://getbootstrap.com/docs/5.2/examples/product/">
<meta name="theme-color" content="#892b7c">
<style>
.bd-placeholder-img {
Expand Down
Loading

0 comments on commit 07c14cd

Please sign in to comment.