Skip to content

Commit

Permalink
update tier list and container
Browse files Browse the repository at this point in the history
  • Loading branch information
ng-jayson committed Nov 18, 2024
1 parent cfba6c7 commit d57e097
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 84 deletions.
6 changes: 5 additions & 1 deletion app/wiki/tier-list/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 (
<TabsContent
value="tier-list"
Expand Down
25 changes: 8 additions & 17 deletions components/wiki/tier-list/tier-list-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,22 @@

import { useRouter } from "next/navigation";
import Image from "next/image";
import { Hero } from "@prisma/client";
import { HeroTierDocument } from "@/lib/mongoose/schema/heroes-tier";

const tiers = [
{ tier: "OP", color: "#3652ba" },
{ tier: "S", color: "#4ade80" },
{ tier: "A", color: "#fde047" },
{ tier: "B", color: "#FFA500" },
{ tier: "C", color: "#FF6347" },
{ tier: "D", color: "#FF4500" },
{ tier: "S", color: "#3652ba" },
{ tier: "A", color: "#4ade80" },
{ tier: "B", color: "#fde047" },
{ tier: "C", color: "#FFA500" },
{ tier: "D", color: "#FF6347" },
];

interface TierListProps {
heroes: Hero[] | null;
heroes: HeroTierDocument[];
}

export default function TierContainer({ heroes }: TierListProps) {
const router = useRouter();
const filteredHeroes = heroes?.filter((hero) =>
tiers.some((tier) => {
if (tier.tier === "OP" && hero.tier === "SS") {
return true;
}
return tier.tier === hero.tier;
})
);
return (
<div className="mt-4 flex w-full flex-col gap-4">
<p className="text-lg ml-2 text-gray-400">
Expand All @@ -36,7 +27,7 @@ export default function TierContainer({ heroes }: TierListProps) {
<div className="flex w-full flex-col gap-4">
{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 (
Expand Down
66 changes: 0 additions & 66 deletions lib/actions/getHeroStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
49 changes: 49 additions & 0 deletions lib/actions/getHeroTier.ts
Original file line number Diff line number Diff line change
@@ -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");
}
}
51 changes: 51 additions & 0 deletions lib/mongoose/schema/heroes-tier.ts
Original file line number Diff line number Diff line change
@@ -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<Stats> = 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<HeroTierDocument> =
mongoose.models.TierList ||
mongoose.model<HeroTierDocument>("TierList", HeroTierSchema);

export default HeroTier;

1 comment on commit d57e097

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for mlbb-fyi ready!

✅ Preview
https://mlbb-naj2g7r8p-jinjays.vercel.app

Built with commit d57e097.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.