boardgame_site_v2/static/javascript/main.js

198 lines
6.1 KiB
JavaScript
Raw Normal View History

2024-08-08 16:14:55 +02:00
const api_url = "http://127.0.0.1:8000"
var all_owned_games
async function makeRequest(url) {
2024-08-08 16:14:55 +02:00
try {
const url_request = new Request(url)
const response = await fetch(url_request);
2024-08-08 16:14:55 +02:00
const result = await response.json();
return result
} catch (error) {
console.error("Error:", error);
}
}
2024-08-11 12:07:16 +02:00
async function loadWishlistedGames() {
var wishlist_priorities = [1,2,3,4]
jQuery.each(wishlist_priorities, function(index, item){
var boardgame_datatable = new DataTable('#wishlist_table'+item, {
ajax: {
url: api_url + '/wishlist?priority='+item,
dataSrc: ''
},
columns: [
{
data: 'wishlist_priority'
},
{
data: 'thumbnail_url',
render: function (data,type){
2024-08-11 17:48:50 +02:00
return '<img src="' + data + '" class="img-fluid" />'
2024-08-11 12:07:16 +02:00
}
},
{
data: 'name'
},
{
2024-08-11 16:20:53 +02:00
data: 'min_players',
render: function(data,type,row){
if (row.min_players != row.max_players){
return row.min_players + '-' + row.max_players
}else{
return row.min_players
}
}
},
{
data: 'min_playing_time',
render: function(data,type,row){
if (row.min_playing_time != row.max_playing_time){
return row.min_playing_time + '-' + row.max_playing_time
}else{
return row.min_playing_time
}
}
2024-08-11 12:07:16 +02:00
},
{
data: 'weight'
}
],
columnDefs: [
{
target: 0,
visible: false
2024-08-11 16:20:53 +02:00
},
{ targets: 'no-sort', orderable: false }
2024-08-11 12:07:16 +02:00
],
order: [[2, 'asc']]
});
$('#wishlist_table'+item).on('click', 'tbody tr', function() {
var boardgame_id = boardgame_datatable.row(this).data().id;
window.location.href = "/boardgame?id=" + boardgame_id
})
});
}
2024-08-08 16:14:55 +02:00
async function loadOwnedGames() {
2024-08-11 12:07:16 +02:00
var boardgame_datatable = new DataTable('.boardgame_table', {
ajax: {
url: api_url + '/owned?filter_expansions_out=true',
dataSrc: ''
},
columns: [
{
data: 'thumbnail_url',
render: function (data,type){
2024-08-11 17:48:50 +02:00
return '<img src="' + data + '" class="img-fluid" />'
}
},
{
data: 'name'
},
{
2024-08-11 16:20:53 +02:00
data: 'min_players',
render: function(data,type,row){
if (row.min_players != row.max_players){
return row.min_players + '-' + row.max_players
}else{
return row.min_players
}
}
},
{
data: 'min_playing_time',
render: function(data,type,row){
if (row.min_playing_time != row.max_playing_time){
return row.min_playing_time + '-' + row.max_playing_time
}else{
return row.min_playing_time
}
}
},
{
data: 'weight'
}
2024-08-11 10:29:40 +02:00
],
2024-08-11 16:20:53 +02:00
columnDefs: [
{ targets: 'no-sort', orderable: false }
],
2024-08-11 12:07:16 +02:00
order: [[1, 'asc']]
});
2024-08-11 10:49:59 +02:00
2024-08-11 10:29:40 +02:00
2024-08-11 12:07:16 +02:00
$('.boardgame_table').on('click', 'tbody tr', function() {
2024-08-11 10:29:40 +02:00
var boardgame_id = boardgame_datatable.row(this).data().id;
window.location.href = "/boardgame?id=" + boardgame_id
})
2024-08-08 16:14:55 +02:00
}
2024-08-11 23:04:30 +02:00
async function loadStatistics(){
const overtimechart = document.getElementById("overtimechart")
games_over_time_statistic = await makeRequest(api_url + '/statistics/amount_of_games_over_time?day_step=30')
games_over_time_statistic_no_expanions = await makeRequest(api_url + '/statistics/amount_of_games_over_time?day_step=30&filter_expansions_out=true')
games_over_time_statistic_only_expanions = await makeRequest(api_url + '/statistics/amount_of_games_over_time?day_step=30&only_expansions=true')
2024-08-11 23:04:30 +02:00
new Chart(overtimechart, {
type: 'bar',
2024-08-11 23:04:30 +02:00
data: {
2024-08-12 08:27:10 +02:00
labels: Object.keys(games_over_time_statistic.result),
2024-08-11 23:04:30 +02:00
datasets: [{
label: games_over_time_statistic.name,
2024-08-11 23:04:30 +02:00
data: Object.values(games_over_time_statistic.result),
borderWidth: 1,
type: 'line'
},
{
label: "Base games",
data: Object.values(games_over_time_statistic_no_expanions.result),
2024-08-11 23:04:30 +02:00
borderWidth: 1
},
{
label: "Expansions",
data: Object.values(games_over_time_statistic_only_expanions.result),
borderWidth: 1
}
]
2024-08-11 23:04:30 +02:00
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
2024-08-12 08:27:10 +02:00
$("#overtimechartname").text(games_over_time_statistic.name)
2024-08-11 23:04:30 +02:00
}
async function loadGame() {
let params = new URLSearchParams(document.location.search);
2024-08-11 10:49:59 +02:00
let boardgame_id = params.get("id");
var loadGameURL = api_url + '/boardgame?id=' + boardgame_id
var requested_game = await makeRequest(loadGameURL)
2024-08-11 10:49:59 +02:00
$('#boardgame_image').attr('src', requested_game.image_url)
$('#boardgame_name').text(requested_game.name)
$('#boardgame_weight').text(requested_game.weight)
$('#boardgame_description').text(requested_game.description)
2024-08-11 10:49:59 +02:00
$('#boardgame_link').attr('href', 'https://boardgamegeek.com/boardgame/' + boardgame_id)
2024-08-08 16:14:55 +02:00
}