Merge branch 'main' of codeberg.org:adrien-lsh/ft_transcendence

This commit is contained in:
2024-05-13 16:44:33 +02:00
9 changed files with 112 additions and 108 deletions

View File

@ -75,14 +75,14 @@ export class Profile extends AExchangeable
}
/**
* @returns {[Object]}
* @returns {Promise<[Object]>}
*/
async getGameHistory()
{
let response = await this.client._get(`/api/games/history/${this.id}`);
let response_data = await response.json();
const response = await this.client._get(`/api/games/history/${this.id}`);
const response_data = await response.json();
let games = [];
const games = [];
response_data.forEach(game_data => {
games.push(game_data);

View File

@ -20,7 +20,6 @@ import TournamentPageView from "./views/tournament/TournamentPageView.js";
import TournamentsListView from "./views/tournament/TournamentsListView.js";
import TournamentCreateView from "./views/tournament/TournamentCreateView.js";
import AuthenticationView from "./views/accounts/AuthenticationView.js";
import GameHistoryView from "./views/GameHistoryView.js";
let client = new Client(location.origin);
let lang = client.lang;
@ -79,7 +78,6 @@ const router = async(uri) => {
const routes = [
{ path: "/", view: HomeView},
{ path: "/profiles/:id/history", view: GameHistoryView },
{ path: "/profiles/:username", view: ProfilePageView },
{ path: "/tournaments/create", view: TournamentCreateView },
{ path: "/tournaments/:id", view: TournamentPageView },

View File

@ -1,66 +0,0 @@
import { client, lang } from "../index.js";
import AbstractAuthenticatedView from "./abstracts/AbstractAuthenticatedView.js";
export default class extends AbstractAuthenticatedView
{
constructor(params)
{
super(params, 'homeWindowTitle');
this.id = params.id;
}
async postInit()
{
this.profile = await client.profiles.getProfileId(this.id);
if (this.profile === null)
return 404;
await this.fillHistory();
}
async fillHistory()
{
let games = await this.profile.getGameHistory();
let game_list = document.getElementById("game-list");
games.forEach(game => {
let a = document.createElement("a");
a.href = `/games/${game.id}/0`;
a.setAttribute("data-link", true);
let game_item = document.createElement("div");
game_item.className = "game-item";
game_item.style.backgroundColor = "grey";
if (game.started)
game_item.style.backgroundColor = "yellow";
if (game.finished)
game_item.style.backgroundColor = this.profile.id === game.winner_id ? "green" : "red";
game.players.forEach(player => {
let player_score = document.createElement("a");
player_score.href = `/profiles/${player.username}`;
player_score.innerText = `${player.username}: ${player.score.length}`;
player_score.setAttribute("data-link", true);
game_item.appendChild(player_score);
});
a.appendChild(game_item);
game_list.appendChild(a);
});
}
async getHtml()
{
return /* HTML */ `
<link rel="stylesheet" href="/static/css/gameHistory.css">
<h1>Game History</h1>
<div id="game-list">
</div>
`;
}
}

View File

@ -16,6 +16,13 @@ export default class extends AbstractView {
if (!this.profile)
return 404;
const games = await this.profile.getGameHistory();
console.log(games)
await this.fillHistory(games);
await this.fillStatistics(games);
if (this.profile.id === client.me.id)
return;
@ -68,13 +75,69 @@ export default class extends AbstractView {
statusIndicator.classList.add('bg-danger');
}
/**
* @param {[Object]} games
*/
async fillStatistics(games)
{
const winrateDiv = document.getElementById("winrate");
const win = 0;
const lose = 0;
games.forEach(game => {
if (game.finished === false)
return
if (client.me.id === game.winner.id)
win++;
else
lose++;
});
winrateDiv.innerText = `winrate: ${win + lose === 0 ? "🤓" : win / (win + lose)}`
}
async fillHistory(games)
{
const game_list = document.getElementById("game-list");
games.forEach(game => {
let a = document.createElement("a");
a.href = `/games/${game.game_type}/${game.id}`;
a.setAttribute("data-link", true);
let game_item = document.createElement("div");
game_item.className = "game-item";
game_item.style.backgroundColor = "grey";
if (game.started)
game_item.style.backgroundColor = "yellow";
if (game.finished)
game_item.style.backgroundColor = this.profile.id === game.winner.id ? "green" : "red";
game.players.forEach(player => {
let player_score = document.createElement("a");
player_score.href = `/profiles/${player.username}`;
player_score.innerText = `${player.username}`;
player_score.setAttribute("data-link", true);
game_item.appendChild(player_score);
});
a.appendChild(game_item);
game_list.appendChild(a);
});
}
async getHtml() {
this.profile = await client.profiles.getProfile(this.username);
if (!this.profile)
return '';
return `
return /* HTML */ `
<div>
<div class='mb-3' id='profileInfo'>
<h1>${this.username}<span id='statusIndicator' style='height:0.75em; width:0.75em' class='ms-2 rounded-circle border d-inline-block'></span></h1>
@ -88,6 +151,14 @@ export default class extends AbstractView {
<button class='btn btn-sm btn-danger d-none' id='blockButton'>Block</button>
<button class='btn btn-sm btn-secondary d-none' id='unblockButton'>Unblock</button>
</div>
<h1>Games</h1>
<div>
<h1 id='winrate'></h1>
</div>
<div>
<link rel="stylesheet" href="/static/css/gameHistory.css">
<div id="game-list"></div>
</div>
</div>
`;
}