Skip to content

Commit

Permalink
changed lobby.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mdev9 committed Aug 16, 2024
1 parent 69e833e commit fabfd55
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 66 deletions.
5 changes: 1 addition & 4 deletions dockers/nginx/static/src/js/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ export const createRoomButton = () => {
.then(data => {
if (data.success) {
history.pushState(null, '', `/${data.join_code}`);
replaceHTML('/static/src/html/lobby.html', false).then(() => {
fetchLobbyInfo(data.join_code);
});

handleRouting();
} else {
console.error('Failed to create room:', data.message);
}
Expand Down
63 changes: 4 additions & 59 deletions dockers/nginx/static/src/js/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {getCookie} from '/static/src/js/cookies.js';
import {handleRouting} from '/static/routers/router.js';
import { getSocket } from '/static/views/lobby.js';

function updateLobbyDetails(document, title, Players, map, mode) {
function updateLobbyDetails(title, players, maxPlayerCount, map, mode) {
document.getElementById('lobbyTitle').innerText = title;
if (mode)
mode = 'Tournament';
Expand All @@ -15,46 +15,9 @@ export function viewProfile(playerId) {
console.log('Viewing profile of:', playerId);
}

export function initLobby() {
//show loading transition screen
fetchLobbyInfo();
//connect to websocket
}

export async function fetchLobbyInfo() {
const currentUrl = new URL(window.location.href);
const lobbyId = currentUrl.pathname.split('/')[1];

try {
const response = await fetch(`/api/lobby/${lobbyId}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});

const result = await response.json();

if (result.success) {
const lobbyInfo = result.lobby_info;
updateLobbyDetails(document, lobbyInfo.lobby_name, `${lobbyInfo.players.length} / ${lobbyInfo.player_count}`, lobbyInfo.map_name, lobbyInfo.is_tournament);
renderPlayerList(lobbyInfo.players, lobbyInfo.admin);

const currentUser = result.current_user;
const startButton = document.querySelector('button[data-action="start"]');
//console.log(currentUser, ' === ', lobbyInfo.admin, currentUser === lobbyInfo.admin);
if (currentUser !== lobbyInfo.admin) {
startButton.style.display = 'none';
}
} else {
history.pushState(null, '', '/join');
handleRouting();
alert(result.message);
}
} catch (error) {
console.error('Error fetching lobby info:', error);
alert('An error occurred while fetching lobby information.');
}
export const refreshLobbyDetails = (title, players, maxPlayerCount, map, mode) => {
renderPlayerList(title, `${players.length} / maxPlayerCount`, map, mode);
updateLobbyDetails(title, );
}

function renderPlayerList(players, admin) {
Expand All @@ -81,25 +44,7 @@ function renderPlayerList(players, admin) {
}

export const leaveRoom = () => {
const csrftoken = getCookie('csrftoken');
const currentUrl = new URL(window.location.href);
const lobbyId = currentUrl.pathname.split('/')[1];

fetch(`/api/lobby/leave/${lobbyId}/`, {
method: 'POST',
headers: {
'X-CSRFToken': csrftoken,
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
history.pushState(null, '', '/join');
handleRouting();
} else {
console.error('Error leaving the lobby: ' + data.message);
}
});
}

export const startButton = () => {
Expand Down
6 changes: 3 additions & 3 deletions dockers/nginx/static/src/js/socket_handling/lobby_socket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initLobby } from '/static/src/js/lobby.js';
import {initLobby, refreshLobbyDetails} from '/static/src/js/lobby.js';

export async function initLobbySocket(lobbyId) {
return new Promise((resolve, reject) => {
Expand All @@ -14,7 +14,7 @@ export async function initLobbySocket(lobbyId) {
console.log('Received message:', message);
if (message.type === 'refresh') {
console.log('refreshing lobby');
initLobby();
//refreshLobbyDetails(title, players, maxPlayerCount, map, mode);
}
};

Expand All @@ -27,4 +27,4 @@ export async function initLobbySocket(lobbyId) {
console.log('WebSocket connection closed.');
};
});
}
}

0 comments on commit fabfd55

Please sign in to comment.