Skip to content

Commit

Permalink
Remove leaderboard endpoint usage
Browse files Browse the repository at this point in the history
Add json-api parsing library
Use human readable dates in leagues config
  • Loading branch information
Sheikah45 committed Aug 10, 2021
1 parent 9c00955 commit b7fb8af
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 387 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LOBBY_API_URL=http://lobby.faforever.com:4000
CHALLONGE_USERNAME=joe
CHALLONGE_APIKEY=12345
PORT=3000
API_URL=https://api.test.faforever.com
API_URL=http://localhost:8010
OAUTH_CLIENT_ID=faf-website
OAUTH_CLIENT_SECRET=banana
HOST=http://localhost:3000
Expand Down
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"camelcase": true,
"curly": false, /* custom */
"eqeqeq": true,
"esversion": 8,
"es3": false,
"forin": true,
"freeze": true,
Expand Down
2 changes: 1 addition & 1 deletion configuration/leagues.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"_comments":["IF YOU MODIFY THIS CONFIGURATION FILE, you MUST put the 'reset' value to TRUE. If you do not, unexpected behavior will occur."],"timeRange":{"from":1627848000, "to":1630022399},"ratingRange":[1800,1300,800,300],"reset":true}
{"_comments":["IF YOU MODIFY THIS CONFIGURATION FILE, you MUST put the 'reset' value to TRUE. If you do not, unexpected behavior will occur."],"timeRange":{"from":"2021-08-01T12:00:00Z", "to":"2021-08-26T19:59:59Z"},"ratingRange":[1800,1300,800,300],"reset":true}
1 change: 0 additions & 1 deletion express.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require('dotenv').config();

let express = require('express');
let expressValidator = require('express-validator');

let middleware = require('./routes/middleware');

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"github-api": "3.3.0",
"highcharts": "9.0.0",
"html-ellipsis": "1.1.1",
"json-api-models": "^0.1.0-beta.4",
"json2csv": "5.0.1",
"moment": "2.27.0",
"moment-timezone": "0.5.31",
Expand Down
124 changes: 64 additions & 60 deletions public/js/app/leaderboards.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,76 @@
/*
https://api.faforever.com/leaderboards/ladder1v1?page[size]=50&page[number]=1
*/
var getPage = function (pageNumber, pageSize, id) {

let pastMonth = moment().subtract(1, "months").format("YYYY-MM-DDTHH:mm:ss") + "Z";

var getPage = function(pageNumber, pageSize, id) {
if (pageNumber === 1) {
$(".first").addClass("disabled");
$(".previous").addClass("disabled");
$(".next").removeClass("disabled");
$(".last").removeClass("disabled");
$(".first").addClass("disabled");
$(".previous").addClass("disabled");
$(".next").removeClass("disabled");
$(".last").removeClass("disabled");
} else {
$(".first").removeClass("disabled");
$(".previous").removeClass("disabled");
$(".next").removeClass("disabled");
$(".last").removeClass("disabled");
$(".first").removeClass("disabled");
$(".previous").removeClass("disabled");
$(".next").removeClass("disabled");
$(".last").removeClass("disabled");
}

if (pageNumber === lastPage) {
$(".first").removeClass("disabled");
$(".previous").removeClass("disabled");
$(".next").addClass("disabled");
$(".last").addClass("disabled");
$(".first").removeClass("disabled");
$(".previous").removeClass("disabled");
$(".next").addClass("disabled");
$(".last").addClass("disabled");
}

$.ajax({
url: apiURL + "/leaderboards/" + ratingType + "?page[size]=" + pageSize + "&page[number]=" + pageNumber,
url: apiURL + "/data/leaderboardRating?include=player&sort=-rating&filter=leaderboard.technicalName==" + ratingType + ";updateTime=ge=" +
pastMonth + "&page[size]=" + pageSize + "&page[number]=" + pageNumber,
success: function (result) {
renderPage(result, document.getElementById("players"), id);
renderPage(result, document.getElementById("players"), id, pageSize, pageNumber);
}
});
};

var renderPage = function (page, element, playerId) {
var renderPage = function (page, element, playerId, pageSize, pageNumber) {
removeAllChildElements(element);

let players = {};

for (let k = 0; k < page.included.length; k++) {
if (page.included[k].type === "player") {
const player = page.included[k];
players[player.id] = player.attributes.login;
}
}

for(var i = 0; i < page.data.length; i++) {
var player = page.data[i];
for (let i = 0; i < page.data.length; i++) {
var playerRating = page.data[i];
var ratedPlayerId = playerRating.relationships.player.data.id;
var tr = document.createElement("tr");

let playerName = players[ratedPlayerId];

// Only show player with matching id when player_id is given.
if (playerId && playerId !== player.id) {
tr.className = 'hidden';
if (playerId && playerId !== ratedPlayerId) {
tr.className = 'hidden';
}

tr.setAttribute("id", "tr" + i);
element.appendChild(tr);
var rank = document.createElement("td");
tr.appendChild(rank);
rank.innerHTML = player.attributes.rank;
rank.innerHTML = (i + 1 + pageSize * (pageNumber - 1)).toString();
var name = document.createElement("td");
tr.appendChild(name);
name.innerHTML = player.attributes.name;
name.innerHTML = playerName;
var rating = document.createElement("td");
tr.appendChild(rating);
rating.innerHTML = player.attributes.rating;
rating.innerHTML = Math.round(playerRating.attributes.rating);
var games = document.createElement("td");
tr.appendChild(games);
games.innerHTML = player.attributes.numGames;
games.innerHTML = playerRating.attributes.totalGames;
var stats = document.createElement("td");
tr.appendChild(stats);
stats.innerHTML = '<button class="player btn btn-primary" data-id="' + player.id + '" data-name="' + player.attributes.name + '">View</button>';
stats.innerHTML = '<button class="player btn btn-primary" data-id="' + ratedPlayerId + '" data-name="' + playerName + '">View</button>';
}
};

Expand All @@ -72,7 +84,7 @@ var removeAllChildElements = function (element) {

/* Page Onclick */

$(".previous").click( function() {
$(".previous").click(function () {
if (currentPage === 1) {
return;
}
Expand All @@ -81,7 +93,7 @@ $(".previous").click( function() {
getPage(currentPage, pageSize);
});

$(".next").click( function() {
$(".next").click(function () {
if (currentPage === lastPage) {
return;
}
Expand All @@ -90,17 +102,17 @@ $(".next").click( function() {
getPage(currentPage, pageSize);
});

$(".first").click( function() {
$(".first").click(function () {
currentPage = 1;
getPage(currentPage, pageSize);
});

$(".last").click( function() {
$(".last").click(function () {
currentPage = lastPage;
getPage(currentPage, pageSize);
});

$("#forget-search").click( function() {
$("#forget-search").click(function () {
init();

if (chart.getInstance()) {
Expand All @@ -113,38 +125,37 @@ var searchbar = document.getElementById("searchbar");

// Show label but insert value into the input:
new Awesomplete(searchbar, {
list: JSON.parse(members),
minChars: 0,
maxItems: 5
list: JSON.parse(members),
minChars: 0,
maxItems: 5
});

searchbar.addEventListener('awesomplete-select', function(e){
searchbar.addEventListener('awesomplete-select', function (e) {
var text = e.text;
currentPage = text.value.page;
getPage(text.value.page, 100, text.value.id);
});

searchbar.addEventListener('awesomplete-selectcomplete', function(e){
searchbar.addEventListener('awesomplete-selectcomplete', function (e) {
var text = e.text;
$("#searchbar").val(text.label);
});

$(document).on('click', '.player', (function(){
$(document).on('click', '.player', (function () {
var labels = [], dataset = [];
var id = $(this).data('id');
var name = $(this).data('name');
var pastYear = moment().subtract(1, 'years').unix();

let featuredMod = ratingType === 'ladder1v1' ? 'ladder1v1' : 'faf';
let pastYear = moment().subtract(1, 'years').format("YYYY-MM-DDTHH:mm:ss") + "Z";

$.ajax({
url: apiURL + '/data/gamePlayerStats?filter=player.id==' + id + ';game.featuredMod.technicalName==' + featuredMod + ';scoreTime=gt='+formatTime(pastYear)+';afterDeviation=isnull=false&fields[gamePlayerStats]=afterMean,afterDeviation,scoreTime',
success: function(result) {
$.each(result.data, function(key, stats){
var date = moment(stats.attributes.scoreTime).format('MMM D, YYYY');
var mean = stats.attributes.afterMean;
var deviation = stats.attributes.afterDeviation;
labels.push(date);
dataset.push(Math.round(mean - 3 * deviation));
url: apiURL + '/data/leaderboardRatingJournal?filter=gamePlayerStats.player.id==' + id + ';leaderboard.technicalName==' + ratingType + ';updateTime=ge=' + pastYear + ';deviationAfter=isnull=false&fields[leaderboardRatingJournal]=meanAfter,deviationAfter,updateTime',
success: function (result) {
$.each(result.data, function (key, stats) {
var date = moment(stats.attributes.updateTime).format('MMM D, YYYY');
var mean = stats.attributes.meanAfter;
var deviation = stats.attributes.deviationAfter;
labels.push(date);
dataset.push(Math.round(mean - 3 * deviation));
});

var data = {
Expand Down Expand Up @@ -179,26 +190,19 @@ $(document).on('click', '.player', (function(){

var chart = {
chart: '',
getInstance: function() {
getInstance: function () {
return this.chart;
},
createChart: function(data) {
createChart: function (data) {
this.chart = Highcharts.chart("stats", data);
}
};

/* Init */
var pageSize = 100;
var currentPage = 1;
var init = function() {
var init = function () {
getPage(1, 100);
};

init();


// Formats a timestamp so that Elide JSON API can understand it
function formatTime(unix_timestamp){
let d = moment(unix_timestamp*1000);
return d.format("YYYY-MM-DDTHH:mm:ss")+"Z";
}
2 changes: 1 addition & 1 deletion public/js/app/leagues.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var renderPage = function (category, element, playerId) {
let favoriteFaction = null;
let favoriteFactionPlayCount = 0;
let total = 0;
for(faction in player.factions){
for(var faction in player.factions){
if (faction < 1 || faction > 4) continue; // Ignoring "Random" and other invalid values*
if (player.factions[faction] >= favoriteFactionPlayCount){
favoriteFaction = faction;
Expand Down
2 changes: 1 addition & 1 deletion routes/views/1v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports = module.exports = function(req, res) {
locals.section = 'competitive';
locals.cSection = '1v1';
locals.ratingTypeTitle = '1v1';
locals.ratingType = 'ladder1v1';
locals.ratingType = 'ladder_1v1';
locals.apiURL = process.env.API_URL;

fs.readFile('members/1v1.json', 'utf8', function (err, data) {
Expand Down
11 changes: 5 additions & 6 deletions routes/views/leagues.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
exports = module.exports = function(req, res, ladderData) {
var moment = require('moment-timezone')
var moment = require('moment-timezone');
var locals = res.locals;
var fs = require('fs');

// locals.section is used to set the currently selected
// item in the header navigation.
Expand All @@ -13,12 +12,12 @@ exports = module.exports = function(req, res, ladderData) {
locals.members = [];
locals.lastPage = [];

const settings = require(process.cwd()+'/configuration/leagues.json');
const leaguesConfig = require(process.cwd()+'/configuration/leagues.json');
locals.rankingCategories = ladderData.playerData || {};
locals.state = +(settings.timeRange.to < moment().unix()) - +(settings.timeRange.from > moment().unix());
locals.state = +(moment(leaguesConfig.timeRange.to).tz("Etc/GMT+0").isBefore(moment())) - +(moment(leaguesConfig.timeRange.from).tz("Etc/GMT+0").isAfter(moment()));
locals.dates = {
"from": moment(settings.timeRange.from*1000).tz("Etc/GMT+0").format("dddd, MMMM Do YYYY, H:mm:ss"),
"to": moment(settings.timeRange.to*1000).tz("Etc/GMT+0").format("dddd, MMMM Do YYYY, H:mm:ss")
"from": moment(leaguesConfig.timeRange.from).tz("Etc/GMT+0").format("dddd, MMMM Do YYYY, H:mm:ss"),
"to": moment(leaguesConfig.timeRange.to).tz("Etc/GMT+0").format("dddd, MMMM Do YYYY, H:mm:ss")
};
for (const category in locals.rankingCategories){
locals.members[category] = [];
Expand Down
Loading

0 comments on commit b7fb8af

Please sign in to comment.