Using champions alias instead of id in urls
All checks were successful
pipeline / build-and-push-images (push) Successful in 25s

This commit is contained in:
2024-11-24 12:13:00 +01:00
parent fc402120ba
commit 862e3f1b99
4 changed files with 27 additions and 13 deletions

View File

@@ -17,7 +17,7 @@ watch(searchText, (newT, oldT) => {
<input v-model="searchText" ref="searchBar" class="search-bar" type="text" placeholder="Champion name..."/> <input v-model="searchText" ref="searchBar" class="search-bar" type="text" placeholder="Champion name..."/>
</div> </div>
<div class="champion-container" style="margin-top: 20px;"> <div class="champion-container" style="margin-top: 20px;">
<RouterLink style="margin-left: 5px; margin-right: 5px;" v-for="champion in filteredChampions" :to="'/champion/' + champion.id"> <RouterLink style="margin-left: 5px; margin-right: 5px;" v-for="champion in filteredChampions" :to="'/champion/' + champion.alias.toLowerCase()">
<img :src="CDRAGON_BASE + mapPath(champion.squarePortraitPath)" :alt="champion.name"/> <img :src="CDRAGON_BASE + mapPath(champion.squarePortraitPath)" :alt="champion.name"/>
</RouterLink> </RouterLink>
</div> </div>

View File

@@ -1,11 +1,16 @@
<script setup> <script setup>
const route = useRoute() const route = useRoute()
const championId = route.params.id const championAlias = route.params.alias
const { data : championData } = await useFetch("/api/champion/" + championId) const { data : championData } = await useFetch("/api/champion/" + championAlias.toLowerCase())
const championId = championData.value.id
</script> </script>
<template> <template>
<Head>
<Title>{{ championData.name }} builds</Title>
</Head>
<div style="display: flex; width: fit-content; margin: auto;"> <div style="display: flex; width: fit-content; margin: auto;">
<div style="margin-top: 64px;"> <div style="margin-top: 64px;">
<ChampionTitle :champion-id="championId" :winrate="championData.winrate" :pickrate="championData.pickrate" :game-count="championData.gameCount" /> <ChampionTitle :champion-id="championId" :winrate="championData.winrate" :pickrate="championData.pickrate" :game-count="championData.gameCount" />

View File

@@ -12,19 +12,19 @@ async function fetchLatestPatch(client) {
const latestPatch = await patches.find().limit(1).sort({date:-1}).next() const latestPatch = await patches.find().limit(1).sort({date:-1}).next()
return latestPatch.patch return latestPatch.patch
} }
async function championInfos(client, patch, championId) { async function championInfos(client, patch, championAlias) {
const database = client.db("champions"); const database = client.db("champions");
const collection = database.collection(patch); const collection = database.collection(patch);
const query = { id:Number(championId) }; const query = { alias:championAlias };
const championInfo = await collection.findOne(query); const championInfo = await collection.findOne(query);
return championInfo return championInfo
} }
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const championId = getRouterParam(event, "id") const championAlias = getRouterParam(event, "alias").toLowerCase()
const client = await connectToDatabase(); const client = await connectToDatabase();
const latestPatch = await fetchLatestPatch(client); const latestPatch = await fetchLatestPatch(client);
const data = await championInfos(client, latestPatch, championId); const data = await championInfos(client, latestPatch, championAlias);
await client.close() await client.close()
return data return data
}) })

View File

@@ -29,8 +29,14 @@ type Builds = {
boots: Array<{data: number, count: number}> boots: Array<{data: number, count: number}>
lateGame: Array<{data: number, count: number}> lateGame: Array<{data: number, count: number}>
} }
type Champion = {
id: Number
name: String
alias: String
}
async function championInfos(client, patch: number, championId: number) { async function championInfos(client, patch: number, champion: Champion) {
const championId = champion.id
const database = client.db("matches"); const database = client.db("matches");
const matches = database.collection(patch) const matches = database.collection(patch)
const allMatches = matches.find() const allMatches = matches.find()
@@ -181,7 +187,9 @@ async function championInfos(client, patch: number, championId: number) {
builds.bootsFirst /= (winningMatches + losingMatches) builds.bootsFirst /= (winningMatches + losingMatches)
builds.lateGame.sort((a, b) => b.count - a.count) builds.lateGame.sort((a, b) => b.count - a.count)
return {id: championId, return {name: champion.name,
alias: champion.alias.toLowerCase(),
id: championId,
winrate:winningMatches / (winningMatches + losingMatches), winrate:winningMatches / (winningMatches + losingMatches),
gameCount:(winningMatches + losingMatches), gameCount:(winningMatches + losingMatches),
pickrate:(winningMatches + losingMatches)/totalMatches, pickrate:(winningMatches + losingMatches)/totalMatches,
@@ -190,8 +198,8 @@ async function championInfos(client, patch: number, championId: number) {
}; };
} }
async function makeChampionStat(client, patch, championId) { async function makeChampionStat(client, patch : number, champion : Champion) {
const championInfo = await championInfos(client, patch, championId) const championInfo = await championInfos(client, patch, champion)
const database = client.db("champions") const database = client.db("champions")
const collection = database.collection(patch) const collection = database.collection(patch)
await collection.updateOne({id: championInfo.id}, {$set: championInfo}, { upsert: true }) await collection.updateOne({id: championInfo.id}, {$set: championInfo}, { upsert: true })
@@ -203,7 +211,7 @@ async function championList() {
return list.slice(1) return list.slice(1)
} }
async function makeChampionsStats(client, patch) { async function makeChampionsStats(client, patch : number) {
var globalItems = await itemList() var globalItems = await itemList()
for(let item of globalItems) { for(let item of globalItems) {
itemDict.set(item.id, item) itemDict.set(item.id, item)
@@ -214,13 +222,14 @@ async function makeChampionsStats(client, patch) {
let i = 0; let i = 0;
for(let champion of list) { for(let champion of list) {
console.log("Entry " + i + "/" + list.length + " (" + champion.name + ")...") console.log("Entry " + i + "/" + list.length + " (" + champion.name + ")...")
await makeChampionStat(client, patch, champion.id) await makeChampionStat(client, patch, champion)
i += 1 i += 1
} }
const database = client.db("champions") const database = client.db("champions")
const collection = database.collection(patch) const collection = database.collection(patch)
await collection.createIndex({id:1}) await collection.createIndex({id:1})
await collection.createIndex({alias:1})
} }
export default {makeChampionsStats} export default {makeChampionsStats}