corps.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const main = document.querySelector('main') as HTMLTextAreaElement;
  2. (async () => {
  3. const loader = document.querySelector('#loader') as HTMLElement;
  4. loader.style.display = 'block';
  5. try {
  6. await render();
  7. } catch (e) {
  8. main.textContent = e instanceof Error ? e.message : String(e);
  9. }
  10. loader.style.display = 'none';
  11. })();
  12. async function render(): Promise<void> {
  13. const corps: Corporation[] = await fetch('corps.json').then((r) => r.json());
  14. corps.sort((a, b) => (a.headquartersAddress?.lines.find((line) => line.type === 'SYSTEM')!.entity.naturalId ?? '\uffff')
  15. .localeCompare(b.headquartersAddress?.lines.find((line) => line.type === 'SYSTEM')!.entity.naturalId ?? '\uffff'));
  16. main.innerHTML = `<table>
  17. <thead>
  18. <tr>
  19. <th>code</th>
  20. <th>name</th>
  21. <th>shareholders</th>
  22. <th>HQ</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. ${corps.map(renderCorpRow).join('')}
  27. </tbody>
  28. </table>`;
  29. }
  30. function renderCorpRow(corp: Corporation): string {
  31. let hq = '';
  32. if (corp.headquartersAddress !== null) {
  33. const planet = corp.headquartersAddress.lines.find((line) => line.type === 'PLANET')!.entity;
  34. hq = planet.name;
  35. if (planet.name !== planet.naturalId)
  36. hq += ` (${planet.naturalId})`;
  37. if (!corp.headquartersFinished)
  38. hq += ' [unfinished]';
  39. }
  40. return `<tr>
  41. <td>${corp.code}</td>
  42. <td>${corp.name}</td>
  43. <td>${corp.shareholders.length}</td>
  44. <td>${hq}</td>
  45. </tr>`;
  46. }
  47. interface Corporation {
  48. name: string;
  49. code: string;
  50. headquartersAddress: {lines: {type: 'SYSTEM' | 'PLANET', entity: {name: string, naturalId: string}}[]} | null;
  51. headquartersFinished: boolean;
  52. shareholders: {}[];
  53. }