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,19 +153,24 @@
</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>
{#if scoreIndex === scoreEditing}
<button
class="outline"
class="outline"
on:click={() => {
scoreEditing = -1;
}}>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}
@@ -139,58 +179,58 @@
<section>
<div>
<button
class="flexrowButton "
disabled={disableEditing}
on:click={() => {
setScore(0);
}}>0</button
>
<button
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(1);
}}>1</button
>
<button
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(2);
}}>2</button
>
<button
class="flexrowButton "
disabled={disableEditing}
on:click={() => {
setScore(3);
}}>3</button
>
<button
class="flexrowButton "
disabled={disableEditing}
on:click={() => {
setScore(4);
}}>4</button
>
<button
class="flexrowButton "
disabled={disableEditing}
on:click={() => {
setScore(5);
}}>5</button
>
<button
class="flexrowButton "
disabled={disableEditing}
on:click={() => {
setScore(6);
}}>6</button
>
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(0);
}}>0</button
>
<button
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(1);
}}>1</button
>
<button
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(2);
}}>2</button
>
<button
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(3);
}}>3</button
>
<button
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(4);
}}>4</button
>
<button
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(5);
}}>5</button
>
<button
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore(6);
}}>6</button
>
</div>
<div>
<button
class="flexrowButton "
class="flexrowButton"
disabled={disableEditing ||
(!currentGameEditing.KillEnabled &&
!isKill(currentScoreEditing) &&
@@ -200,7 +240,7 @@
}}>{getLabelForScore('killHit6')}</button
>
<button
class="flexrowButton "
class="flexrowButton"
disabled={disableEditing ||
(!currentGameEditing.KillEnabled &&
!isKill(currentScoreEditing) &&
@@ -210,7 +250,7 @@
}}>{getLabelForScore('killHit8')}</button
>
<button
class="flexrowButton "
class="flexrowButton"
disabled={disableEditing ||
(!currentGameEditing.KillEnabled &&
!isKill(currentScoreEditing) &&
@@ -220,14 +260,16 @@
}}>{getLabelForScore('killMiss')}</button
>
<button
class="flexrowButton "
class="flexrowButton"
disabled={disableEditing}
on:click={() => {
setScore('drop');
}}>{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,35 +300,39 @@
</thead>
<tbody>
{#each $storedGames as game, gameIndex}
<tr>
<td class="tdSmall">{gameIndex + 1}</td>
<td class="tdSmall">{game.totalScore}</td>
<td class="tdSmall">{game.stats.bulls}</td>
<td class="tdSmall">{game.stats.totalEightKills}</td>
<td class="tdSmall">{game.stats.totalSixKills}</td>
<td class="tdSmall">{game.stats.drops}</td>
<td
><button
disabled={game === currentGameEditing}
on:click={() => {
gameEditing = game.gameId;
}}>Edit</button
>
</td>
<td>
<button
disabled={game === currentGameEditing}
on:click={() => {
const newGames = ($storedGames = $storedGames.filter((g, i) => i !== gameIndex));
if (newGames.length < 1) {
newGames.push(new Game());
}
$storedGames = newGames;
gameEditing = '';
}}>Delete</button
>
</td>
</tr>
{#if game.isComplete || (!game.isComplete && currentGameEditing !== game)}
<tr>
<td class="tdSmall">{gameIndex + 1}</td>
<td class="tdSmall">{game.totalScore}</td>
<td class="tdSmall">{game.stats.bulls}</td>
<td class="tdSmall">{game.stats.totalEightKills}</td>
<td class="tdSmall">{game.stats.totalSixKills}</td>
<td class="tdSmall">{game.stats.drops}</td>
<td
><button
disabled={game === currentGameEditing}
on:click={() => {
gameEditing = game.gameId;
}}>Edit</button
>
</td>
<td>
<button
disabled={game === currentGameEditing}
on:click={() => {
const newGames = ($storedGames = $storedGames.filter(
(g, i) => i !== gameIndex
));
if (newGames.length < 1) {
newGames.push(new Game());
}
$storedGames = newGames;
gameEditing = '';
}}>Delete</button
>
</td>
</tr>
{/if}
{/each}
</tbody>
</table>
@@ -294,7 +343,6 @@
.stats {
display: flex;
justify-content: space-between;
}
.center {
display: flex;
@@ -307,7 +355,7 @@
min-height: 7rem;
flex-wrap: wrap;
}
.flexrowButton {
.flexrowButton {
margin-bottom: 1rem;
}
/* mobile only */

View File

@@ -1,33 +1,39 @@
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;
gameId: string;
scores: Score[];
stats: GameStats;
gameId: string;
}
export class Game implements IStoredGame {
public gameId = Math.random().toString(36).substring(7);
public scores: Score[] = [];
public stats: GameStats = this.GenerateStats();
public addScore(score: Score) {
this.scores.push(score);
this.scores = [...this.scores]
this.stats = this.GenerateStats();
}
public removeScore(scoreNumber: number) {
this.scores.splice(scoreNumber, 1);
this.scores = [...this.scores]
this.stats = this.GenerateStats();
}
public replaceScore(scoreNumber: number, score: Score) {
this.scores[scoreNumber] = score;
this.scores = [...this.scores]
this.stats = this.GenerateStats();
}
public get isComplete() {
return this.scores.length === 10;
}
public convertScoreToNumber(score: Score): number {
public gameId = Math.random().toString(36).substring(7);
public scores: Score[] = [];
public stats: GameStats = this.GenerateStats();
public addScore(score: Score) {
this.scores.push(score);
this.scores = [...this.scores];
this.stats = this.GenerateStats();
}
public removeScore(scoreNumber: number) {
this.scores.splice(scoreNumber, 1);
this.scores = [...this.scores];
this.stats = this.GenerateStats();
}
public replaceScore(scoreNumber: number, score: Score) {
this.scores[scoreNumber] = score;
this.scores = [...this.scores];
this.stats = this.GenerateStats();
}
public get isComplete() {
return this.scores.length === 10;
}
public convertScoreToNumber(score: Score): number {
if (score === 'killHit6') {
return 6;
}
@@ -37,46 +43,51 @@ export class Game implements IStoredGame {
if (score === 'killMiss') {
return 0;
}
if (score === 'drop') {
return 0;
}
if (score === 'drop') {
return 0;
}
return score;
}
public get totalScore() : number {
return this.scores.reduce((acc: number, current) => {
return acc + this.convertScoreToNumber(current);
}, 0);
}
public get KillEnabled() {
const stats = this.GenerateStats();
if(stats.killAttempts < 2) {
return true;
}
if(stats.killAttempts === 2 && stats.drops > 0) {
return true;
}
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 => {
if(score === 6) {
data.bulls += 1
}
if(score === "drop") {
data.drops += 1;
}
if( score === "killHit6" || score === "killHit8" || score === "killMiss") {
data.killAttempts += 1;
}
if(score === "killHit6") {
data.totalSixKills += 1;
}
if(score === "killHit8") {
data.totalEightKills += 1;
}
})
return data;
}
public get totalScore(): number {
return this.scores.reduce((acc: number, current) => {
return acc + this.convertScoreToNumber(current);
}, 0);
}
public get KillEnabled() {
const stats = this.GenerateStats();
if (stats.killAttempts < 2) {
return true;
}
if (stats.killAttempts === 2 && stats.drops > 0) {
return true;
}
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) => {
if (score === 6) {
data.bulls += 1;
}
if (score === 'drop') {
data.drops += 1;
}
if (score === 'killHit6' || score === 'killHit8' || score === 'killMiss') {
data.killAttempts += 1;
}
if (score === 'killHit6') {
data.totalSixKills += 1;
}
if (score === 'killHit8') {
data.totalEightKills += 1;
}
});
return data;
}
}

View File

@@ -1,30 +1,35 @@
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 || 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 => {
browser && localStorage.setItem(DATASTORAGEKEY, JSON.stringify(value));
storedGames.subscribe((value) => {
browser && localStorage.setItem(DATASTORAGEKEY, JSON.stringify(value));
});
function deserializeGames(loadedDate: IStoredGame | Game): Game {
if(loadedDate instanceof Game) {
return loadedDate;
}
const game = new Game();
game.scores = loadedDate.scores;
game.stats = loadedDate.stats;
game.gameId = loadedDate.gameId;
return game;
if (loadedDate instanceof Game) {
return loadedDate;
}
const game = new Game();
game.scores = loadedDate.scores;
game.stats = loadedDate.stats;
game.gameId = loadedDate.gameId;
return game;
}

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' : ''
}
}
};