You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. var total_fair;
  2. var total_euro;
  3. var total_market;
  4. $(function() {
  5. ajax({
  6. type: "GET",
  7. url: "https://api.fairplayground.info/rawdata/faircoin_prices.csv",
  8. }, processData);
  9. function processData(data) {
  10. if(data) priceData = csvToArray(data);
  11. // $(".inner-cont").append("priceData: ");
  12. // $(".inner-cont").append(priceData);
  13. var heads = "<p style='text-align:center;'>If you're happy with the FairCoin Calculator, you can donate at <a href='faircoin:fMGcrP9nAFic4356F4oHDDWVq8YvtJSXZL'>fMGcrP9nAFic4356F4oHDDWVq8YvtJSXZL</a>";
  14. $("body").append(heads);
  15. if (priceData) {
  16. var heads = '<th>Official Rate</th><th>Official Value</th><th>Market Value</th>';
  17. $("thead tr").append(heads);
  18. $('tbody').each(function() {
  19. total_fair = total_euro = total_market = 0;
  20. $(this).find('tr').each(function() {
  21. var dateVar = $(this).find('td:first').text();
  22. var d = new Date(dateVar);
  23. var dateISO = d.toISOString().slice(0, 10);
  24. var faircoins = parseFloat($(this).find('td:nth-child(3)').text());
  25. // $(this).append('<td>'+faircoins+'</td>');
  26. calculateEuros(dateISO, faircoins, this);
  27. });
  28. $(this).append('<tr><td><strong>Totals</strong></td><td></td><td>' + (Math.round(total_fair * 100) / 100) + ' FAIR</td><td></td><td>€ ' + (Math.round(total_euro * 100) / 100) + '</td><td>€ ' + (Math.round(total_market * 100) / 100) + '</td></tr>');
  29. });
  30. }
  31. };
  32. function calculateEuros(date, faircoins, el) {
  33. if (!faircoins) {
  34. console.log("please, fill the faircoin input box");
  35. return;
  36. }
  37. if (!date) {
  38. console.log("no date");
  39. return;
  40. }
  41. //let's find the date in the priceData
  42. var selectedPriceData = [];
  43. for (var i = 0; i < priceData.length; i++) {
  44. if (priceData[i][0] == date) {
  45. selectedPriceData = priceData[i];
  46. }
  47. }
  48. if (selectedPriceData.length < 1) {
  49. console.log("selected date not found in the dataset");
  50. return;
  51. }
  52. // console.log(selectedPriceData);
  53. var euros = faircoins * selectedPriceData[3];
  54. var market = faircoins * selectedPriceData[2];
  55. total_fair = total_fair+faircoins;
  56. total_euro = total_euro+euros;
  57. total_market = total_market+market;
  58. $(el).append('<td>' + parseFloat(selectedPriceData[3]).toString() + '</td>');
  59. $(el).append('<td>€ ' + (Math.round(euros * 100) / 100) + '</td>');
  60. $(el).append('<td>€ ' + (Math.round(market * 100) / 100) + '</td>');
  61. }
  62. function ajax (options, callback) {
  63. var xhr;
  64. xhr = new XMLHttpRequest();
  65. xhr.open(options.type, options.url, options.async || true);
  66. xhr.onreadystatechange = function() {
  67. if (xhr.readyState === 4) {
  68. return callback(xhr.responseText);
  69. }
  70. };
  71. return xhr.send();
  72. };
  73. function csvToArray(csv) {
  74. var a1 = csv.split("\n");
  75. var r = [];
  76. for (var i = 0; i < a1.length; i++) {
  77. var a2 = a1[i].split(",");
  78. r.push(a2);
  79. }
  80. return (r);
  81. }
  82. });