Skip to content

Commit 6f107b2

Browse files
authored
Merge pull request #89 from ut-code/refactor/separate-file-game.ts
game.tsを分割して整理
2 parents 73ce72e + d9ede1d commit 6f107b2

14 files changed

Lines changed: 1276 additions & 1182 deletions

File tree

packages/web/src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getUsers } from "./fetchAPI";
1212
import type { User } from "./component/Emulator";
1313
import { useApiPasswordContext } from "./common/api-password";
1414
import ApiPasswordDialog from "./component/ApiPasswordDialog";
15+
import options from "./options";
1516

1617
export default function App() {
1718
const [currentUser, setCurrentUser] = useState({
@@ -47,7 +48,7 @@ export default function App() {
4748
setIsApiPasswordDialogOpen(true);
4849
}}
4950
/>
50-
<Injection workspaceRef={workspaceRef} />
51+
<Injection workspaceRef={workspaceRef} options={options} />
5152
</Box>
5253
{password && (
5354
<>

packages/web/src/component/Arena.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
2020
import { grey } from "@mui/material/colors";
2121
import { FaFortAwesome } from "react-icons/fa";
2222
import Draggable from "react-draggable";
23-
import type { User } from "./game";
23+
import type { User } from "./game/game";
2424
import { getUser, changeUserName, uploadProgram } from "../fetchAPI";
2525
import { useApiPasswordContext } from "../common/api-password";
2626

packages/web/src/component/Emulator.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useRef } from "react";
2-
import Game from "./game";
3-
import type { Result } from "./game";
2+
import type { Result } from "./game/game";
3+
import Game from "./game/game";
44

55
export interface User {
66
name: string;
@@ -26,6 +26,7 @@ type EmulatorProps = {
2626
executionId: number; // エミュレーターそのものを更新するためのId
2727
handleStatuses: (statuses: Status[]) => void;
2828
onGameCompleted?: (result: Result) => void;
29+
gameModeId: number;
2930
};
3031

3132
export default function Emulator(props: EmulatorProps) {
@@ -38,19 +39,32 @@ export default function Emulator(props: EmulatorProps) {
3839
executionId,
3940
handleStatuses,
4041
onGameCompleted,
42+
gameModeId,
4143
} = props;
4244
const canvasRef = useRef<HTMLCanvasElement>(null);
43-
const gameRef = useRef<Game>();
45+
const gameRef = useRef<any>();
4446
useEffect(() => {
4547
if (!canvasRef.current) throw new Error();
46-
const game = new Game(users, canvasRef.current, (newStatuses: Status[]) => {
47-
handleStatuses(newStatuses);
48-
});
48+
let GameClass;
49+
switch (gameModeId) {
50+
case 0:
51+
GameClass = Game;
52+
break;
53+
default:
54+
GameClass = Game;
55+
}
56+
const game = new GameClass(
57+
users,
58+
canvasRef.current,
59+
(newStatuses: Status[]) => {
60+
handleStatuses(newStatuses);
61+
}
62+
);
4963
gameRef.current = game;
5064
return () => {
5165
game.destroy();
5266
};
53-
}, [users, executionId, handleStatuses]);
67+
}, [users, executionId, handleStatuses, gameModeId]);
5468
useEffect(() => {
5569
if (!gameRef.current) throw new Error();
5670
gameRef.current.onCompleted = (result: Result) => {

packages/web/src/component/Injection.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Blockly from "blockly";
55
import Ja from "blockly/msg/ja";
66
import "../style.css";
77
import { Box } from "@mui/material";
8-
import options from "../options";
98
import modifyTranslation from "../modifyTranslation";
109

1110
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -16,9 +15,11 @@ import modifyTranslation from "../modifyTranslation";
1615

1716
export default function Injection(props: {
1817
workspaceRef: React.MutableRefObject<Blockly.WorkspaceSvg | undefined>;
18+
options: any;
1919
}) {
2020
const { workspaceRef } = props;
2121
const workspaceDivRef = useRef<HTMLDivElement>(null);
22+
const { options } = props;
2223
useEffect(() => {
2324
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2425
// @ts-ignore
@@ -28,7 +29,7 @@ export default function Injection(props: {
2829
return () => {
2930
workspace.dispose();
3031
};
31-
}, [workspaceRef]);
32+
}, [workspaceRef, options]);
3233

3334
return <Box ref={workspaceDivRef} sx={{ width: 1, height: 1 }} />;
3435
}

packages/web/src/component/TestPlay.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { grey } from "@mui/material/colors";
2929
import { HiOutlineScale } from "react-icons/hi";
3030
import Draggable from "react-draggable";
3131
import Emulator, { Status } from "./Emulator";
32-
import type { User } from "./game";
32+
import type { User } from "./game/game";
3333
import { getUsers } from "../fetchAPI";
3434

3535
interface EnemyDialogProps {
@@ -264,6 +264,7 @@ export default function TestPlay(props: TestPlayProps) {
264264
isPaused={isPaused}
265265
executionId={executionId}
266266
handleStatuses={setStatuses}
267+
gameModeId={0}
267268
/>
268269
) : (
269270
<Skeleton width="100%" height="auto" />

packages/web/src/component/Welcome.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
ToggleButtonGroup,
1212
Typography,
1313
} from "@mui/material";
14-
import type { User } from "./game";
14+
import type { User } from "./game/game";
1515
import iconURL from "../icon1.svg";
1616
import { createUser } from "../fetchAPI";
1717
import DropDown from "./DropDown";

0 commit comments

Comments
 (0)