diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-05-27 16:56:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-27 16:56:50 -0500 |
commit | c9097dc2d0749fa63fd731423bc87a5452f0444d (patch) | |
tree | 50afe6b99cf3ed6bd7369e804316c05c7407b5ca /src/database.ts | |
parent | a3662fb5888bd031b27cc81028ba86b0271eb442 (diff) | |
download | skyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.tar.gz skyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.tar.bz2 skyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.zip |
Profile customization (#45)
* add basic discord auth
* add uuid dependency
* add lastUpdated to sessions
* add route to get session from id
* add accounts collection
* update build
* add gm rank
* add `customization` url parameter
* add customization parameter to /player/:user
* add route to get info from discord id
* remove a console.log
* Update database.js
* Update package.json
* fix tests
Diffstat (limited to 'src/database.ts')
-rw-r--r-- | src/database.ts | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/src/database.ts b/src/database.ts index 42fb569..2c679b6 100644 --- a/src/database.ts +++ b/src/database.ts @@ -3,17 +3,19 @@ */ import { categorizeStat, getStatUnit } from './cleaners/skyblock/stats' -import { CleanBasicProfile, CleanFullProfile, CleanProfile } from './cleaners/skyblock/profile' +import { CleanFullProfile } from './cleaners/skyblock/profile' +import { slayerLevels } from './cleaners/skyblock/slayers' import { CleanMember } from './cleaners/skyblock/member' import { Collection, Db, MongoClient } from 'mongodb' import { CleanPlayer } from './cleaners/player' import * as cached from './hypixelCached' import * as constants from './constants' import { shuffle, sleep } from './util' +import * as discord from './discord' import NodeCache from 'node-cache' import Queue from 'queue-promise' import { debug } from '.' -import { slayerLevels } from './cleaners/skyblock/slayers' +import { v4 as uuid4 } from 'uuid' // don't update the user for 3 minutes const recentlyUpdated = new NodeCache({ @@ -57,8 +59,33 @@ const reversedLeaderboards = [ let client: MongoClient let database: Db + +interface SessionSchema { + _id?: string + refresh_token: string + discord_user: { + id: string + name: string + } + lastUpdated: Date +} + +export interface AccountCustomization { + backgroundUrl?: string + pack?: string +} + +export interface AccountSchema { + _id?: string + discordId: string + minecraftUuid?: string + customization?: AccountCustomization +} + let memberLeaderboardsCollection: Collection<any> let profileLeaderboardsCollection: Collection<any> +let sessionsCollection: Collection<SessionSchema> +let accountsCollection: Collection<AccountSchema> async function connect(): Promise<void> { if (!process.env.db_uri) @@ -69,6 +96,8 @@ async function connect(): Promise<void> { database = client.db(process.env.db_name) memberLeaderboardsCollection = database.collection('member-leaderboards') profileLeaderboardsCollection = database.collection('profile-leaderboards') + sessionsCollection = database.collection('sessions') + accountsCollection = database.collection('accounts') } interface StringNumber { @@ -620,6 +649,38 @@ async function fetchAllLeaderboards(fast?: boolean): Promise<void> { if (debug) console.debug('Finished caching leaderboards!') } +export async function createSession(refreshToken: string, userData: discord.DiscordUser): Promise<string> { + const sessionId = uuid4() + await sessionsCollection.insertOne({ + _id: sessionId, + refresh_token: refreshToken, + discord_user: { + id: userData.id, + name: userData.username + '#' + userData.discriminator + }, + lastUpdated: new Date() + }) + return sessionId +} + +export async function fetchSession(sessionId: string): Promise<SessionSchema> { + return await sessionsCollection.findOne({ _id: sessionId }) +} + +export async function fetchAccount(minecraftUuid: string): Promise<AccountSchema> { + return await accountsCollection.findOne({ minecraftUuid }) +} + +export async function fetchAccountFromDiscord(discordId: string): Promise<AccountSchema> { + return await accountsCollection.findOne({ discordId }) +} + +export async function updateAccount(discordId: string, schema: AccountSchema) { + await accountsCollection.updateOne({ + discordId + }, { $set: schema }, { upsert: true }) +} + // make sure it's not in a test if (!globalThis.isTest) { connect().then(() => { |