-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9682c0b
commit 04b613d
Showing
10 changed files
with
605 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import fetch from "node-fetch" | ||
|
||
let cache = new Map() | ||
let cacheTimes = new Map() | ||
let queue = [] | ||
|
||
export function getStats(uuid) { | ||
if (cache.has(uuid)) { | ||
if (performance.now() - cacheTimes.get(uuid) > 300000) { | ||
//if it's too old, re-fetch user data | ||
cache.delete(uuid) | ||
cacheTimes.delete(uuid) | ||
} else { | ||
return cache.get(uuid) | ||
} | ||
} | ||
|
||
let promise = new Promise(resolve => { | ||
queue.push({ | ||
uuid, | ||
resolve | ||
}) | ||
if (queue.length === 1) { | ||
setTimeout(doFetch, 1000) | ||
} | ||
}) | ||
cache.set(uuid, promise) | ||
return promise | ||
} | ||
|
||
async function doFetch() { | ||
let currentQueue = queue | ||
queue = [] | ||
let uuidsToFetch = currentQueue.map(item => item.uuid) | ||
let resultMap = await fetchPlayers(uuidsToFetch) | ||
let now = performance.now() | ||
for (let uuid of uuidsToFetch) { | ||
cacheTimes.set(uuid, now) | ||
} | ||
if (resultMap === null) { //error of some sort | ||
for (let item of currentQueue) { | ||
cache.set(item.uuid, null) | ||
item.resolve(null) | ||
} | ||
return | ||
} | ||
for (let item of currentQueue) { | ||
let uuid = item.uuid | ||
let value = resultMap.get(uuid) | ||
cache.set(uuid, value) | ||
item.resolve(value) | ||
} | ||
} | ||
|
||
async function fetchPlayers(uuids) { | ||
try { | ||
let response = await fetch("https://api.hydropper.info/user/multiple", { | ||
body: JSON.stringify({ | ||
_id: uuids | ||
}), | ||
method: "POST", | ||
headers: { | ||
"User-Agent": "DropperUtilities", | ||
"Content-Type": "application/json" | ||
} | ||
}) | ||
let json = await response.json() | ||
if (json.error) { | ||
throw new Error(JSON.stringify(json)) | ||
} | ||
let users = new Map() | ||
json.users.forEach(user => { | ||
if (user.wins === null) { | ||
console.log("NULL USER", user.uuid) | ||
users.set(user.uuid, null) | ||
return | ||
} | ||
users.set(user.uuid, { | ||
wins: user.wins, | ||
fails: user.fails | ||
}) | ||
}) | ||
return users | ||
} catch (error) { | ||
console.log("Unexpected Dropper API error - please report to lapisfloof on Discord:") | ||
console.log(error) | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.