Advertisement
Serafim

Untitled

Aug 14th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Equipment, ItemsRegistry,
  2.   __hasProp = {}.hasOwnProperty,
  3.   __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  4.  
  5. window.Item = (function(_super) {
  6.   __extends(Item, _super);
  7.  
  8.   function Item(data) {
  9.     data.price = data.funds;
  10.     delete data.funds;
  11.     this.data = data;
  12.     this.setProperties(data);
  13.   }
  14.  
  15.   Item.prototype.buy = function(cb, count) {
  16.     var _this = this;
  17.     if (cb == null) {
  18.       cb = (function() {});
  19.     }
  20.     if (count == null) {
  21.       count = 1;
  22.     }
  23.     return Server.post('items/' + this.id() + '/purchase.json', {
  24.       'count': count
  25.     }, function(data) {
  26.       if (data.status === "success") {
  27.         _this.count(data.remain);
  28.         ko.mapping.fromJS(Object.select(data, 'balance'), currentPlayer);
  29.         currentPlayer.balance.total_balance(currentPlayer.balance.total_balance() - (_this.price.upkeep() * count));
  30.       }
  31.       return cb(data);
  32.     });
  33.   };
  34.  
  35.   Item.prototype.sell = function(cb, count) {
  36.     var _this = this;
  37.     if (cb == null) {
  38.       cb = (function() {});
  39.     }
  40.     if (count == null) {
  41.       count = 1;
  42.     }
  43.     return Server.post('items/' + this.id() + '/sale.json', {
  44.       'count': count
  45.     }, function(data) {
  46.       if (data.status === "success") {
  47.         _this.count(data.remain);
  48.         ko.mapping.fromJS(Object.select(data, 'balance'), currentPlayer);
  49.         currentPlayer.balance.total_balance(currentPlayer.balance.total_balance() + (_this.price.upkeep() * count));
  50.       }
  51.       return cb(data);
  52.     });
  53.   };
  54.  
  55.   Item.prototype.dificitPrice = function(count) {
  56.     if (count == null) {
  57.       count = 1;
  58.     }
  59.     return I18n.notify.error.money + ' <span class="text-money">' + ((this.price.buy() * count) - currentPlayer.balance.cash()) + '</span>';
  60.   };
  61.  
  62.   Item.struct = function() {
  63.     return {
  64.       id: 0,
  65.       title: '',
  66.       type: '',
  67.       level: 0,
  68.       icon: {
  69.         x40: '',
  70.         x70: '',
  71.         x138: ''
  72.       },
  73.       attack: 0,
  74.       defence: 0,
  75.       price: {
  76.         buy: 0,
  77.         sell: 0,
  78.         upkeep: 0
  79.       },
  80.       count: 0
  81.     };
  82.   };
  83.  
  84.   return Item;
  85.  
  86. })(AbstractModel);
  87.  
  88. ItemsRegistry = (function() {
  89.   function ItemsRegistry() {}
  90.  
  91.   ItemsRegistry.items = [];
  92.  
  93.   ItemsRegistry.append = function(item) {
  94.     return ItemsRegistry.items.push(new Item(item));
  95.   };
  96.  
  97.   return ItemsRegistry;
  98.  
  99. }).call(this);
  100.  
  101. Equipment = (function() {
  102.   function Equipment(items) {
  103.     var i, properties, _i, _len, _ref;
  104.     properties = items.p;
  105.     _ref = items.s;
  106.     for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  107.       i = _ref[_i];
  108.       i.count = properties[i.id] || 0;
  109.       ItemsRegistry.append(i);
  110.     }
  111.   }
  112.  
  113.   Equipment.prototype.asActiveRecord = function(logs) {
  114.     var result, special;
  115.     if (logs == null) {
  116.       logs = true;
  117.     }
  118.     special = $.extend(true, {}, this.asSpecial);
  119.     special.constructor = window.ActiveRecord;
  120.     result = this.asSpecial || new window.ActiveRecord(ItemsRegistry.items, logs);
  121.     this.asSpecial = null;
  122.     return result;
  123.   };
  124.  
  125.   Equipment.prototype.exclude = function(exclusions) {
  126.     var items;
  127.     items = ItemsRegistry.items.slice(0).remove(function(item) {
  128.       return exclusions.any(function(exclusion) {
  129.         return (typeof item.title === 'function' ? item.title() : item.title.toString()()) === exclusion;
  130.       });
  131.     });
  132.     this.asSpecial = new window.ActiveRecord(items, false);
  133.     return this;
  134.   };
  135.  
  136.   Equipment.prototype.getById = function(id) {
  137.     return this.asActiveRecord(false).where('id==%1', [id]).limit(1).execute()[0];
  138.   };
  139.  
  140.   Equipment.prototype.getByLevel = function(level) {
  141.     return this.asActiveRecord(false).where('level==%1', [level]).execute();
  142.   };
  143.  
  144.   Equipment.prototype.getBoughtByType = function(type) {
  145.     return this.asActiveRecord().where('type==%1 && count>0', [type]).order('level count', 'desc').execute();
  146.   };
  147.  
  148.   Equipment.prototype.getPreviewItems = function(type) {
  149.     var avl, dsbl;
  150.     avl = this.asActiveRecord().where('type==%1 && level<=%2 && price.buy>0', [type, currentPlayer.indicator.level()]).order('level').desc().execute();
  151.     dsbl = this.asActiveRecord().where('type==%1 && level>%2  && price.buy>0', [type, currentPlayer.indicator.level()]).order('level').limit(2).desc().execute();
  152.     return dsbl.concat(avl);
  153.   };
  154.  
  155.   Equipment.prototype.getAll = function() {
  156.     return ItemsRegistry.items;
  157.   };
  158.  
  159.   Equipment.prototype.getByType = function(type) {
  160.     if (type !== "peoples" && type !== "documents" && type !== "organizations") {
  161.       new Exception('Undefined type in Equipment::getByType');
  162.     } else {
  163.       return this.asActiveRecord().where('type==%1', [type]).order('id').execute();
  164.     }
  165.   };
  166.  
  167.   return Equipment;
  168.  
  169. })();
  170.  
  171. window.Models.register('Equipment', Equipment);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement