Skip to content

Commit

Permalink
Merge 6af2b70 into 81e0f1d
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Billing committed Jun 27, 2021
2 parents 81e0f1d + 6af2b70 commit 4eebbdc
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 70 deletions.
2 changes: 1 addition & 1 deletion tnoodle-ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val yarnInstall = tasks.named("yarn_install") {
val yarnBuild = tasks.named("yarn_build") {
dependsOn(yarnInstall)

inputs.files(fileTree("src").exclude("*.css"))
inputs.files(fileTree("src/main").exclude("*.css"))
inputs.dir("public")
inputs.file("package.json")

Expand Down
3 changes: 2 additions & 1 deletion tnoodle-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"build": "react-scripts build",
"test": "react-scripts test --watchAll --watchAll=false --coverage",
"eject": "react-scripts eject",
"prettier": "prettier --check ."
"prettier": "prettier --check .",
"lint": "prettier --write ."
},
"eslintConfig": {
"extends": [
Expand Down
9 changes: 5 additions & 4 deletions tnoodle-ui/src/main/api/tnoodle.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import WcaEvent from "../model/WcaEvent";
import WcaFormat from "../model/WcaFormat";
import Wcif from "../model/Wcif";
import { ScrambleClient } from "./tnoodle.socket";
import WebsocketBlobResult from "../model/WebsocketBlobResult";
import FrontendStatus from "../model/FrontendStatus";

let backendUrl = new URL("http://localhost:2014");
export const tNoodleBackend = backendUrl.toString().replace(/\/$/g, "");
Expand Down Expand Up @@ -62,22 +64,21 @@ class TnoodleApi {
wcif: Wcif,
mbld: string,
password: string,
status: FrontendStatus,
translations?: Translation[]
) => {
let payload = {
wcif,
multiCubes: { requestedScrambles: mbld },
fmcLanguages: fmcTranslationsHelper(translations),
zipPassword: !password ? null : password,
frontendStatus: status,
};

return scrambleClient.loadScrambles(zipEndpoint, payload, wcif.id);
};

convertToBlob = async (result: {
contentType: string;
payload: string;
}) => {
convertToBlob = async (result: WebsocketBlobResult) => {
let { contentType, payload } = result;
let res = await fetch(`data:${contentType};base64,${payload}`);

Expand Down
18 changes: 8 additions & 10 deletions tnoodle-ui/src/main/api/tnoodle.socket.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { tNoodleBackend } from "./tnoodle.api";
import WebsocketBlobResult from "../model/WebsocketBlobResult";

export type ScrambleHandshakeFn = (payload: Record<string, number>) => void;
export type ScrambleProgressFn = (payload: string) => void;

export type ScramblingBlobResult = any & { contentType: string; payload: any };

enum ScramblingState {
Idle,
Initiate,
Expand All @@ -21,10 +20,10 @@ export class ScrambleClient {

state: ScramblingState;

contentType: string | null;
contentType: string;

resultPayload: object | null;
errorPayload: object | null;
resultPayload: string;
errorPayload: any;

constructor(
onHandshake: ScrambleHandshakeFn,
Expand All @@ -35,17 +34,17 @@ export class ScrambleClient {

this.state = ScramblingState.Idle;

this.contentType = null;
this.contentType = FALLBACK_APPLICATION_TYPE;

this.resultPayload = null;
this.resultPayload = "";
this.errorPayload = null;
}

loadScrambles(
endpoint: String,
payload: object,
targetMarker: String
): Promise<ScramblingBlobResult> {
): Promise<WebsocketBlobResult> {
return new Promise((resolve, reject) => {
let ws = new WebSocket(BASE_URL + endpoint);

Expand All @@ -61,8 +60,7 @@ export class ScrambleClient {
ws.onclose = (cls) => {
if (this.state === ScramblingState.Done && cls.wasClean) {
let resultObject = {
contentType:
this.contentType ?? FALLBACK_APPLICATION_TYPE,
contentType: this.contentType,
payload: this.resultPayload,
};

Expand Down
28 changes: 24 additions & 4 deletions tnoodle-ui/src/main/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import EventPickerTable from "./EventPickerTable";
import Interceptor from "./Interceptor";
import "./Main.css";
import VersionInfo from "./VersionInfo";
import WebsocketBlobResult from "../model/WebsocketBlobResult";

const Main = () => {
const [competitionNameFileZip, setCompetitionNameFileZip] = useState("");
Expand All @@ -33,8 +34,11 @@ const Main = () => {
const generatingScrambles = useSelector(
(state: RootState) => state.scramblingSlice.generatingScrambles
);
const officialZipStatus = useSelector(
(state: RootState) => state.scramblingSlice.officialZipStatus
const isValidSignedBuild = useSelector(
(state: RootState) => state.scramblingSlice.isValidSignedBuild
);
const isAllowedVersion = useSelector(
(state: RootState) => state.scramblingSlice.isAllowedVersion
);
const fileZip = useSelector(
(state: RootState) => state.scramblingSlice.fileZip
Expand Down Expand Up @@ -72,9 +76,23 @@ const Main = () => {
onScrambleProgress
);

let frontendStatus = {
isStaging: isUsingStaging(),
isManual: competitionId == null,
isSignedBuild: isValidSignedBuild,
isAllowedVersion: isAllowedVersion,
};

tnoodleApi
.fetchZip(scrambleClient, wcif, mbld, password, translations)
.then((plainZip: { contentType: string; payload: string }) =>
.fetchZip(
scrambleClient,
wcif,
mbld,
password,
frontendStatus,
translations
)
.then((plainZip: WebsocketBlobResult) =>
dispatch(setFileZip(plainZip))
)
.catch((err: any) => interceptorRef.current?.updateMessage(err))
Expand All @@ -90,6 +108,8 @@ const Main = () => {
// If TNoodle version is not official (as per VersionInfo) or if we generate scrambles using
// a competition from staging, add a [Unofficial]

let officialZipStatus = isValidSignedBuild && isAllowedVersion;

let isUnofficialZip =
!officialZipStatus || (competitionId != null && isUsingStaging());

Expand Down
15 changes: 8 additions & 7 deletions tnoodle-ui/src/main/components/VersionInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { useDispatch } from "react-redux";
import tnoodleApi from "../api/tnoodle.api";
import wcaApi from "../api/wca.api";
import CurrentTnoodle from "../model/CurrentTnoodle";
import { setOfficialZipStatus } from "../redux/slice/ScramblingSlice";
import {
setAllowedVersion,
setValidSignedBuild,
} from "../redux/slice/ScramblingSlice";

const VersionInfo = () => {
const [currentTnoodle, setCurrentTnoodle] = useState<CurrentTnoodle>();
Expand Down Expand Up @@ -46,20 +49,18 @@ const VersionInfo = () => {
}, [dispatch]);

// This avoids global state update while rendering
const analyzeVerion = () => {
const analyzeVersion = () => {
// We wait until both wca and tnoodle answers
if (!allowedTnoodleVersions || !runningVersion) {
return;
}

dispatch(
setOfficialZipStatus(
signatureValid &&
allowedTnoodleVersions.includes(runningVersion)
)
setAllowedVersion(allowedTnoodleVersions.includes(runningVersion))
);
dispatch(setValidSignedBuild(signatureValid));
};
useEffect(analyzeVerion, [
useEffect(analyzeVersion, [
allowedTnoodleVersions,
dispatch,
runningVersion,
Expand Down
6 changes: 6 additions & 0 deletions tnoodle-ui/src/main/model/FrontendStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default interface FrontendStatus {
isStaging: boolean;
isManual: boolean;
isSignedBuild: boolean;
isAllowedVersion: boolean;
}
4 changes: 4 additions & 0 deletions tnoodle-ui/src/main/model/WebsocketBlobResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface WebsocketBlobResult {
contentType: string;
payload: string;
}
23 changes: 14 additions & 9 deletions tnoodle-ui/src/main/redux/slice/ScramblingSlice.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import WebsocketBlobResult from "../../model/WebsocketBlobResult";

interface ScramblingState {
fileZip?: { contentType: string; payload: string };
fileZip?: WebsocketBlobResult;
generatingScrambles: boolean;
officialZipStatus: boolean;
isValidSignedBuild: boolean;
isAllowedVersion: boolean;
password: string;
scramblingProgressCurrent: Record<string, number>;
scramblingProgressTarget: Record<string, number>;
Expand All @@ -12,8 +14,9 @@ interface ScramblingState {
const initialState: ScramblingState = {
fileZip: undefined,
generatingScrambles: false,
isValidSignedBuild: false,
isAllowedVersion: false,
password: "",
officialZipStatus: true,
scramblingProgressCurrent: {},
scramblingProgressTarget: {},
};
Expand All @@ -24,17 +27,18 @@ export const scramblingSlice = createSlice({
reducers: {
setFileZip: (
state,
action: PayloadAction<
{ contentType: string; payload: string } | undefined
>
action: PayloadAction<WebsocketBlobResult | undefined>
) => {
state.fileZip = action.payload;
},
setGeneratingScrambles: (state, action: PayloadAction<boolean>) => {
state.generatingScrambles = action.payload;
},
setOfficialZipStatus: (state, action: PayloadAction<boolean>) => {
state.officialZipStatus = action.payload;
setValidSignedBuild: (state, action: PayloadAction<boolean>) => {
state.isValidSignedBuild = action.payload;
},
setAllowedVersion: (state, action: PayloadAction<boolean>) => {
state.isAllowedVersion = action.payload;
},
setPassword: (state, action: PayloadAction<string>) => {
state.password = action.payload;
Expand All @@ -60,7 +64,8 @@ export const scramblingSlice = createSlice({

export const {
setFileZip,
setOfficialZipStatus,
setValidSignedBuild,
setAllowedVersion,
setPassword,
setGeneratingScrambles,
resetScramblingProgressCurrent,
Expand Down
16 changes: 7 additions & 9 deletions tnoodle-ui/src/test/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { configureStore } from "@reduxjs/toolkit";
import { fireEvent } from "@testing-library/react";
import { shuffle } from "lodash";
import React from "react";
Expand All @@ -10,15 +9,10 @@ import tnoodleApi from "../main/api/tnoodle.api";
import wcaApi from "../main/api/wca.api";
import Translation from "../main/model/Translation";
import Wcif from "../main/model/Wcif";
import { competitionSlice } from "../main/redux/slice/CompetitionSlice";
import { fmcSlice } from "../main/redux/slice/FmcSlice";
import { informationSlice } from "../main/redux/slice/InformationSlice";
import { mbldSlice } from "../main/redux/slice/MbldSlice";
import { scramblingSlice } from "../main/redux/slice/ScramblingSlice";
import { wcifSlice } from "../main/redux/slice/WcifSlice";
import { defaultWcif } from "../main/util/wcif.util";
import {
bestMbldAttempt,
defaultStatus,
events,
formats,
languages,
Expand All @@ -32,12 +26,14 @@ import {
scrambleProgram,
wcifs,
} from "./mock/wca.api.test.mock";
import FrontendStatus from "../main/model/FrontendStatus";

let container = document.createElement("div");

let wcif: Wcif | null = null;
let mbld: string | null = null;
let password: string | null = null;
let status: FrontendStatus | null = null;
let translations: Translation[] | undefined;
beforeEach(() => {
// setup a DOM element as a render target
Expand Down Expand Up @@ -68,13 +64,14 @@ beforeEach(() => {
);

jest.spyOn(tnoodleApi, "fetchZip").mockImplementation(
(scrambleClient, _wcif, _mbld, _password, _translations) => {
(scrambleClient, _wcif, _mbld, _password, _status, _translations) => {
wcif = _wcif;
mbld = _mbld;
password = _password;
status = _status;
translations = _translations;

return Promise.resolve({ ...axiosResponse, data: plainZip });
return Promise.resolve(plainZip);
}
);

Expand Down Expand Up @@ -147,6 +144,7 @@ it("Just generate scrambles", async () => {
expect(wcif!.events.length).toBe(1);

expect(password).toBe("");
expect(status).toEqual(defaultStatus);
});

it("Changes on 333, scramble", async () => {
Expand Down
7 changes: 7 additions & 0 deletions tnoodle-ui/src/test/mock/tnoodle.api.test.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ export const plainZip = {
payload: "UEsDBBQACAgIAK...",
};

export const defaultStatus = {
isStaging: false,
isManual: true,
isSignedBuild: true,
isAllowedVersion: true,
};

export const bestMbldAttempt = {
solved: 60,
attempted: 60,
Expand Down
Loading

0 comments on commit 4eebbdc

Please sign in to comment.