Skip to content

Commit

Permalink
Run linter
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkellly committed May 21, 2024
1 parent 8279071 commit 8b79411
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 70 deletions.
30 changes: 16 additions & 14 deletions src/components/cubing/btCubeDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useStore } from '@tanstack/react-store';
import { experimentalSolve3x3x3IgnoringCenters } from 'cubing/search';
import { TwistyPlayer } from 'cubing/twisty';
import { GanCubeMove } from 'gan-web-bluetooth';
import { useEffect, useRef, useState } from 'react';
import { CubeStore } from '@/lib/smartCube';
import { cn } from '@/lib/utils';
import cubeImage from '/cube-colors.png';
import { experimentalSolve3x3x3IgnoringCenters } from 'cubing/search';
import { GanCubeMove } from 'gan-web-bluetooth';

export default function BTCubeDisplay({ className }: { className: string }) {
const containerRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -48,23 +48,25 @@ export default function BTCubeDisplay({ className }: { className: string }) {
moves.push(ev);
});

experimentalSolve3x3x3IgnoringCenters(CubeStore.state.kpattern!).then(solution => {
player.alg = solution.invert();
experimentalSolve3x3x3IgnoringCenters(CubeStore.state.kpattern!).then(
solution => {
player.alg = solution.invert();

sub.unsubscribe();
moves.forEach(move => {
player.experimentalAddMove(move.move);
});
sub.unsubscribe();
moves.forEach(move => {
player.experimentalAddMove(move.move);
});

sub = cube.events$.subscribe(ev => {
if (ev.type !== 'MOVE') return;
player.experimentalAddMove(ev.move);
});
});
sub = cube.events$.subscribe(ev => {
if (ev.type !== 'MOVE') return;
player.experimentalAddMove(ev.move);
});
}
);

return () => {
sub.unsubscribe();
}
};
}, [player, startingState, cube]);

const classes = cn('flex', className);
Expand Down
14 changes: 8 additions & 6 deletions src/components/timer/timerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,14 @@ export const useCubeTimer = () => {

console.log('Scramble:', TimerStore.state.originalScramble);
let last = times.length > 0 ? times[0].cubeTimestamp : 0;
console.table(algs.map(([alg, idx]) => {
const ms = times[idx].cubeTimestamp - last;
const time = (ms / 1000).toFixed(2);
last = times[idx].cubeTimestamp;
return [alg, time];
}));
console.table(
algs.map(([alg, idx]) => {
const ms = times[idx].cubeTimestamp - last;
const time = (ms / 1000).toFixed(2);
last = times[idx].cubeTimestamp;
return [alg, time];
})
);

newScramble();
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export class MySubClassedDexie extends Dexie {
constructor() {
super('myDatabase');
this.version(1).stores({
friends: '++id, name, age' // Primary key and indexed props
friends: '++id, name, age', // Primary key and indexed props
});
}
}

export const db = new MySubClassedDexie();
export const db = new MySubClassedDexie();
77 changes: 50 additions & 27 deletions src/lib/solutionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,26 @@ function applyRotation(rotation: string, move: string) {

function fixSlicesForComm(alg: Alg, puzzle: KPuzzle) {
const transformation = puzzle.algToTransformation(alg);
const centerPermutations = transformation.transformationData['CENTERS'].permutation;
const centerPermutations =
transformation.transformationData['CENTERS'].permutation;

let neededMove: [string, string] | undefined;
if (centerPermutations[0] == 0 && centerPermutations[5] == 5) {
if (centerPermutations[1] == 4) neededMove = ["U'", "D"];
if (centerPermutations[1] == 2) neededMove = ["U", "D'"];
if (centerPermutations[1] == 3) neededMove = ["U2", "D2"];
if (centerPermutations[1] == 4) neededMove = ["U'", 'D'];
if (centerPermutations[1] == 2) neededMove = ['U', "D'"];
if (centerPermutations[1] == 3) neededMove = ['U2', 'D2'];
}

if (centerPermutations[2] == 2 && centerPermutations[4] == 4) {
if (centerPermutations[0] == 1) neededMove = ["F", "B'"];
if (centerPermutations[0] == 3) neededMove = ["F", "B"];
if (centerPermutations[0] == 5) neededMove = ["F2", "B2"];
if (centerPermutations[0] == 1) neededMove = ['F', "B'"];
if (centerPermutations[0] == 3) neededMove = ['F', 'B'];
if (centerPermutations[0] == 5) neededMove = ['F2', 'B2'];
}

if (centerPermutations[1] == 1 && centerPermutations[3] == 3) {
if (centerPermutations[0] == 4) neededMove = ["L", "R'"];
if (centerPermutations[0] == 2) neededMove = ["L'", "R"];
if (centerPermutations[0] == 5) neededMove = ["L2", "R2"];
if (centerPermutations[0] == 4) neededMove = ['L', "R'"];
if (centerPermutations[0] == 2) neededMove = ["L'", 'R'];
if (centerPermutations[0] == 5) neededMove = ['L2', 'R2'];
}

if (!neededMove) return alg.toString().split(' ');
Expand All @@ -112,8 +113,18 @@ function fixSlicesForComm(alg: Alg, puzzle: KPuzzle) {
const isSecond = move[0] == neededMove[1];
if (isFirst || isSecond) {
const altMove = isFirst ? neededMove[1] : neededMove[0];
const oppositeMove = altMove.length == 1 ? altMove + "'" : altMove[1] == '2' ? altMove : altMove[0];
newMoves = [...moves.slice(0, i), oppositeMove, altMove, ...moves.slice(i)];
const oppositeMove =
altMove.length == 1
? altMove + "'"
: altMove[1] == '2'
? altMove
: altMove[0];
newMoves = [
...moves.slice(0, i),
oppositeMove,
altMove,
...moves.slice(i),
];
break;
}
}
Expand Down Expand Up @@ -215,7 +226,9 @@ export function convertToSliceMoves(moves: string[]) {
return newMoves;
}

function checkTransformationIs3Cycle(transformation: KTransformation): [isEdge: boolean, isCorner: boolean] {
function checkTransformationIs3Cycle(
transformation: KTransformation
): [isEdge: boolean, isCorner: boolean] {
const corners = transformation.transformationData['CORNERS'];
const edges = transformation.transformationData['EDGES'];

Expand All @@ -237,7 +250,10 @@ function checkTransformationIs3Cycle(transformation: KTransformation): [isEdge:
if (positionMatches && orientationMatches) edgeCount++;
}

return [(cornerCount == 8 && edgeCount == 9), (cornerCount == 5 && edgeCount == 12)];
return [
cornerCount == 8 && edgeCount == 9,
cornerCount == 5 && edgeCount == 12,
];
}

function uncancelTransformation(
Expand Down Expand Up @@ -292,15 +308,14 @@ function uncancelTransformation(
}

export function simplify(alg: string) {
return Alg.fromString(alg)
.experimentalSimplify({
cancel: {
puzzleSpecificModWrap: 'canonical-centered',
directional: 'any-direction',
},
puzzleLoader: cube3x3x3,
depth: 1,
});
return Alg.fromString(alg).experimentalSimplify({
cancel: {
puzzleSpecificModWrap: 'canonical-centered',
directional: 'any-direction',
},
puzzleLoader: cube3x3x3,
depth: 1,
});
}

export async function extractAlgs(
Expand Down Expand Up @@ -328,7 +343,12 @@ export async function extractAlgs(
if (uncancelled.length > 0 && moveSet[0][0] === uncancelled.alg[0])
continue;

comms.push([(moves + ' ' + uncancelled.alg).trim(), moveIdx, uncancelled.isEdge, uncancelled.isCorner]);
comms.push([
(moves + ' ' + uncancelled.alg).trim(),
moveIdx,
uncancelled.isEdge,
uncancelled.isCorner,
]);

count = uncancelled.length;
// TODO: There might be a flaw in the logic....
Expand All @@ -337,7 +357,9 @@ export async function extractAlgs(
}

if (moves.length > 0) {
const [isEdge, isCorner] = checkTransformationIs3Cycle(puzzle.algToTransformation(moves));
const [isEdge, isCorner] = checkTransformationIs3Cycle(
puzzle.algToTransformation(moves)
);
comms.push([moves, moveIdx, isEdge, isCorner]);
}

Expand All @@ -349,7 +371,9 @@ export async function extractAlgs(

const isEdgeComm = val[2];
if (isEdgeComm) {
const slicesWithRotations = convertToSliceMoves(simplifiedComm.toString().split(' '));
const slicesWithRotations = convertToSliceMoves(
simplifiedComm.toString().split(' ')
);
const slices = removeRotations(slicesWithRotations);
const fixedAlg = fixSlicesForComm(new Alg(slices.join(' ')), puzzle);
foundComm = commutator.search({
Expand All @@ -372,7 +396,6 @@ export async function extractAlgs(
})[0];
}


if (foundComm.endsWith('.')) {
return [simplify(comm.trim()) + ' // not found', val[1]] as [
string,
Expand Down
15 changes: 9 additions & 6 deletions src/routes/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { createFileRoute } from '@tanstack/react-router';
import { useStore } from '@tanstack/react-store';
import { Copy } from 'lucide-react';
import { useHotkeysContext } from 'react-hotkeys-hook';
import { Timer, TimerRenderer } from 'react-use-precision-timer';
import BTCubeDisplay from '@/components/cubing/btCubeDisplay';
import DrawScramble from '@/components/timer/drawScramble';
import { TimerStore, useCubeTimer } from '@/components/timer/timerStore';
import { Button } from '@/components/ui/button';
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from '@/components/ui/tooltip';
import { CubeStore } from '@/lib/smartCube';
import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';
import { Copy } from 'lucide-react';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';

export const Route = createFileRoute('/dashboard')({
component: Dashboard,
Expand All @@ -26,7 +30,7 @@ function ScrambleDisplay() {

const copyScramble = () => {
window.navigator.clipboard.writeText(TimerStore.state.originalScramble);
}
};

// TODO: Make this not affect timer positioning
return (
Expand All @@ -49,8 +53,7 @@ function ScrambleDisplay() {
{move}
</div>
);
})}
{' '}
})}{' '}
<Tooltip>
<TooltipTrigger asChild>
<Button size="icon" variant="outline" onClick={copyScramble}>
Expand Down
38 changes: 23 additions & 15 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import {
removeRotations,
} from '@/lib/solutionParser';

describe("Slice Moves", () => {
describe('Slice Moves', () => {
test('Converts slice moves M', () => {
const alg = "R' L";
const converted = convertToSliceMoves(alg.split(' '));
expect(converted.join(' ')).toBe("M' x'");
});

test('Converts slice moves E', () => {
const alg = "U D'";
const converted = convertToSliceMoves(alg.split(' '));
expect(converted.join(' ')).toBe('E y');
});

test('Converts slice moves S', () => {
const alg = "F B'";
const converted = convertToSliceMoves(alg.split(' '));
expect(converted.join(' ')).toBe("S' z");
});

test('Converts double slice moves', () => {
const alg = 'F2 B2';
const converted = convertToSliceMoves(alg.split(' '));
Expand All @@ -35,40 +35,46 @@ describe("Slice Moves", () => {
const converted = convertToSliceMoves(alg.split(' '));
const rotationsRemoves = removeRotations(converted);
expect(rotationsRemoves.join(' ')).toBe("U' M U' M' U' M U' M'");
});
});
});

describe("Extract Comms", () => {
describe('Extract Comms', () => {
test('Extract valid comm (EP)', async () => {
const extracted = await extractAlgs("R U' R' D' R U2 R' D R U' R'".split(' '));
const extracted = await extractAlgs(
"R U' R' D' R U2 R' D R U' R'".split(' ')
);
expect(extracted.length).toBe(1);
expect(extracted[0][0]).toBe("[R U': [R' D' R, U2]]");
});

test('Extract valid comm (BG)', async () => {
const extracted = await extractAlgs("D' R' D R U R' D' R U' D".split(' '));
expect(extracted.length).toBe(1);
expect(extracted[0][0]).toBe("[D': [R' D R, U]]");
});

test('Extract invalid comm', async () => {
const extracted = await extractAlgs("R U' R'".split(' '));
expect(extracted.length).toBe(1);
expect(extracted[0][0]).toBe("R U' R' // not found");
});

test('Extract slice alg (FK)', async () => {
const extracted = await extractAlgs("F' B L D L' R F' F' L R' D L' F B'".split(' '));
const extracted = await extractAlgs(
"F' B L D L' R F' F' L R' D L' F B'".split(' ')
);
expect(extracted.length).toBe(1);
expect(extracted[0][0]).toBe("[S U L: [E', L2]]");
});

test('Extract slice alg (GJ) with double moves', async () => {
const extracted = await extractAlgs("R' L F R' F' L R' D R D' L L R' R'".split(' '));
const extracted = await extractAlgs(
"R' L F R' F' L R' D R D' L L R' R'".split(' ')
);
expect(extracted.length).toBe(1);
expect(extracted[0][0]).toBe("[M': [U R' U', M']]");
});

test('Extract slice alg (FK) with extra moves', async () => {
const extracted = await extractAlgs(
"R U B D D' B' U' R' F' B L D L' R F' F' L R' D L' F B'".split(' ')
Expand All @@ -78,8 +84,10 @@ describe("Extract Comms", () => {
});

test('Extract slice alg (RI) with wide move canceling', async () => {
const extracted = await extractAlgs("U' R L' B' R' B F' U B U' F B' L U".split(' '));
const extracted = await extractAlgs(
"U' R L' B' R' B F' U B U' F B' L U".split(' ')
);
expect(extracted.length).toBe(1);
expect(extracted[0][0]).toBe("[U' M: [U', R' E R]]");
})
});
});

0 comments on commit 8b79411

Please sign in to comment.