aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-05-17 20:48:21 -0500
committermat <github@matdoes.dev>2022-05-17 20:48:21 -0500
commitb577c9ace72b39daeff8e5c4278dba1230521625 (patch)
tree1323f58372cb3ce77f3d72a88bb260e319425de1
parent887d7c951581602478233f0213d20095b43b904f (diff)
downloadskyblock-api-b577c9ace72b39daeff8e5c4278dba1230521625.tar.gz
skyblock-api-b577c9ace72b39daeff8e5c4278dba1230521625.tar.bz2
skyblock-api-b577c9ace72b39daeff8e5c4278dba1230521625.zip
use mongodb UUIDs in the database for auctions
-rw-r--r--src/database.ts60
-rw-r--r--src/hypixel.ts8
2 files changed, 56 insertions, 12 deletions
diff --git a/src/database.ts b/src/database.ts
index 2b73c77..f3b8175 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -5,7 +5,7 @@
import { categorizeStat, getStatUnit } from './cleaners/skyblock/stats.js'
import { CleanFullProfile } from './cleaners/skyblock/profile.js'
import { SLAYER_TIERS } from './cleaners/skyblock/slayers.js'
-import { Collection, Db, MongoClient, WithId } from 'mongodb'
+import { Binary, Collection, Db, MongoClient, WithId } from 'mongodb'
import { CleanMember } from './cleaners/skyblock/member.js'
import * as cached from './hypixelCached.js'
import * as constants from './constants.js'
@@ -117,6 +117,19 @@ export interface AccountSchema {
customization?: AccountCustomization
}
+export interface SimpleAuctionSchemaBson {
+ /** The UUID of the auction so we can look it up later. */
+ id: Binary
+ coins: number
+ /**
+ * The timestamp as **seconds** since epoch. It's in seconds instead of ms
+ * since we don't need to be super exact and so it's shorter.
+ */
+ ts: number
+ /** Whether the auction was successfully bought or simply expired. */
+ s: boolean
+ bin: boolean
+}
export interface SimpleAuctionSchema {
/** The UUID of the auction so we can look it up later. */
id: string
@@ -132,15 +145,20 @@ export interface SimpleAuctionSchema {
}
export interface ItemAuctionsSchema {
/** The id of the item */
- _id: string
+ id: string
auctions: SimpleAuctionSchema[]
}
+export interface ItemAuctionsSchemaBson {
+ /** The id of the item */
+ _id: string
+ auctions: SimpleAuctionSchemaBson[]
+}
let memberLeaderboardsCollection: Collection<DatabaseMemberLeaderboardItem>
let profileLeaderboardsCollection: Collection<DatabaseProfileLeaderboardItem>
let sessionsCollection: Collection<SessionSchema>
let accountsCollection: Collection<AccountSchema>
-let itemAuctionsCollection: Collection<ItemAuctionsSchema>
+let itemAuctionsCollection: Collection<ItemAuctionsSchemaBson>
const leaderboardInfos: { [leaderboardName: string]: string } = {
@@ -174,6 +192,10 @@ interface StringNumber {
[name: string]: number
}
+function createUuid(uuid: string): Binary {
+ return new Binary(Buffer.from((uuid).replace(/-/g, ''), 'hex'), Binary.SUBTYPE_UUID)
+}
+
function getMemberCollectionAttributes(member: CleanMember): StringNumber {
const collectionAttributes = {}
for (const collection of member.collections) {
@@ -1089,25 +1111,49 @@ export async function updateAccount(discordId: string, schema: AccountSchema) {
}, { $set: schema }, { upsert: true })
}
+function toItemAuctionsSchema(i: ItemAuctionsSchemaBson) {
+ return {
+ id: i._id,
+ auctions: i.auctions.map(a => {
+ return {
+ ...a,
+ id: a.id.toString('hex'),
+ }
+ }),
+ }
+}
+
+function toItemAuctionsSchemaBson(i: ItemAuctionsSchema) {
+ return {
+ _id: i.id,
+ auctions: i.auctions.map(a => {
+ return {
+ ...a,
+ id: createUuid(a.id)
+ }
+ }),
+ }
+}
+
/** Fetch all the Item Auctions for the item ids in the given array. */
export async function fetchItemsAuctions(itemIds: string[]): Promise<ItemAuctionsSchema[]> {
const auctions = await itemAuctionsCollection?.find({
_id: { $in: itemIds }
}).toArray()
- return auctions
+ return auctions.map(toItemAuctionsSchema)
}
/** Fetch all the Item Auctions for the item ids in the given array. */
export async function fetchPaginatedItemsAuctions(skip: number, limit: number): Promise<ItemAuctionsSchema[]> {
const auctions = await itemAuctionsCollection?.find({}).skip(skip).limit(limit).toArray()
- return auctions
+ return auctions.map(toItemAuctionsSchema)
}
export async function updateItemAuction(auction: ItemAuctionsSchema) {
await itemAuctionsCollection?.updateOne({
- _id: auction._id,
- }, { $set: auction }, { upsert: true })
+ _id: auction.id,
+ }, { $set: toItemAuctionsSchemaBson(auction) }, { upsert: true })
}
/**
diff --git a/src/hypixel.ts b/src/hypixel.ts
index 9d9c078..a2a8749 100644
--- a/src/hypixel.ts
+++ b/src/hypixel.ts
@@ -374,8 +374,6 @@ export async function fetchAuctionUncached(uuid: string) {
// this function is called from database.ts so it starts when we connect to the database
// it should only ever be called once!
export async function periodicallyFetchRecentlyEndedAuctions() {
- return
-
let previousAuctionIds = new Set()
while (true) {
@@ -397,7 +395,7 @@ export async function periodicallyFetchRecentlyEndedAuctions() {
const itemsAuctions = await fetchItemsAuctions(Array.from(newAuctionItemIds))
for (const itemAuctions of itemsAuctions) {
- updatedDatabaseAuctionItems.set(itemAuctions._id, itemAuctions)
+ updatedDatabaseAuctionItems.set(itemAuctions.id, itemAuctions)
}
for (const auction of endedAuctions.auctions) {
@@ -418,7 +416,7 @@ export async function periodicallyFetchRecentlyEndedAuctions() {
bin: auction.bin,
}
// make sure the auction isn't already in there
- if (auctions.findIndex((a) => a.id === simpleAuction.id) === -1) {
+ if (auctions.find((a) => a.id === simpleAuction.id)) {
auctions.push(simpleAuction)
// keep only the last 100 items
if (auctions.length > 100)
@@ -426,7 +424,7 @@ export async function periodicallyFetchRecentlyEndedAuctions() {
}
updatedDatabaseAuctionItems.set(auction.item.id, {
- _id: auction.item.id,
+ id: auction.item.id,
auctions,
})
}