refactor: start using Cdragon instead of DDragon

Done Endpoints: Items and Perks
This commit is contained in:
Valentin Kaelin 2019-11-23 17:34:34 +01:00
parent 57e2fad45d
commit 8f19e570b7
4 changed files with 68 additions and 19 deletions

View file

@ -12,14 +12,16 @@ class MatchTransformer {
* Get global Context with DDragon Data * Get global Context with DDragon Data
*/ */
async getContext() { async getContext() {
const items = await Jax.DDragon.Item.list() const items = await Jax.CDragon.items()
const champions = await Jax.DDragon.Champion.list() const champions = await Jax.DDragon.Champion.list()
const runes = await Jax.DDragon.Rune.list() const perks = await Jax.CDragon.perks()
const perkstyles = await Jax.CDragon.perkstyles()
const version = Jax.DDragon.Version const version = Jax.DDragon.Version
this.champions = champions.data this.champions = champions.data
this.items = items.data this.items = items
this.runes = runes this.perks = perks
this.perkstyles = perkstyles.styles
this.version = version this.version = version
} }
@ -92,20 +94,16 @@ class MatchTransformer {
stats.kp = totalKills === 0 ? 0 : +((stats.kills + stats.assists) * 100 / totalKills).toFixed(1) stats.kp = totalKills === 0 ? 0 : +((stats.kills + stats.assists) * 100 / totalKills).toFixed(1)
} }
let primaryRune = null let primaryRune = null
let secondaryRune = null let secondaryRune = null
if (player.stats.perkPrimaryStyle) { if (player.stats.perkPrimaryStyle) {
const primaryRuneCategory = this.runes.find(r => r.id === player.stats.perkPrimaryStyle) const firstRune = this.perks.find(p => p.id === player.stats.perk0)
for (const subCat of primaryRuneCategory.slots) { const firstRuneUrl = firstRune.iconPath.split('/assets/')[1].toLowerCase()
primaryRune = subCat.runes.find(r => r.id === player.stats.perk0) primaryRune = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${firstRuneUrl}`
if (primaryRune) {
break const secondRuneStyle = this.perkstyles.find(p => p.id === player.stats.perkSubStyle)
} const secondRuneStyleUrl = secondRuneStyle.iconPath.split('/assets/')[1].toLowerCase()
} secondaryRune = `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${secondRuneStyleUrl}`
primaryRune = `https://ddragon.leagueoflegends.com/cdn/img/${primaryRune.icon}`
secondaryRune = this.runes.find(r => r.id === player.stats.perkSubStyle)
secondaryRune = `https://ddragon.leagueoflegends.com/cdn/img/${secondaryRune.icon}`
} }
const items = [] const items = []
@ -115,11 +113,15 @@ class MatchTransformer {
items.push(null) items.push(null)
continue continue
} }
const item = this.items.find(i => i.id === id)
const itemUrl = item.iconPath.split('/assets/')[1].toLowerCase()
items.push({ items.push({
image: `https://ddragon.leagueoflegends.com/cdn/${this.version}/img/item/${id}.png`, image: `https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/${itemUrl}`,
name: this.items[id].name, name: item.name,
description: this.items[id].description, description: item.description,
price: this.items[id].gold.total price: item.priceTotal
}) })
} }

View file

@ -0,0 +1,26 @@
const got = require('got')
class CDragonRequest {
constructor(endpoint) {
this.endpoint = endpoint
}
// https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/items.json
// https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/perks.json
// https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/perkstyles.json
// 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}`
try {
const response = await got(url);
return JSON.parse(response.body)
} catch (error) {
console.log(error.response.body);
}
}
}
module.exports = CDragonRequest

View file

@ -0,0 +1,17 @@
const CDragonRequest = require('../CDragonRequest')
class CDragonEndpoint {
items() {
return new CDragonRequest('items.json').execute()
}
perks() {
return new CDragonRequest('perks.json').execute()
}
perkstyles() {
return new CDragonRequest('perkstyles.json').execute()
}
}
module.exports = CDragonEndpoint

View file

@ -10,6 +10,8 @@ const DDragonChampionEndpoint = require('./Endpoints/DDragonEndpoints/DDragonCha
const DDragonRuneEndpoint = require('./Endpoints/DDragonEndpoints/DDragonRuneEndpoint') const DDragonRuneEndpoint = require('./Endpoints/DDragonEndpoints/DDragonRuneEndpoint')
const DDragonItemEndpoint = require('./Endpoints/DDragonEndpoints/DDragonItemEndpoint') const DDragonItemEndpoint = require('./Endpoints/DDragonEndpoints/DDragonItemEndpoint')
const CDragonEndpoint = require('./Endpoints/CDragonEndpoint')
class Jax { class Jax {
constructor(config) { constructor(config) {
this.key = config.key this.key = config.key
@ -26,6 +28,8 @@ class Jax {
this.Summoner = new SummonerEndpoint(this.config, this.limiter) this.Summoner = new SummonerEndpoint(this.config, this.limiter)
this.initDDragon() this.initDDragon()
this.CDragon = new CDragonEndpoint()
} }
async initDDragon() { async initDDragon() {