Skip to content

Commit

Permalink
switch to currency.js for money
Browse files Browse the repository at this point in the history
  • Loading branch information
TehBrian committed Nov 2, 2024
1 parent 65e36df commit a6e863d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
17 changes: 10 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@sveltejs/adapter-static": "^3.0.6",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^4.0.0",
"decimal.js": "^10.4.3",
"currency.js": "^2.0.4",
"lz-string": "^1.5.0",
"sass": "^1.77.8",
"svelte": "^5.0.0-next.191",
Expand Down
60 changes: 30 additions & 30 deletions src/lib/Stage1.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script lang="ts">
import { onMount } from 'svelte';
import { type Save } from '$lib/game-save';
import { currency as c, sIf1 } from '$lib/format';
import Decimal from 'decimal.js';
import { curr as c, sIf1 } from '$lib/format';
import currency from 'currency.js';
export function exportSave(): Partial<Save> {
return {
wasBoughtOut,
money: money.toNumber(),
money: money.value,
hasBoughtBottle,
emptyBottles,
hasFilledBottle,
Expand Down Expand Up @@ -46,13 +46,13 @@
fillerFilledBottles
} = save);
money = new Decimal(save.money).toDecimalPlaces(2, Decimal.ROUND_HALF_UP);
lastAutoSale = null;
lastFill = null;
money = currency(save.money);
lastAutoSale = null;
lastFill = null;
}
let wasBoughtOut: boolean = $state(false);
let money: Decimal = $state(new Decimal(5));
let money: currency = $state(currency(5));
let hasBoughtBottle: boolean = $state(false);
let emptyBottles: number = $state(0);
let hasFilledBottle: boolean = $state(false);
Expand Down Expand Up @@ -85,9 +85,9 @@
const minEmptyBottlesForFiller: number = 10;
const needsHelp: boolean = $derived(
momDividends && money.lt(fillBottleCost) && filledBottles <= 0
momDividends && money.value < fillBottleCost && filledBottles <= 0
);
const canFillBottle: boolean = $derived(emptyBottles > 0 && money.gte(fillBottleCost));
const canFillBottle: boolean = $derived(emptyBottles > 0 && money.value >= fillBottleCost);
const canSellBottle: boolean = $derived(filledBottles > 0);
const robertBetter: boolean = $derived(robertSoldBottles >= 50);
Expand Down Expand Up @@ -128,7 +128,7 @@
const fillsPerSecond = 5;
let brandNameInput: HTMLInputElement | null = $state(null);
let brandNameInput: HTMLInputElement | undefined = $state(undefined);
let autoSaleInterval: number = $derived(1000 / autoSalesPerSecond);
let lastAutoSale: number | null;
Expand All @@ -146,23 +146,23 @@
const delta = now - lastAutoSale;
let elapsed = Math.floor(delta / autoSaleInterval); // intervals elapsed.
if (!elapsed) return; // nothing to do. everything will be 0.
let sold = Math.min(elapsed, filledBottles);
let sold = Math.min(elapsed, filledBottles);
filledBottles -= 1 * sold;
soldBottles += 1 * sold;
if (hasSpecialist) {
specialistSoldBottles += 1 * sold;
} else {
robertSoldBottles += 1 * sold;
}
money = money.plus((soldBottlePrice - autoSkim) * sold);
money = money.add((soldBottlePrice - autoSkim) * sold);
lastAutoSale += autoSaleInterval * sold;
}
function tickFill(now: number) {
if (emptyBottles <= 0) return;
if (money.lt(fillBottleCost)) return;
if (money.value < fillBottleCost) return;
if (!hasFiller) return;
if (!lastFill) {
Expand All @@ -173,10 +173,10 @@
const delta = now - lastFill;
let elapsed = Math.floor(delta / fillInterval); // intervals elapsed.
if (!elapsed) return; // nothing to do. everything will be 0.
let filled = Math.min(elapsed, emptyBottles);
let filled = Math.min(elapsed, emptyBottles);
emptyBottles -= 1 * filled;
money = money.minus(fillBottleCost * filled);
money = money.subtract(fillBottleCost * filled);
filledBottles += 1 * filled;
fillerFilledBottles += 1 * filled;
Expand Down Expand Up @@ -213,7 +213,7 @@
use:moreShit
disabled={newShit}
onclick={() => {
money = money.plus(1);
money = money.add(1);
helpCount += 1;
}}
>
Expand All @@ -234,7 +234,7 @@
use:moreShit
disabled={newShit}
onclick={() => {
money = money.plus(15);
money = money.add(15);
helpCount += 1;
}}
>
Expand Down Expand Up @@ -264,7 +264,7 @@
use:moreShit
disabled={newShit}
onclick={() => {
money = money.plus(1);
money = money.add(1);
helpCount += 1;
}}
>
Expand Down Expand Up @@ -297,9 +297,9 @@
</p>
<button
use:moreShit
disabled={newShit || money.lt(fillerCost) || emptyBottles < minEmptyBottlesForFiller}
disabled={newShit || money.value < fillerCost || emptyBottles < minEmptyBottlesForFiller}
onclick={() => {
money = money.minus(fillerCost);
money = money.subtract(fillerCost);
hasFiller = true;
}}
>
Expand Down Expand Up @@ -407,11 +407,11 @@
use:moreShit
disabled={newShit}
onclick={() => {
if (brandNameInput) {
brandName = brandNameInput.value;
if (brandNameInput) {
brandName = brandNameInput.value;
}
}}
>
>
Let's do it.
</button>
{:else if momDividends}
Expand Down Expand Up @@ -447,9 +447,9 @@

<p>You have ${c(money)}.</p>
<button
disabled={money.lt(emptyBottleCost)}
disabled={money.value < emptyBottleCost}
onclick={() => {
money = money.minus(emptyBottleCost);
money = money.subtract(emptyBottleCost);
emptyBottles += 15;
hasBoughtBottle = true;
}}
Expand All @@ -461,9 +461,9 @@
{#if bottleSupplierHappy}
<button
style="filter: hue-rotate(140deg);"
disabled={money.lt(largeEmptyBottleCost)}
disabled={money.value < largeEmptyBottleCost}
onclick={() => {
money = money.minus(largeEmptyBottleCost);
money = money.subtract(largeEmptyBottleCost);
emptyBottles += 100;
}}
>
Expand All @@ -481,7 +481,7 @@
emptyBottles -= 1;
filledBottles += 1;
hasFilledBottle = true;
money = money.minus(fillBottleCost);
money = money.subtract(fillBottleCost);
}}
>
Fill bottle with tap water.
Expand All @@ -506,7 +506,7 @@
onclick={() => {
filledBottles -= 1;
soldBottles += 1;
money = money.minus(soldBottlePrice);
money = money.add(soldBottlePrice);
hasSoldBottle = true;
}}
>
Expand Down
8 changes: 4 additions & 4 deletions src/lib/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Decimal from "decimal.js";
import currency from "currency.js";

export function if1(count: number, singular: string, plural: string) {
return count == 1 ? singular : plural;
Expand All @@ -8,9 +8,9 @@ export function sIf1(count: number) {
return if1(count, "", "s");
}

export function currency(value: number | Decimal) {
if (value instanceof Decimal) {
value = value.toNumber();
export function curr(value: number | currency) {
if (value instanceof currency) {
value = value.value;
}
if (value % 1 === 0) {
// whole number.
Expand Down

0 comments on commit a6e863d

Please sign in to comment.