Skip to content

Commit c7a4337

Browse files
committed
Run prettier
1 parent 6207a58 commit c7a4337

18 files changed

+774
-578
lines changed

.github/workflows/deploy-to-gh-pages.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ jobs:
3434
uses: peaceiris/actions-gh-pages@v3
3535
with:
3636
github_token: ${{ secrets.GITHUB_TOKEN }}
37-
publish_dir: ./dist
37+
publish_dir: ./dist

.vscode/extensions.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["esbenp.prettier-vscode"]
3+
}

.vscode/settings.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"editor.tabSize": 2
3-
}
2+
"editor.tabSize": 2,
3+
"editor.defaultFormatter": "esbenp.prettier-vscode",
4+
"editor.formatOnSave": true
5+
}

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4-
};
4+
}

readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ An online Cyberpunk 2077 breach protocol minigame solver using camera + OCR
55
Website: https://govizlora.github.io/optical-breacher
66

77
## Details
8+
89
The puzzle solving algorithm is simply brute force. (Better ideas welcomed!)
910

1011
The target sequences are weighted from top to bottom as this: `1`, `1.1`, `1.2`, ... So it will focus on more hit first, and when even, the lower sequences will have higher priority.
1112

1213
The OCR part took the most time. I initailly used the default English OCR provided by tesseract, but it fails randomly (like recognizing "55" into "5") and the success rate is below 50%. Eventually I trained the model by myself, using tesstrain. Instead of recognizing single English characters, I let the program treat the byte as a whole, so the computer actually think "55" or "1C" as a single character in a mysterious language. The self-trained model worked better, but still not perfect. TBH I think maybe tesseract is not the best option, but since it's the only popular choice in JavaScript and I'm not famailiar with WASM, this will be the way to go for now.
1314

1415
## Local develop
16+
1517
Make sure you have `node` and `yarn` installed, then clone the repo and run `yarn start`.
1618

1719
## Screenshots
20+
1821
![Screenshot 1](assets/sc1.PNG)
1922
![Screenshot 2](assets/sc2.PNG)
2023

2124
## Acknowledgement
25+
2226
- https://github.com/kyle-rader/breach for test data
2327
- https://github.com/naptha/tesseract.js which made this web app possible
2428
- https://github.com/tesseract-ocr/tesseract and https://github.com/tesseract-ocr/tesstrain tesstrain made training the model a lot easier

src/app.tsx

+52-37
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { useCallback, useEffect, useRef, useState } from "react"
2-
import { Camera } from "./camera";
3-
import { Logger, OCR } from "./ocr";
4-
import { Result } from "./result";
5-
import { processMatrix, processTargets } from "./utils";
1+
import { useCallback, useEffect, useRef, useState } from 'react'
2+
import { Camera } from './camera'
3+
import { Logger, OCR } from './ocr'
4+
import { Result } from './result'
5+
import { processMatrix, processTargets } from './utils'
66

7-
const defaultOcrProgress = { matrixProgress: 0, targetsProgress: 0, status: '' };
8-
const defaultOcrResult: { matrix: string[][], targets: string[][], finished: boolean } =
9-
{ matrix: [], targets: [], finished: false }
7+
const defaultOcrProgress = { matrixProgress: 0, targetsProgress: 0, status: '' }
8+
const defaultOcrResult: {
9+
matrix: string[][]
10+
targets: string[][]
11+
finished: boolean
12+
} = { matrix: [], targets: [], finished: false }
1013

1114
// const matrix = [
1215
// ["1c", "55", "ff", "bd", "e9"],
@@ -24,26 +27,28 @@ const defaultOcrResult: { matrix: string[][], targets: string[][], finished: boo
2427
// ];
2528

2629
export function App() {
27-
const OCRref = useRef<OCR>();
30+
const OCRref = useRef<OCR>()
2831
const [ocrResult, setOcrResult] = useState(defaultOcrResult)
29-
const [ocrProgress, setOcrProgress] = useState(defaultOcrProgress);
30-
const [showCamera, setShowCamera] = useState(true);
32+
const [ocrProgress, setOcrProgress] = useState(defaultOcrProgress)
33+
const [showCamera, setShowCamera] = useState(true)
3134
// const canvasRef = useRef<HTMLCanvasElement>(null);
3235
// const [outputs, setOutputs] = useState<string[]>([]);
3336

3437
const logger: Logger = useCallback(({ name, status, progress = 0 }) => {
3538
if (status === 'recognizing text') {
36-
setOcrProgress(prev => ({
39+
setOcrProgress((prev) => ({
3740
status,
3841
matrixProgress: name === 'matrix' ? progress : prev.matrixProgress,
39-
targetsProgress: name === 'targets' ? progress : prev.targetsProgress
42+
targetsProgress: name === 'targets' ? progress : prev.targetsProgress,
4043
}))
4144
}
4245
}, [])
4346

4447
useEffect(() => {
45-
OCRref.current = new OCR(logger);
46-
return () => { OCRref.current?.terminate(); }
48+
OCRref.current = new OCR(logger)
49+
return () => {
50+
OCRref.current?.terminate()
51+
}
4752
}, [])
4853

4954
return (
@@ -55,12 +60,18 @@ export function App() {
5560
// position: "relative"
5661
}}
5762
>
58-
{showCamera ?
63+
{showCamera ? (
5964
<Camera
6065
onCapture={async (canvas) => {
61-
setShowCamera(false);
62-
const result = await OCRref.current!.recognize(canvas, canvas.width, canvas.height);
63-
const { lines: matrix, chars } = processMatrix(result.matrixData.text)
66+
setShowCamera(false)
67+
const result = await OCRref.current!.recognize(
68+
canvas,
69+
canvas.width,
70+
canvas.height
71+
)
72+
const { lines: matrix, chars } = processMatrix(
73+
result.matrixData.text
74+
)
6475
const targets = processTargets(result.targetsData.text, chars)
6576
setOcrResult({ matrix, targets, finished: true })
6677
// setOutputs([
@@ -79,23 +90,27 @@ export function App() {
7990
// canvasRef.current.getContext('2d')?.drawImage(canvas, 0, 0);
8091
// }
8192
}}
82-
/> :
83-
ocrResult.finished ?
84-
<Result
85-
matrix={ocrResult.matrix}
86-
targets={ocrResult.targets}
87-
onStartOver={() => {
88-
setOcrProgress(defaultOcrProgress);
89-
setOcrResult(defaultOcrResult);
90-
setShowCamera(true);
91-
}}
92-
/> :
93-
<progress
94-
style={{ margin: 'auto' }}
95-
value={ocrProgress.status === 'recognizing text' ?
96-
(ocrProgress.matrixProgress + ocrProgress.targetsProgress) / 2 : 0}
97-
/>
98-
}
93+
/>
94+
) : ocrResult.finished ? (
95+
<Result
96+
matrix={ocrResult.matrix}
97+
targets={ocrResult.targets}
98+
onStartOver={() => {
99+
setOcrProgress(defaultOcrProgress)
100+
setOcrResult(defaultOcrResult)
101+
setShowCamera(true)
102+
}}
103+
/>
104+
) : (
105+
<progress
106+
style={{ margin: 'auto' }}
107+
value={
108+
ocrProgress.status === 'recognizing text'
109+
? (ocrProgress.matrixProgress + ocrProgress.targetsProgress) / 2
110+
: 0
111+
}
112+
/>
113+
)}
99114
{/* <div>
100115
<canvas ref={canvasRef} />
101116
</div> */}
@@ -105,4 +120,4 @@ export function App() {
105120
}
106121
</div>
107122
)
108-
}
123+
}

0 commit comments

Comments
 (0)