WIP share solution feature
This commit is contained in:
parent
9796c66ba5
commit
9e4180c2cd
2 changed files with 24 additions and 9 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { For } from "solid-js";
|
import { For, Show } from "solid-js";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
import usePuzzleModel from "./usePuzzleModel";
|
import usePuzzleModel from "./usePuzzleModel";
|
||||||
import FitText from "./FitText";
|
import FitText from "./FitText";
|
||||||
|
@ -134,6 +134,11 @@ function Puzzle() {
|
||||||
Submit
|
Submit
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
<Show when={store.solvedGroups.length === 4}>
|
||||||
|
<pre on:click={() => navigator.clipboard.writeText(store.guessHistory)}>
|
||||||
|
{store.guessHistory}
|
||||||
|
</pre>
|
||||||
|
</Show>
|
||||||
</div>
|
</div>
|
||||||
{store.solvedGroups.length === 4 && <div class="celebration" />}
|
{store.solvedGroups.length === 4 && <div class="celebration" />}
|
||||||
</main>
|
</main>
|
||||||
|
|
|
@ -19,8 +19,11 @@ type PuzzleStore = {
|
||||||
selected: number[];
|
selected: number[];
|
||||||
solvedGroups: Answer[];
|
solvedGroups: Answer[];
|
||||||
puzzle: number[];
|
puzzle: number[];
|
||||||
|
guessHistory: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const emoji = ["🟨", "🟩", "🟦", "🟪"];
|
||||||
|
|
||||||
export default function usePuzzleModel(id: Accessor<number>) {
|
export default function usePuzzleModel(id: Accessor<number>) {
|
||||||
const [store, setStore] = createStore<PuzzleStore>({
|
const [store, setStore] = createStore<PuzzleStore>({
|
||||||
guesses: 0,
|
guesses: 0,
|
||||||
|
@ -28,22 +31,22 @@ export default function usePuzzleModel(id: Accessor<number>) {
|
||||||
selected: [],
|
selected: [],
|
||||||
solvedGroups: [],
|
solvedGroups: [],
|
||||||
puzzle: [],
|
puzzle: [],
|
||||||
|
guessHistory: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
const {
|
const { setSolution } = useAppModel();
|
||||||
setSolution
|
|
||||||
} = useAppModel()
|
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(() => {
|
||||||
id()
|
|
||||||
setStore({
|
setStore({
|
||||||
guesses: 0,
|
guesses: 0,
|
||||||
pinnedCount: 0,
|
pinnedCount: 0,
|
||||||
selected: [],
|
selected: [],
|
||||||
solvedGroups: [],
|
solvedGroups: [],
|
||||||
puzzle: shuffleArray(Array.from({ length: 16 }, (_, i) => i)),
|
puzzle: shuffleArray(Array.from({ length: 16 }, (_, i) => i)),
|
||||||
|
guessHistory: `Connections
|
||||||
|
Puzzle #${id()}`,
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
const answers = (): Answer[] => {
|
const answers = (): Answer[] => {
|
||||||
return connections.find((x) => x.id === id())!.answers;
|
return connections.find((x) => x.id === id())!.answers;
|
||||||
|
@ -61,11 +64,18 @@ export default function usePuzzleModel(id: Accessor<number>) {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleGuess = () => {
|
const handleGuess = () => {
|
||||||
setStore('guesses', x => x+1)
|
setStore("guesses", (x) => x + 1);
|
||||||
const selected = store.puzzle.length === 4 ? [0, 1, 2, 3] : store.selected;
|
const selected = store.puzzle.length === 4 ? [0, 1, 2, 3] : store.selected;
|
||||||
const selectedAnswers = selected.map((x) => getFromPuzzle(x));
|
const selectedAnswers = selected.map((x) => getFromPuzzle(x));
|
||||||
const { level } = selectedAnswers[0];
|
const { level } = selectedAnswers[0];
|
||||||
const isCorrect = selectedAnswers.every((x) => x.level === level);
|
const isCorrect = selectedAnswers.every((x) => x.level === level);
|
||||||
|
const guessHistoryLine = selectedAnswers
|
||||||
|
.map((x) => emoji[x.level])
|
||||||
|
.join("");
|
||||||
|
setStore({
|
||||||
|
guessHistory: `${store.guessHistory}
|
||||||
|
${guessHistoryLine}`,
|
||||||
|
});
|
||||||
if (!isCorrect) {
|
if (!isCorrect) {
|
||||||
// TODO you got it wrong
|
// TODO you got it wrong
|
||||||
alert("wrong");
|
alert("wrong");
|
||||||
|
@ -84,11 +94,11 @@ export default function usePuzzleModel(id: Accessor<number>) {
|
||||||
});
|
});
|
||||||
const newSolvedGroup = answers().find((x) => x.level === level);
|
const newSolvedGroup = answers().find((x) => x.level === level);
|
||||||
if (newSolvedGroup != null) {
|
if (newSolvedGroup != null) {
|
||||||
setStore('solvedGroups', x => x.concat(newSolvedGroup))
|
setStore("solvedGroups", (x) => x.concat(newSolvedGroup));
|
||||||
}
|
}
|
||||||
if (store.puzzle.length === 0) {
|
if (store.puzzle.length === 0) {
|
||||||
// completely solved!
|
// completely solved!
|
||||||
setSolution(id(), store.guesses)
|
setSolution(id(), store.guesses);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue