mirror of
https://github.com/Vikeo/LifeTrinket.git
synced 2025-11-17 08:18:00 +00:00
fix random interval
This commit is contained in:
@@ -88,7 +88,6 @@ const playerCanLose = (player: Player) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type LifeCounterProps = {
|
type LifeCounterProps = {
|
||||||
stopRandom: boolean;
|
|
||||||
player: Player;
|
player: Player;
|
||||||
opponents: Player[];
|
opponents: Player[];
|
||||||
isStartingPlayer?: boolean;
|
isStartingPlayer?: boolean;
|
||||||
@@ -96,16 +95,11 @@ type LifeCounterProps = {
|
|||||||
|
|
||||||
const RECENT_DIFFERENCE_TTL = 3_000;
|
const RECENT_DIFFERENCE_TTL = 3_000;
|
||||||
|
|
||||||
const LifeCounter = ({
|
const LifeCounter = ({ player, opponents }: LifeCounterProps) => {
|
||||||
stopRandom,
|
|
||||||
player,
|
|
||||||
opponents,
|
|
||||||
isStartingPlayer,
|
|
||||||
}: LifeCounterProps) => {
|
|
||||||
const { updatePlayer, updateLifeTotal } = usePlayers();
|
const { updatePlayer, updateLifeTotal } = usePlayers();
|
||||||
const { settings } = useGlobalSettings();
|
const { settings, playing, setPlaying, stopPlayerRandomization } =
|
||||||
|
useGlobalSettings();
|
||||||
const playingTimerRef = useRef<NodeJS.Timeout | undefined>(undefined);
|
const playingTimerRef = useRef<NodeJS.Timeout | undefined>(undefined);
|
||||||
const [playing, setPlaying] = useState(false);
|
|
||||||
|
|
||||||
const [showPlayerMenu, setShowPlayerMenu] = useState(false);
|
const [showPlayerMenu, setShowPlayerMenu] = useState(false);
|
||||||
const [recentDifference, setRecentDifference] = useState(0);
|
const [recentDifference, setRecentDifference] = useState(0);
|
||||||
@@ -122,12 +116,10 @@ const LifeCounter = ({
|
|||||||
trackMouse: true,
|
trackMouse: true,
|
||||||
onSwipedDown: (e) => {
|
onSwipedDown: (e) => {
|
||||||
e.event.stopPropagation();
|
e.event.stopPropagation();
|
||||||
console.log(`User DOWN Swiped on player ${player.index}`);
|
|
||||||
setShowPlayerMenu(true);
|
setShowPlayerMenu(true);
|
||||||
},
|
},
|
||||||
onSwipedUp: (e) => {
|
onSwipedUp: (e) => {
|
||||||
e.event.stopPropagation();
|
e.event.stopPropagation();
|
||||||
console.log(`User UP Swiped on player ${player.index}`);
|
|
||||||
setShowPlayerMenu(false);
|
setShowPlayerMenu(false);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -135,7 +127,6 @@ const LifeCounter = ({
|
|||||||
onSwiping: (e) => e.event.stopPropagation(),
|
onSwiping: (e) => e.event.stopPropagation(),
|
||||||
rotationAngle,
|
rotationAngle,
|
||||||
});
|
});
|
||||||
console.log('stopRandom', stopRandom);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
@@ -160,13 +151,26 @@ const LifeCounter = ({
|
|||||||
}, [recentDifference, document.body.clientHeight, document.body.clientWidth]);
|
}, [recentDifference, document.body.clientHeight, document.body.clientWidth]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
playingTimerRef.current = setTimeout(() => {
|
if (
|
||||||
localStorage.setItem('playing', 'true');
|
player.isStartingPlayer &&
|
||||||
setPlaying(true);
|
((!playing &&
|
||||||
}, 10_000);
|
settings.useRandomStartingPlayerInterval &&
|
||||||
|
stopPlayerRandomization) ||
|
||||||
|
(!settings.useRandomStartingPlayerInterval && !playing))
|
||||||
|
) {
|
||||||
|
playingTimerRef.current = setTimeout(() => {
|
||||||
|
setPlaying(true);
|
||||||
|
}, 10_000);
|
||||||
|
}
|
||||||
|
|
||||||
return () => clearTimeout(playingTimerRef.current);
|
return () => clearTimeout(playingTimerRef.current);
|
||||||
}, []);
|
}, [
|
||||||
|
player.isStartingPlayer,
|
||||||
|
playing,
|
||||||
|
setPlaying,
|
||||||
|
settings.useRandomStartingPlayerInterval,
|
||||||
|
stopPlayerRandomization,
|
||||||
|
]);
|
||||||
|
|
||||||
player.settings.rotation === Rotation.SideFlipped ||
|
player.settings.rotation === Rotation.SideFlipped ||
|
||||||
player.settings.rotation === Rotation.Side;
|
player.settings.rotation === Rotation.Side;
|
||||||
@@ -201,19 +205,19 @@ const LifeCounter = ({
|
|||||||
style={{ rotate: `${calcRotation}deg` }}
|
style={{ rotate: `${calcRotation}deg` }}
|
||||||
{...handlers}
|
{...handlers}
|
||||||
>
|
>
|
||||||
{!playing && settings.showStartingPlayer && isStartingPlayer && (
|
{!playing && settings.showStartingPlayer && player.isStartingPlayer && (
|
||||||
<div
|
<div
|
||||||
className="z-20 flex absolute w-full h-full justify-center items-center select-none cursor-pointer webkit-user-select-none"
|
className="z-20 flex absolute w-full h-full justify-center items-center select-none cursor-pointer webkit-user-select-none"
|
||||||
style={{
|
style={{
|
||||||
rotate: `${calcRotation}deg`,
|
rotate: `${calcRotation}deg`,
|
||||||
backgroundImage:
|
backgroundImage:
|
||||||
stopRandom || !settings.useRandomStartingPlayerInterval
|
stopPlayerRandomization ||
|
||||||
|
!settings.useRandomStartingPlayerInterval
|
||||||
? `radial-gradient(circle at center, ${player.color}, ${baseColors.primary.main})`
|
? `radial-gradient(circle at center, ${player.color}, ${baseColors.primary.main})`
|
||||||
: 'none',
|
: 'none',
|
||||||
}}
|
}}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
clearTimeout(playingTimerRef.current);
|
clearTimeout(playingTimerRef.current);
|
||||||
localStorage.setItem('playing', 'true');
|
|
||||||
setPlaying(true);
|
setPlaying(true);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -224,7 +228,8 @@ const LifeCounter = ({
|
|||||||
>
|
>
|
||||||
<div className="flex flex-col justify-center items-center">
|
<div className="flex flex-col justify-center items-center">
|
||||||
<Paragraph>👑</Paragraph>
|
<Paragraph>👑</Paragraph>
|
||||||
{(stopRandom || !settings.useRandomStartingPlayerInterval) && (
|
{(stopPlayerRandomization ||
|
||||||
|
!settings.useRandomStartingPlayerInterval) && (
|
||||||
<>
|
<>
|
||||||
<Paragraph>You start!</Paragraph>
|
<Paragraph>You start!</Paragraph>
|
||||||
<Paragraph className="text-xl">(Press to hide)</Paragraph>
|
<Paragraph className="text-xl">(Press to hide)</Paragraph>
|
||||||
@@ -238,12 +243,14 @@ const LifeCounter = ({
|
|||||||
{player.hasLost && (
|
{player.hasLost && (
|
||||||
<PlayerLostWrapper $rotation={player.settings.rotation} />
|
<PlayerLostWrapper $rotation={player.settings.rotation} />
|
||||||
)}
|
)}
|
||||||
{settings.useRandomStartingPlayerInterval && !stopRandom && (
|
{settings.useRandomStartingPlayerInterval &&
|
||||||
<div
|
!stopPlayerRandomization &&
|
||||||
className="flex absolute w-full h-full justify-center items-center pointer-events-none select-none webkit-user-select-none z-10"
|
!playing && (
|
||||||
style={{ backgroundColor: player.color }}
|
<div
|
||||||
/>
|
className="flex absolute w-full h-full justify-center items-center pointer-events-none select-none webkit-user-select-none z-10"
|
||||||
)}
|
style={{ backgroundColor: player.color }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<CommanderDamageBar
|
<CommanderDamageBar
|
||||||
opponents={opponents}
|
opponents={opponents}
|
||||||
player={player}
|
player={player}
|
||||||
|
|||||||
@@ -104,7 +104,14 @@ const PlayerMenu = ({
|
|||||||
containerRef: settingsContainerRef,
|
containerRef: settingsContainerRef,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { fullscreen, wakeLock, goToStart, settings } = useGlobalSettings();
|
const {
|
||||||
|
fullscreen,
|
||||||
|
wakeLock,
|
||||||
|
goToStart,
|
||||||
|
settings,
|
||||||
|
setPlaying,
|
||||||
|
setStopPlayerRandomization,
|
||||||
|
} = useGlobalSettings();
|
||||||
const { updatePlayer, resetCurrentGame } = usePlayers();
|
const { updatePlayer, resetCurrentGame } = usePlayers();
|
||||||
|
|
||||||
const handleColorChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
const handleColorChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
@@ -122,6 +129,13 @@ const PlayerMenu = ({
|
|||||||
const handleResetGame = () => {
|
const handleResetGame = () => {
|
||||||
resetCurrentGame();
|
resetCurrentGame();
|
||||||
setShowPlayerMenu(false);
|
setShowPlayerMenu(false);
|
||||||
|
setPlaying(false);
|
||||||
|
setStopPlayerRandomization(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleGoToStart = () => {
|
||||||
|
goToStart();
|
||||||
|
setStopPlayerRandomization(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const toggleFullscreen = () => {
|
const toggleFullscreen = () => {
|
||||||
@@ -291,7 +305,7 @@ const PlayerMenu = ({
|
|||||||
cursor: 'pointer',
|
cursor: 'pointer',
|
||||||
userSelect: 'none',
|
userSelect: 'none',
|
||||||
}}
|
}}
|
||||||
onClick={goToStart}
|
onClick={handleGoToStart}
|
||||||
aria-label="Back to start"
|
aria-label="Back to start"
|
||||||
>
|
>
|
||||||
<Exit size={iconSize} style={{ rotate: '180deg' }} />
|
<Exit size={iconSize} style={{ rotate: '180deg' }} />
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import LifeCounter from '../LifeCounter/LifeCounter';
|
|
||||||
import { Player as PlayerType } from '../../Types/Player';
|
|
||||||
import { twc } from 'react-twc';
|
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
import { twc } from 'react-twc';
|
||||||
import { useGlobalSettings } from '../../Hooks/useGlobalSettings';
|
import { useGlobalSettings } from '../../Hooks/useGlobalSettings';
|
||||||
|
import { usePlayers } from '../../Hooks/usePlayers';
|
||||||
|
import { Player as PlayerType } from '../../Types/Player';
|
||||||
|
import LifeCounter from '../LifeCounter/LifeCounter';
|
||||||
|
|
||||||
const getGridArea = (player: PlayerType) => {
|
const getGridArea = (player: PlayerType) => {
|
||||||
switch (player.index) {
|
switch (player.index) {
|
||||||
@@ -27,23 +28,63 @@ const PlayersWrapper = twc.div`w-full h-full bg-black`;
|
|||||||
|
|
||||||
export const Players = (players: PlayerType[], gridClasses: string) => {
|
export const Players = (players: PlayerType[], gridClasses: string) => {
|
||||||
const randomIntervalRef = useRef<NodeJS.Timeout | null>(null);
|
const randomIntervalRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
const [randomPlayerIndex, setRandomPlayerIndex] = useState<number>(
|
|
||||||
Math.floor(Math.random() * players.length)
|
const prevRandomIndexRef = useRef<number>(-1);
|
||||||
);
|
|
||||||
const [stopRandom, setStopRandom] = useState<boolean>(false);
|
const {
|
||||||
const { settings } = useGlobalSettings();
|
settings,
|
||||||
|
stopPlayerRandomization,
|
||||||
|
setStopPlayerRandomization,
|
||||||
|
playing,
|
||||||
|
} = useGlobalSettings();
|
||||||
|
|
||||||
|
const { setPlayers } = usePlayers();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (settings.useRandomStartingPlayerInterval) {
|
if (
|
||||||
|
settings.useRandomStartingPlayerInterval &&
|
||||||
|
!stopPlayerRandomization &&
|
||||||
|
!playing
|
||||||
|
) {
|
||||||
randomIntervalRef.current = setInterval(() => {
|
randomIntervalRef.current = setInterval(() => {
|
||||||
let randomIndex: number;
|
let randomIndex: number;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
randomIndex = Math.floor(Math.random() * players.length);
|
randomIndex = Math.floor(Math.random() * players.length);
|
||||||
} while (randomIndex === randomPlayerIndex);
|
} while (randomIndex === prevRandomIndexRef.current);
|
||||||
|
|
||||||
setRandomPlayerIndex(randomIndex);
|
prevRandomIndexRef.current = randomIndex;
|
||||||
}, 100);
|
setPlayers(
|
||||||
|
players.map((p) =>
|
||||||
|
p.index === prevRandomIndexRef.current
|
||||||
|
? {
|
||||||
|
...p,
|
||||||
|
isStartingPlayer: true,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
...p,
|
||||||
|
isStartingPlayer: false,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!settings.useRandomStartingPlayerInterval) {
|
||||||
|
const randomPlayerIndex = Math.floor(Math.random() * players.length);
|
||||||
|
setPlayers(
|
||||||
|
players.map((p) =>
|
||||||
|
p.index === randomPlayerIndex
|
||||||
|
? {
|
||||||
|
...p,
|
||||||
|
isStartingPlayer: true,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
...p,
|
||||||
|
isStartingPlayer: false,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return () => {
|
return () => {
|
||||||
if (randomIntervalRef.current) {
|
if (randomIntervalRef.current) {
|
||||||
@@ -52,27 +93,30 @@ export const Players = (players: PlayerType[], gridClasses: string) => {
|
|||||||
};
|
};
|
||||||
}, [
|
}, [
|
||||||
players.length,
|
players.length,
|
||||||
randomPlayerIndex,
|
playing,
|
||||||
|
setPlayers,
|
||||||
settings.useRandomStartingPlayerInterval,
|
settings.useRandomStartingPlayerInterval,
|
||||||
|
stopPlayerRandomization,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PlayersWrapper>
|
<PlayersWrapper>
|
||||||
{settings.useRandomStartingPlayerInterval && (
|
{settings.useRandomStartingPlayerInterval &&
|
||||||
<div
|
!stopPlayerRandomization &&
|
||||||
data-stopRandom={stopRandom}
|
!playing && (
|
||||||
className="absolute flex justify-center items-center bg-black bg-opacity-50 size-full z-50 cursor-pointer text-5xl data-[stopRandom=true]:hidden"
|
<div
|
||||||
onClick={() => {
|
className="absolute flex justify-center items-center bg-black bg-opacity-50 size-full z-50 cursor-pointer text-5xl"
|
||||||
if (randomIntervalRef.current) {
|
onClick={() => {
|
||||||
clearInterval(randomIntervalRef.current);
|
if (randomIntervalRef.current) {
|
||||||
randomIntervalRef.current = null;
|
clearInterval(randomIntervalRef.current);
|
||||||
setStopRandom(true);
|
randomIntervalRef.current = null;
|
||||||
}
|
}
|
||||||
}}
|
setStopPlayerRandomization(true);
|
||||||
>
|
}}
|
||||||
CHOOSE A PLAYER
|
>
|
||||||
</div>
|
CHOOSE A PLAYER
|
||||||
)}
|
</div>
|
||||||
|
)}
|
||||||
<div className={`grid w-full h-full gap-1 box-border ${gridClasses} `}>
|
<div className={`grid w-full h-full gap-1 box-border ${gridClasses} `}>
|
||||||
{players.map((player) => {
|
{players.map((player) => {
|
||||||
const gridArea = getGridArea(player);
|
const gridArea = getGridArea(player);
|
||||||
@@ -83,12 +127,10 @@ export const Players = (players: PlayerType[], gridClasses: string) => {
|
|||||||
className={`flex justify-center items-center align-middle ${gridArea}`}
|
className={`flex justify-center items-center align-middle ${gridArea}`}
|
||||||
>
|
>
|
||||||
<LifeCounter
|
<LifeCounter
|
||||||
stopRandom={stopRandom}
|
|
||||||
player={player}
|
player={player}
|
||||||
opponents={players.filter(
|
opponents={players.filter(
|
||||||
(opponent) => opponent.index !== player.index
|
(opponent) => opponent.index !== player.index
|
||||||
)}
|
)}
|
||||||
isStartingPlayer={randomPlayerIndex === player.index}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ export type GlobalSettingsContextType = {
|
|||||||
setInitialGameSettings: (initialGameSettings: InitialGameSettings) => void;
|
setInitialGameSettings: (initialGameSettings: InitialGameSettings) => void;
|
||||||
settings: Settings;
|
settings: Settings;
|
||||||
setSettings: (settings: Settings) => void;
|
setSettings: (settings: Settings) => void;
|
||||||
|
playing: boolean;
|
||||||
|
setPlaying: (playing: boolean) => void;
|
||||||
|
stopPlayerRandomization: boolean;
|
||||||
|
setStopPlayerRandomization: (stopRandom: boolean) => void;
|
||||||
isPWA: boolean;
|
isPWA: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ export type PlayersContextType = {
|
|||||||
updatePlayer: (updatedPlayer: Player) => void;
|
updatePlayer: (updatedPlayer: Player) => void;
|
||||||
updateLifeTotal: (player: Player, updatedLifeTotal: number) => number;
|
updateLifeTotal: (player: Player, updatedLifeTotal: number) => number;
|
||||||
resetCurrentGame: () => void;
|
resetCurrentGame: () => void;
|
||||||
|
startingPlayerIndex: number;
|
||||||
|
setStartingPlayerIndex: (index: number) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const PlayersContext = createContext<PlayersContextType | null>(null);
|
export const PlayersContext = createContext<PlayersContextType | null>(null);
|
||||||
|
|||||||
@@ -21,11 +21,23 @@ export const GlobalSettingsProvider = ({
|
|||||||
const savedShowPlay = localStorage.getItem('showPlay');
|
const savedShowPlay = localStorage.getItem('showPlay');
|
||||||
const savedGameSettings = localStorage.getItem('initialGameSettings');
|
const savedGameSettings = localStorage.getItem('initialGameSettings');
|
||||||
const savedSettings = localStorage.getItem('settings');
|
const savedSettings = localStorage.getItem('settings');
|
||||||
|
const savedPlaying = localStorage.getItem('playing');
|
||||||
|
|
||||||
|
const [playing, setPlaying] = useState<boolean>(
|
||||||
|
savedPlaying ? savedPlaying === 'true' : false
|
||||||
|
);
|
||||||
|
const setPlayingAndLocalStorage = (playing: boolean) => {
|
||||||
|
setPlaying(playing);
|
||||||
|
localStorage.setItem('playing', String(playing));
|
||||||
|
};
|
||||||
|
|
||||||
const [showPlay, setShowPlay] = useState<boolean>(
|
const [showPlay, setShowPlay] = useState<boolean>(
|
||||||
savedShowPlay ? savedShowPlay === 'true' : false
|
savedShowPlay ? savedShowPlay === 'true' : false
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const [stopPlayerRandomization, setStopPlayerRandomization] =
|
||||||
|
useState<boolean>(false);
|
||||||
|
|
||||||
const [initialGameSettings, setInitialGameSettings] =
|
const [initialGameSettings, setInitialGameSettings] =
|
||||||
useState<InitialGameSettings | null>(
|
useState<InitialGameSettings | null>(
|
||||||
savedGameSettings ? JSON.parse(savedGameSettings) : null
|
savedGameSettings ? JSON.parse(savedGameSettings) : null
|
||||||
@@ -48,6 +60,8 @@ export const GlobalSettingsProvider = ({
|
|||||||
localStorage.removeItem('players');
|
localStorage.removeItem('players');
|
||||||
localStorage.removeItem('playing');
|
localStorage.removeItem('playing');
|
||||||
localStorage.removeItem('showPlay');
|
localStorage.removeItem('showPlay');
|
||||||
|
|
||||||
|
setPlaying(false);
|
||||||
setShowPlay(false);
|
setShowPlay(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -154,10 +168,14 @@ export const GlobalSettingsProvider = ({
|
|||||||
goToStart,
|
goToStart,
|
||||||
showPlay,
|
showPlay,
|
||||||
setShowPlay,
|
setShowPlay,
|
||||||
|
playing,
|
||||||
|
setPlaying: setPlayingAndLocalStorage,
|
||||||
initialGameSettings,
|
initialGameSettings,
|
||||||
setInitialGameSettings,
|
setInitialGameSettings,
|
||||||
settings,
|
settings,
|
||||||
setSettings,
|
setSettings,
|
||||||
|
stopPlayerRandomization,
|
||||||
|
setStopPlayerRandomization,
|
||||||
isPWA: window?.matchMedia('(display-mode: standalone)').matches,
|
isPWA: window?.matchMedia('(display-mode: standalone)').matches,
|
||||||
};
|
};
|
||||||
}, [
|
}, [
|
||||||
@@ -166,10 +184,12 @@ export const GlobalSettingsProvider = ({
|
|||||||
initialGameSettings,
|
initialGameSettings,
|
||||||
isFullscreen,
|
isFullscreen,
|
||||||
isSupported,
|
isSupported,
|
||||||
|
playing,
|
||||||
release,
|
release,
|
||||||
request,
|
request,
|
||||||
settings,
|
settings,
|
||||||
showPlay,
|
showPlay,
|
||||||
|
stopPlayerRandomization,
|
||||||
type,
|
type,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,17 @@ import { InitialGameSettings } from '../Types/Settings';
|
|||||||
export const PlayersProvider = ({ children }: { children: ReactNode }) => {
|
export const PlayersProvider = ({ children }: { children: ReactNode }) => {
|
||||||
const savedPlayers = localStorage.getItem('players');
|
const savedPlayers = localStorage.getItem('players');
|
||||||
|
|
||||||
|
const savedStartingPlayerIndex = localStorage.getItem('startingPlayerIndex');
|
||||||
|
|
||||||
|
const [startingPlayerIndex, setStartingPlayerIndex] = useState<number>(
|
||||||
|
savedStartingPlayerIndex ? parseInt(savedStartingPlayerIndex) : -1
|
||||||
|
);
|
||||||
|
|
||||||
|
const setStartingPlayerIndexAndLocalStorage = (index: number) => {
|
||||||
|
setStartingPlayerIndex(index);
|
||||||
|
localStorage.setItem('startingPlayerIndex', String(index));
|
||||||
|
};
|
||||||
|
|
||||||
const [players, setPlayers] = useState<Player[]>(
|
const [players, setPlayers] = useState<Player[]>(
|
||||||
savedPlayers ? JSON.parse(savedPlayers) : []
|
savedPlayers ? JSON.parse(savedPlayers) : []
|
||||||
);
|
);
|
||||||
@@ -50,6 +61,8 @@ export const PlayersProvider = ({ children }: { children: ReactNode }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newStartingPlayerIndex = Math.floor(Math.random() * players.length);
|
||||||
|
|
||||||
players.forEach((player: Player) => {
|
players.forEach((player: Player) => {
|
||||||
player.commanderDamage.map((damage) => {
|
player.commanderDamage.map((damage) => {
|
||||||
damage.damageTotal = 0;
|
damage.damageTotal = 0;
|
||||||
@@ -61,9 +74,10 @@ export const PlayersProvider = ({ children }: { children: ReactNode }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
player.lifeTotal = initialGameSettings.startingLifeTotal;
|
player.lifeTotal = initialGameSettings.startingLifeTotal;
|
||||||
|
|
||||||
player.hasLost = false;
|
player.hasLost = false;
|
||||||
|
|
||||||
|
player.isStartingPlayer = newStartingPlayerIndex === player.index;
|
||||||
|
|
||||||
updatePlayer(player);
|
updatePlayer(player);
|
||||||
});
|
});
|
||||||
localStorage.setItem('playing', 'false');
|
localStorage.setItem('playing', 'false');
|
||||||
@@ -75,8 +89,10 @@ export const PlayersProvider = ({ children }: { children: ReactNode }) => {
|
|||||||
updatePlayer,
|
updatePlayer,
|
||||||
updateLifeTotal,
|
updateLifeTotal,
|
||||||
resetCurrentGame,
|
resetCurrentGame,
|
||||||
|
startingPlayerIndex,
|
||||||
|
setStartingPlayerIndex: setStartingPlayerIndexAndLocalStorage,
|
||||||
};
|
};
|
||||||
}, [players]);
|
}, [players, startingPlayerIndex]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PlayersContext.Provider value={ctxValue}>
|
<PlayersContext.Provider value={ctxValue}>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export type Player = {
|
|||||||
settings: PlayerSettings;
|
settings: PlayerSettings;
|
||||||
commanderDamage: CommanderDamage[];
|
commanderDamage: CommanderDamage[];
|
||||||
extraCounters: ExtraCounter[];
|
extraCounters: ExtraCounter[];
|
||||||
|
isStartingPlayer: boolean;
|
||||||
hasLost: boolean;
|
hasLost: boolean;
|
||||||
isSide: boolean;
|
isSide: boolean;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user