-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-room.js
197 lines (179 loc) · 4.85 KB
/
game-room.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const BGG_USER = '0xACE';
function parsePlaysXML(xml) {
const plays = [];
xml.querySelectorAll('play').forEach(function(play) {
let players = [];
play.querySelectorAll('player').forEach(function(player) {
let name = player.getAttribute('name');
if (name !== 'Anonymous player') {
players.push(name.split(' ')[0]);
}
});
let comments = '';
if (play.querySelector('comments')) {
comments = play.querySelector('comments').textContent;
}
const p_json = {
'bgg_id': play.querySelector('item').getAttribute('objectid'),
'name': play.querySelector('item').getAttribute('name'),
'date': play.getAttribute('date'),
'players': players.join(', '),
'comments': comments,
'location': play.getAttribute('location')
};
plays.push(p_json);
});
return plays;
}
function parseGamesXML(xml) {
const games = [];
xml.querySelectorAll('item').forEach(function(game) {
let comments = '';
if (game.querySelector('comments')) {
comments = game.querySelector('comments').textContent;
}
const p_game = {
'bgg_id': game.getAttribute('objectid'),
'name': game.querySelector('name').textContent,
'image': game.querySelector('image').textContent,
'year': game.querySelector('yearpublished').textContent,
'plays': game.querySelector('numplays').textContent,
'comments': comments
};
games.push(p_game);
});
return games;
}
function bgg_uri(game) {
return "https://boardgamegeek.com/boardgame/" + game.bgg_id;
}
function bgg_image(game) {
const img_id = 'img_' + game.bgg_id;
if (game.bgg_id > 0) {
$.ajax({
url: "https://boardgamegeek.com/xmlapi2/thing?id=" + game.bgg_id,
}).done(function(xml) {
const src = xml.querySelector('thumbnail').textContent;
$('.' + img_id).attr('src', src);
}).fail(function(err) {
console.log(err);
});
}
return '<img class="game-image ' + img_id + '" />';
}
function render_date(the_date) {
const d = new Date(the_date + "T00:00:00");
return '<span style="white-space: nowrap;">' + the_date + '</span> '
+ d.toLocaleDateString('en-US', { weekday: 'long' });
}
function addLocalGames() {
$.ajax({
url: 'games.xml',
}).done(function(xml) {
var game_list = $('#game-list').DataTable();
const games = parseGamesXML(xml);
game_list.rows.add(games).draw();
}).fail(function(err) {
console.log(err);
});
}
function addLocalPlays() {
$.ajax({
url: 'plays.xml',
}).done(function(xml) {
var play_list = $('#play-list').DataTable();
const games = parsePlaysXML(xml);
play_list.rows.add(games).draw();
}).fail(function(err) {
console.log(err);
});
}
$(document).ready(function() {
$('#play-list').DataTable({
responsive: true,
ajax: {
url: `https://boardgamegeek.com/xmlapi2/plays\?username\=${BGG_USER}`,
dataType: "text",
dataSrc: function(response) {
addLocalPlays();
const xml = (new window.DOMParser()).parseFromString(response, "text/xml");
return parsePlaysXML(xml);
}
},
paging: false,
order: [[0, "desc"]],
columns: [
{ "data": "date" },
{ "data": null, "defaultContent": "", "orderable": false },
{ "data": "name", width: "25%" },
{ "data": "players", "defaultContent": "" },
{ "data": "location", "defaultContent": "" },
{ "data": "comments", "defaultContent": "" }
],
columnDefs: [
{
targets: 0,
responsivePriority: 1,
render: function(data, type, row) {
return render_date(data);
}
},
{
targets: 1,
responsivePriority: 2,
render: function(data, type, row) {
return "<a href='" + bgg_uri(row) + "' target='_blank'>"
+ bgg_image(row) + "</a>";
}
},
{
targets: 2,
responsivePriority: 2,
render: function(data, type, row) {
return "<a href='" + bgg_uri(row) + "' target='_blank'>" + data + "</a>";
}
}
]
});
$('#game-list').DataTable({
responsive: true,
ajax: {
url: `https://boardgamegeek.com/xmlapi2/collection\?username\=${BGG_USER}`,
dataType: "text",
dataSrc: function(response) {
addLocalGames();
const xml = (new window.DOMParser()).parseFromString(response, "text/xml");
return parseGamesXML(xml);
}
},
paging: false,
order: [[1, "asc"]],
columns: [
{ "data": "image", "defaultContent": "", "orderable": false},
{ "data": "name", width: "25%" },
{ "data": "year", "defaultContent": "" },
{ "data": "plays", "defaultContent": "0" },
{ "data": "comments", "defaultContent": "" }
],
columnDefs: [
{
targets: 0,
render: function(data, type, row) {
return "<a href='" + bgg_uri(row) + "' target='_blank'>"
+ bgg_image(row) + "</a>";
}
},
{
targets: 1,
render: function(data, type, row) {
return "<a href='" + bgg_uri(row) + "' target='_blank'>" + data + "</a>";
}
}
]
});
// Collapse navbar on mobile
var navMain = $(".navbar-collapse");
navMain.on("click", "a:not([data-toggle])", null, function () {
navMain.collapse('hide');
});
});