forked from FAForever/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable leaderboards with user-generated cache and tokens
- Loading branch information
1 parent
a09eb7f
commit f6c513a
Showing
15 changed files
with
503 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ public/styles/css/* | |
|
||
#Ignore environment | ||
.env | ||
.env.faf.xyz | ||
|
||
public/js/*.js | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class LeaderboardRepository { | ||
constructor(javaApiClient, monthsInThePast = 12) { | ||
this.javaApiClient = javaApiClient | ||
this.monthsInThePast = monthsInThePast | ||
} | ||
|
||
getUpdateTimeForApiEntries() { | ||
const date = new Date(); | ||
date.setMonth(date.getMonth() - this.monthsInThePast); | ||
|
||
return date.toISOString() | ||
} | ||
|
||
async fetchLeaderboard(id) { | ||
const updateTime = this.getUpdateTimeForApiEntries() | ||
|
||
let response = await this.javaApiClient.get(`/data/leaderboardRating?include=player&sort=-rating&filter=leaderboard.id==${id};updateTime=ge=${updateTime}&page[size]=9999`); | ||
|
||
if (response.status !== 200) { | ||
throw new Error('LeaderboardRepository::fetchLeaderboard failed with response status "' + response.status + '"') | ||
} | ||
|
||
return this.mapResponse(JSON.parse(response.data)) | ||
} | ||
|
||
mapResponse(data) { | ||
if (typeof data !== 'object' || data === null) { | ||
throw new Error('LeaderboardRepository::mapResponse malformed response, not an object') | ||
} | ||
|
||
if (!data.hasOwnProperty('data')) { | ||
throw new Error('LeaderboardRepository::mapResponse malformed response, expected "data"') | ||
} | ||
|
||
if (data.data.length === 0) { | ||
console.log('[info] leaderboard empty') | ||
|
||
return [] | ||
} | ||
|
||
if (!data.hasOwnProperty('included')) { | ||
throw new Error('LeaderboardRepository::mapResponse malformed response, expected "included"') | ||
} | ||
|
||
let leaderboardData = [] | ||
|
||
data.data.forEach((item, index) => { | ||
leaderboardData.push({ | ||
rating: item.attributes.rating, | ||
totalgames: item.attributes.totalGames, | ||
wonGames: item.attributes.wonGames, | ||
date: item.attributes.updateTime, | ||
label: data.included[index].attributes.login, | ||
}) | ||
}) | ||
|
||
return leaderboardData | ||
} | ||
} | ||
|
||
module.exports = LeaderboardRepository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class LeaderboardService { | ||
constructor(cacheService, lockService, leaderboardRepository, lockTimeout = 3000) { | ||
this.lockTimeout = lockTimeout | ||
this.cacheService = cacheService | ||
this.lockService = lockService | ||
this.leaderboardRepository = leaderboardRepository | ||
} | ||
|
||
async getLeaderboard(id) { | ||
|
||
if (typeof (id) !== 'number') { | ||
throw new Error('LeaderboardService:getLeaderboard id must be a number') | ||
} | ||
|
||
const cacheKey = 'leaderboard-' + id | ||
|
||
if (this.cacheService.has(cacheKey)) { | ||
return this.cacheService.get(cacheKey) | ||
} | ||
|
||
if (this.lockService.locked) { | ||
await this.lockService.lock(() => { | ||
}, this.lockTimeout) | ||
return this.getLeaderboard(id) | ||
} | ||
|
||
await this.lockService.lock(async () => { | ||
const result = await this.leaderboardRepository.fetchLeaderboard(id) | ||
this.cacheService.set(cacheKey, result); | ||
}) | ||
|
||
return this.getLeaderboard(id) | ||
} | ||
} | ||
|
||
module.exports = LeaderboardService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const LeaderboardService = require("./LeaderboardService"); | ||
const LeaderboardRepository = require("./LeaderboardRepository"); | ||
const {LockService} = require("./LockService"); | ||
const NodeCache = require("node-cache"); | ||
const {Axios} = require("axios"); | ||
|
||
const leaderboardLock = new LockService() | ||
const cacheService = new NodeCache( | ||
{ | ||
stdTTL: 300, // use 5 min for all caches if not changed with ttl | ||
checkperiod: 600 // cleanup memory every 10 min | ||
} | ||
); | ||
|
||
module.exports = (javaApiBaseURL, token) => { | ||
const config = { | ||
baseURL: javaApiBaseURL, | ||
headers: {Authorization: `Bearer ${token}`} | ||
}; | ||
const javaApiClient = new Axios(config) | ||
|
||
return new LeaderboardService(cacheService, leaderboardLock, new LeaderboardRepository(javaApiClient)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
class LockoutTimeoutError extends Error { | ||
} | ||
|
||
class LockService { | ||
constructor() { | ||
this.queue = []; | ||
this.locked = false; | ||
} | ||
|
||
async lock(callback, timeLimitMS = 500) { | ||
let timeoutHandle; | ||
const lockHandler = {} | ||
|
||
const timeoutPromise = new Promise((resolve, reject) => { | ||
lockHandler.resolve = resolve | ||
lockHandler.reject = reject | ||
|
||
timeoutHandle = setTimeout( | ||
() => reject(new LockoutTimeoutError('LockService timeout reached')), | ||
timeLimitMS | ||
); | ||
}); | ||
|
||
const asyncPromise = new Promise((resolve, reject) => { | ||
if (this.locked) { | ||
lockHandler.resolve = resolve | ||
lockHandler.reject = reject | ||
|
||
this.queue.push(lockHandler); | ||
} else { | ||
this.locked = true; | ||
resolve(); | ||
} | ||
}); | ||
|
||
await Promise.race([asyncPromise, timeoutPromise]).then(async () => { | ||
clearTimeout(timeoutHandle); | ||
try { | ||
if (callback[Symbol.toStringTag] === 'AsyncFunction') { | ||
await callback() | ||
return | ||
} | ||
|
||
callback() | ||
} finally { | ||
this.release() | ||
} | ||
}).catch(e => { | ||
let index = this.queue.indexOf(lockHandler); | ||
|
||
if (index !== -1) { | ||
this.queue.splice(index, 1); | ||
} | ||
|
||
throw e | ||
}) | ||
} | ||
|
||
release() { | ||
if (this.queue.length > 0) { | ||
const queueItem = this.queue.shift(); | ||
queueItem.resolve(); | ||
} else { | ||
this.locked = false; | ||
} | ||
} | ||
} | ||
|
||
module.exports.LockService = LockService | ||
module.exports.LockoutTimeoutError = LockoutTimeoutError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.