Skip to content

Commit

Permalink
implemented join game on lobby
Browse files Browse the repository at this point in the history
  • Loading branch information
Koltheguy committed May 6, 2024
1 parent 571d19f commit e2a29fc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/Game/GameState/ShipPlacement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ const ShipPlacement = ({ user, gameId, isPlayer }) => {
position,
orientation,
}).then((result) => {
if (result === 1) handleNextShip();
else console.error("Failed to place ship on the grid.");
if (result === 1) {
shipsPlaced.push(currentShipIndex);
handleNextShip();
} else console.error("Failed to place ship on the grid.");
});
};

Expand Down Expand Up @@ -70,7 +72,7 @@ const ShipPlacement = ({ user, gameId, isPlayer }) => {
buttonText={"Leave"}
/>
</div>
<div style={{ flex: 1 }}>Bottom Right</div>
<div style={{ flex: 1 }}></div>
</div>
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions src/Game/Grid/Grid.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
height: 100%;
}

table {
.grid table {
height: 100%;
aspect-ratio: 1;
border-collapse: collapse;
Expand All @@ -19,8 +19,8 @@ table {
border: none;
}

th,
td {
.grid th,
.grid td {
overflow: hidden;
border: 1px solid #1eb980;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Lobby/Lobby.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useDocumentData,
useCollectionData,
} from "react-firebase-hooks/firestore";
import { db, auth, changeUserName } from "../firebase";
import { db, auth, changeUserName, joinGame } from "../firebase";
import NewGame from "./NewGame";
import Game from "../Game/Game";
import styles from "./Lobby.module.css";
Expand Down Expand Up @@ -38,6 +38,14 @@ const Lobby = () => {
signOut(auth);
};

const handleJoin = (event) => {
joinGame({
user,
gameId: event.target.id,
isPlayer: Number(event.target.value) < 2,
});
};

const [username, setUsername] = useState(displayName);

useEffect(() => {
Expand Down Expand Up @@ -120,6 +128,9 @@ const Lobby = () => {
<td>
<button
className={`${styles.pureMaterial} ${styles.join}`}
id={item.id}
value={item.players.length}
onClick={handleJoin}
>
Join
</button>
Expand Down
29 changes: 18 additions & 11 deletions src/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,43 @@ const newGame = async ({ user, gameName, timer }) => {
currentPlayer: 0,
timeStarted: serverTimestamp(),
winner: "",

lives1: LIVES_TOTAL,
visible1X: "",
visible1Y: "",
ships1X: "",
ships1Y: "",
visible1: "",
ships1: "",

lives2: LIVES_TOTAL,
visible2X: "",
visible2Y: "",
ships2X: "",
ships2Y: "",
visible2: "",
ships2: "",
});

const gameId = docRef.id;
// console.log(gameId);
updateDoc(doc(db, "Game", gameId), {
id: gameId,
});
updateDoc(doc(db, "User", user.uid), {
currentGame: gameId,
});
};
const joinGame = async ({ user, gameId, isPlayer }) => {
if (isPlayer)
console.log({ user, gameId, isPlayer });
if (isPlayer) {
updateDoc(doc(db, "Game", gameId), {
players: arrayUnion(user.uid),
currentGame: gameId,
});
else
updateDoc(doc(db, "User", user.uid), {
currentGame: gameId,
});
} else {
updateDoc(doc(db, "Game", gameId), {
spectate: arrayUnion(user.uid),
currentGame: gameId,
});
updateDoc(doc(db, "User", user.uid), {
currentGame: gameId,
});
}
};
const leaveGame = async ({ user, gameId, isPlayer }) => {
if (isPlayer) {
Expand Down

0 comments on commit e2a29fc

Please sign in to comment.