Skip to content

Instantly share code, notes, and snippets.

@robere2
Last active November 29, 2023 03:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robere2/88af445d2285682fed9f5c001f30b186 to your computer and use it in GitHub Desktop.
Save robere2/88af445d2285682fed9f5c001f30b186 to your computer and use it in GitHub Desktop.
Calculates the currently displayed player tag on the Hypixel Network, given a response from the API
const ranks = {
"ADMIN": [
[
'c',
"[ADMIN]"
]
],
"MODERATOR": [
[
'2',
"[MOD]"
]
],
"HELPER": [
[
'9',
"[HELPER]"
]
],
"JR_HELPER": [
[
'9',
"[JR HELPER]"
]
],
"YOUTUBER": [
[
'c',
"["
],
[
'f',
"YOUTUBE"
],
[
'c',
"]"
]
],
"SUPERSTAR": [
[
"%r",
"[MVP"
],
[
"%p",
"++"
],
[
"%r",
"]"
]
],
"MVP_PLUS": [
[
'b',
"[MVP"
],
[
"%p",
"+"
],
[
'b',
"]"
]
],
"MVP": [
[
'b',
"[MVP]"
]
],
"VIP_PLUS": [
[
'a',
"[VIP"
],
[
'6',
"+"
],
[
'a',
"]"
]
],
"VIP": [
[
'a',
"[VIP]"
]
],
"DEFAULT": [
[
'7',
""
]
]
};
const colors = { // Convert name-based colors to number-based
"BLACK": '0',
"DARK_BLUE": '1',
"DARK_GREEN": '2',
"DARK_AQUA": '3',
"DARK_RED": '4',
"DARK_PURPLE": '5',
"GOLD": '6',
"GRAY": '7',
"DARK_GRAY": '8',
"BLUE": '9',
"GREEN": 'a',
"AQUA": 'b',
"RED": 'c',
"LIGHT_PURPLE": 'd',
"YELLOW": 'e',
"WHITE": 'f'
};
/**
* Calculate the rank tag for the player object from the Hypixel API
* @param player Player object from Hypixel API
* @returns {*} Tag as an object like in {@link ranks}
*/
function calcTag(player) {
if(player && typeof player === "object") {
// In order of least priority to highest priority
let packageRank = player.packageRank;
let newPackageRank = player.newPackageRank;
let monthlyPackageRank = player.monthlyPackageRank;
let rankPlusColor = player.rankPlusColor;
let monthlyRankColor = player.monthlyRankColor;
let rank = player.rank;
let prefix = player.prefix;
if(rank === "NORMAL") rank = null; // Don't care about normies
if(monthlyPackageRank === "NONE") monthlyPackageRank = null; // Don't care about cheapos
if(packageRank === "NONE") packageRank = null;
if(newPackageRank === "NONE") newPackageRank = null;
if (prefix && typeof prefix === "string") return parseMinecraftTag(prefix);
if (rank || monthlyPackageRank || newPackageRank || packageRank) return replaceCustomColors(ranks[rank || monthlyPackageRank || newPackageRank || packageRank], colors[rankPlusColor], colors[monthlyRankColor]);
}
return replaceCustomColors(ranks.DEFAULT, null, null)
}
/**
* Parse a tag that is in Minecraft form using formatting codes
* @param tag Tag to parse
* @return {*} Tag that is an object like in {@link ranks}
*/
function parseMinecraftTag(tag) {
if(tag && typeof tag === "string") {
let newRank = [];
// Even indexes should be formatting codes, odd indexes should be text
let splitTag = tag.split(/§([a-f0-9])/);
splitTag.unshift('f'); // Beginning is always going to be white (typically empty though)
for(let i = 0; i < splitTag.length; i++) {
let j = Math.floor(i / 2); // First index
let k = i % 2; // Second index
if(!newRank[j]) newRank[j] = [];
if(!newRank[j][k]) newRank[j][k] = [];
newRank[j][k] = splitTag[i];
}
return newRank;
} else return [['f','']]
}
const defaultPlusColor = 'c'; // %p
const defaultRankColor = '6'; // %r
/**
* Replace the custom colors wildcards (%r and %p) with their actual colors in ranks
* @param rank Rank in the structure found in {@link ranks}
* @param p Plus color
* @param r Rank color
* @returns {*} New rank with real colors
*/
function replaceCustomColors(rank, p, r) {
if(!(rank instanceof Array))
return rank;
// Deep copy the rank
let newRank = JSON.parse(JSON.stringify(rank));
// Set defaults
if(!p || typeof p !== "string" || p.length > 1)
p = defaultPlusColor;
if(!r || typeof r !== "string" || r.length > 1)
r = defaultRankColor;
// Go through rank and replace wildcards
newRank.forEach((component) => {
if(component instanceof Array && component.length >= 2) {
if(component[0] === "%p")
component[0] = p;
if(component[0] === "%r")
component[0] = r;
}
});
return newRank;
}
@jtbuter
Copy link

jtbuter commented Apr 7, 2020

I think this doesn't take into account players who have charged back. You'd have to add if pre-eula and post-eula ranks are "NONE" below this line I think
https://gist.github.com/robere2/88af445d2285682fed9f5c001f30b186#file-hypixel-tag-calculator-js-L138

@robere2
Copy link
Author

robere2 commented Apr 22, 2020

I think this doesn't take into account players who have charged back. You'd have to add if pre-eula and post-eula ranks are "NONE" below this line I think
https://gist.github.com/robere2/88af445d2285682fed9f5c001f30b186#file-hypixel-tag-calculator-js-L138

Fixed, thanks!

@gamersindo1223
Copy link

Then How to Use it?

@tmgkopczyk
Copy link

Hey, is it possible to create a python version of this script?

@FlorianWendelborn
Copy link

@tmgkopczyk ChatGPT should be able to do that for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment