Skip to content

Commit

Permalink
Merge pull request #72 from KT-vicddory/week6-feat#54
Browse files Browse the repository at this point in the history
Week6 feat#54
  • Loading branch information
SNXWXH authored Jul 29, 2024
2 parents 9ee59f2 + 65f1489 commit 5648643
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 43 deletions.
45 changes: 45 additions & 0 deletions public/data/daily_rank.json
Original file line number Diff line number Diff line change
Expand Up @@ -538,5 +538,50 @@
{ "rank": 9, "team": "한화" },
{ "rank": 10, "team": "키움" }
]
},
{
"day": 20240726,
"data": [
{ "rank": 1, "team": "KIA" },
{ "rank": 2, "team": "LG" },
{ "rank": 3, "team": "삼성" },
{ "rank": 4, "team": "두산" },
{ "rank": 5, "team": "NC" },
{ "rank": 6, "team": "KT" },
{ "rank": 7, "team": "SSG" },
{ "rank": 8, "team": "한화" },
{ "rank": 9, "team": "롯데" },
{ "rank": 10, "team": "키움" }
]
},
{
"day": 20240727,
"data": [
{ "rank": 1, "team": "KIA" },
{ "rank": 2, "team": "LG" },
{ "rank": 3, "team": "삼성" },
{ "rank": 4, "team": "두산" },
{ "rank": 5, "team": "NC" },
{ "rank": 5, "team": "KT" },
{ "rank": 5, "team": "SSG" },
{ "rank": 8, "team": "롯데" },
{ "rank": 9, "team": "한화" },
{ "rank": 10, "team": "키움" }
]
},
{
"day": 20240728,
"data": [
{ "rank": 1, "team": "KIA" },
{ "rank": 2, "team": "LG" },
{ "rank": 3, "team": "삼성" },
{ "rank": 4, "team": "KT" },
{ "rank": 4, "team": "SSG" },
{ "rank": 6, "team": "두산" },
{ "rank": 7, "team": "NC" },
{ "rank": 8, "team": "롯데" },
{ "rank": 9, "team": "한화" },
{ "rank": 10, "team": "키움" }
]
}
]
94 changes: 55 additions & 39 deletions src/app/(main)/ranking/ai/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Graph from '@/components/ranking/Graph';
import MatchTeam from '@/components/ranking/MatchTeam';
import dateFormat from '@/utils/dateFormat';
// import dateFormat from '@/utils/dateFormat';
import winlossData from '#/data/winlossdata.json';
import july_schedule from '#/data/july_schedule.json';
import Image from 'next/image';
Expand All @@ -18,69 +18,85 @@ import {
export default async function RankingAi() {
const julyScheduleJSON: { [key: string]: number } = july_schedule;
const winlossDataJSON: TWinLossData = winlossData;
const today = dateFormat();
const koreaTime = new Date(
new Date().toLocaleString('en-US', { timeZone: 'Asia/Seoul' }),
);
const today = koreaTime.toISOString().split('T')[0];
const year = new Date().getFullYear();
const month = new Date().getMonth() + 1;
const yearmonth = year + '0' + month;
const dayOfWeek = new Date().getDay();

// 선발투수 정보 API,오늘 경기장 API
const dayOfWeek = koreaTime.getDay();
const day_num: number = julyScheduleJSON[today];

const [pitcherRes, gameRes]: [Response, Response] = await Promise.all([
fetch(`${process.env.BASE_URL}/api/startingPitcher?day_num=${day_num}`, {
cache: 'no-store',
}),
fetch(`${process.env.BASE_URL}/api/todayGame?yearmonth=${yearmonth}`),
]);
let todayGame: TPitcherRecord | null = null;
let stadiums: string[] = [];
let teamScore: string[] = [];
let total: number = 0;
let last: number = 0;
let winPercent: number = 0;
let pitcher: string[] = [];
let team: string[] = [];

const pitcherData: TPitcherData = await pitcherRes.json();
const gameData: TGameData = await gameRes.json();
if (dayOfWeek !== 1) {
const pitcherRes: Response = await fetch(
`${process.env.BASE_URL}/api/startingPitcher?day_num=${day_num}`,
{ cache: 'no-store' },
);
const pitcherData: TPitcherData = await pitcherRes.json();

const todayGame: TPitcherRecord = pitcherData['선발투수']['선발'];
const team: string[] = Object.keys(todayGame);
const pitcher: string[] = Object.values(todayGame);
const score: TTeamRecord = pitcherData['상대전적']['정규시즌전적'];
const teamScore: string[] = Object.values(score);
todayGame = pitcherData['선발투수']['선발'];
team = Object.keys(todayGame);
pitcher = Object.values(todayGame);

const gameDetail: TGameInfo[] = gameData.list.filter(
(item: TGameInfo): boolean => {
const gameDate: string = item.gameDate.toString();
return gameDate === today && (item.home === 'KT' || item.visit === 'KT');
},
);
const stadiums: string[] = gameDetail.map(
(item: TGameInfo): string => item.stadium,
);
const score: TTeamRecord = pitcherData['상대전적']['정규시즌전적'];
teamScore = Object.values(score);

// 전체 승률 및 예상 승률
const total: number = +winlossDataJSON.total[team[1]].winningPercentage;
const last: number = +winlossDataJSON.recent[team[1]].winningPercentage;
// 오늘 경기장 API
const gaemRes: Response = await fetch(
`${process.env.BASE_URL}/api/todayGame?yearmonth=${yearmonth}`,
);
const gameData: TGameData = await gaemRes.json();

// 승리 예측 API
const gamePredict: Response = await fetch(
`${process.env.BASE_URL}/api/predict?opponentTeam=${team[1]}&pastWinRate=${total}&recentWinRate=${last}&stadiumInformatio=${stadiums}&startingPitcherInformation=${pitcher[1]}&weather=`,
{ cache: 'no-store' },
);
const gamePredictData: TGamePredictData = await gamePredict.json();
const winPercent = parseInt(gamePredictData.toString().replace('%', ''));
const gameDetail: TGameInfo[] = gameData.list.filter(
(item: TGameInfo): boolean => {
const gameDate: string = item.gameDate.toString();
return (
gameDate === today && (item.home === 'KT' || item.visit === 'KT')
);
},
);
stadiums = gameDetail.map((item: TGameInfo): string => item.stadium);

// 전체 승률 및 예상 승률
total = +winlossDataJSON.total[team[1]].winningPercentage;
last = +winlossDataJSON.recent[team[1]].winningPercentage;

// 승리 예측 API
const gamePredict: Response = await fetch(
`${process.env.BASE_URL}/api/predict?opponentTeam=${team[1]}&pastWinRate=${total}&recentWinRate=${last}&stadiumInformatio=${stadiums}&startingPitcherInformation=${pitcher[1]}&weather=`,
{ cache: 'no-store' },
);
const gamePredictData: TGamePredictData = await gamePredict.json();
winPercent = parseInt(gamePredictData.toString().replace('%', ''));
}

return (
<>
<div className="flex flex-1 bg-[url('/images/mainBg.png')] bg-cover relative items-center overflow-hidden">
<div className="w-3/4 mx-auto text-white">
{dayOfWeek === 1 ? (
{dayOfWeek === 1 || !todayGame ? (
<div className="flex flex-col justify-center items-center font-semibold text-xl gap-4 h-3/4 max-sm:gap-0">
<div className="flex justify-center h-64 mb-4 max-sm:h-1/4">
<Image
src="/svgs/newsMascot/ddory.svg"
src="/svgs/wiznews/newsMascot/ddory.svg"
alt="ticket"
width={0}
height={0}
className="w-auto h-[100%] "
/>
<Image
src="/svgs/newsMascot/vic.svg"
src="/svgs/wiznews/newsMascot/vic.svg"
alt="ticket"
width={0}
height={0}
Expand Down
4 changes: 2 additions & 2 deletions src/app/(main)/ranking/loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export default function Loading() {
<div className="flex flex-col justify-center items-center bg-black text-white min-h-screen">
<div className="flex justify-center h-64 mb-8 max-sm:h-1/4">
<Image
src="/svgs/newsMascot/ddory.svg"
src="/svgs/wiznews/newsMascot/ddory.svg"
alt="ticket"
width={0}
height={0}
className="w-auto h-[100%] "
/>
<Image
src="/svgs/newsMascot/vic.svg"
src="/svgs/wiznews/newsMascot/vic.svg"
alt="ticket"
width={0}
height={0}
Expand Down
7 changes: 6 additions & 1 deletion src/app/api/predict/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ export async function GET(req: NextRequest) {
weather: '',
},
}),
cache: 'no-store',
});

const data = await response.json();
return NextResponse.json(data.predictWinRate, { status: 200 });
return NextResponse.json(data.predictWinRate, {
headers: {
'Cache-Control': 'no-store',
},
});
} catch (error) {
throw new Error('Server-Failed to fetch predict Data');
}
Expand Down
7 changes: 6 additions & 1 deletion src/app/api/startingPitcher/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ export async function GET(req: NextRequest) {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ day_num }),
cache: 'no-store',
});

const data = await response.json();
return NextResponse.json(data, { status: 200 });
return NextResponse.json(data, {
headers: {
'Cache-Control': 'no-store',
},
});
} catch (error) {
throw new Error('Server-Failed to fetch starting Data');
}
Expand Down

0 comments on commit 5648643

Please sign in to comment.