Skip to content

Commit

Permalink
Merge pull request #98 from cardano-scaling/feat/force-login-join
Browse files Browse the repository at this point in the history
Force login to join game
  • Loading branch information
Quantumplation authored Dec 14, 2024
2 parents e6eaed8 + 61585a9 commit 3e32fb7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
26 changes: 20 additions & 6 deletions src/components/InitialView/InitialView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ interface InitialViewProps {

const InitialView: FC<InitialViewProps> = ({ startGame }) => {
const {
setGameData,
accountData,
isQualified,
setIsQualified,
isLoadingUserData,
isQualified,
isUserDataFetched,
keys,
setAccountData,
setGameData,
setIsQualified,
} = useAppContext();
const publicKeyHashHex = keys?.publicKeyHashHex
const [, setSessionId] = useSessionIdKeyCache();
const pathSegments = window.location.hash.split("/").filter(Boolean);
const code = pathSegments[2];
const [modalTitle, _] = useState("Join Multiplayer");
const [isNameModalOpen, setIsNameModalOpen] = useState(
pathSegments[1] === EGameType.JOIN,
);
const [isNameModalOpen, setIsNameModalOpen] = useState(false);
const isJoin = pathSegments[1] === EGameType.JOIN;

const [isWelcomeModalOpen, setIsWelcomeModalOpen] = useState(false);
const [isLoginModalOpen, setIsLoginModalOpen] = useState(false);
Expand All @@ -42,6 +44,16 @@ const InitialView: FC<InitialViewProps> = ({ startGame }) => {
}
}, [code, setGameData]);

useEffect(() => {
if (isJoin && publicKeyHashHex && !isLoadingUserData) {
if (isUserDataFetched) {
setIsNameModalOpen(true);
} else {
setIsLoginModalOpen(true);
}
}
}, [isLoadingUserData, isUserDataFetched, isJoin, publicKeyHashHex]);

const handleTournamentLogin = () => {
setIsLoginModalOpen(true);
};
Expand Down Expand Up @@ -188,7 +200,9 @@ const InitialView: FC<InitialViewProps> = ({ startGame }) => {
/>
<LoginModal
close={() => setIsLoginModalOpen(false)}
isJoin={isJoin}
isOpen={isLoginModalOpen}
openNameModal={() => setIsNameModalOpen(true)}
showActionButtons={showActionButtons}
/>
</Layout>
Expand Down
11 changes: 9 additions & 2 deletions src/components/LoginModal/LoginModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import { checkSignin, fetchAuthProviders } from "../../utils/requests";

interface LoginModalProps {
close: () => void;
isJoin: boolean;
isOpen: boolean;
openNameModal: () => void;
showActionButtons: () => void;
}

Expand Down Expand Up @@ -77,7 +79,9 @@ const CheckBoxInput: FC<{

const LoginModal: React.FC<LoginModalProps> = ({
close,
isJoin,
isOpen,
openNameModal,
showActionButtons,
}) => {
const shouldShowAllTou = Date.now() >= 1733238000000;
Expand Down Expand Up @@ -120,14 +124,17 @@ const LoginModal: React.FC<LoginModalProps> = ({
if (account) setAccountData(account);
if (session?.session_id) setSessionId(session.session_id);
if (qualifier) setIsQualified(qualifier.is_qualified);
if (isJoin) openNameModal();
}
}, [
close,
isJoin,
openNameModal,
setAccountData,
setIsQualified,
setSessionId,
showActionButtons,
userData,
setIsQualified,
]);

const handleClose = () => {
Expand All @@ -154,7 +161,7 @@ const LoginModal: React.FC<LoginModalProps> = ({
const renderConsentContent = () => {
return (
<div className="text-left flex flex-col gap-4">
<h1 className="text-5xl uppercase">Tournament Consent</h1>
<h1 className="text-5xl uppercase mb-6">Tournament Consent</h1>
{shouldShowAllTou ? (
<>
{/**
Expand Down
6 changes: 4 additions & 2 deletions src/context/AppContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,19 @@ const AppContextProvider: FC<PropsWithChildren> = ({ children }) => {
bots,
gameData,
globalStats,
isUserDataFetched: !!(userData || accountData),
isLoadingGlobalStats,
isLoadingUserData,
isQualified,
keys,
players,
region,
setAccountData,
setBots,
setGameData,
setIsQualified,
setPlayers,
setRegion,
isQualified,
setIsQualified,
}),
[
accountData,
Expand All @@ -107,6 +108,7 @@ const AppContextProvider: FC<PropsWithChildren> = ({ children }) => {
players,
region,
isQualified,
userData,
],
);

Expand Down
10 changes: 6 additions & 4 deletions src/context/useAppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ interface AppContextInterface {
bots: number;
gameData: GameData;
globalStats?: GameStatistics;
isUserDataFetched: boolean;
isLoadingGlobalStats: boolean;
isLoadingUserData: boolean;
isQualified: boolean;
keys: Keys | null;
players: number;
region: Region | null;
isQualified: boolean;
setAccountData: Dispatch<React.SetStateAction<Account | undefined>>;
setBots: Dispatch<React.SetStateAction<number>>;
setGameData: Dispatch<React.SetStateAction<GameData>>;
setIsQualified: Dispatch<React.SetStateAction<boolean>>;
setPlayers: Dispatch<React.SetStateAction<number>>;
setRegion: Dispatch<React.SetStateAction<Region | null>>;
setIsQualified: Dispatch<React.SetStateAction<boolean>>;
}

export const AppContext = createContext<AppContextInterface>({
Expand All @@ -34,18 +35,19 @@ export const AppContext = createContext<AppContextInterface>({
bots: 1,
gameData: { petName: "", code: "", type: EGameType.SOLO },
globalStats: undefined,
isUserDataFetched: false,
isLoadingGlobalStats: false,
isLoadingUserData: false,
isQualified: false,
keys: null,
players: 1,
region: null,
isQualified: false,
setAccountData: () => {},
setBots: () => {},
setGameData: () => {},
setIsQualified: () => {},
setPlayers: () => {},
setRegion: () => {},
setIsQualified: () => {},
});

export const useAppContext = () => {
Expand Down

0 comments on commit 3e32fb7

Please sign in to comment.