diff --git a/server/app/Commands/EditDetailedMatch.js b/server/app/Commands/EditDetailedMatch.js index 1c8c717..cd385ae 100644 --- a/server/app/Commands/EditDetailedMatch.js +++ b/server/app/Commands/EditDetailedMatch.js @@ -3,7 +3,7 @@ const { Command } = require('@adonisjs/ace') const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer') const Database = use('Database') -const Jax = use('Jax') +const Jax = use('App/Services/Jax') const DetailedMatch = use('App/Models/DetailedMatch') const Queue = use('Bee/Queue') diff --git a/server/app/Commands/EditMatch.js b/server/app/Commands/EditMatch.js index 721e682..c85b161 100644 --- a/server/app/Commands/EditMatch.js +++ b/server/app/Commands/EditMatch.js @@ -3,7 +3,7 @@ const { Command } = require('@adonisjs/ace') const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer') const Database = use('Database') -const Jax = use('Jax') +const Jax = use('App/Services/Jax') const Match = use('App/Models/Match') const Queue = use('Bee/Queue') diff --git a/server/app/Controllers/Http/MatchController.js b/server/app/Controllers/Http/MatchController.js index 36d59e2..adef6e0 100644 --- a/server/app/Controllers/Http/MatchController.js +++ b/server/app/Controllers/Http/MatchController.js @@ -1,6 +1,6 @@ 'use strict' -const Jax = use('Jax') +const Jax = use('App/Services/Jax') const DetailedMatch = use('App/Models/DetailedMatch') const DetailedMatchTransformer = use('App/Transformers/DetailedMatchTransformer') const MatchService = use('App/Services/MatchService') diff --git a/server/app/Controllers/Http/SummonerController.js b/server/app/Controllers/Http/SummonerController.js index e210ca3..0c189d5 100644 --- a/server/app/Controllers/Http/SummonerController.js +++ b/server/app/Controllers/Http/SummonerController.js @@ -1,6 +1,6 @@ 'use strict' -const Jax = use('Jax') +const Jax = use('App/Services/Jax') const LiveMatchTransformer = use('App/Transformers/LiveMatchTransformer') const MatchRepository = make('App/Repositories/MatchRepository') const MatchService = use('App/Services/MatchService') diff --git a/server/providers/Jax/JaxConfig.js b/server/app/Services/Jax/JaxConfig.js similarity index 100% rename from server/providers/Jax/JaxConfig.js rename to server/app/Services/Jax/JaxConfig.js diff --git a/server/app/Services/Jax/index.js b/server/app/Services/Jax/index.js new file mode 100644 index 0000000..342706c --- /dev/null +++ b/server/app/Services/Jax/index.js @@ -0,0 +1,4 @@ +const Jax = require('./src/Jax') +const Config = require('./JaxConfig') + +module.exports = new Jax(Config) diff --git a/server/providers/Jax/src/CDragonRequest.js b/server/app/Services/Jax/src/CDragonRequest.js similarity index 64% rename from server/providers/Jax/src/CDragonRequest.js rename to server/app/Services/Jax/src/CDragonRequest.js index 29b298a..d2466bf 100644 --- a/server/providers/Jax/src/CDragonRequest.js +++ b/server/app/Services/Jax/src/CDragonRequest.js @@ -1,8 +1,10 @@ const got = require('got') +const Redis = use('Redis') class CDragonRequest { - constructor(endpoint) { + constructor(endpoint, cacheTime) { this.endpoint = endpoint + this.cacheTime = cacheTime } // https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/items.json @@ -11,10 +13,17 @@ class CDragonRequest { // https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-summary.json async execute() { - let url = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/${this.endpoint}` + const url = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/${this.endpoint}` + + const requestCached = await Redis.get(url) + if (requestCached) { + return JSON.parse(requestCached) + } try { const response = await got(url); + + await Redis.set(url, response.body, 'EX', this.cacheTime) return JSON.parse(response.body) } catch (error) { console.log(error.response.body); diff --git a/server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js b/server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js new file mode 100644 index 0000000..3a020f3 --- /dev/null +++ b/server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js @@ -0,0 +1,25 @@ +const CDragonRequest = require('../CDragonRequest') + +class CDragonEndpoint { + champions() { + return new CDragonRequest('champion-summary.json', 36000).execute() + } + + items() { + return new CDragonRequest('items.json', 36000).execute() + } + + perks() { + return new CDragonRequest('perks.json', 36000).execute() + } + + perkstyles() { + return new CDragonRequest('perkstyles.json', 36000).execute() + } + + summonerSpells() { + return new CDragonRequest('summoner-spells.json', 36000).execute() + } +} + +module.exports = CDragonEndpoint diff --git a/server/providers/Jax/src/Endpoints/LeagueEndpoint.js b/server/app/Services/Jax/src/Endpoints/LeagueEndpoint.js similarity index 92% rename from server/providers/Jax/src/Endpoints/LeagueEndpoint.js rename to server/app/Services/Jax/src/Endpoints/LeagueEndpoint.js index e74a6c0..db19fb8 100644 --- a/server/providers/Jax/src/Endpoints/LeagueEndpoint.js +++ b/server/app/Services/Jax/src/Endpoints/LeagueEndpoint.js @@ -11,7 +11,8 @@ class LeagueEndpoint { region, this.config, `league/v4/entries/by-summoner/${summonerID}`, - this.limiter + this.limiter, + 300 ).execute() } } diff --git a/server/providers/Jax/src/Endpoints/MatchEndpoint.js b/server/app/Services/Jax/src/Endpoints/MatchEndpoint.js similarity index 92% rename from server/providers/Jax/src/Endpoints/MatchEndpoint.js rename to server/app/Services/Jax/src/Endpoints/MatchEndpoint.js index 2be0e22..9cce2c9 100644 --- a/server/providers/Jax/src/Endpoints/MatchEndpoint.js +++ b/server/app/Services/Jax/src/Endpoints/MatchEndpoint.js @@ -13,7 +13,8 @@ class MatchEndpoint { region, this.config, `match/v4/matches/${matchID}`, - this.limiter + this.limiter, + 1500 ).execute() } } diff --git a/server/providers/Jax/src/Endpoints/MatchlistEndpoint.js b/server/app/Services/Jax/src/Endpoints/MatchlistEndpoint.js similarity index 92% rename from server/providers/Jax/src/Endpoints/MatchlistEndpoint.js rename to server/app/Services/Jax/src/Endpoints/MatchlistEndpoint.js index 13dbf25..9d6df7c 100644 --- a/server/providers/Jax/src/Endpoints/MatchlistEndpoint.js +++ b/server/app/Services/Jax/src/Endpoints/MatchlistEndpoint.js @@ -11,7 +11,8 @@ class MatchlistEndpoint { region, this.config, `match/v4/matchlists/by-account/${accountID}?beginIndex=${beginIndex}`, - this.limiter + this.limiter, + 1500 ).execute() } } diff --git a/server/providers/Jax/src/Endpoints/SpectatorEndpoint.js b/server/app/Services/Jax/src/Endpoints/SpectatorEndpoint.js similarity index 93% rename from server/providers/Jax/src/Endpoints/SpectatorEndpoint.js rename to server/app/Services/Jax/src/Endpoints/SpectatorEndpoint.js index b21c39d..cf73bbd 100644 --- a/server/providers/Jax/src/Endpoints/SpectatorEndpoint.js +++ b/server/app/Services/Jax/src/Endpoints/SpectatorEndpoint.js @@ -11,7 +11,8 @@ class SpectatorEndpoint { region, this.config, `spectator/v4/active-games/by-summoner/${summonerID}`, - this.limiter + this.limiter, + 0 ).execute() } } diff --git a/server/providers/Jax/src/Endpoints/SummonerEndpoint.js b/server/app/Services/Jax/src/Endpoints/SummonerEndpoint.js similarity index 92% rename from server/providers/Jax/src/Endpoints/SummonerEndpoint.js rename to server/app/Services/Jax/src/Endpoints/SummonerEndpoint.js index 58e39fc..3d88eda 100644 --- a/server/providers/Jax/src/Endpoints/SummonerEndpoint.js +++ b/server/app/Services/Jax/src/Endpoints/SummonerEndpoint.js @@ -11,7 +11,8 @@ class SummonerEndpoint { region, this.config, `summoner/v4/summoners/by-name/${encodeURI(summonerName)}`, - this.limiter + this.limiter, + 36000 ).execute() } } diff --git a/server/providers/Jax/src/Jax.js b/server/app/Services/Jax/src/Jax.js similarity index 100% rename from server/providers/Jax/src/Jax.js rename to server/app/Services/Jax/src/Jax.js diff --git a/server/providers/Jax/src/JaxRequest.js b/server/app/Services/Jax/src/JaxRequest.js similarity index 64% rename from server/providers/Jax/src/JaxRequest.js rename to server/app/Services/Jax/src/JaxRequest.js index 07461db..81fe23d 100644 --- a/server/providers/Jax/src/JaxRequest.js +++ b/server/app/Services/Jax/src/JaxRequest.js @@ -1,26 +1,40 @@ const { promisify } = require('util') +const Redis = use('Redis') class JaxRequest { - constructor(region, config, endpoint, limiter) { + constructor(region, config, endpoint, limiter, cacheTime) { this.region = region this.config = config this.endpoint = endpoint this.limiter = limiter + this.cacheTime = cacheTime this.retries = config.requestOptions.retriesBeforeAbort this.sleep = promisify(setTimeout) } async execute() { + const url = `https://${this.region}.api.riotgames.com/lol/${this.endpoint}` + + // Redis cache + if (this.cacheTime > 0) { + const requestCached = await Redis.get(url) + if (requestCached) { + return JSON.parse(requestCached) + } + } + try { const resp = await this.limiter.executing({ - url: `https://${this.region}.api.riotgames.com/lol/${this.endpoint}`, + url, token: this.config.key, resolveWithFullResponse: false }) + if (this.cacheTime > 0) { + await Redis.set(url, resp, 'EX', this.cacheTime) + } return JSON.parse(resp) - } catch ({ statusCode, ...rest }) { this.retries-- diff --git a/server/app/Services/MatchService.js b/server/app/Services/MatchService.js index 6cf1c86..53c801d 100644 --- a/server/app/Services/MatchService.js +++ b/server/app/Services/MatchService.js @@ -1,7 +1,7 @@ 'use strict' const Logger = use('Logger') -const Jax = use('Jax') +const Jax = use('App/Services/Jax') const BasicMatchTransformer = use('App/Transformers/BasicMatchTransformer') const { getSeasonNumber } = use('App/helpers') diff --git a/server/app/Services/SummonerService.js b/server/app/Services/SummonerService.js index 7f145e1..9f33d32 100644 --- a/server/app/Services/SummonerService.js +++ b/server/app/Services/SummonerService.js @@ -1,7 +1,6 @@ 'use strict' -const Jax = use('Jax') -const Redis = use('Redis') +const Jax = use('App/Services/Jax') class SummonerService { constructor() { @@ -28,16 +27,7 @@ class SummonerService { */ async getAccount(summonerName, region) { const name = summonerName.toLowerCase().replace(/ /g, '') - const accountCache = await Redis.get(`${region}-${name}`) - if (accountCache) { - console.log('ACCOUNT CACHED') - return JSON.parse(accountCache) - } - const account = await Jax.Summoner.summonerName(name, region) - if (account) { - await Redis.set(`${region}-${name}`, JSON.stringify(account), 'EX', 36000) - } return account } @@ -47,19 +37,12 @@ class SummonerService { * @param region */ async getRanked(account, region) { - const rankedCache = await Redis.get(`ranked-${account.puuid}`) - if (rankedCache) { - console.log('RANKED CACHED') - return JSON.parse(rankedCache) - } - const ranked = await Jax.League.summonerID(account.id, region) const result = { soloQ: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_SOLO_5x5')) || null, flex5v5: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_FLEX_SR')) || null, flex3v3: this._getleagueData(ranked.find(e => e.queueType === 'RANKED_FLEX_TT')) || null } - await Redis.set(`ranked-${account.puuid}`, JSON.stringify(result), 'EX', 1500) return result } } diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js index cb7d798..ff00ec5 100644 --- a/server/app/Transformers/MatchTransformer.js +++ b/server/app/Transformers/MatchTransformer.js @@ -1,6 +1,6 @@ 'use strict' -const Jax = use('Jax') +const Jax = use('App/Services/Jax') const Helpers = use('App/helpers') /** diff --git a/server/providers/Jax/JaxProvider.js b/server/providers/Jax/JaxProvider.js deleted file mode 100644 index 7118055..0000000 --- a/server/providers/Jax/JaxProvider.js +++ /dev/null @@ -1,17 +0,0 @@ -const { ServiceProvider } = require('@adonisjs/fold') -const Jax = require('./src/Jax') -const Config = require('./JaxConfig') - -class JaxProvider extends ServiceProvider { - register () { - this.app.singleton('Jax', () => { - return new Jax(Config) - }) - } - - boot() { - use('Jax') - } -} - -module.exports = JaxProvider \ No newline at end of file diff --git a/server/providers/Jax/src/Endpoints/CDragonEndpoint.js b/server/providers/Jax/src/Endpoints/CDragonEndpoint.js deleted file mode 100644 index 829ceea..0000000 --- a/server/providers/Jax/src/Endpoints/CDragonEndpoint.js +++ /dev/null @@ -1,25 +0,0 @@ -const CDragonRequest = require('../CDragonRequest') - -class CDragonEndpoint { - champions() { - return new CDragonRequest('champion-summary.json').execute() - } - - items() { - return new CDragonRequest('items.json').execute() - } - - perks() { - return new CDragonRequest('perks.json').execute() - } - - perkstyles() { - return new CDragonRequest('perkstyles.json').execute() - } - - summonerSpells() { - return new CDragonRequest('summoner-spells.json').execute() - } -} - -module.exports = CDragonEndpoint diff --git a/server/start/app.js b/server/start/app.js index ebb5353..626c1f3 100644 --- a/server/start/app.js +++ b/server/start/app.js @@ -19,7 +19,6 @@ const providers = [ 'lucid-mongo/providers/LucidMongoProvider', '@adonisjs/redis/providers/RedisProvider', - join(__dirname, '../providers/Jax/JaxProvider'), join(__dirname, '../providers/Queue/Provider') ]