Skip to content

Commit

Permalink
Finish vanilla js refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
zachgoll committed Jan 31, 2023
1 parent 69e9551 commit 71f858e
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 432 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
dist
dist
live-vanilla-build
live-react-build
35 changes: 0 additions & 35 deletions Notes.md

This file was deleted.

17 changes: 0 additions & 17 deletions Vanilla Refactor.md

This file was deleted.

16 changes: 0 additions & 16 deletions Video.md

This file was deleted.

2 changes: 0 additions & 2 deletions live-vanilla-build/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ button:hover {
}

.turn {
color: var(--yellow);

grid-column-start: 1;
grid-column-end: 3;
align-self: center;
Expand Down
16 changes: 8 additions & 8 deletions live-vanilla-build/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
</head>
<body>
<main>
<div class="grid">
<div class="grid" data-id="grid">
<!-- Turn indicator -->
<div class="turn yellow" data-id="turn">
<i class="fa-solid fa-x"></i>
<p>Player 1, you're up!</p>
<div class="turn" data-id="turn">
<i class="fa-solid fa-x turquoise"></i>
<p class="turquoise">Player 1, you're up!</p>
</div>

<!-- Dropdown menu -->
<div class="menu" data-id="menu">
<button class="menu-btn">
<button class="menu-btn" data-id="menu-btn">
Actions
<i class="fa-solid fa-chevron-down"></i>
</button>
Expand All @@ -47,15 +47,15 @@
<!-- Scoreboard -->
<div class="score shadow" style="background-color: var(--turquoise)">
<p>Player 1</p>
<span>0 Wins</span>
<span data-id="p1-wins">0 Wins</span>
</div>
<div class="score shadow" style="background-color: var(--light-gray)">
<p>Ties</p>
<span data-id="ties">0</span>
</div>
<div class="score shadow" style="background-color: var(--yellow)">
<p>Player 2</p>
<span>0 Wins</span>
<span data-id="p2-wins">0 Wins</span>
</div>
</div>
</main>
Expand Down Expand Up @@ -83,6 +83,6 @@
</div>
</div>

<script src="js/app.js"></script>
<script src="js/app.js" type="module"></script>
</body>
</html>
183 changes: 47 additions & 136 deletions live-vanilla-build/js/app.js
Original file line number Diff line number Diff line change
@@ -1,148 +1,59 @@
const App = {
// All of our selected HTML elements
$: {
menu: document.querySelector('[data-id="menu"]'),
menuItems: document.querySelector('[data-id="menu-items"]'),
resetBtn: document.querySelector('[data-id="reset-btn"]'),
newRoundBtn: document.querySelector('[data-id="new-round-btn"]'),
squares: document.querySelectorAll('[data-id="square"]'),
modal: document.querySelector('[data-id="modal"]'),
modalText: document.querySelector('[data-id="modal-text"]'),
modalBtn: document.querySelector('[data-id="modal-btn"]'),
turn: document.querySelector('[data-id="turn"]'),
import Store from "./store.js";
import View from "./view.js";

const players = [
{
id: 1,
name: "Player 1",
iconClass: "fa-x",
colorClass: "turquoise",
},

state: {
moves: [],
},

getGameStatus(moves) {
const p1Moves = moves
.filter((move) => move.playerId === 1)
.map((move) => +move.squareId);
const p2Moves = moves
.filter((move) => move.playerId === 2)
.map((move) => +move.squareId);

const winningPatterns = [
[1, 2, 3],
[1, 5, 9],
[1, 4, 7],
[2, 5, 8],
[3, 5, 7],
[3, 6, 9],
[4, 5, 6],
[7, 8, 9],
];

let winner = null;

winningPatterns.forEach((pattern) => {
const p1Wins = pattern.every((v) => p1Moves.includes(v));
const p2Wins = pattern.every((v) => p2Moves.includes(v));

if (p1Wins) winner = 1;
if (p2Wins) winner = 2;
});

return {
status: moves.length === 9 || winner != null ? "complete" : "in-progress", // in-progress | complete
winner, // 1 | 2 | null
};
},

init() {
App.registerEventListeners();
{
id: 2,
name: "Player 2",
iconClass: "fa-o",
colorClass: "yellow",
},
];

registerEventListeners() {
// DONE
App.$.menu.addEventListener("click", (event) => {
App.$.menuItems.classList.toggle("hidden");
});

// TODO
App.$.resetBtn.addEventListener("click", (event) => {
console.log("Reset the game");
});

// TODO
App.$.newRoundBtn.addEventListener("click", (event) => {
console.log("Add a new round");
});

App.$.modalBtn.addEventListener("click", (event) => {
App.state.moves = [];
App.$.squares.forEach((square) => square.replaceChildren());
App.$.modal.classList.add("hidden");
});

// TODO
App.$.squares.forEach((square) => {
square.addEventListener("click", (event) => {
// Check if there is already a play, if so, return early
const hasMove = (squareId) => {
const existingMove = App.state.moves.find(
(move) => move.squareId === squareId
);
return existingMove !== undefined;
};
function init() {
const view = new View();
const store = new Store("live-t3-storage-key", players);

if (hasMove(+square.id)) {
return;
}
// Current tab state changes
store.addEventListener("statechange", () => {
view.render(store.game, store.stats);
});

// Determine which player icon to add to the square
const lastMove = App.state.moves.at(-1);
const getOppositePlayer = (playerId) => (playerId === 1 ? 2 : 1);
const currentPlayer =
App.state.moves.length === 0
? 1
: getOppositePlayer(lastMove.playerId);
const nextPlayer = getOppositePlayer(currentPlayer);
// A different tab state changes
window.addEventListener("storage", () => {
console.log("State changed from another tab");
view.render(store.game, store.stats);
});

const squareIcon = document.createElement("i");
const turnIcon = document.createElement("i");
const turnLabel = document.createElement("p");
turnLabel.innerText = `Player ${nextPlayer}, you are up!`;
// The first load of the document
view.render(store.game, store.stats);

if (currentPlayer === 1) {
squareIcon.classList.add("fa-solid", "fa-x", "yellow");
turnIcon.classList.add("fa-solid", "fa-o", "turquoise");
turnLabel.classList = "turquoise";
} else {
squareIcon.classList.add("fa-solid", "fa-o", "turquoise");
turnIcon.classList.add("fa-solid", "fa-x", "yellow");
turnLabel.classList = "yellow";
}
view.bindGameResetEvent((event) => {
store.reset();
});

App.$.turn.replaceChildren(turnIcon, turnLabel);
view.bindNewRoundEvent((event) => {
store.newRound();
});

App.state.moves.push({
squareId: +square.id,
playerId: currentPlayer,
});
view.bindPlayerMoveEvent((square) => {
const existingMove = store.game.moves.find(
(move) => move.squareId === +square.id
);

square.replaceChildren(squareIcon);
if (existingMove) {
return;
}

// Check if there is a winner or tie game
const game = App.getGameStatus(App.state.moves);

if (game.status === "complete") {
App.$.modal.classList.remove("hidden");

let message = "";
if (game.winner) {
message = `Player ${game.winner} wins!`;
} else {
message = "Tie game!";
}

App.$.modalText.textContent = message;
}
});
});
},
};
// Advance to the next state by pushing a move to the moves array
store.playerMove(+square.id);
});
}

window.addEventListener("load", App.init);
window.addEventListener("load", init);
Loading

0 comments on commit 71f858e

Please sign in to comment.