Repository navigation
Expand file tree
/
Copy pathprofile.js
More file actions
66 lines (62 loc) · 3.51 KB
/
Copy pathprofile.js
File metadata and controls
66 lines (62 loc) · 3.51 KB
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
// A player's page (profile.html?p=<owner>): their community photos, videos and shared designs, from
// the site's own data/gallery.json, data/videos.json and data/designs.json (photos.js, videos.js and
// designs.js draw the lists, filtered by the owner), and here the heading: the gamertag of their
// newest item and how many photos, designs and likes they have (and videos, when they have any:
// only those videos.js draws, with its own check). All of it is already public on the site; the
// owner id is the player id the game server publishes (a salted hash, never a XUID), and nothing
// here comes from the accounts service. The gamertag is the player's own text: textContent only.
(function () {
"use strict";
var nameEl = document.getElementById("profile-name");
var summary = document.getElementById("profile-summary");
if (!nameEl || !summary) return;
var wanted = new URLSearchParams(location.search).get("p") || "";
if (!/^[0-9a-f]{12}$/.test(wanted)) {
summary.textContent = "There is no such player. Open a player's page from a photo, a video or a design.";
return;
}
// a video counts only if videos.js draws it (no videos.js: none is drawn)
var drawnVideo = window.FHVideos ? window.FHVideos.usable : function () { return false; };
function list(path, field, drawn) {
return fetch(path, { cache: "no-cache" }).then(function (r) {
if (!r.ok) throw new Error("HTTP " + r.status);
return r.json();
}).then(function (doc) {
return doc && Array.isArray(doc[field]) ? doc[field].filter(function (i) {
return i && i.owner === wanted && typeof i.gamertag === "string" && i.gamertag && (!drawn || drawn(i));
}) : [];
}, function () { return null; });
}
function count(n, one, many) { return n.toLocaleString("en") + " " + (n === 1 ? one : many); }
function likes(items) {
return items.reduce(function (sum, i) {
return sum + (typeof i.likes === "number" && isFinite(i.likes) && i.likes > 0 ? Math.round(i.likes) : 0);
}, 0);
}
Promise.all([list("data/gallery.json", "photos"), list("data/designs.json", "designs"),
list("data/videos.json", "videos", drawnVideo)]).then(function (r) {
var photos = r[0] || [], designs = r[1] || [], videos = r[2] || [];
if (r[0] === null && r[1] === null && r[2] === null) {
summary.textContent = "This page could not be loaded. Try again later.";
return;
}
if (!photos.length && !designs.length && !videos.length) {
summary.textContent = "This player has no photos, videos or designs on the site right now.";
return;
}
// The gamertag on their newest photo (photos and videos are listed newest first), else their
// newest video, else their newest design.
var newestDesign = designs.slice().sort(function (a, b) { return (b.createdAt || 0) - (a.createdAt || 0); })[0];
var tag = photos.length ? photos[0].gamertag : videos.length ? videos[0].gamertag : newestDesign.gamertag;
nameEl.textContent = tag;
document.title = tag + " - Forza Horizon 1: ReImagined";
var parts = [count(photos.length, "photo", "photos")];
if (videos.length) parts.push(count(videos.length, "video", "videos")); // nothing for a player with none
parts.push(count(designs.length, "design", "designs"));
var total = likes(photos) + likes(videos) + likes(designs);
if (photos.concat(videos, designs).some(function (i) { return typeof i.likes === "number"; })) {
parts.push(count(total, "like", "likes"));
}
summary.textContent = parts.join(" · ");
});
})();