Skip to content

Commit

Permalink
awesome
Browse files Browse the repository at this point in the history
  • Loading branch information
axiinyaa committed Sep 3, 2024
1 parent 7e3d31f commit e793b70
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 3 deletions.
40 changes: 39 additions & 1 deletion app/components/database-parse-type.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,37 @@ export class UserData {
) {}
}

export interface NikogotchiValues {
value: number;
max: number;
}

export interface NikogotchiData {

last_interacted: Date;
hatched: Date;
nikogotchi_available: boolean;
rarity: string;

_id?: string;
data: object;
pancakes: number;
golden_pancakes: number;
glitched_pancakes: number;

level: number;
health: NikogotchiValues;
energy: NikogotchiValues;
hunger: NikogotchiValues;
cleanliness: NikogotchiValues;
happiness: NikogotchiValues;

room_data: number[];
name: string;
status: number;
immortal: boolean;
}

export interface LeaderboardUser {
name: string,
type: string,
Expand Down Expand Up @@ -82,12 +113,19 @@ export interface Badges {
[key: string]: Badge;
}

export interface NikogotchiInformation {
name: string;
emoji: string;
type: string;
}

export interface ItemData {
backgrounds: Backgrounds;
items: {
capsules: Item[];
pancakes: Item[];
treasures: Treasures;
};
badges: Badges
badges: Badges;
nikogotchi: NikogotchiInformation[];
}
16 changes: 14 additions & 2 deletions app/database.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use server'
import axios from 'axios';
import { Collection, MongoClient, ObjectId } from 'mongodb';
import { UserData, LeaderboardUser, ItemData } from './components/database-parse-type'
import { UserData, LeaderboardUser, ItemData, NikogotchiInformation, NikogotchiData } from './components/database-parse-type'

let collection: null | Collection<UserData> = null

Expand Down Expand Up @@ -37,6 +37,18 @@ export async function Fetch(user: string): Promise<UserData | null> {
}


export async function GetNikogotchiData(user: string): Promise<NikogotchiData | null> {
const user_data_collection = await connectToDatabase('UserNikogotchis');
const userDataFromDB = await user_data_collection.findOne({ _id: user });

if (userDataFromDB) {
return userDataFromDB as unknown as NikogotchiData;
} else {
return null;
}
}


export async function Update(user: UserData) {
const user_data_collection = await connectToDatabase();

Expand Down Expand Up @@ -95,7 +107,7 @@ export async function FetchItemData() {
}

export async function GetDiscordData(userID: string) {
const response = await axios.get(`https://discordlookup.mesavirep.xyz/v1/user/${userID}`)
const response = await axios.get(`https://discordlookup.mesalytic.moe/v1/user/${userID}`)

return response.data;
}
Expand Down
150 changes: 150 additions & 0 deletions app/nikogotchi/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
'use client'

import { signIn, useSession } from "next-auth/react";
import Desktop from "../components/desktop";
import Window from '../components/window';
import { useState, useEffect } from "react";
import axios from "axios";

import { Fetch as fetchFromDatabase, FetchItemData, GetBackgrounds, GetNikogotchiData, Update as updateToDatabase } from "../database";
import { redirect } from "next/navigation";

export default function Page() {

const [pageStatus, setPageStatus] = useState('loading');
const [userID, setUserID] = useState<null | string>(null);

function PageStatus(status: string) {

if (status === 'authenticating') {
return (
<Desktop>
<Window title='Nikogotchi' className="">
<div className="text-xl text-black">Authenticating...</div>
</Window>
</Desktop>
)
} else if (status === 'loading') {
return (
<Desktop>
<Window title='Nikogotchi' className="">
<div className="text-xl text-black">Loading...</div>
</Window>
</Desktop>
)
} else if (status === 'authenticated') {
return (
<Desktop>
<Window title='Nikogotchi' className="">
<div className="text-xl text-black">Loading Nikogotchi...</div>
</Window>
</Desktop>
)
} else if (status === 'error') {
return (
<Desktop>
<Window title='Error!' className="">
<div className="text-xl text-black">An error has occurred. Please try again later.</div>
</Window>
</Desktop>
)
} else if (status === 'no-nikogotchi') {
return (
<Desktop>
<Window title='Error!' className="">
<div className="text-l text-black">You don't have a Nikogotchi! Use the shop to buy a capsule and then use /nikogotchi check!</div>
<img className="mx-auto" src="/nikogotchi/demonstration.gif" width={300}/>
<button onClick={() => window.location.href = '/profile'}>Go Back</button>
</Window>
</Desktop>
)
} else if (status === 'unauthenticated') {
return (
<Desktop>
<Window title='Error!' className="grid justify-center items-center">
<div className="text-xl text-black text-center">You need to be signed in to Discord to access this page!</div>
<div className="mx-auto mt-5 scale-120">
<button onClick={() => signIn('discord')}>Sign In</button> <button onClick={() => window.location.href = '/'}>Okay</button>
</div>
</Window>
</Desktop>
)
}
}

const { data: discordData, status } = useSession()

useEffect(() => {
const login = async () => {

if (pageStatus === 'success') { return; } // We don't need to get discord data when page is successfully loaded.

if (status === 'loading') {
setPageStatus('loading');
return;
}

setPageStatus('authenticating');

try {

if (!discordData) { // If discordData is null, then user has not signed in to the website.
setPageStatus('unauthenticated')
return;
}

if (!discordData?.access_token) { return } // If there's no access token then we don't need to do anything yet.

const response = await axios.get('https://discord.com/api/users/@me', { headers: { Authorization: `Bearer ${discordData.access_token}` } })

setUserID(response.data.id)
}
catch (error) {
console.error('Error fetching data from discord:', error);
setPageStatus('error')
}
}

login();

}, [discordData]);

useEffect(() => {
const fetchData = async () => {
try {

if (!userID) { return; } // Don't do anything if we don't have the userID yet.

if (pageStatus === 'success') { return; }

setPageStatus('authenticated')

const data = await GetNikogotchiData(userID);

console.log(data);

if (!data) {
console.error('For some reason data was never fetched.');
setPageStatus('error'); // Run error scenario if data doesn't exist... for whatever reason.
return
}

setPageStatus('no-nikogotchi');

} catch (error) {
console.error('Error fetching data from database:', error);
setPageStatus('error');
}

}

fetchData();

}, [userID])

if (pageStatus === 'success') {
return Page();
} else {
return PageStatus(pageStatus)
}
}
Binary file added public/nikogotchi/demonstration.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e793b70

Please sign in to comment.