cards.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function replaceAll(find, replace, str)
  2. {return str.replace(new RegExp(find, 'g'), replace);}
  3. function Card(newName, newSetId, newSetName, newCost, newNote)
  4. {
  5. this.name = newName;
  6. this.setId=newSetId;
  7. this.setName=newSetName.replace(" ", "").toLowerCase();
  8. this.cost=newCost;
  9. this.note=newNote;
  10. }
  11. Card.prototype.getImage = function()
  12. {
  13. return "images/" + this.setName + "/" + replaceAll(' ', '', this.name).replace('\'', "") + ".jpg";
  14. };
  15. Card.prototype.drawTableCell = function(drawName, drawImage)
  16. {
  17. var element = "<td class='cardCell " + this.setName + "'>";
  18. if (drawName)
  19. {element = element + this.name;}
  20. if (this.note != null)
  21. {element = element + " (" + this.note + ")";}
  22. if (drawImage)
  23. {element = element + "<img class='cardImg' src='" + this.getImage() + "' />";}
  24. element = element + "</td>";
  25. return element;
  26. };
  27. function makeSortFn(keys, fns, i)
  28. {
  29. var fn;
  30. switch (keys.toLowerCase())
  31. {
  32. case "set":
  33. fn = function (a, b)
  34. {
  35. if (a.setId > b.setId)
  36. {return 1;}
  37. else if (a.setId < b.setId)
  38. {return -1;}
  39. return fns[i+1](a, b);
  40. };
  41. break;
  42. case "cost":
  43. fn = function (a, b)
  44. {
  45. if (a.cost > b.cost)
  46. {return 1;}
  47. else if (a.cost < b.cost)
  48. {return -1;}
  49. return fns[i+1](a, b);
  50. };
  51. break;
  52. case "name":
  53. fn = function (a, b)
  54. {
  55. var tmp = a.name.localeCompare(b.name);
  56. if (tmp > 0)
  57. {return 1;}
  58. else if (tmp < 0)
  59. {return -1;}
  60. return fns[i+1](a, b);
  61. };
  62. break;
  63. }
  64. return fn;
  65. }
  66. function sortCards(cardArray, keys)
  67. {
  68. if (typeof(cardArray) == typeof(undefined))
  69. {
  70. return;
  71. }
  72. var fns = new Array;
  73. fns[keys.length] = function(a, b) {return 0};
  74. for (var i = 0; i < keys.length; i++)
  75. {fns[i] = makeSortFn(keys[i], fns, i);}
  76. return cardArray.sort(fns[0]);
  77. }