Skip to content

Commit

Permalink
Get dice rolls from last remote cursor
Browse files Browse the repository at this point in the history
Problems: Remote can re-generate quickly to find bad dice?

TODO: find second last remote cursor when spectating and validating
TODO: Show when validating and local-random
  • Loading branch information
cjmalloy committed Oct 19, 2023
1 parent f90f5b3 commit 00e6cc8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
59 changes: 49 additions & 10 deletions src/app/component/backgammon/backgammon.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { StompService } from '../../service/api/stomp.service';
import { AuthzService } from '../../service/authz.service';
import { ConfigService } from '../../service/config.service';
import { Store } from '../../store/store';
import { hash } from 'src/app/model/tag';

export type Piece = 'r' | 'b';
export type Spot = {
Expand Down Expand Up @@ -158,7 +159,7 @@ export class BackgammonComponent implements OnInit, AfterViewInit, OnDestroy {
if (prev.length || !current.length) return;
this.ref!.comment = u.comment;
this.store.eventBus.refresh(this.ref);
this.load(current);
this.load(current, u.origin !== this.store.account.origin);
const roll = current.find(m => m.includes('-'));
if (roll) {
const lastRoll = this.incomingRolling = roll.split(' ')[0] as Piece;
Expand Down Expand Up @@ -222,6 +223,42 @@ export class BackgammonComponent implements OnInit, AfterViewInit, OnDestroy {
this.resizeObserver?.disconnect();
}

hash(step = 0) {
let ts = this.cursor;
if (!ts) {
if (!this.board.length) {
// Allow first move to use local cursor
ts = this.ref?.modifiedString;
} else {
console.warn($localize`Can only use RNG after remote turn.`);
return Math.random();
}
}
return hash(ts, step);
}

remoteHash(step = 0) {
let ts = this.ref?.modifiedString;
if (!ts) {
if (!this.board.length) {
// Allow first move to use local cursor
ts = this.cursor;
} else {
console.warn($localize`Can only use RNG after remote turn.`);
return Math.random();
}
}
return hash(ts, step);
}

rng(step = 0) {
return Math.floor(this.remoteHash(step) * 6) + 1;
}

checkRng(step = 0) {
return Math.floor(this.hash(step) * 6) + 1;
}

get ref() {
return this._ref;
}
Expand Down Expand Up @@ -341,7 +378,7 @@ export class BackgammonComponent implements OnInit, AfterViewInit, OnDestroy {
this.load(board.split('\n').map(m => m.trim()).filter(m => !!m));
}

load(moves?: string[]) {
load(moves?: string[], checkRng = false) {
this.moves = this.getAllMoves();
if (!moves) return;
for (const m of moves) {
Expand All @@ -351,6 +388,13 @@ export class BackgammonComponent implements OnInit, AfterViewInit, OnDestroy {
const ds = p === 'r' ? this.redDice : this.blackDice;
ds[0] = parseInt(m[2]);
ds[1] = parseInt(m[4]);
if (checkRng) {
// This does not work when observing a game between two separate players
// Need to maintain second last remote cursor
if (ds[0] !== this.checkRng(0) || ds[1] !== this.checkRng(3)) {
window.alert(`Dice were ${ds[0]}-${ds[1]} but should be ${this.checkRng(0)}-${this.checkRng(3)}`)
}
}
this.board.push(`${p} ${ds[0]}-${ds[1]}`);
this.diceUsed = [];
if (!this.turn && this.redDice[0] && this.blackDice[0]) {
Expand Down Expand Up @@ -718,13 +762,13 @@ export class BackgammonComponent implements OnInit, AfterViewInit, OnDestroy {
if ((!this.first || this.turn !== p) && this.moves.length) throw $localize`Must move`;
if (!this.turn) {
if (ds[0]) return;
ds[0] = this.r();
ds[0] = this.rng(0);
this.board.push(`${p} ${ds[0]}-0`);
} else {
if (!this.first && this.turn === p) throw $localize`Not your turn`;
this.turn = p;
ds[0] = this.r();
ds[1] = this.r();
ds[0] = this.rng(0);
ds[1] = this.rng(3);
this.board.push(`${p} ${ds[0]}-${ds[1]}`)
}
if (!this.turn && this.redDice[0] && this.blackDice[0]) {
Expand All @@ -741,9 +785,4 @@ export class BackgammonComponent implements OnInit, AfterViewInit, OnDestroy {
this.moves = this.getAllMoves();
this.save();
}

r() {
// TODO: Hash cursor
return Math.floor(Math.random() * 6) + 1;
}
}
11 changes: 11 additions & 0 deletions src/app/model/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ export interface Cursor extends HasOrigin {
modifiedString?: string;
}

export function hash(ts?: string, shift = 0) {
if (shift > 5) throw 'Only 6 numbers available';
const m = ts?.match(/(\d{6})Z?$/)?.[1].split('')!;
if (!m) throw 'No hash available';
while (shift) {
shift--;
m.unshift(m.pop()!);
}
return parseInt(m.join('')) / 1000000.0;
}

export interface Tag extends Cursor {
type?: 'ext' | 'user' | 'plugin' | 'template';
tag: string;
Expand Down

0 comments on commit 00e6cc8

Please sign in to comment.