Permalink
Browse files

Switched some properties to getters

  • Loading branch information...
1 parent ceb4bfc commit d9e136ba1eda54cf793774222b7b5d0ca990066d @brussell98 committed Oct 19, 2016
Showing with 91 additions and 21 deletions.
  1. +1 −1 .eslintrc
  2. +16 −2 lib/Base/Beatmap.js
  3. +8 −1 lib/Base/Event.js
  4. +26 −8 lib/Base/Game.js
  5. +16 −2 lib/Base/Match.js
  6. +18 −4 lib/Base/Score.js
  7. +4 −1 lib/Base/User.js
  8. +1 −1 package.json
  9. +1 −1 test/test.js
View
@@ -23,7 +23,7 @@
"arrow-spacing": 1,
"comma-spacing": ["warn", { "before": false, "after": true }],
"no-mixed-spaces-and-tabs": ["error", "smart-tabs"],
- "indent": ["error", "tab"]
+ "indent": ["error", "tab", { "SwitchCase": 1 }]
},
extends: "eslint:recommended"
}
View
@@ -20,8 +20,8 @@ class Beatmap {
this.mode = Constants.Beatmaps.mode[data.mode];
this.tags = data.tags.split(' ');
this.approvalStatus = Constants.Beatmaps.approved[data.approved];
- this.approvedDate = data.approved_date ? new Date(Date.parse(data.approved_date) - 28800000 - (new Date().getTimezoneOffset() * 60000)) : null;
- this.lastUpdate = new Date(Date.parse(data.last_update) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ this.raw_approvedDate = data.approved_date;
+ this.raw_lastUpdate = data.last_update;
this.beatmapSetId = data.beatmapset_id;
this.maxCombo = data.max_combo;
this.difficulty = {
@@ -43,6 +43,20 @@ class Beatmap {
};
}
+ get approvedDate() {
+ if (this._approvedDate !== undefined)
+ return this._approvedDate;
+ this._approvedDate = this.raw_approvedDate ? new Date(Date.parse(this.raw_approvedDate) - 28800000 - (new Date().getTimezoneOffset() * 60000)) : null;
+ return this._approvedDate;
+ }
+
+ get lastUpdate() {
+ if (this._lastUpdate !== undefined)
+ return this._lastUpdate;
+ this._lastUpdate = new Date(Date.parse(this.raw_lastUpdate) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ return this._lastUpdate;
+ }
+
}
module.exports = Beatmap;
View
@@ -6,10 +6,17 @@ class Event {
this.html = data.display_html;
this.beatmapId = data.beatmap_id;
this.beatmapsetId = data.beatmapset_id;
- this.date = new Date(Date.parse(data.date) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ this.raw_date = data.date;
this.epicFactor = data.epicfactor; // 1-32
}
+ get date() {
+ if (this._date !== undefined)
+ return this._date;
+ this._date = new Date(Date.parse(this.raw_date) - 28800000 - (new Date().getTimezoneOffset() * 60000)); // UTC+8 so we subtract 8 hours to get the UTC time
+ return this._date;
+ }
+
}
module.exports = Event;
View
@@ -7,22 +7,40 @@ class Game {
constructor(data) {
this.id = data.game_id;
- this.start = new Date(Date.parse(data.start_time) - 28800000 - (new Date().getTimezoneOffset() * 60000));
- this.end = data.end_time === null ? null : new Date(Date.parse(data.end_time) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ this.raw_start = data.start_time;
+ this.raw_end = data.end_time;
this.beatmapId = data.beatmap_id;
this.mode = Constants.Beatmaps.mode[data.play_mode];
this.matchType = data.match_type; // Unknown
this.scoringType = Constants.Multiplayer.scoringType[data.scoring_type];
this.teamType = Constants.Multiplayer.teamType[data.team_type];
+ this.raw_mods = data.mods;
+ this.scores = data.scores.map(g => new MultiplayerScore(g));
+ }
- this.mods = [];
- for (let mod in Constants.Mods) {
- if (data.mods & Constants.Mods[mod])
- this.mods.push(mod);
- }
+ get start() {
+ if (this._start !== undefined)
+ return this._start;
+ this._start = new Date(Date.parse(this.raw_start) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ return this._start;
+ }
+ get end() {
+ if (this._end !== undefined)
+ return this._end;
+ this._end = new Date(Date.parse(this.raw_end) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ return this._end;
+ }
- this.scores = data.scores.map(g => new MultiplayerScore(g));
+ get mods() {
+ if (this._mods !== undefined)
+ return this._mods;
+ this._mods = [];
+ for (let mod in Constants.Mods) {
+ if (this.raw_mods & Constants.Mods[mod])
+ this._mods.push(mod);
+ }
+ return this._mods;
}
}
View
@@ -7,12 +7,26 @@ class Match {
constructor(data) {
this.id = data.match.match_id;
this.name = data.match.name;
- this.start = new Date(Date.parse(data.match.start_time) - 28800000 - (new Date().getTimezoneOffset() * 60000));
- this.end = data.match.end_time === null ? null : new Date(Date.parse(data.match.end_time) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ this.raw_start = data.match.start_time;
+ this.raw_end = data.match.end_time;
this.games = data.games.map(g => new Game(g));
}
+ get start() {
+ if (this._start !== undefined)
+ return this._start;
+ this._start = new Date(Date.parse(this.raw_start) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ return this._start;
+ }
+
+ get end() {
+ if (this._end !== undefined)
+ return this._end;
+ this._end = new Date(Date.parse(this.raw_end) - 28800000 - (new Date().getTimezoneOffset() * 60000));
+ return this._end;
+ }
+
}
module.exports = Match;
View
@@ -21,15 +21,29 @@ class Score {
};
this.maxCombo = data.maxcombo;
this.perfect = data.perfect === '1';
- this.date = new Date(Date.parse(data.date) - 28800000 - (new Date().getTimezoneOffset() * 60000)); // UTC+8 so we subtract 8 hours to get the UTC time
+ this.raw_date = data.date;
this.rank = data.rank;
this.pp = data.pp || null
- this.mods = [];
+ this.raw_mods = data.enabled_mods;
+ }
+
+ get date() {
+ if (this._date !== undefined)
+ return this._date;
+ this._date = new Date(Date.parse(this.raw_date) - 28800000 - (new Date().getTimezoneOffset() * 60000)); // UTC+8 so we subtract 8 hours to get the UTC time
+ return this._date;
+ }
+
+ get mods() {
+ if (this._mods !== undefined)
+ return this._mods;
+ this._mods = [];
for (let mod in Mods) {
- if (data.enabled_mods & Mods[mod])
- this.mods.push(mod);
+ if (this.raw_mods & Mods[mod])
+ this._mods.push(mod);
}
+ return this._mods;
}
}
View
@@ -28,10 +28,13 @@ class User {
this.country = data.country;
this.level = data.level;
this.accuracy = data.accuracy;
- this.accuracyFormatted = parseFloat(data.accuracy).toFixed(2) + '%';
this.events = data.events.map(ev => new Event(ev));
}
+ get accuracyFormatted() {
+ return parseFloat(this.accuracy).toFixed(2) + '%';
+ }
+
}
module.exports = User;
View
@@ -1,6 +1,6 @@
{
"name": "node-osu",
- "version": "1.1.0",
+ "version": "1.2.0",
"description": "An object-oriented library for easy interaction with the osu api",
"main": "index.js",
"scripts": {
View
@@ -99,7 +99,7 @@ describe('osu!api methods', function() {
expect(scores[0]).to.be.an.instanceOf(osu.Score);
expect(scores).to.satisfy(scores => scores.filter(s => s.score && s.beatmapId).length === scores.length);
expect(scores[4].date).to.be.a('date');
- expect(scores[7].perfect).to.be.a('boolean');
+ expect(scores[3].perfect).to.be.a('boolean');
expect(scores[2].mods).to.be.an('array');
}).catch(error => {
throw error;

0 comments on commit d9e136b

Please sign in to comment.