Guest User

Untitled

a guest
Jan 28th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { createContext, useContext, useState, useEffect, useRef } from 'react';
  2. import DeckService from '../services/DeckAPIService';
  3. import Hand from '../models/Hand';
  4. import {determineOutcome, GameOutcome} from "../utils/gameLogic";
  5.  
  6. export interface GameContextType {
  7.   playerHand: Hand;
  8.   houseHand: Hand;
  9.   handleHit: () => Promise<void>;
  10.   handleStand: () => void;
  11.   resetGame: () => void;
  12.   gameStatus: string | null;
  13. }
  14.  
  15. const GameContext = createContext<GameContextType | undefined>(undefined);
  16.  
  17. export function useGame() {
  18.   const context = useContext(GameContext);
  19.   if (!context) {
  20.     throw new Error('useGame must be used within a GameProvider');
  21.   }
  22.   return context;
  23. }
  24.  
  25. function GameProvider({ children }: { children: React.ReactNode }) {
  26.   const [playerHand, setPlayerHand] = useState(new Hand());
  27.   const [houseHand, setHouseHand] = useState(new Hand());
  28.   const [gameStatus, setGameStatus] = useState<string | null>(null);
  29.   const [deckService, setDeckService] = useState(new DeckService());
  30.  
  31.   const hasInitialized = useRef(false);
  32.  
  33.   useEffect(() => {
  34.     if (!hasInitialized.current) {
  35.       hasInitialized.current = true;
  36.       setupGame();
  37.     }
  38.   }, []);
  39.  
  40.   async function setupGame() {
  41.     await deckService.createDeck();
  42.     const playerCards = await deckService.drawCard(2);
  43.     const houseCards = await deckService.drawCard(2);
  44.  
  45.     const newPlayerHand = new Hand();
  46.     const newHouseHand = new Hand();
  47.  
  48.     newPlayerHand.addCards(playerCards);
  49.     newHouseHand.addCards(houseCards);
  50.  
  51.     setPlayerHand(newPlayerHand);
  52.     setHouseHand(newHouseHand);
  53.     setGameStatus(null);
  54.   }
  55.  
  56.   async function handleHit() {
  57.     const newCards = await deckService.drawCard(1);
  58.     playerHand.addCard(newCards[0]);
  59.     setPlayerHand(new Hand([...playerHand.cards]));
  60.  
  61.     if (playerHand.isBusted()) {
  62.       endRound();
  63.     }
  64.   }
  65.  
  66.   function handleStand() {
  67.       endRound();
  68.   }
  69.  
  70.   function endRound() {
  71.     const outcome = determineOutcome(playerHand, houseHand);
  72.     if (outcome !== GameOutcome.CONTINUE_GAME) {
  73.       setGameStatus(outcome);
  74.     }
  75.   }
  76.  
  77.   function resetGame() {
  78.     setupGame();
  79.   }
  80.  
  81.   const value: GameContextType = {
  82.     playerHand,
  83.     houseHand,
  84.     handleHit,
  85.     handleStand,
  86.     resetGame,
  87.     gameStatus
  88.   };
  89.  
  90.   return (
  91.     <GameContext.Provider value={value}>
  92.       {children}
  93.     </GameContext.Provider>
  94.   );
  95. }
  96.  
  97. export default GameProvider;
Advertisement
Add Comment
Please, Sign In to add comment