File size: 4,425 Bytes
a90e74c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function sortPlayerData(myDict){
  const sortedArray = Object.entries(myDict)
  .map(([key, value]) => [key, value[0], value.length-1])
  .sort((a, b) => b[2] - a[2] || a[0] - b[0])
  .map(([key, firstValue, lengthMinusOne], index) => [index + 1, firstValue, lengthMinusOne]);
  var data = sortedArray;
  data.unshift(['#','Name','Potatoes']);
  const table = document.createElement('table');
  table.setAttribute("id", "content-table");
  if (data.length === 1) {
    data.push(['–','–','–'])
  };

  const headerRow = table.insertRow();
  data[0].forEach((header) => {
    const headerCell = headerRow.insertCell();
    headerCell.textContent = header;
    headerCell.classList.add('boldHeader');
  });

  for (let i = 1; i < data.length; i++) {
    const row = table.insertRow();
    data[i].forEach((cell, index) => {
      const cellElement = row.insertCell();
      cellElement.textContent = cell;
    });
  }
  
  const container = document.getElementById('table-container');
  container.appendChild(table);
};

function getPlayerData(playerData, PDATA){
  var result = {};
  for (const key in PDATA) {
    const count = PDATA[key].filter(item => playerData.slice(1).includes(item)).length;
    if (count > 0) {
      result[key] = count;
    }
  }
  console.log(result)
  var sortedArray = Object.entries(result)
  .map(([key, value]) => [key, key, value])
  .sort((a, b) => a[2] - b[2] || a[0] - b[0])
  .map(([_, firstValue, lengthMinusOne], index) => [index + 1, firstValue, lengthMinusOne]);
  var data = sortedArray;
  data.unshift(['#','Plot ID','Diamonds']);
  const table = document.createElement('table');
  table.setAttribute("id", "content-table");
  if (data.length === 1) {
    data.push(['–','–','–'])
  };

  const headerRow = table.insertRow();
  data[0].forEach((header) => {
    const headerCell = headerRow.insertCell();
    headerCell.textContent = header;
    headerCell.classList.add('boldHeader');
  });

  for (let i = 1; i < data.length; i++) {
    const row = table.insertRow();
    data[i].forEach((cell, index) => {
      const cellElement = row.insertCell();
      cellElement.textContent = cell;
    });
  }
  
  const container = document.getElementById('table-container');
  container.appendChild(table);
};

function findPlayerKey(obj, str) {
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (obj[key][0].toLowerCase() === str.toLowerCase()) {
        return key;
      }
    }
  }
  return null;
};

function storePlayerData(playerData){
  playersjson = playerData;
};

function storePlotData(plotData){
  plotsjson = plotData;
};


fetch('data/playerdata.json')
  .then(response => response.text())
  .then(data => {
    storePlayerData(JSON.parse(data));
    sortPlayerData(JSON.parse(data));
  })
  .catch(error => console.error(error));

fetch('data/plotdata.json')
  .then(response => response.text())
  .then(data => {
    storePlotData(JSON.parse(data));;
  })
  .catch(error => console.error(error));

var searchbox = document.getElementById("searchbox");

function processInput() {
  var value = searchbox.value.trim();
  document.getElementById("content-table").remove();
  if (value === ''){
    sortPlayerData(playersjson);
    console.log('clear');
  } else {
    var playerName = findPlayerKey(playersjson, value);
    if (playerName === null) {
      console.log('no data found');
      const table = document.createElement('table');
      table.setAttribute("id", "content-table");
      const headerRow = table.insertRow(0);
      const headerCell = headerRow.insertCell(0);
      headerCell.textContent = "No Data Found";
      headerCell.classList.add('redHeader');
      console.log(table);
      const container = document.getElementById('table-container');
      container.appendChild(table);
      return
    } else {
      console.log(playerName)
      getPlayerData(playersjson[playerName],plotsjson);
    }
    console.log(value);
  };
}

searchbox.addEventListener("keyup", function(event) {
  if (event.key === "Enter") {
    processInput();
  }
});

var submitSearch = document.getElementById("submit-search");
submitSearch.addEventListener("click", function() {
  submitSearch.classList.add("clicked");
  processInput();
});

var clearSearch = document.getElementById("clear-search");
clearSearch.addEventListener("click", function() {
  clearSearch.classList.add("clicked");
  searchbox.value = "";
  processInput();
});