import { drawGrid, drawEnnemyGrid, play, selectPiece } from "./game.js";
export const socket = io();
export let roomId = "";
function startConnection() {
socket.emit("first connection", socket.id)
drawGrid();
selectPiece();
document
.querySelector("#start")
.addEventListener("click", onCreateRoom());
document
.querySelector("#join")
.addEventListener("click", onJoinRoom());
}
socket.on("start game", (username) => {
const ennemyBoard = document.querySelector("#ennemy_board");
ennemyBoard.classList.remove("hidden-element");
drawGrid();
drawEnnemyGrid();
});
socket.on("end game", () => {
console.log("end game");
const ennemyBoard = document.querySelector("#ennemy_board");
const loader = document.querySelector("#loader");
loader.classList.remove("hidden-element");
ennemyBoard.classList.add("hidden-element");
});
socket.on("play", () => {
const notification = document.querySelector("#play_notification");
notification.classList.remove("hidden-element");
play();
});
socket.on("played move", (isHit, isWin) => {
const hitNotification = document.querySelector("#hit_notification");
const winNotification = document.querySelector("#win_notification");
if (isHit) hitNotification.classList.remove("hidden-element");
else hitNotification.classList.add("hidden-element");
if (isWin) gameEnd()
if (isWin) winNotification.classList.remove("hidden-element");
else winNotification.classList.add("hidden-element");
drawGrid();
drawEnnemyGrid();
});
socket.on('opponent left', () => {
const modal = document.getElementById('opponentLeftModal');
modal.style.display = 'block';
})
socket.on("go to menu", () => {
const modal = document.getElementById("gameEndedModal")
const ennemyGrid = document.getElementById("ennemy_board")
const loader = document.getElementById("loader")
const roomkeyHolder = document.getElementById("roomkeyHolder")
const notifications = document.getElementById("inGameNotification")
roomId = ""
notifications.style.display = 'none'
modal.style.display = 'none'
ennemyGrid.style.display = 'none'
loader.classList.remove = "hidden-element"
roomkeyHolder.style.display = 'none'
drawGrid()
})
function gameEnd() {
const modal = document.getElementById('gameEndedModal');
modal.style.display = 'block';
}
export function sendMove(move) {
const notification = document.querySelector("#play_notification");
socket.emit("play", roomId, socket.id, move);
notification.classList.add("hidden-element");
}
function onCreateRoom() {
const handler = function (event) {
event.preventDefault();
const loader = document.querySelector("#loader");
const roomkeyHolder = document.querySelector("#roomkeyHolder");
loader.classList.add("hidden-element");
socket.emit("room creation", socket.id, (response) => {
roomId = response.roomId;
roomkeyHolder.innerHTML += `Your room key is : ` + roomId + ``;
});
};
return handler;
}
function onJoinRoom() {
const handler = function (event) {
event.preventDefault();
const loader = document.querySelector("#loader");
const roomKey = document.querySelector("#roomKey").value;
const roomkeyHolder = document.querySelector("#roomkeyHolder");
const errorHolder = document.querySelector("#errorHandler")
roomId = roomKey;
socket.emit("ask for room", roomKey, socket.id, (response) => {
if (response.status !== true) {
if (errorHolder.textContent == "") {
errorHolder.append("Error : Room Id don't exist")
}
} else {
loader.classList.add("hidden-element");
roomkeyHolder.innerHTML += `Your room key is : ` + roomId + ``;
}
});
};
return handler;
}
document.getElementById('closeModalButton').addEventListener('click', () => {
const modal = document.getElementById('opponentLeftModal');
const ennemyBoard = document.querySelector("#ennemy_board");
ennemyBoard.classList.add("hidden-element");
modal.style.display = 'none';
socket.emit("reset grid", roomId)
drawGrid()
});
document.getElementById('goToMenuButton').addEventListener('click', () => {
socket.emit("game ended", roomId);
})
setTimeout(startConnection, 100);