Added support of duplicate fragment

Fragments sharing the same effect will now be merged in the summary.
This commit is contained in:
borisflagell 2022-05-11 20:13:03 +02:00
parent cc6089092f
commit 5542d43fb9

@ -1,4 +1,4 @@
import React, { useState, useEffect } from "react"; import React from "react";
import { ActiveFragment } from "../ActiveFragment"; import { ActiveFragment } from "../ActiveFragment";
import { IStaneksGift } from "../IStaneksGift"; import { IStaneksGift } from "../IStaneksGift";
import { FragmentType, Effect } from "../FragmentType"; import { FragmentType, Effect } from "../FragmentType";
@ -6,45 +6,84 @@ import { numeralWrapper } from "../../ui/numeralFormat";
import Paper from "@mui/material/Paper"; import Paper from "@mui/material/Paper";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box"; import Table from "@mui/material/Table";
import { TableBody, TableCell, TableRow } from "@mui/material";
type IProps = { type IProps = {
gift: IStaneksGift; gift: IStaneksGift;
}; };
function formatEffect(effect: number, type: FragmentType): string {
if (Effect(type).includes("+x%")) {
return Effect(type).replace(/-*x%/, numeralWrapper.formatPercentage(effect - 1));
} else if (Effect(type).includes("-x%")) {
const perc = numeralWrapper.formatPercentage(1 - 1 / effect);
return Effect(type).replace(/-x%/, perc);
} else {
return Effect(type);
}
}
export function ActiveFragmentSummary(props: IProps): React.ReactElement { export function ActiveFragmentSummary(props: IProps): React.ReactElement {
const [, setC] = useState(new Date()); const summary: { coordinate: { x: number; y: number }[]; effect: number; type: FragmentType }[] = [];
// Iterate through Active Fragment
useEffect(() => { props.gift.fragments.forEach((fragment: ActiveFragment) => {
const id = setInterval(() => setC(new Date()), 250);
return () => clearInterval(id);
}, []);
const effects = props.gift.fragments.map((fragment: ActiveFragment) => {
const f = fragment.fragment(); const f = fragment.fragment();
// Discard ToolBrush and Booster.
let effect = "N/A"; if (![FragmentType.Booster, FragmentType.None, FragmentType.Delete].includes(f.type)) {
effect = "[" + fragment.x + "," + fragment.y + "] "; // Check for an existing entry in summary for this fragment's type
// Boosters and cooling don't deal with heat. const entry = summary.find((e) => {
if ([FragmentType.Booster, FragmentType.None, FragmentType.Delete].includes(f.type)) { return e.type === f.type;
return ""; });
} else if (Effect(f.type).includes("+x%")) { if (entry) {
effect += Effect(f.type).replace(/-*x%/, numeralWrapper.formatPercentage(props.gift.effect(fragment) - 1)); // If there's one, update the existing entry
} else if (Effect(f.type).includes("-x%")) { entry.effect *= props.gift.effect(fragment);
const effectAmt = props.gift.effect(fragment); entry.coordinate.push({ x: fragment.x, y: fragment.y });
const perc = numeralWrapper.formatPercentage(1 - 1 / effectAmt); } else {
effect += Effect(f.type).replace(/-x%/, perc); // If there's none, create a new entry
summary.push({
coordinate: [{ x: fragment.x, y: fragment.y }],
effect: props.gift.effect(fragment),
type: f.type,
});
}
} }
return <Typography>{effect}</Typography>;
}); });
return ( return (
<Paper sx={{ mb: 1 }}> <Paper sx={{ mb: 1 }}>
<Typography variant="h5">Summary of current effects:</Typography> <Typography variant="h5">Summary of active fragments:</Typography>
{effects.length <= 0 && <Typography>None currently.</Typography>} {summary.length <= 0 && <Typography>None currently.</Typography>}
<Box sx={{ display: "list", width: "fit-content", gridTemplateColumns: "repeat(1, 1fr)" }}>{effects}</Box> <Table sx={{ display: "table", width: "100%" }}>
<TableBody>
<TableRow>
<TableCell sx={{ borderBottom: "none", p: 0, m: 0 }}>
<Typography>Coordinate</Typography>
</TableCell>
<TableCell sx={{ borderBottom: "none", p: 0, m: 0 }}>
<Typography>Effect</Typography>
</TableCell>
</TableRow>
{summary.map((entry) => {
return (
<TableRow>
<TableCell sx={{ borderBottom: "none", p: 0, m: 0 }}>
<Typography>
{entry.coordinate.map((coord) => {
return "[" + coord.x + "," + coord.y + "]";
})}
</Typography>
</TableCell>
<TableCell sx={{ borderBottom: "none", p: 0, m: 0 }}>
<Typography>{formatEffect(entry.effect, entry.type)}</Typography>
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</Paper> </Paper>
); );
} }