From d57e097fec046089af9a7505d5aed768f11c5811 Mon Sep 17 00:00:00 2001 From: jayson237 Date: Mon, 18 Nov 2024 10:07:44 +0800 Subject: [PATCH] update tier list and container --- app/wiki/tier-list/page.tsx | 6 +- .../wiki/tier-list/tier-list-container.tsx | 25 +++---- lib/actions/getHeroStats.ts | 66 ------------------- lib/actions/getHeroTier.ts | 49 ++++++++++++++ lib/mongoose/schema/heroes-tier.ts | 51 ++++++++++++++ 5 files changed, 113 insertions(+), 84 deletions(-) create mode 100644 lib/actions/getHeroTier.ts create mode 100644 lib/mongoose/schema/heroes-tier.ts diff --git a/app/wiki/tier-list/page.tsx b/app/wiki/tier-list/page.tsx index f7e84a91..9688a101 100644 --- a/app/wiki/tier-list/page.tsx +++ b/app/wiki/tier-list/page.tsx @@ -8,6 +8,7 @@ import { Hero, NewHero } from "@prisma/client"; import { TabsContent } from "@/components/shared/tabs"; import TierContainer from "@/components/wiki/tier-list/tier-list-container"; import { defaultOpenGraphMD, defaultTwitterMD } from "@/lib/configs/metadata"; +import { getHeroTierWithNames } from "@/lib/actions/getHeroTier"; export const metadata: Metadata = { title: "Tier List", @@ -29,7 +30,10 @@ export const metadata: Metadata = { }; async function TierListPage() { - const heroes: Hero[] | null = await getOldOldHeroes(); + const heroes = await getHeroTierWithNames({ + select: + "-_id -combinedScore -currentMetaScore -currentMetaStats -tournamentScore -tournamentStats -updatedAt", + }); return ( - tiers.some((tier) => { - if (tier.tier === "OP" && hero.tier === "SS") { - return true; - } - return tier.tier === hero.tier; - }) - ); return (

@@ -36,7 +27,7 @@ export default function TierContainer({ heroes }: TierListProps) {

{tiers.map((item, i) => { const filteredHeroes = heroes?.filter((hero) => - hero.tier === "SS" ? item.tier === "OP" : hero.tier === item.tier + hero.tier === "S" ? item.tier === "S" : hero.tier === item.tier ); return ( diff --git a/lib/actions/getHeroStats.ts b/lib/actions/getHeroStats.ts index e794c865..15106a96 100644 --- a/lib/actions/getHeroStats.ts +++ b/lib/actions/getHeroStats.ts @@ -2,72 +2,6 @@ import clientPromise from "@/lib/mongoose"; import HeroStats from "@/lib/mongoose/schema/heroes-statistics"; import { Types } from "mongoose"; -// export default async function getHeroStats(heroId: string) { -// try { -// await clientPromise("game-core"); - -// const heroStatsData = await HeroStats.aggregate([ -// { -// $match: { -// heroId: new Types.ObjectId(heroId), -// }, -// }, -// { -// $sort: { -// createdAt: -1, -// }, -// }, -// { -// $limit: 1, -// }, -// { -// $unwind: "$data", -// }, -// { -// $match: { -// "data.rankName": "all", -// }, -// }, -// { $unwind: "$data.counters" }, -// { -// $lookup: { -// from: "Heroes", -// let: { heroId: "$data.counters.heroId" }, -// pipeline: [ -// { $match: { $expr: { $eq: ["$_id", "$$heroId"] } } }, -// { $project: { heroName: 1 } }, -// ], -// as: "heroData", -// }, -// }, -// { -// $addFields: { -// "data.counters.heroName": { $arrayElemAt: ["$heroData.heroName", 0] }, -// }, -// }, -// { -// $unset: [ -// "_id", -// "heroData", -// "data.counters.heroId", -// "data.countersLast.heroId", -// ], -// }, -// { -// $group: { -// _id: "$_id", -// data: { $push: "$data" }, -// }, -// }, -// ]); - -// return heroStatsData; -// } catch (error) { -// console.error("Error", error); -// throw new Error("Failed to fetch hero stats"); -// } -// } - export default async function getHeroStats(heroId: string) { try { await clientPromise("game-core"); diff --git a/lib/actions/getHeroTier.ts b/lib/actions/getHeroTier.ts new file mode 100644 index 00000000..b518a060 --- /dev/null +++ b/lib/actions/getHeroTier.ts @@ -0,0 +1,49 @@ +import clientPromise from "@/lib/mongoose"; +import HeroTier from "@/lib/mongoose/schema/heroes-tier"; + +export async function getHeroTier({ select }: { select?: string }) { + try { + await clientPromise("game-core"); + const heroesData = await HeroTier.find() + .select(select || "") + .lean(); + + return heroesData; + } catch (error) { + console.error(error); + throw new Error("Failed to fetch hero tiers"); + } +} + +export async function getHeroTierWithNames({ select }: { select?: string }) { + try { + await clientPromise("game-core"); + + const heroesData = await HeroTier.aggregate([ + { + $lookup: { + from: "Heroes", + localField: "heroId", + foreignField: "_id", + as: "hero", + }, + }, + { + $unwind: "$hero", + }, + { + $project: { + _id: 0, + tier: 1, + name: "$hero.heroName", + ...(select ? { [select]: 1 } : {}), + }, + }, + ]); + console.log(heroesData); + return heroesData; + } catch (error) { + console.error(error); + throw new Error("Failed to fetch hero tiers with names"); + } +} diff --git a/lib/mongoose/schema/heroes-tier.ts b/lib/mongoose/schema/heroes-tier.ts new file mode 100644 index 00000000..d3886aaa --- /dev/null +++ b/lib/mongoose/schema/heroes-tier.ts @@ -0,0 +1,51 @@ +import mongoose, { Schema, Types, Model } from "mongoose"; + +interface Stats { + pickRate: number; + banRate: number; + winRate: number; +} + +export interface HeroTierDocument { + heroId: Types.ObjectId; + name: string; + combinedScore: number; + currentMetaScore: number; + currentMetaStats: Stats; + tier: string; + tournamentScore: number; + tournamentStats: Stats; + updatedAt: Date; +} + +const StatsSchema: Schema = new Schema( + { + pickRate: { type: Number, required: true }, + banRate: { type: Number, required: true }, + winRate: { type: Number, required: true }, + }, + { _id: false } +); + +const HeroTierSchema: Schema = new Schema( + { + heroId: { type: Types.ObjectId, required: true, ref: "Heroes" }, + combinedScore: { type: Number, required: true }, + currentMetaScore: { type: Number, required: true }, + currentMetaStats: { type: StatsSchema, required: true }, + tier: { type: String, required: true }, + tournamentScore: { type: Number, required: true }, + tournamentStats: { type: StatsSchema, required: true }, + updatedAt: { type: Date, default: Date.now }, + }, + { + collection: "TierList", + timestamps: true, + } +); + +const HeroTier: Model = + mongoose.models.TierList || + mongoose.model("TierList", HeroTierSchema); + +export default HeroTier;