-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
177 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,97 @@ | ||
import { Component, Inject, OnInit } from '@angular/core'; | ||
import { FuelTank } from './fuel-gages/fuel-tank.model'; | ||
import { Subscription, combineLatest, interval, timer } from 'rxjs'; | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
Inject, | ||
OnInit, | ||
computed, | ||
signal, | ||
} from '@angular/core'; | ||
import { FuelTank } from './fuel-tank.model'; | ||
import { Subscription, interval, timer } from 'rxjs'; | ||
import { FuelGagesState } from './fuel-gages/fuel-gages.state'; | ||
import { Result } from './result.model'; | ||
import { RESULTS_SERVICE, ResultsService } from './results.service'; | ||
import { Result } from './results/result.model'; | ||
import { RESULTS_SERVICE, ResultsService } from './results/results.service'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class AppComponent implements OnInit { | ||
private static readonly MAX_DURATION = 30; | ||
|
||
readonly tanks = [new FuelTank(64), new FuelTank(112)]; | ||
readonly totalCapacity = this.tanks | ||
readonly totalCapacity: number = this.tanks | ||
.map((tank) => tank.getCapacity()) | ||
.reduce((a, b) => a + b, 0); | ||
|
||
private static readonly MAX_DURATION = 30; | ||
totalQuantity = computed(() => { | ||
let sum = 0; | ||
for (const tank of this.tanks) { | ||
sum += tank.getQuantity$()(); | ||
} | ||
return sum; | ||
}); | ||
fuelGagesState = signal(FuelGagesState.HIDDEN); | ||
time = signal(0); | ||
result = signal<Result | null>(null); | ||
showOverlay = signal(false); | ||
|
||
totalQuantity: number = 0; | ||
fuelGagesState = FuelGagesState.HIDDEN; | ||
time = 0; | ||
timeSubscription: Subscription | null = null; | ||
result: Result | null = null; | ||
showOverlay: boolean = false; | ||
private _timeSubscription: Subscription | null = null; | ||
|
||
constructor( | ||
@Inject(RESULTS_SERVICE) private readonly resultsService: ResultsService, | ||
) { | ||
const quantityObservables = []; | ||
for (const tank of this.tanks) { | ||
quantityObservables.push(tank.getQuantity$()); | ||
} | ||
combineLatest(quantityObservables).subscribe((quantities) => { | ||
let sum = 0; | ||
for (let quantity of quantities) { | ||
sum += quantity; | ||
} | ||
this.totalQuantity = sum; | ||
}); | ||
} | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.roll(); | ||
} | ||
|
||
restartButtonClicked(): void { | ||
if (this.fuelGagesState === FuelGagesState.VISIBLE) { | ||
if (this.fuelGagesState() === FuelGagesState.VISIBLE) { | ||
this.roll(); | ||
} | ||
} | ||
|
||
private roll(): void { | ||
this.fuelGagesState = FuelGagesState.RESETTING; | ||
this.fuelGagesState.set(FuelGagesState.RESETTING); | ||
for (const tank of this.tanks) { | ||
tank.setLevel(Math.random() * 100); | ||
} | ||
// FIXME: Instead of a timer, can we get an event when the CSS transition has completed? | ||
timer(2000).subscribe((_value) => { | ||
this.fuelGagesState = FuelGagesState.HIDDEN; | ||
this.time = 0; | ||
this.timeSubscription = interval(1000).subscribe((_value) => { | ||
++this.time; | ||
if (this.time === AppComponent.MAX_DURATION) { | ||
this.timeSubscription?.unsubscribe(); | ||
this.fuelGagesState.set(FuelGagesState.HIDDEN); | ||
this.time.set(0); | ||
this._timeSubscription = interval(1000).subscribe((_value) => { | ||
this.time.update((t) => t + 1); | ||
if (this.time() === AppComponent.MAX_DURATION) { | ||
this._timeSubscription?.unsubscribe(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
private reveal(guess: number): void { | ||
this.timeSubscription?.unsubscribe(); | ||
this.fuelGagesState = FuelGagesState.VISIBLE; | ||
this.result = new Result(new Date(), guess, this.totalQuantity, this.time); | ||
this.resultsService.add(this.result); | ||
this._timeSubscription?.unsubscribe(); | ||
this.fuelGagesState.set(FuelGagesState.VISIBLE); | ||
const result = new Result( | ||
new Date(), | ||
guess, | ||
this.totalQuantity(), | ||
this.time(), | ||
); | ||
this.result.set(result); | ||
this.resultsService.add(result); | ||
} | ||
|
||
guessSubmitted(guess: number): void { | ||
if (this.fuelGagesState === FuelGagesState.HIDDEN) { | ||
if (this.fuelGagesState() === FuelGagesState.HIDDEN) { | ||
this.reveal(guess); | ||
} | ||
} | ||
|
||
overlayToggled(visible: boolean): void { | ||
this.showOverlay = visible; | ||
this.showOverlay.set(visible); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.