Skip to content

Commit

Permalink
fix json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSammyM committed Jan 14, 2025
1 parent ba1835a commit d93e3d0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 24 deletions.
14 changes: 9 additions & 5 deletions client/src/components/gameModeSettings/OutlineSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function RoleOutlineSelector(props: RoleOutlineSelectorProps): Re
{props.roleOutline.map((option, index) => {
let roleOrRoleSet: RoleOrRoleSet;

if (option.role) {
if ("role" in option) {
roleOrRoleSet = {
type: "role",
role: option.role
Expand Down Expand Up @@ -71,12 +71,16 @@ export default function RoleOutlineSelector(props: RoleOutlineSelectorProps): Re
let options = [...props.roleOutline];
switch (value.type) {
case "role":
delete options[index].roleSet;
options[index].role = value.role;
options[index] = {
role: value.role,
...options[index]
}
break;
case "roleSet":
options[index].roleSet = value.roleSet;
delete options[index].role;
options[index] = {
roleSet: value.roleSet,
...options[index]
}
break;
}

Expand Down
102 changes: 88 additions & 14 deletions client/src/components/gameModeSettings/gameMode/dataFixer/v4.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { VersionConverter } from ".";
import { GameMode, GameModeData, GameModeStorage, ShareableGameMode } from "..";
import { Conclusion, CONCLUSIONS, INSIDER_GROUPS, InsiderGroup } from "../../../../game/gameState.d";
import { getDefaultSettings, Settings } from "../../../../game/localStorage";
import { RoleOutline, RoleOutlineOption } from "../../../../game/roleListState.d";
import { RoleOutline, RoleOutlineOption, RoleSet } from "../../../../game/roleListState.d";
import { Role } from "../../../../game/roleState.d";
import { Failure, ParseResult, ParseSuccess, Success, isFailure } from "../parse";
import { parseName, parsePhaseTimes, parseRole, parseRoleSet } from "./initial";
Expand Down Expand Up @@ -214,21 +215,94 @@ function parseRoleOutlineOptionList(json: NonNullable<any>): ParseResult<RoleOut
}

function parseRoleOutlineOption(json: NonNullable<any>): ParseResult<RoleOutlineOption> {
if (json.role !== undefined) {
const role = parseRole(json.role);
if (isFailure(role)) return role;

return Success({
role: role.value
});
} else if (json.roleSet !== undefined) {
const roleSet = parseRoleSet(json.roleSet);
if (isFailure(roleSet)) return roleSet;
let out: {
insiderGroups?: InsiderGroup[],
winIfAny?: Conclusion[],
role?: Role,
roleSet?: RoleSet
} = {}


if("insiderGroups" in json) {
const insiderGroupsResult = parseRoleOutlineOptionInsiderGroups(json.insiderGroups);
if (isFailure(insiderGroupsResult)) return insiderGroupsResult;
out.insiderGroups = insiderGroupsResult.value;
}

if("winIfAny" in json) {
const winIfAnyResult = parseRoleOutlineOptionWinIfAny(json.winIfAny);
if (isFailure(winIfAnyResult)) return winIfAnyResult;
out.winIfAny = winIfAnyResult.value;
}

return Success ({
roleSet: roleSet.value
});
if("role" in json && "roleSet" in json) {
return Failure("roleOutlineOptionBothRoleAndRoleSet", json);
}

if ("role" in json) {
const roleResult = parseRole(json.role);
if (isFailure(roleResult)) return roleResult;
out.role = roleResult.value;
} else if ("roleSet" in json) {
const roleSetResult = parseRoleSet(json.roleSet);
if (isFailure(roleSetResult)) return roleSetResult;
out.roleSet = roleSetResult.value;
} else {
return Failure("roleOutlineOptionInvalidType", json);
return Failure("roleOutlineOptionNeitherRoleNorRoleSet", json);
}

return Success(out as RoleOutlineOption);
}


function parseRoleOutlineOptionWinIfAny(json: NonNullable<any>): ParseResult<Conclusion[]> {
if (!Array.isArray(json)) {
return Failure("winIfAnyNotArray", json);
}

const conclusions = json.map(parseConclusion);
for (const conclusion of conclusions) {
if (isFailure(conclusion)) return conclusion;
}

return Success(conclusions.map(success => (success as ParseSuccess<Conclusion>).value));
}

function parseConclusion(json: NonNullable<any>): ParseResult<Conclusion> {
if (typeof json !== "string") {
return Failure("conclusionNotString", json);
}

if (!CONCLUSIONS.includes(json as Conclusion)) {
return Failure("conclusionInvalid", json);
}

return Success(json as Conclusion);
}


function parseRoleOutlineOptionInsiderGroups(json: NonNullable<any>): ParseResult<InsiderGroup[]> {
if (!Array.isArray(json)) {
return Failure("insiderGroupsNotArray", json);
}

const insiderGroups = json.map(parseInsiderGroup);
for (const group of insiderGroups) {
if (isFailure(group)) return group;
}

return Success(insiderGroups.map(success => (success as ParseSuccess<InsiderGroup>).value));
}

function parseInsiderGroup(json: NonNullable<any>): ParseResult<InsiderGroup> {
if (typeof json !== "string") {
return Failure("insiderGroupNotString", json);
}

if (!INSIDER_GROUPS.includes(json as InsiderGroup)) {
return Failure("insiderGroupInvalid", json);
}

return Success(json as InsiderGroup);
}
8 changes: 3 additions & 5 deletions client/src/game/roleListState.d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ export type RoleOutline = RoleOutlineOption[];

export type RoleOutlineOption = ({
roleSet: RoleSet
role?: undefined
} | {
roleSet?: undefined
role: Role
}) & {
winIfAny?: Conclusion[],
Expand Down Expand Up @@ -84,9 +82,9 @@ export function translateRoleOutlineOption(roleOutlineOption: RoleOutlineOption)
if (roleOutlineOption.winIfAny) {
out += `${translateWinCondition({ type: "gameConclusionReached", winIfAny: roleOutlineOption.winIfAny })} `
}
if (roleOutlineOption.roleSet) {
if ("roleSet" in roleOutlineOption) {
out += translate(roleOutlineOption.roleSet)
} else if (roleOutlineOption.role) {
} else {
out += translate("role."+roleOutlineOption.role+".name")
}
return out;
Expand All @@ -103,7 +101,7 @@ export function getRolesFromOutline(roleOutline: RoleOutline): Role[] {
return roleOutline.flatMap((option) => getRolesFromOutlineOption(option));
}
export function getRolesFromOutlineOption(roleOutlineOption: RoleOutlineOption): Role[] {
if (roleOutlineOption.roleSet) {
if ("roleSet" in roleOutlineOption) {
return getRolesFromRoleSet(roleOutlineOption.roleSet)
} else {
return [roleOutlineOption.role]
Expand Down

0 comments on commit d93e3d0

Please sign in to comment.