gold
-
rank-silver
+
+
+
+
+
+
+
+
+
diff --git a/client/src/helpers/summoner.js b/client/src/helpers/summoner.js
index d27c2ff..7fd4ffe 100644
--- a/client/src/helpers/summoner.js
+++ b/client/src/helpers/summoner.js
@@ -2,7 +2,6 @@ import { timeDifference } from '@/helpers/functions.js'
import { maps, gameModes } from '@/data/data.js'
import summonersJSON from '@/data/summoner.json'
-const uniqueLeagues = ['CHALLENGER', 'GRANDMASTER', 'MASTER']
const leaguesNumbers = { 'I': 1, 'II': 2, 'III': 3, 'IV': 4 }
/**
@@ -87,9 +86,7 @@ export function createSummonerData(RiotData) {
function getLeagueData(leagueData, leagueName) {
if (!leagueData) return null
- leagueData.fullRank = uniqueLeagues.includes(leagueData.tier) ? leagueData.tier : `${leagueData.tier} ${leagueData.rank}`
leagueData.rankImgLink = getRankImg(leagueData)
- leagueData.winrate = +(leagueData.wins * 100 / (leagueData.wins + leagueData.losses)).toFixed(1) + '%'
leagueData.name = leagueName
return leagueData
}
diff --git a/server/app/Controllers/Http/MatchController.js b/server/app/Controllers/Http/MatchController.js
index aad5b65..47e25d7 100644
--- a/server/app/Controllers/Http/MatchController.js
+++ b/server/app/Controllers/Http/MatchController.js
@@ -33,19 +33,28 @@ class MatchController {
* POST - Return details data for one specific match
*/
async show({ request, response }) {
+ console.time('MatchDetails')
console.log('Match details request')
const gameId = request.input('gameId')
const region = request.input('region')
console.log(gameId, region)
- let matchFromRiot = await Jax.Match.get(gameId)
+ let matchDetails = {}
+ const alreadySaved = await DetailedMatch.where({ gameId, region }).first()
+ if (alreadySaved) {
+ console.log('MATCH DETAILS ALREADY SAVED')
+ matchDetails = alreadySaved
+ } else {
+ matchDetails = await Jax.Match.get(gameId)
+ matchDetails = await DetailedMatchTransformer.transform(matchDetails)
+ await DetailedMatch.create(matchDetails)
+ }
- matchFromRiot = await DetailedMatchTransformer.transform(matchFromRiot)
- // DetailedMatch.save(matchFromRiot)
+ console.timeEnd('MatchDetails')
return response.json({
- matchDetails: matchFromRiot
+ matchDetails
})
}
}
diff --git a/server/app/Controllers/Http/SummonerController.js b/server/app/Controllers/Http/SummonerController.js
index 7921026..bd0a761 100644
--- a/server/app/Controllers/Http/SummonerController.js
+++ b/server/app/Controllers/Http/SummonerController.js
@@ -25,7 +25,7 @@ class SummonerController {
Jax.regionName = region
try {
- const account = await Jax.Summoner.summonerName(summoner)
+ const account = await SummonerService.getAccount(summoner, region)
// Check if the summoner is found
if (!account) return response.json(null)
account.region = region
diff --git a/server/app/Services/SummonerService.js b/server/app/Services/SummonerService.js
index b824413..c26a8e9 100644
--- a/server/app/Services/SummonerService.js
+++ b/server/app/Services/SummonerService.js
@@ -4,21 +4,62 @@ const Jax = use('Jax')
const Redis = use('Redis')
class SummonerService {
+ constructor() {
+ this.uniqueLeagues = ['CHALLENGER', 'GRANDMASTER', 'MASTER']
+ this.leaguesNumbers = { 'I': 1, 'II': 2, 'III': 3, 'IV': 4 }
+ }
+
+ /**
+ * Helper to transform League Data from the Riot API
+ * @param league raw data of the league from Riot API
+ */
+ _getleagueData(league) {
+ if (!league) return null
+ league.fullRank = this.uniqueLeagues.includes(league.tier) ? league.tier : `${league.tier} ${league.rank}`
+ league.winrate = +(league.wins * 100 / (league.wins + league.losses)).toFixed(1) + '%'
+ league.shortName = this.uniqueLeagues.includes(league.tier) ? league.leaguePoints : league.tier[0] + this.leaguesNumbers[league.rank]
+ return league
+ }
+
+ /**
+ * Get account infos for a searched summoner name
+ * @param summonerName
+ * @param region
+ */
+ 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)
+ if (account) {
+ await Redis.set(`${region}-${name}`, JSON.stringify(account), 'EX', 36000)
+ }
+ return account
+ }
+
+ /**
+ * Get ranked data for a specific Summoner
+ * @param account
+ */
async getRanked(account) {
const rankedCache = await Redis.get(`ranked-${account.puuid}`)
if (rankedCache) {
console.log('RANKED CACHED')
return JSON.parse(rankedCache)
- } else {
- const ranked = await Jax.League.summonerID(account.id)
- const result = {
- soloQ: ranked.find(e => e.queueType === 'RANKED_SOLO_5x5') || null,
- flex5v5: ranked.find(e => e.queueType === 'RANKED_FLEX_SR') || null,
- flex3v3: ranked.find(e => e.queueType === 'RANKED_FLEX_TT') || null
- }
- await Redis.set(`ranked-${account.puuid}`, JSON.stringify(result), 'EX', 10)
- return result
}
+
+ const ranked = await Jax.League.summonerID(account.id)
+ 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/DetailedMatchTransformer.js b/server/app/Transformers/DetailedMatchTransformer.js
index 4cef473..534e0d9 100644
--- a/server/app/Transformers/DetailedMatchTransformer.js
+++ b/server/app/Transformers/DetailedMatchTransformer.js
@@ -1,6 +1,7 @@
'use strict'
const MatchTransformer = use('App/Transformers/MatchTransformer')
+const SummonerService = use('App/Services/SummonerService')
/**
* DetailedMatchTransformer class
@@ -19,8 +20,8 @@ class DetailedMatchTransformer extends MatchTransformer {
const globalInfos = super.getGameInfos(match)
// Teams
- const firstTeam = this.getTeamData(match, match.teams[0])
- const secondTeam = this.getTeamData(match, match.teams[1])
+ const firstTeam = await this.getTeamData(match, match.teams[0])
+ const secondTeam = await this.getTeamData(match, match.teams[1])
return {
gameId: match.gameId,
@@ -36,7 +37,7 @@ class DetailedMatchTransformer extends MatchTransformer {
* @param match raw match data from Riot API
* @param team raw team data from Riot API
*/
- getTeamData(match, team) {
+ async getTeamData(match, team) {
let win = team.win
if (match.gameDuration < 300) {
win = 'Remake'
@@ -72,10 +73,13 @@ class DetailedMatchTransformer extends MatchTransformer {
}
// Players
- const players = teamPlayers
+ let players = teamPlayers
.map(p => super.getPlayerData(match, p, true, teamStats))
.sort(this.sortTeamByRole)
+ const requests = players.map(p => this.getPlayerRank(p, match.platformId))
+ players = await Promise.all(requests)
+
return {
bans,
barons: team.baronKills,
@@ -89,6 +93,23 @@ class DetailedMatchTransformer extends MatchTransformer {
towers: team.towerKills,
}
}
+
+ /**
+ * Get the soloQ rank of all the players of the team
+ * @param summoner all the data of the summoner
+ * @param region of the match
+ */
+ async getPlayerRank(summoner, region) {
+ const account = await SummonerService.getAccount(summoner.name, region)
+ if (account) {
+ const ranked = await SummonerService.getRanked(account)
+ summoner.rank = ranked.soloQ ? (({ tier, shortName }) => ({ tier, shortName }))(ranked.soloQ) : null
+ } else {
+ summoner.rank = null
+ }
+
+ return summoner
+ }
}
module.exports = new DetailedMatchTransformer()
diff --git a/server/app/Transformers/MatchTransformer.js b/server/app/Transformers/MatchTransformer.js
index fda5cf0..e868486 100644
--- a/server/app/Transformers/MatchTransformer.js
+++ b/server/app/Transformers/MatchTransformer.js
@@ -44,6 +44,7 @@ class MatchTransformer {
map: match.mapId,
gamemode: match.queueId,
date: match.gameCreation,
+ region: match.platformId.toLowerCase(),
time: match.gameDuration
}
}