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.

146 lines
4.2 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
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; background:#fff'>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. if($("thead tr").length){ // faircoin 2
  18. $("thead tr").append(heads);
  19. $('tbody').each(function() {
  20. total_fair = total_euro = total_market = 0;
  21. $(this).find('tr').each(function() {
  22. var dateVar = $(this).find('td:first').text();
  23. var d = new Date(dateVar);
  24. var dateISO = d.toISOString().slice(0, 10);
  25. var faircoins = parseFloat($(this).find('td:nth-child(3)').text());
  26. // $(this).append('<td>'+faircoins+'</td>');
  27. calculateEuros(dateISO, faircoins, this);
  28. });
  29. $(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>');
  30. });
  31. } else { // faircoin 1
  32. $("tbody tr:first-child").append(heads);
  33. $('tbody tr.direct').each(function() {
  34. var dateVar = $(this).find('td:nth-child(3)').text();
  35. var d = new Date(dateVar);
  36. var dateISO = d.toISOString().slice(0, 10);
  37. var faircoins = cleanUpCurrency($(this).find('td:nth-child(4)').text());
  38. // $(this).append('<td>'+faircoins+'</td>');
  39. calculateEuros(dateISO, faircoins, this);
  40. });
  41. }
  42. }
  43. };
  44. function calculateEuros(date, faircoins, el) {
  45. if (!faircoins) {
  46. console.log("please, fill the faircoin input box");
  47. return;
  48. }
  49. if (!date) {
  50. console.log("no date");
  51. return;
  52. }
  53. //let's find the date in the priceData
  54. var selectedPriceData = [];
  55. for (var i = 0; i < priceData.length; i++) {
  56. if (priceData[i][0] == date) {
  57. selectedPriceData = priceData[i];
  58. }
  59. }
  60. if (selectedPriceData.length < 1) {
  61. console.log("selected date not found in the dataset");
  62. return;
  63. }
  64. // console.log(selectedPriceData);
  65. var euros = faircoins * selectedPriceData[3];
  66. var market = faircoins * selectedPriceData[2];
  67. total_fair = total_fair+faircoins;
  68. total_euro = total_euro+euros;
  69. total_market = total_market+market;
  70. $(el).append('<td>' + parseFloat(selectedPriceData[3]).toString() + '</td>');
  71. $(el).append('<td>€ ' + (Math.round(euros * 100) / 100) + '</td>');
  72. $(el).append('<td>€ ' + (Math.round(market * 100) / 100) + '</td>');
  73. }
  74. function ajax (options, callback) {
  75. var xhr;
  76. xhr = new XMLHttpRequest();
  77. xhr.open(options.type, options.url, options.async || true);
  78. xhr.onreadystatechange = function() {
  79. if (xhr.readyState === 4) {
  80. return callback(xhr.responseText);
  81. }
  82. };
  83. return xhr.send();
  84. };
  85. function csvToArray(csv) {
  86. var a1 = csv.split("\n");
  87. var r = [];
  88. for (var i = 0; i < a1.length; i++) {
  89. var a2 = a1[i].split(",");
  90. r.push(a2);
  91. }
  92. return (r);
  93. }
  94. function cleanUpCurrency(s){
  95. var expression = /\(.(.+)\)/;
  96. //Check if it is in the proper format
  97. if(s.match(expression)){
  98. //It matched - strip out parentheses and append - at front
  99. return parseFloat('-' + s.replace(/[\$\(\),]/g,''));
  100. }
  101. else{
  102. return parseFloat(s);
  103. }
  104. }
  105. });