Brozy123 commited on
Commit
fe79507
·
verified ·
1 Parent(s): 8df09f7

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +53 -11
index.js CHANGED
@@ -1773,11 +1773,62 @@ const FORMATIONS = {
1773
  };
1774
 
1775
  /**
1776
- * Auto-detects positions from a comma-separated string and maps them to coordinate keys.
1777
- * Handles duplicate positions by appending numbers (CB1, CB2, etc.)
1778
  */
1779
  function autoDetectFormation(input) {
1780
  if (!input) return FORMATIONS["default"];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1781
  const parts = input.split(/[,|\s]+/).map(s => s.trim().toUpperCase()).filter(s => s.length > 0);
1782
  const counts = {};
1783
  const result = [];
@@ -1785,15 +1836,9 @@ function autoDetectFormation(input) {
1785
  parts.forEach(pos => {
1786
  counts[pos] = (counts[pos] || 0) + 1;
1787
  let key = pos;
1788
-
1789
- // Find total count of this position in the whole string
1790
  const totalCount = parts.filter(p => p === pos).length;
1791
-
1792
  if (totalCount > 1) {
1793
- // Map to variant keys like CB1, CB2, CM1, CM2 etc.
1794
  key = `${pos}${counts[pos]}`;
1795
-
1796
- // Special mapping for common 2-player variants (LCB/RCB, LCM/RCM etc.)
1797
  if (totalCount === 2) {
1798
  if (pos === 'CB') key = counts[pos] === 1 ? 'LCB' : 'RCB';
1799
  if (pos === 'CM') key = counts[pos] === 1 ? 'LCM' : 'RCM';
@@ -1802,14 +1847,11 @@ function autoDetectFormation(input) {
1802
  if (pos === 'CAM') key = counts[pos] === 1 ? 'CAM1' : 'CAM2';
1803
  }
1804
  } else {
1805
- // Single position, use the base key
1806
  key = pos;
1807
  }
1808
-
1809
  result.push([key, pos]);
1810
  });
1811
 
1812
- // Ensure at least a GK if missing
1813
  if (!result.some(r => r[1] === 'GK')) {
1814
  result.unshift(['GK', 'GK']);
1815
  }
 
1773
  };
1774
 
1775
  /**
1776
+ * Auto-detects positions from a formation string (like "4-3-3") or a comma-separated list.
 
1777
  */
1778
  function autoDetectFormation(input) {
1779
  if (!input) return FORMATIONS["default"];
1780
+
1781
+ // 1. Check for numeric formation pattern (e.g. 4-3-3 or 4-2-3-1)
1782
+ const numRegex = /^(\d)-(\d)-(\d)(?:-(\d))?$/;
1783
+ const numMatch = input.trim().match(numRegex);
1784
+
1785
+ if (numMatch) {
1786
+ const result = [['GK', 'GK']];
1787
+ const dfCount = parseInt(numMatch[1]);
1788
+ const mid1Count = parseInt(numMatch[2]);
1789
+ const mid2Count = parseInt(numMatch[3]);
1790
+ const fwCount = numMatch[4] ? parseInt(numMatch[4]) : 0;
1791
+
1792
+ // --- Assign Defenders ---
1793
+ if (dfCount === 1) result.push(['CB', 'CB']);
1794
+ else if (dfCount === 2) result.push(['LCB', 'CB'], ['RCB', 'CB']);
1795
+ else if (dfCount === 3) result.push(['LB', 'LB'], ['CB', 'CB'], ['RB', 'RB']);
1796
+ else if (dfCount === 4) result.push(['LB', 'LB'], ['LCB', 'CB'], ['RCB', 'CB'], ['RB', 'RB']);
1797
+ else if (dfCount === 5) result.push(['LB', 'LB'], ['LCB', 'CB'], ['CB', 'CB'], ['RCB', 'CB'], ['RB', 'RB']);
1798
+
1799
+ if (!numMatch[4]) {
1800
+ // 3-PART FORMATION (DF-MF-FW)
1801
+ // Midfielders
1802
+ if (mid1Count === 1) result.push(['CM', 'CM']);
1803
+ else if (mid1Count === 2) result.push(['LCM', 'CM'], ['RCM', 'CM']);
1804
+ else if (mid1Count === 3) result.push(['LCM', 'CM'], ['CM', 'CM'], ['RCM', 'CM']);
1805
+ else if (mid1Count === 4) result.push(['LM', 'LM'], ['LCM', 'CM'], ['RCM', 'CM'], ['RM', 'RM']);
1806
+ else if (mid1Count === 5) result.push(['LM', 'LM'], ['LCM', 'CM'], ['CM', 'CM'], ['RCM', 'CM'], ['RM', 'RM']);
1807
+
1808
+ // Forwards
1809
+ if (mid2Count === 1) result.push(['ST', 'ST']);
1810
+ else if (mid2Count === 2) result.push(['ST1', 'ST'], ['ST2', 'ST']);
1811
+ else if (mid2Count === 3) result.push(['LW', 'LW'], ['ST', 'ST'], ['RW', 'RW']);
1812
+ else if (mid2Count === 4) result.push(['LW', 'LW'], ['ST1', 'ST'], ['ST2', 'ST'], ['RW', 'RW']);
1813
+ } else {
1814
+ // 4-PART FORMATION (DF-DM-AM-FW)
1815
+ // CDMs
1816
+ if (mid1Count === 1) result.push(['CDM', 'CDM']);
1817
+ else if (mid1Count === 2) result.push(['CDM1', 'CDM'], ['CDM2', 'CDM']);
1818
+
1819
+ // AMs
1820
+ if (mid2Count === 1) result.push(['CAM', 'CAM']);
1821
+ else if (mid2Count === 2) result.push(['CAM1', 'CAM'], ['CAM2', 'CAM']);
1822
+ else if (mid2Count === 3) result.push(['LM', 'LM'], ['CAM', 'CAM'], ['RM', 'RM']);
1823
+
1824
+ // Forwards
1825
+ if (fwCount === 1) result.push(['ST', 'ST']);
1826
+ else if (fwCount === 2) result.push(['ST1', 'ST'], ['ST2', 'ST']);
1827
+ }
1828
+ return result;
1829
+ }
1830
+
1831
+ // 2. Fallback to comma/space separated list of positions
1832
  const parts = input.split(/[,|\s]+/).map(s => s.trim().toUpperCase()).filter(s => s.length > 0);
1833
  const counts = {};
1834
  const result = [];
 
1836
  parts.forEach(pos => {
1837
  counts[pos] = (counts[pos] || 0) + 1;
1838
  let key = pos;
 
 
1839
  const totalCount = parts.filter(p => p === pos).length;
 
1840
  if (totalCount > 1) {
 
1841
  key = `${pos}${counts[pos]}`;
 
 
1842
  if (totalCount === 2) {
1843
  if (pos === 'CB') key = counts[pos] === 1 ? 'LCB' : 'RCB';
1844
  if (pos === 'CM') key = counts[pos] === 1 ? 'LCM' : 'RCM';
 
1847
  if (pos === 'CAM') key = counts[pos] === 1 ? 'CAM1' : 'CAM2';
1848
  }
1849
  } else {
 
1850
  key = pos;
1851
  }
 
1852
  result.push([key, pos]);
1853
  });
1854
 
 
1855
  if (!result.some(r => r[1] === 'GK')) {
1856
  result.unshift(['GK', 'GK']);
1857
  }