SKarajic /
LoLNode
| 1 | View Code Duplication | import { getJSON, getRegion } from './utils'; |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 2 | import { SummonerMastery } from './mastery'; |
||
| 3 | import { RegionDomains } from './regions'; |
||
| 4 | import { Summoner } from './summoner'; |
||
| 5 | import { Cache } from './cache'; |
||
| 6 | import { Realm } from './realm'; |
||
| 7 | import { Icon } from './icon'; |
||
| 8 | var LoLNode = /** @class */ (function () { |
||
| 9 | /** |
||
| 10 | * Represents the Riot API Wrapper Object. |
||
| 11 | * @constructor |
||
| 12 | * @param {string} APIKEY - Riot API Key |
||
| 13 | */ |
||
| 14 | function LoLNode(APIKEY) { |
||
| 15 | this.cacheList = {}; |
||
| 16 | this.APIKEY = APIKEY; |
||
| 17 | for (var region in RegionDomains) { |
||
|
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty.
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
| 18 | if (this.cacheList[region] !== {}) { |
||
| 19 | this.cacheList[region] = {}; |
||
| 20 | } |
||
| 21 | } |
||
| 22 | } |
||
| 23 | /** |
||
| 24 | * @function |
||
| 25 | * Creates a Realm Object of said region |
||
| 26 | * @param {string} region - Region being called |
||
| 27 | * @callback Realm - Returns a Realm Object |
||
| 28 | */ |
||
| 29 | LoLNode.prototype.realm = function (region, callback) { |
||
| 30 | var _this = this; |
||
| 31 | region = region.toUpperCase(); |
||
| 32 | var realmCache = this.cacheList[region].realm; |
||
| 33 | var cacheState; |
||
| 34 | if (typeof realmCache === 'undefined') { |
||
| 35 | cacheState = 0; |
||
| 36 | } |
||
| 37 | else if (realmCache.get() === null) { |
||
| 38 | cacheState = 1; |
||
| 39 | } |
||
| 40 | else { |
||
| 41 | cacheState = 2; |
||
| 42 | } |
||
| 43 | if (cacheState < 2) { |
||
| 44 | var regionURL = getRegion(region); |
||
| 45 | var url = regionURL + "/lol/static-data/v3/realms?api_key=" + this.APIKEY; |
||
| 46 | getJSON(url, function (res, err) { |
||
| 47 | if (err) { |
||
| 48 | callback(null, err); |
||
| 49 | } |
||
| 50 | else { |
||
| 51 | var data = res.data; |
||
| 52 | var realm = new Realm(data.lg, data.dd, data.l, data.n, data.profileiconmax, data.v, data.cdn, data.css); |
||
| 53 | cacheState === 1 ? |
||
| 54 | _this.cacheList[region].realm.refresh(realm) : |
||
| 55 | _this.cacheList[region].realm = new Cache(realm, 1800); |
||
| 56 | callback(realm); |
||
| 57 | } |
||
| 58 | }); |
||
| 59 | } |
||
| 60 | else { |
||
| 61 | callback(realmCache.get()); |
||
| 62 | } |
||
| 63 | }; |
||
| 64 | /** |
||
| 65 | * Creates a Summoner Object of said region |
||
| 66 | * @param {string} region - Region being called |
||
| 67 | * @param {array} identifier Identification for |
||
| 68 | * how you enter your data. |
||
| 69 | * _________________________ |
||
| 70 | * structure => [type, val] |
||
| 71 | * |
||
| 72 | * - type => account|name|id |
||
| 73 | * - - account => The Account ID |
||
| 74 | * - - name => The Summoner Name |
||
| 75 | * - - id => The Summoner ID |
||
| 76 | * |
||
| 77 | * - val => Either a string containing the name or |
||
| 78 | * a number containing the Account ID or Summoner ID |
||
| 79 | * |
||
| 80 | * @param callback - Returns a Summoner Object |
||
| 81 | * @callback Summoner - Returns a Summoner Object |
||
| 82 | */ |
||
| 83 | LoLNode.prototype.summoner = function (region, identifier, callback) { |
||
| 84 | var _this = this; |
||
| 85 | region = region.toUpperCase(); |
||
| 86 | var regionURL = getRegion(region); |
||
| 87 | var url = regionURL + "/lol/summoner/v3/summoners"; |
||
| 88 | switch (identifier[0]) { |
||
|
0 ignored issues
–
show
|
|||
| 89 | case 'account': |
||
| 90 | url += "/by-account/" + identifier[1] + "?api_key=" + this.APIKEY; |
||
| 91 | break; |
||
| 92 | case 'name': |
||
| 93 | url += "/by-name/" + identifier[1] + "?api_key=" + this.APIKEY; |
||
| 94 | break; |
||
| 95 | case 'id': |
||
| 96 | url += "/" + identifier[1] + "?api_key=" + this.APIKEY; |
||
| 97 | break; |
||
| 98 | } |
||
| 99 | getJSON(url, function (res, err) { |
||
| 100 | if (err) { |
||
| 101 | callback(null, err); |
||
| 102 | } |
||
| 103 | else { |
||
| 104 | var data_1 = res.data; |
||
| 105 | _this.realm(region, function (_a) { |
||
| 106 | var dd = _a.dd, cdn = _a.cdn; |
||
| 107 | callback(new Summoner(data_1.id, data_1.name, new Icon(data_1.profileIconId, dd, cdn), data_1.summonerLevel, data_1.accountId, data_1.revisionDate, new SummonerMastery(_this.APIKEY, region, data_1.id))); |
||
| 108 | }); |
||
| 109 | } |
||
| 110 | }); |
||
| 111 | }; |
||
| 112 | /** |
||
| 113 | * |
||
| 114 | * @param region |
||
| 115 | * @param identifier |
||
| 116 | * @param type |
||
| 117 | * @param callback |
||
| 118 | */ |
||
| 119 | LoLNode.prototype.mastery = function (region, identifier, type, callback) { |
||
| 120 | this.summoner(region, identifier, function (summoner) { |
||
| 121 | switch (type[0]) { |
||
| 122 | case 'level': |
||
| 123 | summoner.mastery.level(function (level) { return callback(level); }); |
||
| 124 | break; |
||
| 125 | case 'points': |
||
| 126 | summoner.mastery.level(function (points) { return callback(points); }); |
||
| 127 | break; |
||
| 128 | case 'top': |
||
| 129 | var size = void 0; |
||
|
0 ignored issues
–
show
|
|||
| 130 | type.length > 1 ? size = type[1] : size = 0; |
||
| 131 | summoner.mastery.top(size, function (data) { return callback(data); }); |
||
| 132 | break; |
||
| 133 | case 'champion': |
||
| 134 | summoner.mastery.champion(type[1], function (data) { return callback(data); }); |
||
| 135 | break; |
||
| 136 | default: |
||
| 137 | callback(null, 'incorrect type to mastery method in the LoLNode object'); |
||
| 138 | } |
||
| 139 | }); |
||
| 140 | }; |
||
| 141 | /** |
||
| 142 | * |
||
| 143 | * @param region |
||
| 144 | * @param value |
||
| 145 | * - test |
||
| 146 | * -- test2 |
||
| 147 | * @param callback |
||
| 148 | */ |
||
| 149 | LoLNode.prototype.champion = function (region, value, options, callback) { |
||
| 150 | callback(); |
||
| 151 | }; |
||
| 152 | return LoLNode; |
||
| 153 | }()); |
||
| 154 | export { LoLNode }; |
||
| 155 | //# sourceMappingURL=index.js.map |