Converted main codebase to typescript
Signed-off-by: Taken <taken@mairimashita.org>
This commit is contained in:
@@ -1,31 +1,35 @@
|
||||
const fetch = require("axios")
|
||||
const apikey = process.env.HYPIXELAPIKEY
|
||||
import fetch from "axios"
|
||||
import config from "../Config"
|
||||
import { Profile, Profile2 } from "../../typings"
|
||||
import { Player, PlayerData } from "../../interfaces"
|
||||
import { Guild, GuildData } from "../../interfaces"
|
||||
const apikey = config.prod.hypixelapikey
|
||||
const mojang = "https://api.mojang.com/users/profiles/minecraft/"
|
||||
const mojanguuid = "https://sessionserver.mojang.com/session/minecraft/profile/"
|
||||
const hypixel = "https://api.hypixel.net/player"
|
||||
const guild = "https://api.hypixel.net/guild"
|
||||
const minotar = "https://minotar.net/helm/"
|
||||
|
||||
async function getUUID(ign) {
|
||||
async function getUUID(ign: string): Promise<string|null> {
|
||||
try {
|
||||
const req = await fetch(mojang + ign)
|
||||
const req: Profile = await fetch(mojang + ign)
|
||||
return req.data.id
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async function getIGN(uuid) {
|
||||
async function getIGN(uuid: string): Promise<string|null> {
|
||||
try {
|
||||
const req = await fetch(mojanguuid + uuid)
|
||||
const req: Profile2 = await fetch(mojanguuid + uuid)
|
||||
return req.data.name
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async function getPlayer(uuid) {
|
||||
const playerReq = await fetch(hypixel, {
|
||||
async function getPlayer(uuid: string): Promise<PlayerData|null> {
|
||||
const playerReq: Player = await fetch(hypixel, {
|
||||
params: {
|
||||
key: apikey,
|
||||
uuid: uuid
|
||||
@@ -39,10 +43,10 @@ async function getPlayer(uuid) {
|
||||
return playerReq.data.player
|
||||
}
|
||||
|
||||
async function getGuild(query, type) {
|
||||
async function getGuild(query: string, type?: string): Promise<GuildData|null> {
|
||||
const reqType = type ? type : "player"
|
||||
|
||||
const guildReq = await fetch(guild, {
|
||||
const guildReq: Guild = await fetch(guild, {
|
||||
params: {
|
||||
key: apikey,
|
||||
[reqType]: query
|
||||
@@ -56,11 +60,11 @@ async function getGuild(query, type) {
|
||||
return guildReq.data.guild
|
||||
}
|
||||
|
||||
async function getHeadURL(ign) {
|
||||
async function getHeadURL(ign: string): Promise<string|null> {
|
||||
return minotar + ign
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
export {
|
||||
getUUID,
|
||||
getIGN,
|
||||
getPlayer,
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Code used from the slothpixel project https://github.com/slothpixel/core
|
||||
*/
|
||||
function getExpForLevel(level) {
|
||||
function getExpForLevel(level: number): number {
|
||||
if (level == 0) return 0
|
||||
|
||||
const respectedLevel = getLevelRespectingPrestige(level)
|
||||
@@ -22,7 +22,7 @@ function getExpForLevel(level) {
|
||||
return 5000
|
||||
}
|
||||
|
||||
function getLevelRespectingPrestige(level) {
|
||||
function getLevelRespectingPrestige(level: number): number {
|
||||
if (level > HIGHEST_PRESTIGE * LEVELS_PER_PRESTIGE) {
|
||||
return level - HIGHEST_PRESTIGE * LEVELS_PER_PRESTIGE
|
||||
}
|
||||
@@ -36,7 +36,7 @@ const EASY_LEVELS_XP = 7000
|
||||
const XP_PER_PRESTIGE = 96 * 5000 + EASY_LEVELS_XP
|
||||
const LEVELS_PER_PRESTIGE = 100
|
||||
const HIGHEST_PRESTIGE = 50
|
||||
function bedwarsLevel(exp) {
|
||||
function bedwarsLevel(exp: number): number {
|
||||
const prestiges = Math.floor(exp / XP_PER_PRESTIGE)
|
||||
let level = prestiges * LEVELS_PER_PRESTIGE
|
||||
let expWithoutPrestiges = exp - (prestiges * XP_PER_PRESTIGE)
|
||||
@@ -52,4 +52,4 @@ function bedwarsLevel(exp) {
|
||||
return level + expWithoutPrestiges / 5000
|
||||
}
|
||||
|
||||
module.exports = { bedwarsLevel }
|
||||
export { bedwarsLevel }
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Code used from the slothpixel project https://github.com/slothpixel/core
|
||||
*/
|
||||
function guildLevel(exp) {
|
||||
function guildLevel(exp: number): number {
|
||||
const EXP_NEEDED = [
|
||||
100000,
|
||||
150000,
|
||||
@@ -47,10 +47,11 @@ function guildLevel(exp) {
|
||||
/*
|
||||
Code used from the hypixel-guild-bot project https://github.com/SimplyNo/hypixel-guild-bot
|
||||
*/
|
||||
function scaledGEXP(input) {
|
||||
function scaledGEXP(input: number): number {
|
||||
if (input <= 200000) return Number(input)
|
||||
if (input <= 700000) return Number(Math.round(((input - 200000) / 10) + 200000))
|
||||
if (input > 700000) return Number(Math.round(((input - 700000) / 33) + 250000))
|
||||
return 0
|
||||
}
|
||||
|
||||
module.exports = { guildLevel, scaledGEXP }
|
||||
export { guildLevel, scaledGEXP }
|
||||
@@ -8,30 +8,30 @@ const REVERSE_PQ_PREFIX = -(BASE - 0.5 * GROWTH) / GROWTH
|
||||
const REVERSE_CONST = REVERSE_PQ_PREFIX * REVERSE_PQ_PREFIX
|
||||
const GROWTH_DIVIDES_2 = 2 / GROWTH
|
||||
|
||||
function getLevel(exp) {
|
||||
function getLevel(exp: number): number {
|
||||
return exp <= 1 ? 1 : Math.floor(1 + REVERSE_PQ_PREFIX + Math.sqrt(REVERSE_CONST + GROWTH_DIVIDES_2 * exp))
|
||||
}
|
||||
|
||||
function hypixelLevel(exp) {
|
||||
function hypixelLevel(exp: number): number {
|
||||
return getLevel(exp) + getPercentageToNextLevel(exp)
|
||||
}
|
||||
|
||||
function getTotalExpToLevel(level) {
|
||||
function getTotalExpToLevel(level: number): number {
|
||||
const lv = Math.floor(level); const
|
||||
x0 = getTotalExpToFullLevel(lv)
|
||||
if (level === lv) return x0
|
||||
return (getTotalExpToFullLevel(lv + 1) - x0) * (level % 1) + x0
|
||||
}
|
||||
|
||||
function getTotalExpToFullLevel(level) {
|
||||
function getTotalExpToFullLevel(level: number): number {
|
||||
return (HALF_GROWTH * (level - 2) + BASE) * (level - 1)
|
||||
}
|
||||
|
||||
function getPercentageToNextLevel(exp) {
|
||||
function getPercentageToNextLevel(exp: number): number {
|
||||
const lv = getLevel(exp)
|
||||
const x0 = getTotalExpToLevel(lv)
|
||||
return (exp - x0) / (getTotalExpToLevel(lv + 1) - x0)
|
||||
}
|
||||
|
||||
|
||||
module.exports = { hypixelLevel }
|
||||
export { hypixelLevel }
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
Code used from the slothpixel project https://github.com/slothpixel/core
|
||||
*/
|
||||
function skywarsLevel(xp) {
|
||||
function skywarsLevel(xp: number): number {
|
||||
const xps = [0, 20, 70, 150, 250, 500, 1000, 2000, 3500, 6000, 10000, 15000]
|
||||
let exactLevel = 0
|
||||
if (xp >= 15000) {
|
||||
exactLevel = (xp - 15000) / 10000 + 12
|
||||
return exactLevel
|
||||
} else {
|
||||
} else if (xp < 15000) {
|
||||
for (let i = 0; i < xps.length; i++) {
|
||||
if (xp < xps[i]) {
|
||||
exactLevel = i + (xp - xps[i - 1]) / (xps[i] - xps[i - 1])
|
||||
@@ -15,6 +15,8 @@ function skywarsLevel(xp) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
module.exports = { skywarsLevel }
|
||||
export { skywarsLevel }
|
||||
@@ -1,5 +1,5 @@
|
||||
function formatUuid(uuid) {
|
||||
function formatUuid(uuid: string): string {
|
||||
return uuid.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, "$1-$2-$3-$4-$5")
|
||||
}
|
||||
|
||||
module.exports = { formatUuid }
|
||||
export { formatUuid }
|
||||
Reference in New Issue
Block a user