From 50b7dee93b4a1fe5d056c28fe56e705789842693 Mon Sep 17 00:00:00 2001 From: mgabor <9047995+mgabor3141@users.noreply.github.com> Date: Wed, 16 Oct 2024 11:47:12 +0200 Subject: [PATCH] Add version/DLC switcher - update signals - make it clearer that this only works with Factorio 2.0 --- app/components/result-stage.tsx | 105 +- app/components/select-stage.tsx | 7 +- .../song-to-factorio.test.ts.snap | 2 +- app/lib/blueprint.ts | 58 +- app/lib/data/signals-dlc.json | 2323 +++++++++-------- app/lib/data/signals.json | 1917 ++++++-------- app/lib/song-to-factorio.test.ts | 3 +- app/lib/song-to-factorio.ts | 5 +- package.json | 2 +- tools/data/signals-dlc.csv | 60 +- tools/data/signals.csv | 436 +--- tools/process-factorio-signals.ts | 12 +- 12 files changed, 2140 insertions(+), 2790 deletions(-) diff --git a/app/components/result-stage.tsx b/app/components/result-stage.tsx index d8bf931..15493db 100644 --- a/app/components/result-stage.tsx +++ b/app/components/result-stage.tsx @@ -1,34 +1,105 @@ import { Dispatch, useCallback, useState } from 'react' import { Song } from '@/app/components/select-stage' import { songToFactorio } from '@/app/lib/song-to-factorio' +import signals from '@/app/lib/data/signals.json' +import signalsDlc from '@/app/lib/data/signals-dlc.json' +import { usePostHog } from 'posthog-js/react' + +/** + * @param text + * @returns boolean: success + */ +const copyToClipboard = async (text: string): Promise => { + try { + const type = 'text/plain' + const blob = new Blob([text], { type }) + const data = [new ClipboardItem({ [type]: blob })] + await navigator.clipboard.write(data) + return true + } catch (e: unknown) { + console.warn(`Could not copy to clipboard. Update your browser.`, e) + return false + } +} export type ResultStageProps = { song: Song - onBack?: Dispatch + onBack: Dispatch } export const ResultStage = ({ song, onBack }: ResultStageProps) => { + const postHog = usePostHog() + + type version = '1' | '2' | '2sa' + const [targetVersion, setTargetVersion] = useState('2') + + const [copySuccess, setCopySuccess] = useState(false) const [blueprintString, setBlueprintString] = useState('') - const getBlueprint = useCallback(() => { - setBlueprintString(songToFactorio(song)) - }, [song]) + const getBlueprint = useCallback(async () => { + setCopySuccess(false) + const signalSet = targetVersion === '2sa' ? signalsDlc : signals + + const bp = songToFactorio(song, signalSet) + const copyAttempt = await copyToClipboard(bp) + setCopySuccess(copyAttempt) + setBlueprintString(bp) + postHog?.capture('Generated blueprint', { + Blueprint: bp, + 'Clipboard Success': copyAttempt, + }) + }, [postHog, song, targetVersion]) + + const versionOptions: { value: version; title: string }[] = [ + { value: '1', title: 'Factorio 1.x' }, + { value: '2', title: 'Factorio 2.x' }, + { value: '2sa', title: 'Factorio 2.x with Space Age DLC' }, + ] return ( -
-
- - -