Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
fix: zstandの処理の追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Sea10wood committed Aug 18, 2024
1 parent e7ebf3e commit e4c0fdf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
7 changes: 2 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { useState } from "react";
import { Route, Routes } from "react-router-dom";
import Home from "./pages/home";
import Result from "./pages/result";
import Shooter from "./pages/shooter";
import Yatai from "./pages/yatai";

const AppRoutes = () => {
const [score, setScore] = useState(0);

return (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/shooter" element={<Shooter setScore={setScore} />} />
<Route path="/result" element={<Result score={score} />} />
<Route path="/shooter" element={<Shooter />} />
<Route path="/result" element={<Result score={0} />} />
<Route path="/yatai" element={<Yatai />} />
</Routes>
);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/result/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Result = ({ score }: ResultProps) => {
</div>
<div className={styles["share-btn"]}>
<a
href={`https://twitter.com/intent/tweet?text=Webの射的で遊んだよ。${score}本倒した!%20%23virtualnatsumatsuri`}
href={`https://twitter.com/intent/tweet?text=Webの射的で遊んだよ。${score}本倒した!%20%23ハックツ夏祭り`}
target="_blank"
rel="noopener noreferrer"
>
Expand Down
24 changes: 8 additions & 16 deletions src/pages/shooter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ import { useOrientation } from "../../hooks/useOrientation";
import { useSocketReceiver } from "../../hooks/useSocketReceiver";
import { useSocketSender } from "../../hooks/useSocketSender";
import { useUUIDStore } from "../../store";
import { useScoreStore } from "../../store/useScoreStore";
import { message_type } from "../../type/schema";
import { MessageType } from "../../type/shooting";
import style from "./index.module.css";

type ShooterProps = {
setScore: (score: number) => void;
};

const Shooter = ({ setScore }: ShooterProps) => {
const Shooter = () => {
const [isOpen, setIsOpen] = useState(true);
const [score, setScoreState] = useState<number>(0);
const { orientationDiff } = useOrientation();
const { sendData } = useSocketSender();
const { onMessage } = useSocketReceiver();
const uuid = useUUIDStore((state) => state.uuid);
const navigate = useNavigate();
const score = useScoreStore((state) => state.score);
const setScore = useScoreStore((state) => state.setScore);

const initialImages = [
"/2D_material/cork.webp",
Expand All @@ -38,28 +36,22 @@ const Shooter = ({ setScore }: ShooterProps) => {
sendData(message_type.status, uuid, orientationDiff);
}, 100);

return () => {
if (intervalId !== null) {
clearInterval(intervalId);
}
};
return () => clearInterval(intervalId);
}, [uuid, orientationDiff, sendData]);

useEffect(() => {
onMessage((data) => {
if (data.message_type === MessageType.Hit && data.id === uuid) {
const newScore = score + 1;
setScoreState(newScore);
setScore((prevScore: number) => prevScore + 1);
}
});
}, [onMessage, uuid, score]);
}, [onMessage, uuid, setScore]);

useEffect(() => {
if (images.length === 0) {
setScore(score);
navigate("/result", { state: { score } });
}
}, [images, navigate, score, setScore]);
}, [images, navigate, score]);

const handleClick = () => {
const audio = new Audio("/sound/cork_sound.mp3");
Expand Down
11 changes: 11 additions & 0 deletions src/store/useScoreStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import create from "zustand";

type Store = {
score: number;
setScore: (updater: (prevScore: number) => number) => void;
};

export const useScoreStore = create<Store>((set) => ({
score: 0,
setScore: (updater) => set((state) => ({ score: updater(state.score) })),
}));

0 comments on commit e4c0fdf

Please sign in to comment.