utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. function parseYdk(ydkContent) {
  2. const lines = ydkContent.split('\n');
  3. const deck = {
  4. main: [],
  5. extra: [],
  6. side: [],
  7. };
  8. let currentSection = null;
  9. for (const line of lines) {
  10. const trimmedLine = line.trim();
  11. if (!trimmedLine || trimmedLine.startsWith('#') && !['#main', '#extra'].includes(trimmedLine)) {
  12. continue;
  13. }
  14. if (trimmedLine === '#main') {
  15. currentSection = 'main';
  16. } else if (trimmedLine === '#extra') {
  17. currentSection = 'extra';
  18. } else if (trimmedLine === '!side') {
  19. currentSection = 'side';
  20. } else if (currentSection) {
  21. const cardId = parseInt(trimmedLine, 10);
  22. if (!isNaN(cardId)) {
  23. deck[currentSection].push(String(cardId));
  24. }
  25. }
  26. }
  27. return deck;
  28. }
  29. function genYdk(deck) {
  30. let ydkContent = '#created by mistivia.com\n';
  31. ydkContent += '#main\n';
  32. ydkContent += deck.main.join('\n') + '\n';
  33. ydkContent += '#extra\n';
  34. ydkContent += deck.extra.join('\n') + '\n';
  35. ydkContent += '!side\n';
  36. ydkContent += deck.side.join('\n') + '\n';
  37. return ydkContent;
  38. }
  39. function downloadStringAsFile(text) {
  40. const blob = new Blob([text], { type: 'text/plain' });
  41. let filename= `ygodeck_${Date.now()}.ydk`;
  42. const link = document.createElement('a');
  43. link.href = URL.createObjectURL(blob);
  44. link.download = filename;
  45. document.body.appendChild(link);
  46. link.click();
  47. document.body.removeChild(link);
  48. URL.revokeObjectURL(link.href);
  49. }
  50. function ydkeEncode(d) {
  51. let deck = [];
  52. for (let card of d) {
  53. let num = parseInt(card);
  54. if (isNaN(num)) continue;
  55. deck.push(num);
  56. }
  57. const buffer = new ArrayBuffer(deck.length * 4);
  58. const view = new DataView(buffer);
  59. for (let i = 0; i < deck.length; i++) {
  60. // Little-endian
  61. view.setUint32(i * 4, deck[i], true);
  62. }
  63. return btoa(String.fromCharCode(...new Uint8Array(buffer)));
  64. }
  65. function ydkeDecode(encodedDeck) {
  66. const binaryString = atob(encodedDeck);
  67. const buffer = new ArrayBuffer(binaryString.length);
  68. const view = new DataView(buffer);
  69. for (let i = 0; i < binaryString.length; i++) {
  70. view.setUint8(i, binaryString.charCodeAt(i));
  71. }
  72. const deck = [];
  73. for (let i = 0; i < buffer.byteLength; i += 4) {
  74. deck.push(String(view.getUint32(i, true))); // Little-endian
  75. }
  76. return deck;
  77. }
  78. function parseYdke(ydkeString) {
  79. const [mainEncoded, extraEncoded, sideEncoded] = ydkeString.split('!');
  80. const main = ydkeDecode(mainEncoded);
  81. const extra = ydkeDecode(extraEncoded);
  82. const side = ydkeDecode(sideEncoded);
  83. return { main, extra, side };
  84. }
  85. function genYdke(deck) {
  86. const mainEncoded = ydkeEncode(deck.main);
  87. const extraEncoded = ydkeEncode(deck.extra);
  88. const sideEncoded = ydkeEncode(deck.side);
  89. return `${mainEncoded}!${extraEncoded}!${sideEncoded}`;
  90. }
  91. function cardImageUrl(id) {
  92. if (id.length <= 8) {
  93. return "https://cdn.233.momobako.com/ygopro/pics/" + id + '.jpg';
  94. }
  95. return 'https://cdn02.moecube.com:444/ygopro-super-pre/data/pics/' + id + '.jpg';
  96. }
  97. export {
  98. parseYdk,
  99. genYdk,
  100. parseYdke,
  101. genYdke,
  102. downloadStringAsFile,
  103. cardImageUrl,
  104. };