mirror of
https://github.com/Vikeo/LifeTrinket.git
synced 2025-11-20 09:48:00 +00:00
Counters stored in local storage
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { ReactNode, useRef, useState } from 'react';
|
import { ReactNode, useRef, useState } from 'react';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
import { CounterType } from '../../Types/Player';
|
||||||
|
|
||||||
export const StyledExtraCounterButton = styled.button`
|
export const StyledExtraCounterButton = styled.button`
|
||||||
position: relative;
|
position: relative;
|
||||||
@@ -36,16 +37,27 @@ export const CenteredText = styled.div`
|
|||||||
|
|
||||||
type ExtraCounterProps = {
|
type ExtraCounterProps = {
|
||||||
Icon: ReactNode;
|
Icon: ReactNode;
|
||||||
|
counterTotal?: number;
|
||||||
|
type: CounterType;
|
||||||
|
setCounterTotal: (updatedCounterTotal: number, type: CounterType) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ExtraCounter = ({ Icon }: ExtraCounterProps) => {
|
const ExtraCounter = ({
|
||||||
const [count, setCount] = useState(0);
|
Icon,
|
||||||
|
counterTotal,
|
||||||
|
setCounterTotal,
|
||||||
|
type,
|
||||||
|
}: ExtraCounterProps) => {
|
||||||
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
|
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
|
||||||
const [timeoutFinished, setTimeoutFinished] = useState(false);
|
const [timeoutFinished, setTimeoutFinished] = useState(false);
|
||||||
const [hasPressedDown, setHasPressedDown] = useState(false);
|
const [hasPressedDown, setHasPressedDown] = useState(false);
|
||||||
|
|
||||||
const handleCountChange = (increment: number) => {
|
const handleCountChange = (increment: number) => {
|
||||||
setCount(count + increment);
|
if (!counterTotal) {
|
||||||
|
setCounterTotal(increment, type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setCounterTotal(counterTotal + increment, type);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDownInput = () => {
|
const handleDownInput = () => {
|
||||||
@@ -82,7 +94,7 @@ const ExtraCounter = ({ Icon }: ExtraCounterProps) => {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{Icon}
|
{Icon}
|
||||||
<CenteredText>{count ? count : undefined}</CenteredText>
|
<CenteredText>{counterTotal ? counterTotal : undefined}</CenteredText>
|
||||||
</StyledExtraCounterButton>
|
</StyledExtraCounterButton>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import * as S from './LifeCounter.style';
|
import * as S from './LifeCounter.style';
|
||||||
import { Player } from '../../Types/Player';
|
import { CounterType, Player } from '../../Types/Player';
|
||||||
import { useSwipeable } from 'react-swipeable';
|
import { useSwipeable } from 'react-swipeable';
|
||||||
import AddLifeButton from '../Buttons/AddLifeButton';
|
import AddLifeButton from '../Buttons/AddLifeButton';
|
||||||
import SubtractLifeButton from '../Buttons/SubtractLifeButton';
|
import SubtractLifeButton from '../Buttons/SubtractLifeButton';
|
||||||
@@ -33,6 +33,36 @@ const LifeCounter = ({
|
|||||||
onPlayerChange(updatedPlayer);
|
onPlayerChange(updatedPlayer);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleCounterChange = (
|
||||||
|
updatedCounterTotal: number,
|
||||||
|
type: CounterType
|
||||||
|
) => {
|
||||||
|
const existingCounter = player.extraCounters.find(
|
||||||
|
(counter) => counter.type === type
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!existingCounter) {
|
||||||
|
const newCounter = {
|
||||||
|
type: type,
|
||||||
|
value: updatedCounterTotal,
|
||||||
|
};
|
||||||
|
const updatedExtraCounters = [...player.extraCounters, newCounter];
|
||||||
|
const updatedPlayer = { ...player, extraCounters: updatedExtraCounters };
|
||||||
|
onPlayerChange(updatedPlayer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedExtraCounters = player.extraCounters.map((counter) => {
|
||||||
|
if (counter.type === type) {
|
||||||
|
return { ...counter, value: updatedCounterTotal };
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
});
|
||||||
|
|
||||||
|
const updatedPlayer = { ...player, extraCounters: updatedExtraCounters };
|
||||||
|
onPlayerChange(updatedPlayer);
|
||||||
|
};
|
||||||
|
|
||||||
const [showPlayerMenu, setShowPlayerMenu] = useState(false);
|
const [showPlayerMenu, setShowPlayerMenu] = useState(false);
|
||||||
|
|
||||||
const swipeHandlers = useSwipeable({
|
const swipeHandlers = useSwipeable({
|
||||||
@@ -68,19 +98,66 @@ const LifeCounter = ({
|
|||||||
</S.LifeCountainer>
|
</S.LifeCountainer>
|
||||||
<S.ExtraCountersGrid>
|
<S.ExtraCountersGrid>
|
||||||
{player.settings.useCommanderDamage && (
|
{player.settings.useCommanderDamage && (
|
||||||
<ExtraCounter Icon={<CommanderTaxIcon size="8vmin" />} />
|
<ExtraCounter
|
||||||
|
Icon={<CommanderTaxIcon size="8vmin" />}
|
||||||
|
type={CounterType.CommanderTax}
|
||||||
|
counterTotal={
|
||||||
|
player.extraCounters?.find(
|
||||||
|
(counter) => counter.type === 'commanderTax'
|
||||||
|
)?.value
|
||||||
|
}
|
||||||
|
setCounterTotal={handleCounterChange}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
{Boolean(
|
{Boolean(
|
||||||
player.settings.useCommanderDamage && player.settings.usePartner
|
player.settings.useCommanderDamage && player.settings.usePartner
|
||||||
) && <ExtraCounter Icon={<PartnerTaxIcon size="8vmin" />} />}
|
) && (
|
||||||
|
<ExtraCounter
|
||||||
|
Icon={<PartnerTaxIcon size="8vmin" />}
|
||||||
|
type={CounterType.PartnerTax}
|
||||||
|
counterTotal={
|
||||||
|
player.extraCounters?.find(
|
||||||
|
(counter) => counter.type === 'partnerTax'
|
||||||
|
)?.value
|
||||||
|
}
|
||||||
|
setCounterTotal={handleCounterChange}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
{player.settings.usePoison && (
|
{player.settings.usePoison && (
|
||||||
<ExtraCounter Icon={<PoisonIcon size="8vmin" />} />
|
<ExtraCounter
|
||||||
|
Icon={<PoisonIcon size="8vmin" />}
|
||||||
|
type={CounterType.Poison}
|
||||||
|
counterTotal={
|
||||||
|
player.extraCounters?.find(
|
||||||
|
(counter) => counter.type === 'poison'
|
||||||
|
)?.value
|
||||||
|
}
|
||||||
|
setCounterTotal={handleCounterChange}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
{player.settings.useEnergy && (
|
{player.settings.useEnergy && (
|
||||||
<ExtraCounter Icon={<EnergyIcon size="8vmin" />} />
|
<ExtraCounter
|
||||||
|
Icon={<EnergyIcon size="8vmin" />}
|
||||||
|
type={CounterType.Energy}
|
||||||
|
counterTotal={
|
||||||
|
player.extraCounters?.find(
|
||||||
|
(counter) => counter.type === 'energy'
|
||||||
|
)?.value
|
||||||
|
}
|
||||||
|
setCounterTotal={handleCounterChange}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
{player.settings.useExperience && (
|
{player.settings.useExperience && (
|
||||||
<ExtraCounter Icon={<ExperienceIcon size="8vmin" />} />
|
<ExtraCounter
|
||||||
|
Icon={<ExperienceIcon size="8vmin" />}
|
||||||
|
type={CounterType.Experience}
|
||||||
|
counterTotal={
|
||||||
|
player.extraCounters?.find(
|
||||||
|
(counter) => counter.type === 'experience'
|
||||||
|
)?.value
|
||||||
|
}
|
||||||
|
setCounterTotal={handleCounterChange}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</S.ExtraCountersGrid>
|
</S.ExtraCountersGrid>
|
||||||
</S.LifeCounterContentContainer>
|
</S.LifeCounterContentContainer>
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ export const initialPlayers: Player[] = [
|
|||||||
usePoison: false,
|
usePoison: false,
|
||||||
flipped: true,
|
flipped: true,
|
||||||
},
|
},
|
||||||
|
extraCounters: [],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
lifeTotal: 40,
|
lifeTotal: 40,
|
||||||
@@ -26,6 +27,7 @@ export const initialPlayers: Player[] = [
|
|||||||
usePoison: false,
|
usePoison: false,
|
||||||
flipped: true,
|
flipped: true,
|
||||||
},
|
},
|
||||||
|
extraCounters: [],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
lifeTotal: 40,
|
lifeTotal: 40,
|
||||||
@@ -39,6 +41,7 @@ export const initialPlayers: Player[] = [
|
|||||||
usePoison: false,
|
usePoison: false,
|
||||||
flipped: false,
|
flipped: false,
|
||||||
},
|
},
|
||||||
|
extraCounters: [],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
lifeTotal: 40,
|
lifeTotal: 40,
|
||||||
@@ -52,5 +55,6 @@ export const initialPlayers: Player[] = [
|
|||||||
usePoison: false,
|
usePoison: false,
|
||||||
flipped: false,
|
flipped: false,
|
||||||
},
|
},
|
||||||
|
extraCounters: [],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ export type Player = {
|
|||||||
key: number;
|
key: number;
|
||||||
color: string;
|
color: string;
|
||||||
settings: PlayerSettings;
|
settings: PlayerSettings;
|
||||||
extraCounters?: ExtraCounters;
|
extraCounters: ExtraCounter[];
|
||||||
};
|
};
|
||||||
|
|
||||||
type PlayerSettings = {
|
type PlayerSettings = {
|
||||||
@@ -15,10 +15,15 @@ type PlayerSettings = {
|
|||||||
useExperience?: boolean;
|
useExperience?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ExtraCounters = {
|
type ExtraCounter = {
|
||||||
commanderDamage?: number;
|
type: CounterType;
|
||||||
partnerDamage?: number;
|
value: number;
|
||||||
poison?: number;
|
|
||||||
energy?: number;
|
|
||||||
experience?: number;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum CounterType {
|
||||||
|
CommanderTax = 'commanderTax',
|
||||||
|
PartnerTax = 'partnerTax',
|
||||||
|
Poison = 'poison',
|
||||||
|
Energy = 'energy',
|
||||||
|
Experience = 'experience',
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user