Переглянути джерело

newbies: get faction reputation

raylu 1 місяць тому
батько
коміт
0acf0d6e51
1 змінених файлів з 30 додано та 1 видалено
  1. 30 1
      ts/newbies.ts

+ 30 - 1
ts/newbies.ts

@@ -50,15 +50,39 @@ async function _render(): Promise<void> {
 	const threshold = sinceInput.valueAsDate!.getTime();
 	const sortedJoined = Array.from(joined.entries().filter(([_, join]) => join.timestamp >= threshold))
 			.sort((a, b) => a[1].timestamp - b[1].timestamp);
+	const reputation = await getReputations(sortedJoined.map(([username, _]) => username));
 	const lines: string[] = [];
 	for (const [username, join] of sortedJoined) {
 		const dtStr = new Date(join.timestamp).toISOString().replace('T', ' ').replace('Z', '');
-		lines.push(`${username}\t${dtStr}\t${join.planet}`);
+		lines.push(`${username}\t${dtStr}\t${join.planet}\t${reputation.get(username) ?? ''}`);
 	}
 	newbies.value = lines.join('\n');
 	newbies.style.display = 'block';
 }
 
+async function getReputations(usernames: string[]): Promise<Map<string, number>> {
+	const relevantCos = new Set<string>();
+	for (const username of usernames) {
+		const response = await fetch('https://rest.fnar.net/user/' + encodeURIComponent(username));
+		if (response.status !== 204) {
+			const companyCode = (await response.json()).CompanyCode;
+			relevantCos.add(companyCode);
+		}
+	}
+
+	const reputation = new Map<string, number>();
+	const companyCodes = Array.from(relevantCos);
+	for (let start = 0; start < companyCodes.length; start += 10) {
+		const batch = companyCodes.slice(start, start + 10);
+		const companies: Company[] = await fetchJSON('https://api.fnar.net/company/lookup?' +
+				batch.map((companyCode) => 'company=' + encodeURIComponent(companyCode)).join('&'));
+		for (const company of companies)
+			if (company !== null && company.UserName !== null)
+				reputation.set(company.UserName.toLowerCase(), company.Reputation);
+	}
+	return reputation;
+}
+
 document.querySelector('#copy')!.addEventListener('click', () => {
 	navigator.clipboard.writeText(newbies.value);
 });
@@ -80,3 +104,8 @@ interface Join {
 	timestamp: number;
 	planet: string;
 }
+interface Company {
+	UserName: string | null;
+	CompanyCode: string;
+	Reputation: number;
+}