groups.helper.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import type { FactorioRawData } from "../lua-api/models.ts";
  2. /**
  3. * Compare two prototype objects by their `order` field.
  4. * If `order` is missing, it defaults to an empty string.
  5. *
  6. * @param a - First prototype object.
  7. * @param b - Second prototype object.
  8. * @returns Negative if `a.order < b.order`, positive if greater, zero otherwise.
  9. */
  10. function orderFactorioPrototype(a: { order?: string }, b: { order?: string }) {
  11. return (a.order ?? "")?.localeCompare(b.order ?? "");
  12. }
  13. export type SubGroup<T> = {
  14. name: string;
  15. order?: string;
  16. children: Array<T>;
  17. };
  18. export type Group<T> = {
  19. name: string;
  20. order?: string;
  21. icon?: string;
  22. subGroup: Array<SubGroup<T>>;
  23. };
  24. /** Type of an object that can be grouped by its `subgroup` property. */
  25. export interface GroupablaPrototype {
  26. subgroup: string;
  27. order?: string;
  28. }
  29. /**
  30. * Create a map of subgroup names to their corresponding `SubGroup` objects.
  31. *
  32. * @param prototypes - The prototypes to group.
  33. * @param subGroupsDefinitions - Definition objects for the available subgroups.
  34. * @returns Map where the key is the subgroup name and the value is a `SubGroup`.
  35. */
  36. function createSubGroupMap<T extends GroupablaPrototype>(
  37. prototypes: Array<T>,
  38. subGroupsDefinitions: Array<{ name: string; order?: string }>
  39. ): Map<string, SubGroup<T>> {
  40. const map = new Map<string, SubGroup<T>>();
  41. for (const proto of prototypes) {
  42. const subgroupId = proto.subgroup;
  43. if (!map.has(subgroupId)) {
  44. const subGroup = subGroupsDefinitions.find((sg) => sg.name === subgroupId);
  45. if (subGroup) {
  46. map.set(subgroupId, {
  47. name: subGroup.name,
  48. order: subGroup.order ?? "",
  49. children: [],
  50. });
  51. } else {
  52. console.error(`Subgroup information not found "${proto.subgroup}"`);
  53. }
  54. }
  55. const subgroup = map.get(subgroupId);
  56. subgroup?.children.push(proto);
  57. }
  58. return map;
  59. }
  60. /**
  61. * Create a map of group names to their corresponding `Group` objects.
  62. *
  63. * @param subGroupMap - Map of subgroup names to `SubGroup` objects.
  64. * @param groupsDef - Definition objects for the available groups.
  65. * @param subGroupsDef - Definition objects for the available subgroups.
  66. * @returns Map where the key is the group name and the value is a `Group`.
  67. */
  68. function createGroupMap<T extends GroupablaPrototype>(
  69. subGroupMap: Map<string, SubGroup<T>>,
  70. groupsDef: Array<{ name: string; order?: string }>,
  71. subGroupsDef: Array<{ name: string; group: string; order?: string }>
  72. ): Map<string, Group<T>> {
  73. const map = new Map<string, Group<T>>();
  74. for (const subGroupDef of subGroupsDef) {
  75. const subGroup = subGroupMap.get(subGroupDef.name);
  76. if (!subGroup || subGroup.children.length === 0) continue;
  77. if (!map.has(subGroupDef.group)) {
  78. const groupDef = groupsDef.find((g) => g.name === subGroupDef.group);
  79. if (groupDef) {
  80. map.set(subGroupDef.group, {
  81. name: groupDef.name,
  82. icon: `item-group/${groupDef.name}.png`,
  83. order: groupDef.order,
  84. subGroup: [],
  85. });
  86. }
  87. }
  88. map.get(subGroupDef.group)?.subGroup.push(subGroup);
  89. }
  90. return map;
  91. }
  92. /**
  93. * Sort groups, subgroups, and their children by the `order` field.
  94. *
  95. * @param groups - Array of `Group` objects to be sorted.
  96. */
  97. function sortGroups<T extends GroupablaPrototype>(groups: Group<T>[]) {
  98. groups.sort(orderFactorioPrototype);
  99. for (const group of groups) {
  100. group.subGroup.sort(orderFactorioPrototype);
  101. for (const subGroup of group.subGroup) {
  102. subGroup.children.sort(orderFactorioPrototype);
  103. }
  104. }
  105. }
  106. /**
  107. * Remove the `order` property from all groups, subgroups, and prototypes.
  108. *
  109. * @param groups - Array of `Group` objects from which to delete `order`.
  110. */
  111. function stripOrder<T extends GroupablaPrototype>(groups: Group<T>[]) {
  112. for (const group of groups) {
  113. delete group.order;
  114. group.subGroup.sort(orderFactorioPrototype);
  115. for (const subGroup of group.subGroup) {
  116. delete subGroup.order;
  117. for (const child of subGroup.children) {
  118. delete child.order;
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * Group the factorio prototypes according to there subgroups
  125. * @param arr the array of item to be grouped
  126. * @param data the rawData object extracted from Factorio
  127. * @param keepOrder flag to indicate if the order values must be kept in the final output
  128. * @returns
  129. */
  130. export function groupPrototypes<T extends GroupablaPrototype>(
  131. arr: Array<T>,
  132. data: FactorioRawData,
  133. keepOrder: boolean = true
  134. ): Group<T>[] {
  135. const itemGroups = Object.values(data["item-group"] ?? {}).filter((o) => !o.hidden);
  136. const itemSubGroups = Object.values(data["item-subgroup"] ?? {}).filter((o) => !o.hidden);
  137. const subGroupMap = createSubGroupMap(arr, itemSubGroups);
  138. const groupMap = createGroupMap(subGroupMap, itemGroups, itemSubGroups);
  139. const output = Array.from(groupMap.values());
  140. // Sort the output
  141. sortGroups(output);
  142. if (!keepOrder) {
  143. stripOrder(output);
  144. }
  145. return output;
  146. }