Skip to content

Commit

Permalink
Merge pull request #15 from alexander-zibert/add-code-list
Browse files Browse the repository at this point in the history
Add code list
  • Loading branch information
alexander-zibert authored Dec 7, 2023
2 parents 07cbdd9 + 0aad291 commit 32827a8
Show file tree
Hide file tree
Showing 24 changed files with 25,187 additions and 100 deletions.
16 changes: 15 additions & 1 deletion code.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using verifier_factory = std::function<bool(const code_t &)>;
using code_mask_t = std::bitset<NUM_CODES>;

const code_mask_t all_ones_mask = []() { return code_mask_t{}.set(); }();
const code_mask_t all_zeros_mask = code_mask_t{};

constexpr auto iterate_codes = [](const auto &consumer) {
auto i = 0;
Expand All @@ -53,6 +54,7 @@ const auto create_verifier = [](const verifier_factory &is_valid) {
});
return result;
};

const std::vector<std::string> human_codes = []() {
auto result = std::vector<std::string>{};
iterate_codes([&result](int codeIdx, const code_t &code) {
Expand All @@ -61,10 +63,22 @@ const std::vector<std::string> human_codes = []() {
});
return result;
}();

const std::vector<code_t> code_map = []() {
auto result = std::vector<code_t>{};
iterate_codes([&result](int codeIdx, const code_t &code) {
result.emplace_back(code);
});
return result;
}();
}();

const auto get_codes_from_mask = [](const code_mask_t &mask) {
auto result = std::vector<std::string>{};
iterate_codes([&result, &mask](int codeIdx, const code_t &code) {
if (mask[codeIdx]) {
result.emplace_back(std::to_string(code[0]) + std::to_string(code[1]) +
std::to_string(code[2]));
}
});
return result;
};
5 changes: 5 additions & 0 deletions frontend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "es5",
"semi": true,
"tabWidth": 2
}
2 changes: 1 addition & 1 deletion frontend/public/wasm/wasmWrapper.js

Large diffs are not rendered by default.

Binary file modified frontend/public/wasm/wasmWrapper.wasm
Binary file not shown.
44 changes: 40 additions & 4 deletions frontend/public/wasm/worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ globalThis.onerror = (event) => {
importScripts("/turing-machine-board-game-solver/wasm/wasmWrapper.js");

function getPossibleCombinations({
state,
id,
verifierCards,
queries,
mode,
Expand Down Expand Up @@ -97,14 +97,50 @@ function getPossibleCombinations({
}

return {
state,
id,
codes,
possibleVerifiers,
possibleLetters,
};
}

this.onmessage = function onmessage(e) {
const result = getPossibleCombinations(e.data);
function handleData(data) {
if (data.type === "solve_wasm") {
return getPossibleCombinations(data);
}
return callWasmFunction(Module.asm[data.type], Module.asm.memory, data);
}

async function delay(ms) {
return new Promise((res) => setTimeout(res, ms));
}

async function waitForWasmModule() {
while (!Module?.asm) {
await delay(100);
}
}

this.onmessage = async function onmessage(e) {
await waitForWasmModule();
const { data } = e;
const result = handleData(data);
result.id = data.id;
console.log(result);
this.postMessage(result);
};

function callWasmFunction(asmFunction, memory, inputData) {
const data = JSON.stringify(inputData);
const encoder = new TextEncoder();
const input = new Uint8Array(memory.buffer, 0);
const { written } = encoder.encodeInto(data, input);
input[written] = 0;

const output = new Uint8Array(memory.buffer, written + 1, 1000);
const outputSize = asmFunction(input.byteOffset, output.byteOffset);

const decoder = new TextDecoder();
const decoded = decoder.decode(output.slice(0, outputSize));
return JSON.parse(decoded);
}
12 changes: 6 additions & 6 deletions frontend/src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ const Card: FC<Props> = (props) => {
? 21
: 23
: rows === 2
? isDownMd
? 30
: 32
: isDownMd
? 64
: 68,
? isDownMd
? 30
: 32
: isDownMd
? 64
: 68,
width: "100%",
borderRadius: getRadius(slot, rows),
"&:hover": {
Expand Down
126 changes: 75 additions & 51 deletions frontend/src/components/LanguageSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as React from 'react';
import { FC } from 'react';
import { Select as BaseSelect, SelectProps, SelectRootSlotProps, } from '@mui/base/Select';
import { Option as BaseOption, optionClasses } from '@mui/base/Option';
import { SelectOption } from '@mui/base/useOption';
import { styled } from '@mui/system';
import { Popper as BasePopper } from '@mui/base/Popper';
import KeyboardArrowDownRounded from '@mui/icons-material/KeyboardArrowDownRounded';
import * as React from "react";
import { FC } from "react";
import {
Select as BaseSelect,
SelectProps,
SelectRootSlotProps,
} from "@mui/base/Select";
import { Option as BaseOption, optionClasses } from "@mui/base/Option";
import { SelectOption } from "@mui/base/useOption";
import { styled } from "@mui/system";
import { Popper as BasePopper } from "@mui/base/Popper";
import KeyboardArrowDownRounded from "@mui/icons-material/KeyboardArrowDownRounded";
import { useTheme } from "@mui/material/styles";
import { CircleFlag } from "react-circle-flags";

Expand All @@ -23,91 +27,91 @@ declare type Language = {
};

const availableCountries: Language[] = [
{code: 'pt', language: "Português", tm: 'BR'},
{code: 'cn', language: '简体中文', tm: 'CNS'},
{code: 'cn', language: '繁體中文', tm: 'CNT'},
{code: 'cz', language: 'Česky', tm: "CZ"},
{code: 'de', language: 'Deutsch', tm: "DE"},
{code: 'uk', language: 'English', tm: 'EN'},
{code: 'fr', language: 'Français', tm: 'FR'},
{code: 'gr', language: 'Ελληνικά', tm: 'GR'},
{code: 'hu', language: 'Magyar', tm: 'HU'},
{code: 'it', language: 'Italiano', tm: 'IT'},
{code: 'jp', language: '日本語', tm: 'JP'},
{code: 'kr', language: '한국어', tm: 'KR'},
{code: 'nl', language: 'Dutch', tm: 'NL'},
{code: 'pl', language: 'Polski', tm: 'PL'},
{code: 'ru', language: 'Русский', tm: 'RU'},
{code: 'es', language: 'Español', tm: "SP"},
{code: 'th', language: 'ไทย', tm: 'TH'},
{code: 'ua', language: 'Українська', tm: 'UA'},
{ code: "pt", language: "Português", tm: "BR" },
{ code: "cn", language: "简体中文", tm: "CNS" },
{ code: "cn", language: "繁體中文", tm: "CNT" },
{ code: "cz", language: "Česky", tm: "CZ" },
{ code: "de", language: "Deutsch", tm: "DE" },
{ code: "uk", language: "English", tm: "EN" },
{ code: "fr", language: "Français", tm: "FR" },
{ code: "gr", language: "Ελληνικά", tm: "GR" },
{ code: "hu", language: "Magyar", tm: "HU" },
{ code: "it", language: "Italiano", tm: "IT" },
{ code: "jp", language: "日本語", tm: "JP" },
{ code: "kr", language: "한국어", tm: "KR" },
{ code: "nl", language: "Dutch", tm: "NL" },
{ code: "pl", language: "Polski", tm: "PL" },
{ code: "ru", language: "Русский", tm: "RU" },
{ code: "es", language: "Español", tm: "SP" },
{ code: "th", language: "ไทย", tm: "TH" },
{ code: "ua", language: "Українська", tm: "UA" },
];

const LanguageSelect: FC<Props> = (props) => {

function getCountry() {
return availableCountries.find(c => c.tm === props.value);
return availableCountries.find((c) => c.tm === props.value);
}

return (
<Select
id={`${props.prefixId}-select`}
disabled={props.disabled}
value={getCountry()}
renderValue={(selected: SelectOption<Language> | null) =>
<CircleFlag countryCode={selected?.value.code || "uk"} height={24}/>}
renderValue={(selected: SelectOption<Language> | null) => (
<CircleFlag countryCode={selected?.value.code || "uk"} height={24} />
)}
onChange={(_, value) => {
props.onChange && props.onChange((value && value.tm) || "EN");
}}
>
{availableCountries.map((country: Language) => (
<Option
key={`${country.code}-${country.tm}`}
value={country}
>
<CircleFlag countryCode={country?.code || "uk"} height={24}/>
<Option key={`${country.code}-${country.tm}`} value={country}>
<CircleFlag countryCode={country?.code || "uk"} height={24} />
{country.language}
</Option>
))}
</Select>
);
}
};

export default LanguageSelect;

const Select = React.forwardRef(function CustomSelect(
props: SelectProps<Language, false>,
ref: React.ForwardedRef<any>
) {
const slots: SelectProps<Language, false>['slots'] = {
const slots: SelectProps<Language, false>["slots"] = {
root: Button,
listbox: Listbox,
popper: Popper,
...props.slots,
};

return <BaseSelect {...props} ref={ref} slots={slots}/>;
return <BaseSelect {...props} ref={ref} slots={slots} />;
});

const Button = React.forwardRef(function Button<
TValue extends {},
Multiple extends boolean
Multiple extends boolean,
>(
props: SelectRootSlotProps<TValue, Multiple>,
ref: React.ForwardedRef<HTMLButtonElement>
) {
const theme = useTheme();
const {ownerState, ...other} = props;
const { ownerState, ...other } = props;
return (
<StyledButton type="button" {...other} ref={ref}>
{other.children}
<KeyboardArrowDownRounded fontSize="large" sx={{color: theme.palette.primary.light}}/>
<KeyboardArrowDownRounded
fontSize="large"
sx={{ color: theme.palette.primary.light }}
/>
</StyledButton>
);
});

const StyledButton = styled('button', {shouldForwardProp: () => true})(
({theme}) => `
const StyledButton = styled("button", { shouldForwardProp: () => true })(
({ theme }) => `
box-sizing: border-box;
min-width: 80px;
padding: 8px;
Expand Down Expand Up @@ -138,8 +142,8 @@ const StyledButton = styled('button', {shouldForwardProp: () => true})(
`
);

const Listbox = styled('ul')(
({theme}) => `
const Listbox = styled("ul")(
({ theme }) => `
font-family: "Plus Jakarta Sans";
font-size: 1rem;
box-sizing: border-box;
Expand All @@ -151,27 +155,47 @@ const Listbox = styled('ul')(
overflow: auto;
outline: 0px;
background: ${theme.palette.background.paper};
border: 1px solid ${theme.palette.mode === 'dark' ? theme.palette.languageSwitch[800] : theme.palette.languageSwitch[100]};
border: 1px solid ${
theme.palette.mode === "dark"
? theme.palette.languageSwitch[800]
: theme.palette.languageSwitch[100]
};
`
);

const Option = styled(BaseOption)(
({theme}) => `
({ theme }) => `
list-style: none;
padding: 4px;
border-radius: 4px;
cursor: default;
&.${optionClasses.selected},
&.${optionClasses.highlighted}.${optionClasses.selected} {
background-color: ${theme.palette.mode === 'dark' ? theme.palette.languageSwitch[900] : theme.palette.languageSwitch[100]};
color: ${theme.palette.mode === 'dark' ? theme.palette.languageSwitch[100] : theme.palette.languageSwitch[900]};
background-color: ${
theme.palette.mode === "dark"
? theme.palette.languageSwitch[900]
: theme.palette.languageSwitch[100]
};
color: ${
theme.palette.mode === "dark"
? theme.palette.languageSwitch[100]
: theme.palette.languageSwitch[900]
};
}
&:hover,
&.${optionClasses.highlighted} {
background-color: ${theme.palette.mode === 'dark' ? theme.palette.languageSwitch[800] : theme.palette.languageSwitch[100]};
color: ${theme.palette.mode === 'dark' ? theme.palette.languageSwitch[300] : theme.palette.languageSwitch[900]};
background-color: ${
theme.palette.mode === "dark"
? theme.palette.languageSwitch[800]
: theme.palette.languageSwitch[100]
};
color: ${
theme.palette.mode === "dark"
? theme.palette.languageSwitch[300]
: theme.palette.languageSwitch[900]
};
}
&:hover {
Expand Down
Loading

0 comments on commit 32827a8

Please sign in to comment.