This commit is contained in:
Tommy Parnell
2024-05-10 00:49:29 -04:00
parent 0ea6df0a49
commit 4ad624b369
6 changed files with 246 additions and 184 deletions

View File

@@ -3,11 +3,9 @@
</script>
<body>
<main class="container-fluid">
<slot />
</main>
</body>
<style>

View File

@@ -9,7 +9,9 @@
$storedGames[$storedGames.length - 1];
$: currentScoreEditing = scoreEditing > -1 ? currentGameEditing.scores[scoreEditing] : null;
$: gamesComplete = $storedGames.filter((g) => g.isComplete);
$: completedStats = calculateTotalGameStats(gamesComplete);
$: disableEditing = currentGameEditing.isComplete && scoreEditing === -1;
$: storedGames;
function isKill(score: Score | undefined | null) {
return score === 'killHit6' || score === 'killHit8' || score === 'killMiss';
}
@@ -92,6 +94,39 @@
return current.stats.totalEightKills + current.stats.totalSixKills + accum;
}, 0);
}
// function calculateBullsHit(games: Game[]) {
// return games.reduce((accum: number, current) => {
// return current.stats.bulls + accum;
// }, 0);
// }
// function calculateTotalScore(games: Game[]) {
// return games.reduce((accum: number, current) => {
// return current.totalScore + accum;
// }, 0);
// }
function calculateTotalGameStats(gamesToCalc: Game[]) {
const stats = gamesToCalc.reduce(
(accum, current) => {
return {
bulls: current.stats.bulls + accum.bulls,
kills: current.stats.totalEightKills + current.stats.totalSixKills + accum.kills,
drops: current.stats.drops + accum.drops,
totalScore: current.totalScore + accum.totalScore
};
},
{ bulls: 0, kills: 0, drops: 0, totalScore: 0 } as {
bulls: number;
kills: number;
drops: number;
totalScore: number;
}
);
return {
...stats,
average: stats.totalScore && gamesToCalc.length ? stats.totalScore / gamesToCalc.length : 0,
rating: calculateRating(gamesToCalc)
};
}
function getLabelForScore(score: Score) {
if (typeof score === 'number') {
return score.toString();
@@ -118,7 +153,10 @@
</svelte:head>
<h1 class="center">WATL rating simulator</h1>
<section>
<div class="stats"><div>Score {currentGameEditing.totalScore}</div> <div>Throw: {currentGameEditing.scores.length}</div></div>
<div class="stats">
<div>Score {currentGameEditing.totalScore}</div>
<div>Throw: {currentGameEditing.scores.length + 1}</div>
</div>
<div class="flexrow">
{#each currentGameEditing.scores as score, scoreIndex}
<div>
@@ -130,7 +168,9 @@
}}>Cancel</button
>
{:else}
<button class="outline secondary" on:click={() => (scoreEditing = scoreIndex)}>{getLabelForScore(score)}</button>
<button class="outline secondary" on:click={() => (scoreEditing = scoreIndex)}
>{getLabelForScore(score)}</button
>
{/if}
</div>
{/each}
@@ -227,7 +267,9 @@
}}>{getLabelForScore('drop')}</button
>
<!-- {#if currentGameEditing.isComplete} -->
<button disabled={!currentGameEditing.isComplete} class="contrast" on:click={saveGame}>Save Game</button>
<button disabled={!currentGameEditing.isComplete} class="contrast" on:click={saveGame}
>New Game</button
>
<!-- {/if} -->
</div>
</section>
@@ -236,11 +278,14 @@
<h2>Past Games</h2>
<button on:click={deleteAllGames}>Delete All</button><br /><br />
<p>
Rating: {calculateRating(gamesComplete)} <br /> Average: {calculateAverage(gamesComplete)} <br /> total kills:
{calculateKillsHit(gamesComplete)}
Rating: {completedStats.rating} <br />
Average: {completedStats.average} <br />
Kills: {completedStats.kills} <br />
Bulls: {completedStats.bulls} <br />
Score: {completedStats.totalScore}
</p>
<div class="overflow-auto">
<table class="striped">
<table class="">
<thead>
<tr>
<th>Game</th>
@@ -255,6 +300,7 @@
</thead>
<tbody>
{#each $storedGames as game, gameIndex}
{#if game.isComplete || (!game.isComplete && currentGameEditing !== game)}
<tr>
<td class="tdSmall">{gameIndex + 1}</td>
<td class="tdSmall">{game.totalScore}</td>
@@ -274,7 +320,9 @@
<button
disabled={game === currentGameEditing}
on:click={() => {
const newGames = ($storedGames = $storedGames.filter((g, i) => i !== gameIndex));
const newGames = ($storedGames = $storedGames.filter(
(g, i) => i !== gameIndex
));
if (newGames.length < 1) {
newGames.push(new Game());
}
@@ -284,6 +332,7 @@
>
</td>
</tr>
{/if}
{/each}
</tbody>
</table>
@@ -294,7 +343,6 @@
.stats {
display: flex;
justify-content: space-between;
}
.center {
display: flex;

View File

@@ -1,5 +1,11 @@
export type Score = 8 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | "drop" | "killHit8" | "killHit6" | "killMiss";
export interface GameStats { totalSixKills: number; totalEightKills: number; killAttempts: number, drops: number, bulls: number}
export type Score = 8 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 'drop' | 'killHit8' | 'killHit6' | 'killMiss';
export interface GameStats {
totalSixKills: number;
totalEightKills: number;
killAttempts: number;
drops: number;
bulls: number;
}
export interface IStoredGame {
scores: Score[];
stats: GameStats;
@@ -11,17 +17,17 @@ export class Game implements IStoredGame {
public stats: GameStats = this.GenerateStats();
public addScore(score: Score) {
this.scores.push(score);
this.scores = [...this.scores]
this.scores = [...this.scores];
this.stats = this.GenerateStats();
}
public removeScore(scoreNumber: number) {
this.scores.splice(scoreNumber, 1);
this.scores = [...this.scores]
this.scores = [...this.scores];
this.stats = this.GenerateStats();
}
public replaceScore(scoreNumber: number, score: Score) {
this.scores[scoreNumber] = score;
this.scores = [...this.scores]
this.scores = [...this.scores];
this.stats = this.GenerateStats();
}
public get isComplete() {
@@ -57,26 +63,31 @@ export class Game implements IStoredGame {
}
return false;
}
public GenerateStats(): { totalSixKills: number; totalEightKills: number; killAttempts: number, drops: number, bulls: number} {
const data = { totalSixKills: 0, totalEightKills: 0, killAttempts: 0, drops: 0, bulls: 0}
this.scores.forEach(score => {
public GenerateStats(): {
totalSixKills: number;
totalEightKills: number;
killAttempts: number;
drops: number;
bulls: number;
} {
const data = { totalSixKills: 0, totalEightKills: 0, killAttempts: 0, drops: 0, bulls: 0 };
this.scores.forEach((score) => {
if (score === 6) {
data.bulls += 1
data.bulls += 1;
}
if(score === "drop") {
if (score === 'drop') {
data.drops += 1;
}
if( score === "killHit6" || score === "killHit8" || score === "killMiss") {
if (score === 'killHit6' || score === 'killHit8' || score === 'killMiss') {
data.killAttempts += 1;
}
if(score === "killHit6") {
if (score === 'killHit6') {
data.totalSixKills += 1;
}
if(score === "killHit8") {
if (score === 'killHit8') {
data.totalEightKills += 1;
}
})
});
return data;
}
}

View File

@@ -1,20 +1,25 @@
import { writable } from "svelte/store";
import { Game, type IStoredGame } from "./interfaces";
import { browser } from "$app/environment";
const DATASTORAGEKEY = "WATLCALC";
import { writable } from 'svelte/store';
import { Game, type IStoredGame } from './interfaces';
import { browser } from '$app/environment';
const DATASTORAGEKEY = 'WATLCALC';
const gamesFromLocalStorage = browser ? localStorage.getItem(DATASTORAGEKEY) : null;
const parsedGamesFromLocalStorage: IStoredGame[] | undefined | null = gamesFromLocalStorage ? JSON.parse(gamesFromLocalStorage) : null;
const parsedGamesFromLocalStorage: IStoredGame[] | undefined | null = gamesFromLocalStorage
? JSON.parse(gamesFromLocalStorage)
: null;
let gamesParsed: Game[] = [];
if (!parsedGamesFromLocalStorage || parsedGamesFromLocalStorage?.length === 0) {
gamesParsed = [new Game()];
}
if(parsedGamesFromLocalStorage && Array.isArray(parsedGamesFromLocalStorage) && parsedGamesFromLocalStorage.length > 0) {
gamesParsed = parsedGamesFromLocalStorage.map(i => deserializeGames(i));
if (
parsedGamesFromLocalStorage &&
Array.isArray(parsedGamesFromLocalStorage) &&
parsedGamesFromLocalStorage.length > 0
) {
gamesParsed = parsedGamesFromLocalStorage.map((i) => deserializeGames(i));
}
export const storedGames = writable<Game[]>(gamesParsed);
storedGames.subscribe(value => {
storedGames.subscribe((value) => {
browser && localStorage.setItem(DATASTORAGEKEY, JSON.stringify(value));
});

View File

@@ -12,10 +12,10 @@ const config = {
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter({
fallback: 'index.html',
fallback: 'index.html'
}),
paths: {
base: process.env.NODE_ENV === 'production' ? '/WATLRatingCalculator' : '',
base: process.env.NODE_ENV === 'production' ? '/WATLRatingCalculator' : ''
}
}
};