profile: add: game history

This commit is contained in:
2024-05-13 13:50:33 +02:00
parent f576dc208f
commit 5675f64268
6 changed files with 85 additions and 100 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

@ -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,51 @@ export default class extends AbstractView {
statusIndicator.classList.add('bg-danger');
}
async fillStatistics(games)
{
}
async fillHistory(games)
{
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.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 +133,11 @@ 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>
<div>
<link rel="stylesheet" href="/static/css/gameHistory.css">
<h1>Game History</h1>
<div id="game-list"></div>
</div>
</div>
`;
}