Skip to content

Commit

Permalink
check turn validity if cached
Browse files Browse the repository at this point in the history
  • Loading branch information
mrosack committed Dec 13, 2023
1 parent a8d4e0a commit 0e15b5c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
18 changes: 15 additions & 3 deletions ui/playTurn/playTurn.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,20 @@ export class PlayTurnComponent implements OnInit, OnDestroy {

this.turnDownloader.startDownload();

if (this.turnDownloader.data$.value) {
this.status = "Turn already downloaded, moving to save location...";
const curData = this.turnDownloader.data$.value;

if (curData) {
this.status = "Turn already downloaded, validating...";

const turn = await this.gameService.getTurn(this.playTurnState.game.gameId, "yup").toPromise();

if (turn.version === curData.version) {
this.status = "Turn downloaded & validated, moving to save location...";
} else {
this.status = "Turn data is out of date, re-downloading...";
this.turnDownloader.abort();
this.turnDownloader.startDownload();
}
}

this.turnDownloader.error$.subscribe(err => {
Expand All @@ -126,7 +138,7 @@ export class PlayTurnComponent implements OnInit, OnDestroy {
}, 500);

try {
window.pydtApi.fs.writeFileSync(this.saveFileToPlay, data);
window.pydtApi.fs.writeFileSync(this.saveFileToPlay, data.data);

await new Promise(sleepResolve => setTimeout(sleepResolve, 500));

Expand Down
7 changes: 5 additions & 2 deletions ui/shared/turnCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PydtSettingsFactory } from "./pydtSettings";
export class TurnDownloader {
private xhr: XMLHttpRequest;
private downloading = false;
public readonly data$ = new BehaviorSubject<Uint8Array>(null);
public readonly data$ = new BehaviorSubject<{ data: Uint8Array; version?: string }>(null);
public readonly error$ = new BehaviorSubject<string>(null);
public readonly curBytes$ = new BehaviorSubject<number>(0);
public readonly maxBytes$ = new BehaviorSubject<number>(0);
Expand Down Expand Up @@ -113,7 +113,10 @@ export class TurnDownloader {
// Ignore - file probably wasn't gzipped...
}

this.data$.next(data);
this.data$.next({
data,
version: resp.version,
});
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.error$.next(err);
Expand Down

0 comments on commit 0e15b5c

Please sign in to comment.