-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
127 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { doc } from "firebase/firestore"; | ||
import { useDocumentData } from "react-firebase-hooks/firestore"; | ||
import { db, attack, view } from "../../firebase"; | ||
|
||
import Grid from "../Grid/Grid"; | ||
import LeaveButton from "../LeaveButton"; | ||
import styles from "./Gameplay.module.css"; | ||
|
||
const Gameplay = ({ user, gameId, playerNum, gameName, isCurrent }) => { | ||
const [gridData1, setGridData1] = useState({}); | ||
const [gridData2, setGridData2] = useState({}); | ||
const [gameDoc, isGameDocLoading] = useDocumentData( | ||
doc(db, "Game", gameId) | ||
); | ||
|
||
const shipString = isGameDocLoading | ||
? "" | ||
: playerNum === 0 | ||
? gameDoc.ships1 | ||
: gameDoc.ships2; | ||
|
||
let hitsOther; | ||
let missesOther; | ||
let hitsSelf; | ||
let missesSelf; | ||
|
||
useEffect(() => { | ||
//own ships | ||
let tempShipString = shipString; | ||
const newGridData = {}; | ||
while (tempShipString.length > 0) { | ||
const shipLength = tempShipString.at(0); | ||
const shipCoords = tempShipString.slice(1, shipLength * 2 + 1); | ||
for (let i = 0; i < shipLength; i++) { | ||
newGridData[ | ||
`${shipCoords.at(i * 2)}${shipCoords.at(i * 2 + 1)}` | ||
] = "ship"; | ||
} | ||
tempShipString = tempShipString.slice(shipLength * 2 + 1); | ||
} | ||
setGridData2(newGridData); | ||
}, [gameDoc, shipString, setGridData2]); | ||
|
||
const handleCellClick = (position) => { | ||
if (!isCurrent) return; | ||
attack({ | ||
user, | ||
gameId, | ||
playerNum: playerNum === 0 ? 1 : 0, | ||
isCurrent, | ||
position, | ||
hits: hitsOther, | ||
misses: missesOther, | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className={styles.gameplay}> | ||
<div style={{ flex: 3 }}> | ||
<h1>{gameName}</h1> | ||
<Grid handleGridClick={handleCellClick} gridData={gridData1} /> | ||
</div> | ||
<div style={{ flex: 2 }}> | ||
<div style={{ flex: 1 }}> | ||
<LeaveButton | ||
user={user} | ||
gameId={gameId} | ||
isLose={false} | ||
buttonText={"Leave"} | ||
/> | ||
</div> | ||
<div style={{ flex: 1 }}> | ||
<h3>Reference</h3> | ||
<Grid handleGridClick={() => {}} gridData={gridData2} /> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Gameplay; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.gameplay { | ||
display: flex; | ||
height: 100vh; | ||
width: 100vw; | ||
} | ||
|
||
.gameplay h2, | ||
.gameplay h3, | ||
.gameplay h4, | ||
.gameplay h5 { | ||
color: #1eb980; | ||
} | ||
|
||
.gameplay button { | ||
background-color: #1eb980; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters