-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper: toggle real input, paste testinput keybinds
- Loading branch information
1 parent
19d6e86
commit acb62e0
Showing
1 changed file
with
49 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,63 @@ | ||
import "jsr:@std/dotenv/load"; | ||
import { exists } from "https://deno.land/[email protected]/fs/mod.ts"; | ||
import { readKeypress } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { paste } from 'https://deno.land/x/clipboard/mod.ts'; | ||
|
||
const date = new Date() | ||
if (date.getMonth() === 11) { | ||
console.log('Checking for downloadable data...') | ||
const filename = `S${date.getFullYear().toString().slice(-2)}${date.getDate().toString().padStart(2, '0')}.txt`; | ||
if (Array.from(Deno.readDirSync('../data')).map(entry => entry.name).includes(filename)) { | ||
console.log('Data is already downloaded.') | ||
} else { | ||
console.log('Downloading data...') | ||
const data = await fetch(`https://adventofcode.com/${date.getFullYear()}/day/${date.getDate()}/input`, { | ||
headers: { | ||
cookie: 'session=' + Deno.env.get('AOC_SESSION') | ||
} | ||
}).then(response => response.text()) | ||
Deno.writeTextFileSync('../data/' + filename, data.trimEnd()) | ||
} | ||
} else { | ||
console.log('Not December, skipping data download.') | ||
} | ||
|
||
let realinput = false; | ||
console.log('Connecting to websocket...'); | ||
websocket(); | ||
function websocket() { | ||
const ws = new WebSocket('ws://localhost:9000/wss'); | ||
|
||
let latestsolution: string; | ||
ws.onmessage = async (event) => { | ||
ws.onmessage = (event) => { | ||
const json = JSON.parse(event.data); | ||
if (json.type === 'solutions') { | ||
console.log('Solutions received, triggering latest solution...'); | ||
latestsolution = json.data.sort((a: string, b: string) => Number(b.replace('S', '')) - Number(a.replace('S', '')))[0] | ||
latestsolution = json.data.sort((a: string, b: string) => Number(b.replace('S', '')) - Number(a.replace('S', '')))[ 0 ] | ||
if (realinput) { | ||
ws.send(JSON.stringify({ | ||
type: 'data', | ||
name: latestsolution, | ||
})); | ||
if (await exists('./testinput.txt')) { | ||
const testinput = Deno.readTextFileSync('./testinput.txt'); | ||
if (testinput !== '') { | ||
ws.send(JSON.stringify({ | ||
type: 'run', | ||
solution: latestsolution, | ||
input: testinput, | ||
})); | ||
} | ||
} | ||
sendTestInput(); | ||
} else if (json.type === 'result') { | ||
console.log(`${json.data}`); | ||
} else if (json.type === 'data') { | ||
ws.send(JSON.stringify({ type: 'run', solution: latestsolution, input: json.data })); | ||
if (realinput) { | ||
ws.send(JSON.stringify({ type: 'run', solution: latestsolution, input: json.data })); | ||
} | ||
} | ||
}; | ||
|
||
ws.onopen = () => { | ||
ws.onopen = async () => { | ||
console.log('Connected to websocket'); | ||
console.log("Press 'r' to toggle real input testing, 'v' to copy clipboard to testinput.txt, 'ctrl+c' to exit"); | ||
for await (const keypress of readKeypress()) { | ||
if (keypress.key === 'r') { | ||
realinput = !realinput; | ||
console.log(`Turned ${realinput ? 'on' : 'off'} real input testing`); | ||
if (latestsolution) { | ||
sendTestInput(); | ||
if (realinput) { | ||
ws.send(JSON.stringify({ | ||
type: 'data', | ||
name: latestsolution, | ||
})); | ||
} | ||
} | ||
} | ||
if (keypress.key === 'v') { | ||
const input = await paste(); | ||
Deno.writeTextFileSync("testinput.txt", input); | ||
console.log('Copied clipboard to testinput.txt'); | ||
sendTestInput(); | ||
} | ||
if (keypress.key === 'c' && keypress.ctrlKey) { | ||
Deno.exit(); | ||
} | ||
} | ||
} | ||
|
||
ws.onclose = () => { | ||
|
@@ -67,4 +71,17 @@ function websocket() { | |
ws.onerror = (error) => { | ||
console.log((error as ErrorEvent).message); | ||
} | ||
|
||
async function sendTestInput() { | ||
if (await exists('./testinput.txt')) { | ||
const testinput = Deno.readTextFileSync('./testinput.txt'); | ||
if (testinput !== '') { | ||
ws.send(JSON.stringify({ | ||
type: 'run', | ||
solution: latestsolution, | ||
input: testinput, | ||
})); | ||
} | ||
} | ||
} | ||
} |