Skip to main content
Loading...

More JavaScript Posts

//Use at https://mee6.xyz/leaderboard/732262089447702588
//Higher the spammy stat, the more spammy that person is.
//This is because mee doesn't give experience to people who post more comments in a minute.
function statBlock(title, value){
	let elm = document.createElement("div");
	elm.className = "leaderboardPlayerStatBlock";
	let titleElm = document.createElement("div");
	titleElm.className = "leaderboardPlayerStatName";
	titleElm.textContent = title;
	let valueElm = document.createElement("div");
	valueElm.className = "leaderboardPlayerStatValue";
	valueElm.textContent = value;
	elm.appendChild(titleElm);
	elm.appendChild(valueElm);
	elm.remove = function(){
	this.parentElement.removeChild(this);
	}
	return elm;
}
for(let player of Array.from(document.getElementsByClassName("leaderboardPlayer"))){
	if(player.spamminess) player.spamminess.remove();
	let messages = null;
	let experience = null;
	const statBlockArray = Array.from(player.querySelectorAll(".leaderboardPlayerStatBlock"));
	for(let statBlock of statBlockArray){
	const statName = statBlock.querySelector(".leaderboardPlayerStatName").textContent;
	const text = statBlock.querySelector(".leaderboardPlayerStatValue").textContent;
	const number = ((text.includes("k"))? (text.replace("k","") * 1000.0) : +(text));
	if(statName.includes("MESSAGES")){
	messages = number;
	}else if(statName.includes("EXPERIENCE")){
	experience = number;
	}
	}
	const stats = player.querySelector(".leaderboardPlayerStats");
	const messagesElement = stats.firstChild;

	const spamminess = ((messages/experience)*2000.0).toFixed(2);
	
	player.spamminess = stats.insertBefore(statBlock("SPAMMINESS", spamminess), messagesElement);
}
class TreeNode {
  constructor(data, depth) {
    this.data = data;
    this.children = [];
    this.depth = depth;
  }
}

function buildNaryTree(hosts) {
  const nodeMap = {};

  // Step 1: Create a map of nodes using fqdn as the key
  hosts.forEach((host) => {
    nodeMap[host.fqdn] = new TreeNode(host, 0); // Initialize depth to 0
  });

  // Step 2: Iterate through the array and add each node to its parent's list of children
  hosts.forEach((host) => {
    if (host.displayParent) {
      if (nodeMap[host.displayParent]) {
        const parent = nodeMap[host.displayParent];
        const node = nodeMap[host.fqdn];
        node.depth = parent.depth + 1; // Update the depth
        parent.children.push(node);
      } else {
        console.error(`Parent with fqdn ${host.displayParent} not found for ${host.fqdn}`);
      }
    }
  });

  // Find the root nodes (nodes with no parent)
  const rootNodes = hosts.filter((host) => !host.displayParent).map((host) => nodeMap[host.fqdn]);

  return rootNodes;
}

function treeToObjects(node) {
  const result = [];

  function inOrder(node) {
    if (!node) {
      return;
    }

    
    // Visit the current node and add it to the result
    result.push(node.data);

    // Visit children nodes first
    node.children.forEach((child) => {
      inOrder(child);
    });
  }

  inOrder(node);
  return result;
}

// Usage example
const hosts = [
  {
    fqdn: 'fqdn_1',
    display_name: 'Host 1',
  },
  {
    fqdn: 'fqdn_2',
    display_name: 'Host 2',
    displayParent: 'fqdn_1',
  },
  {
    fqdn: 'fqdn_3',
    display_name: 'Host 3'
  },
  {
    fqdn: 'fqdn_4',
    display_name: 'Host 4',
    displayParent: 'fqdn_3',
  },
  {
    fqdn: 'fqdn_5',
    display_name: 'Host 5',
    displayParent: 'fqdn_1',
  },
];

const tree = buildNaryTree(hosts);

// Function to convert the tree to an array of objects
function convertTreeToArray(nodes) {
  const result = [];
  nodes.forEach((node) => {
    result.push(...treeToObjects(node));
  });
  return result;
}

// Convert the tree back to an array of objects
const arrayFromTree = convertTreeToArray(tree);

console.log(arrayFromTree);