Skip to content

Commit

Permalink
Merge pull request #26 from deanblackborough/main
Browse files Browse the repository at this point in the history
v0.14.0
  • Loading branch information
deanblackborough authored Jul 31, 2022
2 parents f882f13 + ee89e46 commit 2994369
Show file tree
Hide file tree
Showing 14 changed files with 269 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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.14.0] - [2022-07-31]
### Added
- Added a message about your bonus!

## [0.13.0] - [2022-07-31]
### Added
- Added pagination to the games list, we can now see all games and how many we have played.
Expand Down
125 changes: 125 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,129 @@ protected function fetchPlayerScores(

return $scores;
}

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!';
return $this->playerBonusView($game_id, $player_id, $message);
}

$total = 0;
$dice_scored = [];
$dice_scratched = [];
$dice_to_score = ['ones', 'twos', 'threes', 'fours', 'fives', 'sixes'];
$dice_values = ['ones' => 1, 'twos' => 2, 'threes' => 3, 'fours' => 4, 'fives' => 5, 'sixes' => 6];
foreach ($upper_section as $dice => $score) {
$total += $score;
if ($score !== 0) {
unset($dice_to_score[array_search($dice, $dice_to_score, true)]);
$dice_scored[] = $dice;
} else {
unset($dice_to_score[array_search($dice, $dice_to_score, true)]);
$dice_scratched[] = $dice;
}
}

if ($total === 63 && count($dice_scored) === 6) {
$message = 'Damn, that was close, next time, give yourself some breathing room!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($total > 75 && count($dice_scored) === 6) {
$message = 'WOW, someone is showing off!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($total > 63 && count($dice_scored) === 6) {
$message = 'Awesome, plenty of breathing room!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($total > 75 && count($dice_scratched) > 0) {
$message = 'Um! How exactly did you manage to get your bonus!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($total > 63 && count($dice_scratched) > 0) {
$message = 'WOW, nothing like scoring the bonus whilst scratching!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($total === 63 && (count($dice_scored) + count($dice_scratched)) === 6) {
$message = 'Damn, that was close, next time, give yourself some breathing room!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($total === 62 && (count($dice_scored) + count($dice_scratched)) === 6) {
$message = 'You were robbed! You needed one point, anyone got one spare?';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($total < 62 && (count($dice_scored) + count($dice_scratched)) === 6) {
$message = 'Oh! Time to play the tiny little violin just for you!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if (
$total < 63 && (count($dice_scored) + count($dice_scratched)) < 6
) {
$threes_total = $total;
$fours_total = $total;
foreach($dice_to_score as $dice) {
$threes_total += ($dice_values[$dice] * 3);
$fours_total += ($dice_values[$dice] * 4);
}

if ($threes_total === 63) {
$message = 'Looking good, you haven\'t messed up yet, you just need three of everything left!';
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!';
return $this->playerBonusView($game_id, $player_id, $message);
}
$message = 'You can still easily get the bonus';
return $this->playerBonusView($game_id, $player_id, $message);
}
if ($fours_total === 63) {
$message = 'Looking good, you haven\'t messed up yet, you can still score the bonus without a Yahtzee!';
return $this->playerBonusView($game_id, $player_id, $message);
}
if ($fours_total > 63) {
if ((count($dice_scored) + count($dice_scratched)) === 5) {
$message = 'You can still get the bonus, four of the last 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!';
return $this->playerBonusView($game_id, $player_id, $message);
}

if ($threes_total < 63) {
$message = 'Scoring three of everything won\'t help!';
return $this->playerBonusView($game_id, $player_id, $message);
}
}

$message = 'No message for you! I can\'t think of anything to say, do something interesting and things might change!';
return $this->playerBonusView($game_id, $player_id, $message);
}

protected function playerBonusView(string $game_id, string $player_id, string $message)
{
return view(
'bonus',
[
'message' => $message,
'game_id' => $game_id,
'player_id' => $player_id,
]
);
}
}
31 changes: 30 additions & 1 deletion app/Http/Controllers/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Actions\Game\AddPlayers;
use App\Actions\Game\Complete;
use App\Actions\Game\Create;
use App\Actions\Game\Score;
use App\Models\ShareToken;
use Illuminate\Http\Request;

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

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

$game_response = $this->api->getGame(
$this->resource_type_id,
$this->resource_id,
$game_id
);

if ($game_response['status'] !== 200) {
abort(404, 'Game not found');
}

$player_score_sheet_response = $this->api->getPlayerScoreSheet(
$this->resource_type_id,
$this->resource_id,
$game_id,
$player_id
);

if ($player_score_sheet_response['status'] !== 200) {
abort(404, 'Player score sheet not found');
}

$upper_section = $player_score_sheet_response['content']['value']['upper-section'];

return $this->playerBonusMessage($game_id, $player_id, $upper_section);
}

public function playerScores(Request $request, string $game_id)
{
$this->boostrap($request);
Expand Down
33 changes: 32 additions & 1 deletion app/Http/Controllers/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace App\Http\Controllers;

use App\Actions\Game\Score;
use App\Api\Service;
use App\Models\ShareToken;
use Illuminate\Http\Request;
Expand All @@ -16,6 +15,38 @@
*/
class Share extends Controller
{
public function playerBonus(Request $request, string $token)
{
$parameters = $this->getParameters($token);

$api = new Service($parameters['owner_bearer']);

$game_response = $api->getGame(
$parameters['resource_type_id'],
$parameters['resource_id'],
$parameters['game_id']
);

if ($game_response['status'] !== 200) {
abort(404, 'Game not found');
}

$player_score_sheet_response = $api->getPlayerScoreSheet(
$parameters['resource_type_id'],
$parameters['resource_id'],
$parameters['game_id'],
$parameters['player_id']
);

if ($player_score_sheet_response['status'] !== 200) {
abort(404, 'Player score sheet not found');
}

$upper_section = $player_score_sheet_response['content']['value']['upper-section'];

return $this->playerBonusMessage($parameters['game_id'], $parameters['player_id'], $upper_section);
}

public function playerScores(Request $request, string $token)
{
$parameters = $this->getParameters($token);
Expand Down
2 changes: 1 addition & 1 deletion 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.13.0',
'version' => '0.14.0',
'release_date' => '31st July 2022'
];
5 changes: 5 additions & 0 deletions public/css/theme.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/css/theme.css.map

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions public/js/bonus-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(function (axios) {
'use strict'

let game_id = document.getElementById('game_id');
let player_id = document.getElementById('player_id');

let sleep = time => new Promise(resolve => setTimeout(resolve, time))
let poll = (promiseFn, time) => promiseFn().then(
sleep(time).then(() => poll(promiseFn, time)))

let fetchBonusMessage = function() {
let bonus_message = document.querySelector('div.bonus-message');

axios.get('/game/' + game_id.value + '/player/' + player_id.value + '/bonus')
.then(response => {
if (response.data.length > 0) {
bonus_message.innerHTML = response.data;
}
});
}

poll(() => new Promise(() => fetchBonusMessage()), 1000 * 5)
})(axios);
22 changes: 22 additions & 0 deletions public/js/public-bonus-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(function (axios) {
'use strict'

let token = document.getElementById('token');

let sleep = time => new Promise(resolve => setTimeout(resolve, time))
let poll = (promiseFn, time) => promiseFn().then(
sleep(time).then(() => poll(promiseFn, time)))

let fetchBonusMessage = function() {
let bonus_message = document.querySelector('div.bonus-message');

axios.get('/public/game/' + token.value + '/bonus')
.then(response => {
if (response.data.length > 0) {
bonus_message.innerHTML = response.data;
}
});
}

poll(() => new Promise(() => fetchBonusMessage()), 1000 * 5)
})(axios);
5 changes: 5 additions & 0 deletions resources/scss/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ h2.score {

a.nav-link.active {
text-decoration: underline;
}

div.bonus-message p {
margin-top: 1rem;
border: 1px solid rgba(137, 43, 124, 1);
}
1 change: 1 addition & 0 deletions resources/views/bonus.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p class="p-2">{{ $message }}</p>
4 changes: 4 additions & 0 deletions resources/views/public-score-sheet.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
<h3 class="text-center score text-black"><strong>Total</strong></h3>
<h2 class="text-center mb-0 score total" id="upper-total">{{ $score_sheet['score']['upper'] + $score_sheet['score']['bonus'] }}</h2>
</div>
<div class="col-12 bonus-message">

</div>
</div>

<div class="row">
Expand Down Expand Up @@ -540,5 +543,6 @@
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js" defer></script>
<script src="{{ asset('js/public-score-sheet.js?v0.7.0') }}" defer></script>
<script src="{{ asset('js/public-player-scores.js?v0.7.0') }}" defer></script>
<script src="{{ asset('js/public-bonus-message.js?v0.14.0') }}" defer></script>
</body>
</html>
8 changes: 6 additions & 2 deletions resources/views/score-sheet.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
<h3 class="text-center score text-black"><strong>Total</strong></h3>
<h2 class="text-center mb-0 score total" id="upper-total">{{ $score_sheet['score']['upper'] + $score_sheet['score']['bonus'] }}</h2>
</div>
<div class="col-12 bonus-message">

</div>
</div>

<div class="row">
Expand Down Expand Up @@ -547,8 +550,9 @@
<script src="{{ asset('node_modules/axios/dist/axios.min.js') }}" defer></script>
<script src="{{ asset('node_modules/bootstrap/dist/js/bootstrap.js') }}" defer></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js" defer></script>
<script src="{{ asset('js/score-sheet.js?v0.7.0') }}" defer></script>
<script src="{{ asset('js/player-scores.js?v0.7.0') }}" defer></script>
<script src="{{ asset('js/score-sheet.js?v0.14.0') }}" defer></script>
<script src="{{ asset('js/player-scores.js?v0.14.0') }}" defer></script>
<script src="{{ asset('js/bonus-message.js?v0.14.0') }}" defer></script>
@endif
</body>
</html>
10 changes: 10 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
[Share::class, 'playerScores']
)->name('public.player-scores');

Route::get(
'/public/game/{token}/bonus',
[Share::class, 'playerBonus']
)->name('public.bonus');

Route::group(
[
'middleware' => [
Expand Down Expand Up @@ -95,6 +100,11 @@ static function() {
[Game::class, 'scoreUpper']
)->name('game.score-upper');

Route::get(
'/game/{game_id}/player/{player_id}/bonus',
[Game::class, 'playerBonus']
)->name('game.player.bonus');

Route::post(
'/game/score-lower',
[Game::class, 'scoreLower']
Expand Down

0 comments on commit 2994369

Please sign in to comment.