Skip to content

Commit

Permalink
Merge pull request #18 from deanblackborough/main
Browse files Browse the repository at this point in the history
Show player scores
  • Loading branch information
deanblackborough authored Jul 22, 2022
2 parents 81bff9b + 6bf8b06 commit d0ffcbe
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 56 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

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.7.0] - [2022-07-23]
### Added
- Added a table to the bottom of each score sheet, shows all the player scores, delayed by thirty seconds.

### Changed
- Changed the URIs for all the share pages.

### Fixed
- You can only score a Yahtzee bonus when a Yahtzee has been scored.

## [0.6.0] - 2022-07-22
### Added
- Added toast messages for scoreboard actions, toasts are selected from a random list for each action.
Expand Down
41 changes: 41 additions & 0 deletions app/Http/Controllers/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,47 @@ public function complete(Request $request, string $game_id)
abort(500, 'Unable to complete the game, returned status code: ' . $result['status']);
}

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

$players_response = $this->api->getGamePlayers(
$this->resource_type_id,
$this->resource_id,
$game_id
);
if ($players_response['status'] !== 200) {
abort(404, 'Unable to find the game players');
}

$game_score_sheets_response = $this->api->getGameScoreSheets(
$this->resource_type_id,
$this->resource_id,
$game_id
);

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

$scores = [];
foreach ($players_response['content'] as $player) {
$scores[$player['category']['id']] = [
'name' => $player['category']['name'],
'score' => 0
];
}

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

return view(
'player-scores',
['scores' => $scores]
);
}

public function scoreSheet(Request $request, string $game_id, string $player_id)
{
$this->boostrap($request);
Expand Down
44 changes: 44 additions & 0 deletions app/Http/Controllers/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,50 @@
*/
class Share extends Controller
{
public function playerScores(Request $request, string $token)
{
$parameters = $this->getParameters($token);

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

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

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

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

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

$scores = [];
foreach ($players_response['content'] as $player) {
$scores[$player['category']['id']] = [
'name' => $player['category']['name'],
'score' => 0
];
}

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

return view(
'player-scores',
['scores' => $scores]
);
}

public function scoreSheet(Request $request, string $token)
{
$parameters = $this->getParameters($token);
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.6.0',
'release_date' => '22nd July 2022'
'version' => '0.7.0',
'release_date' => '23rd July 2022'
];
22 changes: 22 additions & 0 deletions public/js/player-scores.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(function (axios) {
'use strict'

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

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

let fetchPlayerScores = function() {
let player_scores = document.querySelector('div.player-scores');

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

poll(() => new Promise(() => fetchPlayerScores()), 1000 * 30)
})(axios);
22 changes: 22 additions & 0 deletions public/js/public-player-scores.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 fetchPlayerScores = function() {
let player_scores = document.querySelector('div.player-scores');

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

poll(() => new Promise(() => fetchPlayerScores()), 1000 * 30)
})(axios);
56 changes: 30 additions & 26 deletions public/js/public-score-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
clearTimeout(timeout);
timeout = setTimeout(() => {
axios.post(
'/score-sheet/' + token.value + '/score-upper',
'/public/score-sheet/' + token.value + '/score-upper',
{
dice: this.value,
score: 0
Expand Down Expand Up @@ -69,7 +69,7 @@
clearTimeout(timeout);
timeout = setTimeout(() => {
axios.post(
'/score-sheet/' + token.value + '/score-upper',
'/public/score-sheet/' + token.value + '/score-upper',
{
dice: this.id,
score: score
Expand Down Expand Up @@ -242,7 +242,7 @@

timeout = setTimeout(() => {
axios.post(
'/score-sheet/' + token.value + '/score-lower',
'/public/score-sheet/' + token.value + '/score-lower',
{
combo: element.id,
score: score
Expand Down Expand Up @@ -270,38 +270,42 @@
}

let score_yahtzee_bonus = function(element, show_toast = 'none') {
clearTimeout(timeout);

timeout = setTimeout(() => {
axios.post(
'/score-sheet/' + token.value + '/score-lower',
{
combo: element.id,
score: 100
}
)
.then(response => {
element.classList.remove('active');
element.classList.add('disabled');
element.disabled = true;
let yahtzee = document.querySelector('input[type="checkbox"]#yahtzee.disabled');
if (yahtzee !== null && yahtzee.checked === true) {
clearTimeout(timeout);

timeout = setTimeout(() => {
axios.post(
'/public/score-sheet/' + token.value + '/score-lower',
{
combo: element.id,
score: 100
}
)
.then(response => {
element.classList.remove('active');
element.classList.add('disabled');
element.disabled = true;

score_lower.innerText = response.data.score.lower;
total_score.innerText = response.data.score.upper + response.data.score.bonus + response.data.score.lower;
score_lower.innerText = response.data.score.lower;
total_score.innerText = response.data.score.upper + response.data.score.bonus + response.data.score.lower;

display_toast(show_toast);
})
.catch(error => {
console.log(error);
});
}, delay);
display_toast(show_toast);
})
.catch(error => {
console.log(error);
});
}, delay);
}
}

let scratch_lower_combo = function(element, show_toast = 'none') {
clearTimeout(timeout);

timeout = setTimeout(() => {
axios.post(
'/score-sheet/' + token.value + '/score-lower',
'/public/score-sheet/' + token.value + '/score-lower',
{
combo: element.id.toString().replace('scratch_', ''),
score: 0
Expand Down Expand Up @@ -331,7 +335,7 @@

timeout = setTimeout(() => {
axios.post(
'/score-sheet/' + token.value + '/score-lower',
'/public/score-sheet/' + token.value + '/score-lower',
{
combo: element.id.toString().replace('scratch_', ''),
score: 0
Expand Down
50 changes: 27 additions & 23 deletions public/js/score-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,32 +277,36 @@
}

let score_yahtzee_bonus = function(element, show_toast = 'none') {
clearTimeout(timeout);

timeout = setTimeout(() => {
axios.post(
'/game/score-lower',
{
game_id: game_id.value,
player_id: player_id.value,
combo: element.id,
score: 100
}
)
.then(response => {
element.classList.remove('active');
element.classList.add('disabled');
element.disabled = true;
let yahtzee = document.querySelector('input[type="checkbox"]#yahtzee.disabled');
if (yahtzee !== null && yahtzee.checked === true) {
clearTimeout(timeout);

timeout = setTimeout(() => {
axios.post(
'/game/score-lower',
{
game_id: game_id.value,
player_id: player_id.value,
combo: element.id,
score: 100
}
)
.then(response => {
element.classList.remove('active');
element.classList.add('disabled');
element.disabled = true;

score_lower.innerText = response.data.score.lower;
total_score.innerText = response.data.score.upper + response.data.score.bonus + response.data.score.lower;
score_lower.innerText = response.data.score.lower;
total_score.innerText = response.data.score.upper + response.data.score.bonus + response.data.score.lower;

display_toast(show_toast);
})
.catch(error => {
console.log(error);
});
}, delay);
display_toast(show_toast);
})
.catch(error => {
console.log(error);
});
}, delay);
}
}

let scratch_lower_combo = function(element, show_toast = 'none') {
Expand Down
24 changes: 24 additions & 0 deletions resources/views/player-scores.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="table-responsive">

<hr />

<h2 class="text-primary">Player Scores</h2>

<table class="table table-striped table-sm">
<caption>Player scores, delayed by thirty seconds.</caption>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Score</th>
</tr>
</thead>
<tbody class="table-group-divider">
@foreach($scores as $__score)
<tr>
<th scope="row">{{ $__score['name'] }}</th>
<td>{{ $__score['score'] }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
4 changes: 3 additions & 1 deletion resources/views/public-score-sheet.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@
</div>
</div>
</form>
<div class="player-scores"></div>
<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>
Expand All @@ -534,6 +535,7 @@
<x-toast />
<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="{{ asset('js/public-score-sheet.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>
</body>
</html>
4 changes: 3 additions & 1 deletion resources/views/score-sheet.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@
</div>
</div>
</form>
<div class="player-scores"></div>
<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>
Expand All @@ -545,7 +546,8 @@
<x-toast />
<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="{{ asset('js/score-sheet.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>
@endif
</body>
</html>
Loading

0 comments on commit d0ffcbe

Please sign in to comment.