- Add function for request game
This commit is contained in:
Kum1ta
2024-09-15 14:02:05 +02:00
parent 1d9db521d3
commit e1b8275c16
4 changed files with 67 additions and 19 deletions

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/07 17:40:15 by edbernar #+# #+# */
/* Updated: 2024/09/14 23:59:41 by edbernar ### ########.fr */
/* Updated: 2024/09/15 10:32:23 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
@ -189,18 +189,18 @@ function connect(e)
e.preventDefault();
sendRequest("login", {type: "byPass", mail: mail, password: e.target.password.value});
waitForLogin().then((isConnected) => {
if (isConnected)
{
usernameNode = document.createTextNode(userMeInfo.username);
loginButton.replaceChild(usernameNode, pLoginButton);
CN.new("Connected successfully", "Welcome " + userMeInfo.username, CN.defaultIcon.success);
popout.style.display = 'none';
}
}).catch((err) => {
console.error(err);
CN.new("Error", "An error occured while trying to connect", CN.defaultIcon.error);
});
// waitForLogin().then((isConnected) => {
// if (isConnected)
// {
// usernameNode = document.createTextNode(userMeInfo.username);
// loginButton.replaceChild(usernameNode, pLoginButton);
// CN.new("Connected successfully", "Welcome " + userMeInfo.username, CN.defaultIcon.success);
// popout.style.display = 'none';
// }
// }).catch((err) => {
// console.error(err);
// CN.new("Error", "An error occured while trying to connect", CN.defaultIcon.error);
// });
}
export { Login, changeWindowLoginBack };

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* typeGame.js :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/15 12:00:01 by edbernar #+# #+# */
/* Updated: 2024/09/15 12:46:12 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import { WaitingGamePage } from "/static/javascript/waitingGame/main.js"
import { pageRenderer } from '/static/javascript/main.js'
function typeGame(content)
{
if (content.action == 1)
{
if (pageRenderer.actualPage == WaitingGamePage)
WaitingGamePage.showOpponent(content.username);
}
}
export { typeGame };

View File

@ -6,14 +6,15 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 21:20:45 by edbernar #+# #+# */
/* Updated: 2024/09/14 23:24:24 by edbernar ### ########.fr */
/* Updated: 2024/09/15 12:57:52 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import { sendRequest } from "/static/javascript/websocket.js";
import { pageRenderer } from '/static/javascript/main.js'
let intervalPoints = null;
let intervalPoints = null;
let timeout = null;
class WaitingGamePage
{
@ -36,7 +37,9 @@ class WaitingGamePage
points = '';
sentence.innerText = text + points;
}, 500);
sendRequest("game", {action: 0})
timeout = setTimeout(() => {
sendRequest("game", {action: 0})
}, 1500);
returnButton.addEventListener('click', returnToLobby);
}
@ -45,11 +48,29 @@ class WaitingGamePage
if (intervalPoints)
clearInterval(intervalPoints);
intervalPoints = null;
if (timeout)
clearTimeout(timeout);
timeout = null;
returnButton.removeEventListener('click', returnToLobby);
}
static showOpponent(username)
{
const returnButton = document.getElementById('returnToLobbyButton');
const sentence = document.getElementById('sentence');
if (intervalPoints)
clearInterval(intervalPoints);
intervalPoints = null;
sentence.innerText = "Match found";
document.body.removeChild(returnButton);
}
}
function returnToLobby()
{
sendRequest("game", {action: 2});
for (let i = 0; i < document.body.children.length; i++)
{
document.body.children[i].style.animation = "anim3 0.6s";

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/31 22:17:24 by edbernar #+# #+# */
/* Updated: 2024/09/12 18:18:53 by edbernar ### ########.fr */
/* Updated: 2024/09/15 11:55:47 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@ import { typeCreateAccount } from "/static/javascript/typeResponse/typeCreateAcc
import { typeAllListUser }from "/static/javascript/typeResponse/typeAllListUser.js";
import { createNotification as CN } from "/static/javascript/notification/main.js";
import { typeLogin } from "/static/javascript/typeResponse/typeLogin.js";
import { typeGame } from "/static/javascript/typeResponse/typeGame.js"
let socket = null;
let status = 0;
@ -29,8 +30,8 @@ function launchSocket()
{
socket = new WebSocket('/ws');
const typeResponse = ["logged_in", "login", "private_list_user", "private_list_message", "new_private_message", "all_list_user", "create_account"];
const functionResponse = [typeLogin, typeLogin, typePrivateListUser, typePrivateListMessage, typeNewPrivateMessage, typeAllListUser, typeCreateAccount];
const typeResponse = ["logged_in", "login", "private_list_user", "private_list_message", "new_private_message", "all_list_user", "create_account", "game"];
const functionResponse = [typeLogin, typeLogin, typePrivateListUser, typePrivateListMessage, typeNewPrivateMessage, typeAllListUser, typeCreateAccount, typeGame];
const errorCode = [9007, 9010, 9011];
const errorFunction = [typeErrorInvalidPassword, typeErrorInvalidToken42, typeErrorUnknown42Account];