Skip to content

Commit

Permalink
Добавлен метод getOwnedGames
Browse files Browse the repository at this point in the history
  • Loading branch information
atreslesne committed Dec 14, 2017
1 parent 66dad08 commit 4ec80c4
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,38 @@ Team Fortress 2
date: 0
}]
```

### getOwnedGames

Метод возвращает информацию об играх пользователя.
Для вызова метода необходим ключ Steam API Key.

`getOwnedGames(playerId)`

* `playerId` - идентификатор пользователя.

```js
const Steam = require('steam-web-api');
const steam = new Steam('STEAM_API_KEY');

steam.getOwnedGames('76561198030288194')
.then(result => {
console.dir(result.games);
})
.catch(err => console.error(err.message));
```

Результат:

```js
[{
id: 220,
name: 'Half-Life 2',
playtime: {
'forever': 1060,
'2weeks': 0
},
icon: 'http://media.steampowered.com/steamcommunity/public/images/apps/220/fcfb366051782b8ebf2aa297f3b746395858cb62.jpg',
logo: 'http://media.steampowered.com/steamcommunity/public/images/apps/220/e4ad9cf1b7dc8475c1118625daf9abd4bdcbcad0.jpg'
}]
```
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class Steam {
'steamid': playerId
});
}

getOwnedGames(playerId) {
return this.client.request('getOwnedGames', {
'steamid': playerId
});
}
}

module.exports = Steam;
16 changes: 16 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const EntityAchievements = require('./entity/entityAchievements');
const EntityPlayers = require('./entity/entityPlayers');
const EntityFriends = require('./entity/entityFriends');
const EntityPlayerAchievements = require('./entity/entityPlayerAchievements');
const EntityGames = require('./entity/entityGames');

class SteamClient {
static get methods() {
Expand Down Expand Up @@ -80,6 +81,21 @@ class SteamClient {
'defaults': {},
'key': true,
'entity': (data, method, url) => { return new EntityPlayerAchievements(data, method, url); }
},
'getOwnedGames': {
'name': 'getOwnedGames',
'url': {
'protocol': 'http',
'host': 'api.steampowered.com',
'path': '/IPlayerService/GetOwnedGames/v0001/'
},
'args': ['steamid', 'format', 'include_appinfo'],
'defaults': {
'format': 'json',
'include_appinfo': '1'
},
'key': true,
'entity': (data, method, url) => { return new EntityGames(data, method, url); }
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/entity/entityGames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const Entity = require('./entity');

class EntityGames extends Entity {
constructor(rawData, method, url) {
super(rawData, method, url);

let data = [];
for (let index in rawData['response']['games']) {
let item = rawData['response']['games'][index];
data.push({
'id': item['appid'],
'name': item['name'],
'playtime': {
'forever': item['playtime_forever'],
'2weeks': (item['playtime_2weeks'] ? item['playtime_2weeks'] : 0)
},
'icon': `http://media.steampowered.com/steamcommunity/public/images/apps/${item['appid']}/${item['img_icon_url']}.jpg`,
'logo': `http://media.steampowered.com/steamcommunity/public/images/apps/${item['appid']}/${item['img_logo_url']}.jpg`,
});
}

this.games = data;
}
}

module.exports = EntityGames;
13 changes: 13 additions & 0 deletions test/testSteam.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const EntityAchievements = require('../src/entity/entityAchievements');
const EntityPlayers = require('../src/entity/entityPlayers');
const EntityFriends = require('../src/entity/entityFriends');
const EntityPlayerAchievements = require('../src/entity/entityPlayerAchievements');
const EntityGames = require('../src/entity/entityGames');

const STEAM_KEY = process.env.STEAM_KEY;

Expand Down Expand Up @@ -90,4 +91,16 @@ describe('Steam', function () {
})
.catch(err => done(err));
});

it('getOwnedGames', (done) => {
steam.getOwnedGames('76561198030288194')
.then(res => {
assert.instanceOf(res, EntityGames);
assert.equal(res.url, 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?steamid=76561198030288194&key=' + STEAM_KEY + '&format=json&include_appinfo=1');
assert.equal(res.method, 'getOwnedGames');
assert.isTrue(res.games.length > 0);
done();
})
.catch(err => done(err));
});
});

0 comments on commit 4ec80c4

Please sign in to comment.