feat: added more stuff

This commit is contained in:
Mike 2024-07-21 22:22:21 -04:00
parent 2c7bbfb044
commit 4902ed48f9
6 changed files with 103 additions and 31 deletions

View file

@ -6,6 +6,7 @@ from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
import logging import logging
import requests import requests
from random import shuffle
import os import os
from .photos import Pictures from .photos import Pictures
@ -78,11 +79,14 @@ def ai_cards(card: schemas.CardCreate, db: Session = Depends(get_db)):
@app.get("/cards") @app.get("/cards")
def read_cards(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): def read_cards(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
cards = crud.get_cards(db, skip=skip, limit=limit) cards = crud.get_cards(db, skip=skip, limit=limit)
# return the items in a random order for the game
shuffle(cards)
return cards return cards
@app.get("/load-data") @app.get("/load-data")
def load_data(): async def load_data():
topics = ai.generate_topics() topics = ai.generate_topics()
try: try:
@ -90,8 +94,6 @@ def load_data():
logger.info(item) logger.info(item)
picture_data = photos.search(item["topic"]) picture_data = photos.search(item["topic"])
logger.info(picture_data)
break
card_json = ai.generate_card_json(picture_data, item) card_json = ai.generate_card_json(picture_data, item)
logger.info(card_json) logger.info(card_json)

Binary file not shown.

View file

@ -23,9 +23,10 @@ function App() {
gameStarted={gameStarted} gameStarted={gameStarted}
/> />
<GameBoard <GameBoard
gameStarted={gameStarted}
setGameStarted={setGameStarted}
score={score} score={score}
setScore={setScore} setScore={setScore}
gameStarted={gameStarted}
highScore={highScore} highScore={highScore}
setHighScore={setHighScore} setHighScore={setHighScore}
/> />

View file

@ -2,13 +2,7 @@ import "../styles/card.css";
export default function Card(props) { export default function Card(props) {
return ( return (
<div <div className="card" onClick={props.onClick} data-cardname={props.title}>
className="card"
onClick={(event) =>
onClick(event, props.clicked, props.setScore, props.score)
}
data-cardname={props.title}
>
<div className="card-img"> <div className="card-img">
<Image src={props.imgSrc} alt={props.imgAlt} /> <Image src={props.imgSrc} alt={props.imgAlt} />
</div> </div>
@ -20,10 +14,3 @@ export default function Card(props) {
function Image(props) { function Image(props) {
return <img src={props.src} alt={props.alt} />; return <img src={props.src} alt={props.alt} />;
} }
function onClick(event, clicked, setScore, score) {
// implement something to handle the things ID
setScore(score + 1);
clicked = true;
console.log(event.currentTarget.dataset.cardname);
}

View file

@ -1,10 +1,21 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import Card from "./card"; import Card from "./card";
import "../styles/gameboard.css"; import "../styles/gameboard.css";
import { GameLogic } from "../gameLogic";
export default function GameBoard(props) { export default function GameBoard(props) {
const [cards, setCards] = useState([]); const [cards, setCards] = useState([]);
const gl = new GameLogic(
cards,
setCards,
props.score,
props.setScore,
props.highScore,
props.setHighScore,
props.setGameStarted,
);
useEffect(() => { useEffect(() => {
fetchCards({ setCards }); fetchCards({ setCards });
}, []); }, []);
@ -14,16 +25,13 @@ export default function GameBoard(props) {
{props.gameStarted ? ( {props.gameStarted ? (
<div className="gameboard"> <div className="gameboard">
{cards.map((item) => { {cards.map((item) => {
console.log(item.topic, item.clicked);
return ( return (
<Card <Card
title={item.topic} title={item.topic}
imgSrc={item.medium_url} imgSrc={item.medium_url}
imgAl="Placeholder" imgAlt={item.alt}
key={item.key} key={item.key}
setScore={props.setScore} onClick={() => gl.handleClick(item.key)}
score={props.score}
clicked={item.clicked}
/> />
); );
})} })}
@ -43,11 +51,3 @@ async function fetchCards({ setCards }) {
setCards(gameCards); setCards(gameCards);
} }
function shuffleCards(arr) {
for (let i = arr.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
return arr;
}
}

View file

@ -0,0 +1,82 @@
class GameLogic {
constructor(
cards,
setCards,
score,
setScore,
highScore,
setHighScore,
setGameStarted,
) {
this.cards = cards;
this.setCards = setCards;
this.score = score;
this.setScore = setScore;
this.highScore = highScore;
this.setHighScore = setHighScore;
this.setGameStarted = setGameStarted;
}
handleClick(key) {
this.updateScore(key);
const tempCards = this.cards.map((card) => {
if (card.key === key) {
card.clicked = true;
}
return card;
});
this.setCards(tempCards);
this.randomArrayOrder();
}
randomArrayOrder() {
const randomArr = [...this.cards];
let len = randomArr.length,
t,
i;
while (len) {
i = Math.floor(Math.random() * len--);
t = randomArr[len];
randomArr[len] = randomArr[i];
randomArr[i] = t;
}
this.setCards(randomArr);
}
checkClick(key) {
const cardClicked = this.cards.filter(
(item) => item.key === key && item.clicked === true,
);
return cardClicked.length > 0;
}
updateScore(key) {
if (!this.checkClick(key)) {
this.setScore(this.score + 1);
} else {
console.log("Game Over");
if (this.score + 1 > this.highScore) {
this.setHighScore(this.score);
}
// reset state
this.score = 0;
this.setScore(this.score);
this.setGameStarted(false);
}
}
async fetchCards() {
const cards = await fetch("http://localhost:8000/cards");
const jsonCards = await cards.json();
let gameCards = await jsonCards.map((c) => {
return { ...c, clicked: false, key: crypto.randomUUID() };
});
this.setCards(gameCards);
}
}
export { GameLogic };