/src/static/js/d20item.js

https://github.com/nt3rp/d20-character · JavaScript · 132 lines · 113 code · 11 blank · 8 comment · 7 complexity · 168eb57f2feb1291cedbedfa0692db74 MD5 · raw file

  1. var d20Item = Backbone.Model.extend({
  2. defaults: function() {
  3. return {
  4. weight: 0,
  5. slot: d20Equipment.SLOT.slotless,
  6. description: '',
  7. name: 'Unknown Item',
  8. equipped: false
  9. }
  10. }
  11. });
  12. var d20Equipment = Backbone.Collection.extend({
  13. model: d20Item,
  14. list: function(config) {
  15. var results;
  16. if (!config) {
  17. results = this.filter(function() {
  18. return true;
  19. });
  20. } else {
  21. results = this.where(config);
  22. }
  23. return _.invoke(results, 'toJSON');
  24. },
  25. equip: function(item) {
  26. // Takes in an item, or the name of an item
  27. var foundItem = this.get(item),
  28. slotCount, slot;
  29. if (!foundItem) {
  30. foundItem = (this.where({'name': item}) || [])[0]; // `_.findWhere` not available :(
  31. }
  32. // TODO: Do we want to throw an exception? Or just return at this point?
  33. if (_.isUndefined(foundItem)) {
  34. throw new Error('Item not found');
  35. }
  36. // TODO: Do we want to throw an exception? Or just return at this point?
  37. if (foundItem.get('equipped')) {
  38. throw new Error('Item already equipped');
  39. }
  40. // Check if item slot is full
  41. slot = foundItem.get('slot');
  42. slotCount = this.select(function(elem) {
  43. return (elem.get('equipped') === true) &&
  44. _.isEqual(slot, elem.get('slot'));
  45. }).length;
  46. if (slotCount >= slot.limit) {
  47. throw new Error(
  48. 'Exceeded slot limit equipping \'' + foundItem.get('name') + '\'' +
  49. 'to \'' + slot.name + '\' (Limit: ' + slot.limit+ ', Equipped: ' + slotCount + ')'
  50. );
  51. }
  52. foundItem.set('equipped', true);
  53. }
  54. }, {
  55. //TODO: Replace plain old javascript objects with Models? http://stackoverflow.com/a/6351460/165988
  56. SLOT: { //TODO: What properties? Limits maybe? Are limits based on race?
  57. 'armor': {
  58. 'name': 'armor',
  59. 'limit': 1 // TODO: Should limits be optional; assumed to be 1?
  60. },
  61. 'belt': {
  62. 'name': 'belt',
  63. 'limit': 1
  64. },
  65. 'body': {
  66. 'name': 'body',
  67. 'limit': 1
  68. },
  69. 'chest': {
  70. 'name': 'chest',
  71. 'limit': 1
  72. },
  73. 'eyes': {
  74. 'name': 'eyes',
  75. 'limit': 1
  76. },
  77. 'feet': {
  78. 'name': 'feet',
  79. 'limit': 1
  80. },
  81. 'hands': {
  82. 'name': 'hands',
  83. 'limit': 1
  84. },
  85. 'head': {
  86. 'name': 'head',
  87. 'limit': 1
  88. },
  89. 'headband': {
  90. 'name': 'headband',
  91. 'limit': 1
  92. },
  93. 'neck': {
  94. 'name': 'neck',
  95. 'limit': 1
  96. },
  97. 'ring': {
  98. 'name': 'ring',
  99. 'limit': 2
  100. },
  101. 'shield': {
  102. 'name': 'shield',
  103. 'limit': 1
  104. },
  105. 'shoulders': {
  106. 'name': 'shoulders',
  107. 'limit': 1
  108. },
  109. 'wrist': {
  110. 'name': 'wrist',
  111. 'limit': 1
  112. },
  113. 'slotless': {
  114. 'name': 'slotless',
  115. 'limit': 1
  116. }
  117. }
  118. });
  119. // TODO: Is a character's gear different than a general item collection?
  120. // TODO: How to handle equipped gear vs. held gear?
  121. // TODO: How to handle weapons?