UNPKG

modaq

Version:

Quiz Bowl Reader using TypeScript, React, and MobX

168 lines 8.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validatePronunicationGuideMarker = exports.validatePowerSettings = exports.resetGameFormat = exports.normalizeGameFormat = exports.isGameFormatValid = exports.changePairTossupsBonuses = exports.changeOvertimeBonuses = exports.changeBounceback = exports.changeMinimumQuestionsInOvertime = exports.changePronunciationGuideMarkers = exports.changePowerValues = exports.changePowerMarkers = exports.changeNegValue = exports.changeRegulationTossupCount = void 0; require("../state/CustomizeGameFormatState"); require("../state/IGameFormat"); const customFormatName = "Custom"; function changeRegulationTossupCount(customizeGameFormatState, newValue) { const count = getNumberOrUndefined(newValue); if (count != undefined) { customizeGameFormatState.updateGameFormat({ regulationTossupCount: count }); } } exports.changeRegulationTossupCount = changeRegulationTossupCount; function changeNegValue(customizeGameFormatState, newValue) { const negValue = getNumberOrUndefined(newValue); if (negValue != undefined) { customizeGameFormatState.updateGameFormat({ negValue }); } } exports.changeNegValue = changeNegValue; function changePowerMarkers(customizeGameFormatState, newValue) { if (newValue == undefined) { return; } else if (newValue.trim() === "") { customizeGameFormatState.setPowerMarkers([]); return; } // TODO: Handle power markers with commas, which would require handling escapes const powerMarkers = newValue.split(",").map((value) => value.trim()); customizeGameFormatState.setPowerMarkers(powerMarkers); } exports.changePowerMarkers = changePowerMarkers; function changePowerValues(customizeGameFormatState, newValue) { if (newValue == undefined) { return; } customizeGameFormatState.setPowerValues(newValue); } exports.changePowerValues = changePowerValues; function changePronunciationGuideMarkers(customizeGameFormatState, newValue) { if (newValue == undefined || newValue.trim() === "") { customizeGameFormatState.setPronunciationGuideMarkers([]); return; } const guides = newValue.split(","); customizeGameFormatState.setPronunciationGuideMarkers(guides); } exports.changePronunciationGuideMarkers = changePronunciationGuideMarkers; function changeMinimumQuestionsInOvertime(customizeGameFormatState, newValue) { const minimumOvertimeQuestionCount = getNumberOrUndefined(newValue); if (minimumOvertimeQuestionCount != undefined) { customizeGameFormatState.updateGameFormat({ minimumOvertimeQuestionCount }); } } exports.changeMinimumQuestionsInOvertime = changeMinimumQuestionsInOvertime; function changeBounceback(customizeGameFormatState, checked) { if (checked == undefined) { return; } customizeGameFormatState.updateGameFormat({ bonusesBounceBack: checked }); } exports.changeBounceback = changeBounceback; function changeOvertimeBonuses(customizeGameFormatState, checked) { if (checked == undefined) { return; } customizeGameFormatState === null || customizeGameFormatState === void 0 ? void 0 : customizeGameFormatState.updateGameFormat({ overtimeIncludesBonuses: checked }); } exports.changeOvertimeBonuses = changeOvertimeBonuses; function changePairTossupsBonuses(customizeGameFormatState, checked) { if (checked == undefined) { return; } customizeGameFormatState === null || customizeGameFormatState === void 0 ? void 0 : customizeGameFormatState.updateGameFormat({ pairTossupsBonuses: checked }); } exports.changePairTossupsBonuses = changePairTossupsBonuses; function isGameFormatValid(state) { if (state == undefined) { throw new Error("Tried changing a format with no modified format"); } return (state.powerMarkerErrorMessage == undefined && state.powerValuesErrorMessage == undefined && state.pronunciationGuideMarkersErrorMessage == undefined); } exports.isGameFormatValid = isGameFormatValid; function normalizeGameFormat(state, oldGameFormat) { const updatedGameFormat = Object.assign({}, state.gameFormat); if (updatedGameFormat.displayName !== customFormatName) { // TS will complain about implicit any if we use Object.keys to do a deep comparison, so cast the objects as any, // and then do a deep comparison // eslint-disable-next-line @typescript-eslint/no-explicit-any const anyUpdatedGameFormat = updatedGameFormat; // eslint-disable-next-line @typescript-eslint/no-explicit-any const anyExistingGameFormat = oldGameFormat; for (const key of Object.keys(updatedGameFormat)) { if (anyUpdatedGameFormat[key] !== anyExistingGameFormat[key]) { updatedGameFormat.displayName = customFormatName; break; } } } // Power values should be in descending order, so sort them before saving the game format const powers = []; const powerValues = state.powerValues.split(",").map((value) => parseInt(value, 10)); for (let i = 0; i < state.powerMarkers.length; i++) { powers.push({ marker: state.powerMarkers[i], points: powerValues[i] }); } powers.sort(sortPowersDescending); updatedGameFormat.powers = powers; if (state.pronunicationGuideMarkers == undefined || state.pronunicationGuideMarkers.length === 0) { updatedGameFormat.pronunciationGuideMarkers = undefined; } else if (state.pronunicationGuideMarkers.length === 2 && state.pronunicationGuideMarkers.every((value) => value.length > 0)) { updatedGameFormat.pronunciationGuideMarkers = [ state.pronunicationGuideMarkers[0], state.pronunicationGuideMarkers[1], ]; } state.updateGameFormat(updatedGameFormat); } exports.normalizeGameFormat = normalizeGameFormat; function resetGameFormat(customizeGameFormatState, gameFormat) { customizeGameFormatState.updateGameFormat(gameFormat); } exports.resetGameFormat = resetGameFormat; function validatePowerSettings(customizeGameFormatState) { customizeGameFormatState.clearPowerErrorMessages(); const powerValues = customizeGameFormatState.powerValues.split(",").map((value) => parseInt(value, 10)); if (customizeGameFormatState.powerMarkers.length < powerValues.length) { customizeGameFormatState.setPowerMarkerErrorMessage("More power markers needed. The number of power markers and values must be the same."); } else if (powerValues.length < customizeGameFormatState.powerMarkers.length) { customizeGameFormatState.setPowerValuesErrorMessage("More values needed. The number of power markers and values must be the same."); } else if (powerValues.some((value) => isNaN(value))) { customizeGameFormatState.setPowerValuesErrorMessage("Power values must be integers."); } } exports.validatePowerSettings = validatePowerSettings; function validatePronunicationGuideMarker(customizeGameFormatState) { const pronunciationGuideMarkers = customizeGameFormatState.pronunicationGuideMarkers; if (pronunciationGuideMarkers == undefined) { return; } if (pronunciationGuideMarkers.length !== 0 && pronunciationGuideMarkers.length !== 2) { customizeGameFormatState.setPronunciationGuideMarkersErrorMessage("Either no pronunciation guide is used, or there only needs to be a start and end marker specifeid"); } else if (pronunciationGuideMarkers.some((value) => value.length === 0)) { customizeGameFormatState.setPronunciationGuideMarkersErrorMessage("Pronunciation guide markers cannot be empty; put a character before or after the comma"); } else { customizeGameFormatState.clearPronunciationGuideMarkersErrorMessage(); } } exports.validatePronunicationGuideMarker = validatePronunicationGuideMarker; function getNumberOrUndefined(value) { if (value == undefined) { return undefined; } const number = Number.parseInt(value, 10); return isNaN(number) ? undefined : number; } function sortPowersDescending(left, right) { return right.points - left.points; } //# sourceMappingURL=CustomizeGameFormatFormController.js.map