LeagueStats/client/src/views/Summoner.vue

319 lines
9.6 KiB
Vue
Raw Normal View History

2019-03-30 22:55:48 +00:00
<template>
<div>
2019-08-23 14:48:16 +00:00
<button @click="resetLocalStorage" class="debug"></button>
2019-03-30 22:55:48 +00:00
2019-04-10 20:05:52 +00:00
<header class="search mb-4 bg-teal-900 text-teal-100">
<div class="container mx-auto flex justify-between py-8">
2019-08-19 22:08:13 +00:00
<router-link
to="/"
class="flex items-center text-lg text-teal-100 mr-8 hover:text-teal-200"
>Accueil</router-link>
2019-04-10 20:05:52 +00:00
2019-08-19 22:08:13 +00:00
<SearchForm @formSubmit="redirect" />
2019-04-08 20:06:22 +00:00
<button
v-if="summonerFound"
2019-08-23 14:48:16 +00:00
@click="apiCall"
2019-08-19 22:08:13 +00:00
id="refresh"
2019-04-10 20:05:52 +00:00
class="input btn w-20 rounded-lg ml-2 relative"
2019-04-08 20:06:22 +00:00
:disabled="loading"
>
2019-08-19 22:08:13 +00:00
<v-icon name="sync" class="absolute vertical-center horizontal-center" />
2019-04-08 20:06:22 +00:00
</button>
2019-03-30 22:55:48 +00:00
</div>
2019-04-10 20:05:52 +00:00
</header>
2019-08-19 22:08:13 +00:00
<template v-if="summonerFound && !loading">
<div class="container mx-auto pb-16">
2019-04-07 17:44:01 +00:00
<div class="player bg-blue-100">
2019-08-19 22:08:13 +00:00
<div
class="player__pp"
:style="{background: `url(https://ddragon.leagueoflegends.com/cdn/${this.$patch}/img/profileicon/${localInfos.profileIconId}.png) center/cover`}"
></div>
<h1 class="player__name">{{ localInfos.name }}</h1>
<h3 class="player__level">{{ localInfos.level }}</h3>
<h3 class="player__rank">{{ localInfos.rank }}</h3>
2019-08-19 22:08:13 +00:00
<div
class="player__rank-img"
:style="{background: `url(${localInfos.rankImgLink}) center/cover`}"
></div>
<h3
class="player__ratio"
>{{ localInfos.rankedWins ? localInfos.rankedWins + ' wins / ' + localInfos.rankedLosses + ' losses' : "Joueur non classé" }}</h3>
2019-08-23 14:48:16 +00:00
<RecentActivity v-show="localInfos.allMatches" :matches="localInfos.allMatches" />
2019-03-30 22:55:48 +00:00
<ul class="list-matches--debug">
<Match
2019-08-19 22:08:13 +00:00
v-for="(match, index) in localInfos.matches"
:key="index"
:data="localInfos.matches[index]"
/>
</ul>
</div>
2019-03-30 22:55:48 +00:00
</div>
</template>
<template v-else-if="loading">
2019-08-19 22:08:13 +00:00
<div
class="flex items-center justify-center bg-white max-w-xs mx-auto p-5 rounded-lg shadow-xl"
>
2019-08-23 14:48:16 +00:00
<dot-loader :loading="loading" />
</div>
</template>
<template v-else>
<p>Le joueur est introuvable.</p>
</template>
2019-03-30 22:55:48 +00:00
</div>
</template>
<script>
2019-05-17 20:58:36 +00:00
// import itemsJSON from '@/data/item.json'
import summonersJSON from '@/data/summoner.json'
2019-08-23 14:48:16 +00:00
import RecentActivity from '@/components/RecentActivity.vue'
import Match from '@/components/Match.vue'
import SearchForm from '@/components/SearchForm.vue'
import { maps, gameModes } from '@/data/data.js'
import { timeDifference, secToTime, getRankImg } from '@/helpers/functions.js'
2019-03-30 22:55:48 +00:00
export default {
components: {
2019-04-08 20:06:22 +00:00
Match,
2019-08-19 22:08:13 +00:00
RecentActivity,
2019-04-08 20:06:22 +00:00
SearchForm
2019-03-30 22:55:48 +00:00
},
2019-08-23 14:48:16 +00:00
data() {
2019-03-30 22:55:48 +00:00
return {
2019-05-17 20:58:36 +00:00
championsInfos: [],
2019-03-30 22:55:48 +00:00
localInfos: {},
2019-08-19 22:08:13 +00:00
summonerFound: false,
2019-04-08 20:06:22 +00:00
loading: false,
regionsList: {
2019-08-19 22:08:13 +00:00
'br': 'br1',
'eune': 'eun1',
'euw': 'euw1',
2019-04-08 20:06:22 +00:00
'jp': 'jp1',
'kr': 'kr',
'lan': 'la1',
2019-08-19 22:08:13 +00:00
'las': 'la2',
2019-04-08 20:06:22 +00:00
'na': 'na1',
2019-08-19 22:08:13 +00:00
'oce': 'oc1',
2019-04-08 20:06:22 +00:00
'tr': 'tr1',
'ru': 'ru'
}
2019-08-23 14:48:16 +00:00
}
2019-03-30 22:55:48 +00:00
},
2019-08-23 14:48:16 +00:00
2019-04-07 17:44:01 +00:00
computed: {
2019-08-23 14:48:16 +00:00
summoner() {
2019-04-07 17:44:01 +00:00
return this.$route.params.name
2019-04-08 20:06:22 +00:00
},
2019-08-23 14:48:16 +00:00
region() {
2019-04-08 20:06:22 +00:00
return this.$route.params.region
2019-04-07 17:44:01 +00:00
}
},
2019-08-23 14:48:16 +00:00
watch: {
$route() {
console.log('route changed')
this.checkLocalStorage()
}
},
created: function () {
this.getChampionData()
2019-08-23 14:48:16 +00:00
},
mounted: function () {
this.checkLocalStorage()
},
2019-03-30 22:55:48 +00:00
methods: {
2019-08-31 17:19:48 +00:00
async apiCall() {
2019-08-23 14:48:16 +00:00
const summoner = this.summoner
const region = this.regionsList[this.region]
this.loading = true
2019-08-31 17:19:48 +00:00
try {
const resp = await this.$axios(({ url: 'api', data: { summoner, region }, method: 'POST' }))
if (resp.data) {
this.summonerFound = true
this.createObject(resp.data)
} else {
this.summonerFound = false
2019-08-23 14:48:16 +00:00
this.loading = false
this.$store.dispatch('notification/add', {
type: 'error',
message: 'Summoner not found.'
})
2019-08-31 17:19:48 +00:00
console.log('Summoner not found')
}
} catch (error) {
this.loading = false
console.log(error)
}
},
2019-08-23 14:48:16 +00:00
checkLocalStorage() {
2019-04-08 20:06:22 +00:00
if (localStorage[`${this.summoner}:${this.region}`]) {
console.log('cached')
this.summonerFound = true
2019-04-08 20:06:22 +00:00
this.localInfos = JSON.parse(localStorage[`${this.summoner}:${this.region}`])
} else {
this.apiCall()
}
},
2019-08-23 14:48:16 +00:00
createObject(JSONData) {
2019-05-17 20:58:36 +00:00
console.time('frontend')
console.log('--- ALL INFOS ---')
2019-08-23 14:48:16 +00:00
console.log(JSONData)
2019-03-30 22:55:48 +00:00
const userStats = JSONData.account
const soloQStats = JSONData.soloQ
const matches = JSONData.matchesDetails
2019-03-30 22:55:48 +00:00
2019-08-23 14:48:16 +00:00
const matchesInfos = []
2019-03-30 22:55:48 +00:00
// Loop on all matches
for (let i = 0; i < matches.length; i++) {
2019-08-23 14:48:16 +00:00
const currentMatch = matches[i]
const participantId = currentMatch.participantIdentities.find((p) => p.player.currentAccountId === userStats.accountId).participantId
2019-03-30 22:55:48 +00:00
2019-08-23 14:48:16 +00:00
const teamId = currentMatch.participants[participantId - 1].teamId
2019-04-05 20:41:32 +00:00
const win = currentMatch.teams.find((t) => t.teamId === teamId).win === 'Win'
2019-03-30 22:55:48 +00:00
2019-08-23 14:48:16 +00:00
const map = maps[currentMatch.mapId]
let mode = gameModes[currentMatch.queueId]
2019-03-30 22:55:48 +00:00
if (!mode)
2019-08-23 14:48:16 +00:00
mode = 'Undefined gamemode'
2019-05-17 20:58:36 +00:00
//console.log(Object.entries(this.championsInfos))
//console.log(this.championsInfos)
const champion = Object.entries(this.championsInfos).find(([, champion]) => Number(champion.key) === currentMatch.participants[participantId - 1].championId)[0]
//const champion = championsId[currentMatch.participants[participantId - 1].championId];
2019-08-23 14:48:16 +00:00
const role = currentMatch.participants[participantId - 1].timeline.lane
const timeAgo = timeDifference(currentMatch.gameCreation)
const time = secToTime(currentMatch.gameDuration)
const kills = currentMatch.participants[participantId - 1].stats.kills
const deaths = currentMatch.participants[participantId - 1].stats.deaths
const assists = currentMatch.participants[participantId - 1].stats.assists
const level = currentMatch.participants[participantId - 1].stats.champLevel
2019-03-30 22:55:48 +00:00
2019-08-23 14:48:16 +00:00
const items = []
2019-03-30 22:55:48 +00:00
for (let i = 0; i < 6; i++) {
2019-08-23 14:48:16 +00:00
const currentItem = 'item' + i
items.push(this.getItemLink(currentMatch.participants[participantId - 1].stats[currentItem]))
2019-03-30 22:55:48 +00:00
}
2019-08-23 14:48:16 +00:00
const gold = (currentMatch.participants[participantId - 1].stats.goldEarned / 1000).toFixed(1) + 'k'
const minions = currentMatch.participants[participantId - 1].stats.totalMinionsKilled + currentMatch.participants[participantId - 1].stats.neutralMinionsKilled
2019-03-30 22:55:48 +00:00
2019-08-23 14:48:16 +00:00
const firstSum = currentMatch.participants[participantId - 1].spell1Id
const secondSum = currentMatch.participants[participantId - 1].spell2Id
2019-03-30 22:55:48 +00:00
matchesInfos.push({
result: win,
map: map,
gamemode: mode,
champ: champion,
role: role,
date: timeAgo,
time: time,
kills: kills,
deaths: deaths,
assists: assists,
level: level,
items: items,
gold: gold,
minions: minions,
firstSum: this.getSummonerLink(firstSum),
secondSum: this.getSummonerLink(secondSum)
2019-08-23 14:48:16 +00:00
})
2019-04-05 20:41:32 +00:00
} // end loop matches
console.log('matches infos just below')
2019-08-23 14:48:16 +00:00
console.log(matchesInfos)
2019-03-30 22:55:48 +00:00
2019-08-19 22:08:13 +00:00
this.localInfos = {
2019-03-30 22:55:48 +00:00
accountId: userStats.accountId,
allMatches: JSONData.allMatches,
2019-03-30 22:55:48 +00:00
matches: matchesInfos,
profileIconId: userStats.profileIconId,
name: userStats.name,
level: userStats.summonerLevel,
rank: soloQStats ? soloQStats.tier + ' ' + soloQStats.rank : 'Joueur non classé',
rankImgLink: getRankImg(soloQStats),
2019-03-30 22:55:48 +00:00
rankedWins: soloQStats ? soloQStats.wins : undefined,
rankedLosses: soloQStats ? soloQStats.losses : undefined
}
2019-08-23 14:48:16 +00:00
console.log('====== Saved infos ======')
console.log(this.localInfos)
2019-03-30 22:55:48 +00:00
2019-08-23 14:48:16 +00:00
localStorage[`${this.summoner}:${this.region}`] = JSON.stringify(this.localInfos)
2019-05-17 20:58:36 +00:00
console.timeEnd('frontend')
2019-08-23 14:48:16 +00:00
this.loading = false
2019-08-19 22:08:13 +00:00
2019-05-17 20:58:36 +00:00
},
2019-08-31 17:19:48 +00:00
async getChampionData() {
2019-05-17 20:58:36 +00:00
console.log('API CALL FOR CHAMPIONS')
const endpoint = 'Champion'
2019-08-31 17:19:48 +00:00
const resp = await this.$axios(({ url: 'ddragon', data: { endpoint }, method: 'POST' }))
this.championsInfos = resp.data.data
2019-03-30 22:55:48 +00:00
},
2019-08-23 14:48:16 +00:00
getItemLink(id) {
return `url('https://ddragon.leagueoflegends.com/cdn/${this.$patch}/img/item/${id === 0 ? 3637 : id}.png') no-repeat center center / contain`
},
2019-08-23 14:48:16 +00:00
getSummonerLink(id) {
2019-04-05 20:41:32 +00:00
const spellName = Object.entries(summonersJSON.data).find(([, spell]) => Number(spell.key) === id)[0]
2019-08-23 14:48:16 +00:00
return `https://ddragon.leagueoflegends.com/cdn/${this.$patch}/img/spell/${spellName}.png`
},
2019-08-23 14:48:16 +00:00
redirect(summoner, region) {
2019-04-08 20:06:22 +00:00
this.$router.push(`/summoner/${region}/${summoner}`)
2019-03-30 22:55:48 +00:00
},
2019-08-23 14:48:16 +00:00
resetLocalStorage() {
console.log('CLEAR LOCALSTORAGE')
localStorage.clear()
2019-03-30 22:55:48 +00:00
}
}
2019-08-23 14:48:16 +00:00
}
2019-03-30 22:55:48 +00:00
</script>
<style scoped>
.debug {
position: absolute;
right: 0;
top: 0;
width: 40px;
height: 40px;
border: none;
z-index: 9999999999;
}
.debug:hover {
background: #ef5753;
}
.player {
text-align: center;
margin: 16px auto 0;
border: 1px solid #ebebeb;
padding: 16px;
/* background: #fff; */
2019-03-30 22:55:48 +00:00
}
.player__pp {
width: 75px;
height: 75px;
background: #ebebeb;
margin: 0 auto;
}
.player__rank-img {
width: 75px;
height: 75px;
background: #ebebeb;
margin: 0 auto;
}
.list-matches {
list-style-type: none;
padding: 0;
}
</style>