Fix FactionsDev issue

Receive all invites button was only setting the alreadyInvited flag for the selected faction, instead of all factions.

Also some slight refactoring / variable name improvements.
This commit is contained in:
Snarling 2023-11-05 04:40:30 -05:00
parent 9585913e0a
commit 99afd2ff2a

@ -28,74 +28,67 @@ import { Adjuster } from "./Adjuster";
import { Factions } from "../../Faction/Factions"; import { Factions } from "../../Faction/Factions";
import { getRecordValues } from "../../Types/Record"; import { getRecordValues } from "../../Types/Record";
import { getEnumHelper } from "../../utils/EnumHelper"; import { getEnumHelper } from "../../utils/EnumHelper";
import { useRerender } from "../../ui/React/hooks";
const bigNumber = 1e12; const bigNumber = 1e12;
export function FactionsDev(): React.ReactElement { export function FactionsDev(): React.ReactElement {
const [factionName, setFactionName] = useState(FactionName.Illuminati); const [selectedFaction, setSelectedFaction] = useState(Factions[FactionName.Illuminati]);
const [factionDiscovery, setFactionDiscovery] = useState(Factions[FactionName.Illuminati].discovery); const rerender = useRerender();
function setFactionDropdown(event: SelectChangeEvent): void { function setFactionDropdown(event: SelectChangeEvent): void {
if (!getEnumHelper("FactionName").isMember(event.target.value)) return; if (!getEnumHelper("FactionName").isMember(event.target.value)) return;
setFactionName(event.target.value); setSelectedFaction(Factions[event.target.value]);
setFactionDiscovery(Factions[event.target.value].discovery);
} }
function receiveInvite(): void { function receiveInvite(): void {
Player.receiveInvite(factionName); Player.receiveInvite(selectedFaction.name);
Factions[factionName].alreadyInvited = true; selectedFaction.alreadyInvited = true;
rerender();
} }
function receiveAllInvites(): void { function receiveAllInvites(): void {
Object.values(FactionName).forEach((faction) => { getRecordValues(Factions).forEach((faction) => {
Player.receiveInvite(faction); Player.receiveInvite(faction.name);
Factions[factionName].alreadyInvited = true; faction.alreadyInvited = true;
}); });
rerender();
} }
function receiveRumor(): void { function receiveRumor(): void {
Player.receiveRumor(factionName); Player.receiveRumor(selectedFaction.name);
setFactionDiscovery(Factions[factionName].discovery); rerender();
} }
function receiveAllRumors(): void { function receiveAllRumors(): void {
Object.values(FactionName).forEach((faction) => Player.receiveRumor(faction)); getRecordValues(FactionName).forEach((factionName) => Player.receiveRumor(factionName));
rerender();
} }
function resetAllDiscovery(): void { function resetAllDiscovery(): void {
Object.values(Factions).forEach((faction) => { getRecordValues(Factions).forEach((faction) => (faction.discovery = FactionDiscovery.unknown));
faction.discovery = FactionDiscovery.unknown;
});
Player.factionRumors.clear(); Player.factionRumors.clear();
setFactionDiscovery(Factions[factionName].discovery); rerender();
} }
function modifyFactionRep(modifier: number): (x: number) => void { function modifyFactionRep(modifier: number): (x: number) => void {
return function (reputation: number): void { return function (reputation: number): void {
const fac = Factions[factionName]; if (!isNaN(reputation)) selectedFaction.playerReputation += reputation * modifier;
if (!isNaN(reputation)) {
fac.playerReputation += reputation * modifier;
}
}; };
} }
function resetFactionRep(): void { function resetFactionRep(): void {
const fac = Factions[factionName]; selectedFaction.playerReputation = 0;
fac.playerReputation = 0;
} }
function modifyFactionFavor(modifier: number): (x: number) => void { function modifyFactionFavor(modifier: number): (x: number) => void {
return function (favor: number): void { return function (favor: number): void {
const fac = Factions[factionName]; if (!isNaN(favor)) selectedFaction.favor += favor * modifier;
if (!isNaN(favor)) {
fac.favor += favor * modifier;
}
}; };
} }
function resetFactionFavor(): void { function resetFactionFavor(): void {
const fac = Factions[factionName]; selectedFaction.favor = 0;
fac.favor = 0;
} }
function tonsOfRep(): void { function tonsOfRep(): void {
@ -123,9 +116,9 @@ export function FactionsDev(): React.ReactElement {
} }
function setDiscovery(event: React.ChangeEvent<HTMLInputElement>, value: string): void { function setDiscovery(event: React.ChangeEvent<HTMLInputElement>, value: string): void {
const disco = value as FactionDiscovery; if (!getEnumHelper("FactionDiscovery").isMember(value)) return;
Factions[factionName].discovery = disco; selectedFaction.discovery = value;
setFactionDiscovery(disco); rerender();
} }
return ( return (
@ -147,15 +140,15 @@ export function FactionsDev(): React.ReactElement {
labelId="factions-select" labelId="factions-select"
id="factions-dropdown" id="factions-dropdown"
onChange={setFactionDropdown} onChange={setFactionDropdown}
value={factionName} value={selectedFaction.name}
startAdornment={ startAdornment={
<> <>
<Tooltip title={`Hear rumor about ${factionName}`}> <Tooltip title={`Hear rumor about ${selectedFaction}`}>
<IconButton onClick={receiveRumor} size="large"> <IconButton onClick={receiveRumor} size="large">
<ChatIcon /> <ChatIcon />
</IconButton> </IconButton>
</Tooltip> </Tooltip>
<Tooltip title={`Receive invitation to ${factionName}`}> <Tooltip title={`Receive invitation to ${selectedFaction}`}>
<IconButton onClick={receiveInvite} size="large"> <IconButton onClick={receiveInvite} size="large">
<ReplyIcon /> <ReplyIcon />
</IconButton> </IconButton>
@ -163,7 +156,7 @@ export function FactionsDev(): React.ReactElement {
</> </>
} }
> >
{Object.values(Factions).map((faction) => ( {getRecordValues(Factions).map((faction) => (
<MenuItem key={faction.name} value={faction.name}> <MenuItem key={faction.name} value={faction.name}>
{faction.name} {faction.name}
</MenuItem> </MenuItem>
@ -178,9 +171,9 @@ export function FactionsDev(): React.ReactElement {
</td> </td>
<td> <td>
<FormControl> <FormControl>
<RadioGroup onChange={setDiscovery} value={factionDiscovery} row> <RadioGroup onChange={setDiscovery} value={selectedFaction.discovery} row>
{Object.entries(FactionDiscovery).map(([discoveryLabel, discovery]) => ( {getRecordValues(FactionDiscovery).map((discovery) => (
<FormControlLabel key={discovery} value={discovery} label={discoveryLabel} control={<Radio />} /> <FormControlLabel key={discovery} value={discovery} label={discovery} control={<Radio />} />
))} ))}
</RadioGroup> </RadioGroup>
</FormControl> </FormControl>