Permalink
Browse files

Finish up for release

User-Agent
Fixed test
Added Matches
Made all endpoints return objects (except for replay)
Various other fixes
  • Loading branch information...
1 parent 893dbd5 commit 809d2aa52483e5b71fb70b3ac46eb05951fe070b @brussell98 committed Aug 28, 2016
Showing with 609 additions and 72 deletions.
  1. +3 −2 .eslintrc
  2. +11 −8 index.js
  3. +27 −6 lib/Api.js
  4. +1 −1 lib/Base/Beatmap.js
  5. +30 −0 lib/Base/Game.js
  6. +8 −1 lib/Base/Match.js
  7. +28 −0 lib/Base/MultiplayerScore.js
  8. +1 −0 lib/Base/Score.js
  9. +54 −35 lib/Constants.js
  10. +3 −2 package.json
  11. +349 −0 test/sampleMatch.json
  12. +94 −17 test/test.js
View
@@ -9,7 +9,7 @@
"modules": true
},
"rules": {
- "no-console": 0,
+ "no-console": 1,
"no-control-regex": 0,
"no-alert": 1,
"no-else-return": 1,
@@ -22,7 +22,8 @@
"space-before-function-paren": ["warn", "never"],
"arrow-spacing": 1,
"comma-spacing": ["warn", { "before": false, "after": true }],
- "no-mixed-spaces-and-tabs": ["error", "smart-tabs"]
+ "no-mixed-spaces-and-tabs": ["error", "smart-tabs"],
+ "indent": ["error", "tab"]
},
extends: "eslint:recommended"
}
View
@@ -1,10 +1,13 @@
"use strict";
-const Api = require('./lib/Api.js'),
- Constants = require('./lib/Constants.js'),
- Beatmap = require('./lib/Base/Beatmap.js'),
- Score = require('./lib/Base/Score.js'),
- User = require('./lib/Base/User.js'),
- Event = require('./lib/Base/Event.js');
-
-module.exports = {Api, Constants, Beatmap, Score, User, Event};
+module.exports = {
+ Api: require('./lib/Api.js'),
+ Constants: require('./lib/Constants.js'),
+ Beatmap: require('./lib/Base/Beatmap.js'),
+ Score: require('./lib/Base/Score.js'),
+ User: require('./lib/Base/User.js'),
+ Match: require('./lib/Base/Match.js'),
+ Game: require('./lib/Base/Game.js'),
+ MultiplayerScore: require('./lib/Base/MultiplayerScore.js'),
+ Event: require('./lib/Base/Event.js')
+};
View
@@ -1,8 +1,10 @@
"use strict";
-var request = require('superagent'),
+const request = require('superagent'),
+ userAgent = `node-osu v${require('../package.json').version} (https://github.com/brussell98/node-osu)`,
Beatmap = require('./Base/Beatmap.js'),
Score = require('./Base/Score.js'),
+ Match = require('./Base/Match.js'),
User = require('./Base/User.js');
class Api {
@@ -18,11 +20,12 @@ class Api {
return reject(new Error('apiKey not set'));
options['k'] = this.apiKey;
request.get(this.baseUrl + endpoint)
+ .set('User-Agent', userAgent)
.query(options)
.end((error, response) => {
- if (!error && response.status === 200) {
+ if (!error && response.status === 200)
resolve(response.body);
- } else
+ else
reject(new Error(error.status || error.response));
});
});
@@ -51,23 +54,41 @@ class Api {
getScores(options) {
return new Promise((resolve, reject) => {
this.apiCall('/get_scores', options).then(response => {
+ if (response.length === 0)
+ return reject(new Error('No scores'));
resolve(response.map(sc => new Score(sc)));
}).catch(reject);
});
}
getUserBest(options) {
- return this.apiCall('/get_user_best', options);
+ return new Promise((resolve, reject) => {
+ this.apiCall('/get_user_best', options).then(response => {
+ if (response.length === 0)
+ return reject(new Error('No scores'));
+ resolve(response.map(sc => new Score(sc)));
+ }).catch(reject);
+ });
}
getUserRecent(options) {
- return this.apiCall('/get_user_recent', options);
+ return new Promise((resolve, reject) => {
+ this.apiCall('/get_user_recent', options).then(response => {
+ if (response.length === 0)
+ return reject(new Error('No scores'));
+ resolve(response.map(sc => new Score(sc)));
+ }).catch(reject);
+ });
}
getMatch(options) {
return new Promise((resolve, reject) => {
this.apiCall('/get_match', options).then(response => {
- resolve(response[0]);
+ if (response.length === 0)
+ return reject(new Error('Match not found'));
+ if (response[0].match === 0)
+ return reject(new Error('Match too old'));
+ resolve(new Match(response[0]));
}).catch(reject);
});
}
View
@@ -20,7 +20,7 @@ class Beatmap {
this.mode = Constants.Beatmaps.mode[data.mode];
this.tags = data.tags.split(' ');
this.approvalStatus = Constants.Beatmaps.approved[data.approved];
- this.approvedDate = data.approvedDate ? new Date(Date.parse(data.approvedDate) - 28800000 - (new Date().getTimezoneOffset() * 60000)) : null; // UTC+8
+ this.approvedDate = data.approved_date ? new Date(Date.parse(data.approved_date) - 28800000 - (new Date().getTimezoneOffset() * 60000)) : null; // UTC+8
this.lastUpdate = new Date(Date.parse(data.lastUpdate) - 28800000 - (new Date().getTimezoneOffset() * 60000)); // UTC+8
this.beatmapSetId = data.beatmapset_id;
this.maxCombo = data.max_combo;
View
@@ -0,0 +1,30 @@
+"use strict";
+
+const MultiplayerScore = require('./MultiplayerScore.js'),
+ Constants = require('../Constants.js');
+
+class Game {
+
+ constructor(data) {
+ this.id = data.game_id;
+ this.start = new Date(Date.parse(data.start_time) - 28800000 - (new Date().getTimezoneOffset() * 60000)); // UTC+8
+ this.end = data.end_time === null ? null : new Date(Date.parse(data.end_time) - 28800000 - (new Date().getTimezoneOffset() * 60000)); // UTC+8
+ 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.mods = [];
+ for (let mod in Constants.Mods) {
+ if (data.mods & Constants.Mods[mod])
+ this.mods.push(mod);
+ }
+
+
+ this.scores = data.scores.map(g => new MultiplayerScore(g));
+ }
+
+}
+
+module.exports = Game;
View
@@ -1,9 +1,16 @@
"use strict";
+const Game = require('./Game.js');
+
class Match {
constructor(data) {
- this.data = 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)); // UTC+8
+ this.end = data.end_time === null ? null : new Date(Date.parse(data.match.end_time) - 28800000 - (new Date().getTimezoneOffset() * 60000)); // UTC+8
+
+ this.games = data.games.map(g => new Game(g));
}
}
@@ -0,0 +1,28 @@
+"use strict";
+
+const Team = require('../Constants.js').Multiplayer.team
+
+class MultiplayerScore {
+
+ constructor(data) {
+ this.slot = data.slot;
+ this.team = Team[data.team];
+ this.userId = data.user_id;
+ this.score = data.score;
+ this.maxCombo = data.maxcombo;
+ this.rank = null; // Not used
+ this.counts = {
+ '300': data.count300,
+ '100': data.count100,
+ '50': data.count50,
+ 'geki': data.countgeki,
+ 'katu': data.countkatu,
+ 'miss': data.countmiss
+ };
+ this.perfect = data.perfect === '1';
+ this.pass = data.pass === '1';
+ }
+
+}
+
+module.exports = MultiplayerScore;
View
@@ -10,6 +10,7 @@ class Score {
'name': data.username,
'id': data.user_id
}
+ this.beatmapId = data.beatmap_id || null;
this.counts = {
'300': data.count300,
'100': data.count100,
View
@@ -2,37 +2,37 @@
module.exports = {
Mods: {
- "None" : 0,
- "NoFail" : 1,
- "Easy" : 2,
- "NoVideo" : 4,
- "Hidden" : 8,
- "HardRock" : 16,
- "SuddenDeath" : 32,
- "DoubleTime" : 64,
- "Relax" : 128,
- "HalfTime" : 256,
- "Nightcore" : 512,
- "Flashlight" : 1024,
- "Autoplay" : 2048,
- "SpunOut" : 4096,
- "Autopilot" : 8192,
- "Perfect" : 16384,
- "Key4" : 32768,
- "Key5" : 65536,
- "Key6" : 131072,
- "Key7" : 262144,
- "Key8" : 524288,
- "keyMod" : 1015808,
- "FadeIn" : 1048576,
- "Random" : 2097152,
- "LastMod" : 4194304,
- "FreeModAllowed": 2077883,
- "Key9" : 16777216,
- "Key10" : 33554432,
- "Key1" : 67108864,
- "Key3" : 134217728,
- "Key2" : 268435456
+ "None" : 0,
+ "NoFail" : 1,
+ "Easy" : 2,
+ "NoVideo" : 4,
+ "Hidden" : 8,
+ "HardRock" : 16,
+ "SuddenDeath" : 32,
+ "DoubleTime" : 64,
+ "Relax" : 128,
+ "HalfTime" : 256,
+ "Nightcore" : 512,
+ "Flashlight" : 1024,
+ "Autoplay" : 2048,
+ "SpunOut" : 4096,
+ "Autopilot" : 8192,
+ "Perfect" : 16384,
+ "Key4" : 32768,
+ "Key5" : 65536,
+ "Key6" : 131072,
+ "Key7" : 262144,
+ "Key8" : 524288,
+ "keyMod" : 1015808,
+ "FadeIn" : 1048576,
+ "Random" : 2097152,
+ "LastMod" : 4194304,
+ "FreeModAllowed": 2077883,
+ "Key9" : 16777216,
+ "Key10" : 33554432,
+ "Key1" : 67108864,
+ "Key3" : 134217728,
+ "Key2" : 268435456
},
URLSchemas: {
multiplayerMatch: (id, password) => `osu://mp/${id}${password !== undefined ? '/' + password : ''}`,
@@ -42,15 +42,15 @@ module.exports = {
spectate: (user) => `osu://spectate/${user}`
},
Beatmaps: {
- "approved": {
+ approved: {
"-2": "Graveyard",
"-1": "WIP",
"0": "Pending",
"1": "Ranked",
"2": "Approved",
"3": "Qualified"
},
- "genre": {
+ genre: {
"0": "Any",
"1": "Unspecified",
"2": "Video Game",
@@ -62,7 +62,7 @@ module.exports = {
"9": "Hip Hop",
"10": "Electronic"
},
- "language": {
+ language: {
"0": "Any",
"1": "Other",
"2": "English",
@@ -76,11 +76,30 @@ module.exports = {
"10": "Spanish",
"11": "Italian"
},
- "mode": {
+ mode: {
"0": "Standard",
"1": "Taiko",
"2": "Catch the Beat",
"3": "Mania"
}
+ },
+ Multiplayer: {
+ scoringType: {
+ "0": "Score",
+ "1": "Accuracy",
+ "2": "Combo",
+ "3": "Score v2"
+ },
+ teamType: {
+ "0": "Head to Head",
+ "1": "Tag Co-op",
+ "2": "Team vs",
+ "3": "Tag Team vs"
+ },
+ team: {
+ "0": "None",
+ "1": "Blue",
+ "2": "Red"
+ }
}
}
View
@@ -4,7 +4,7 @@
"description": "An object-oriented library for easy interaction with the osu!api",
"main": "index.js",
"scripts": {
- "test": "./node_modules/.bin/mocha --reporter spec"
+ "test": "./node_modules/.bin/mocha -t 3000 --reporter spec"
},
"repository": {
"type": "git",
@@ -13,7 +13,8 @@
"keywords": [
"osu",
"api",
- "es6"
+ "es6",
+ "promise"
],
"author": "brussell98",
"license": "MIT",
Oops, something went wrong.

0 comments on commit 809d2aa

Please sign in to comment.