Skip to content

Commit d4bf460

Browse files
committed
Finishing up GetPlayerItem addresses #49
1 parent f0a91a3 commit d4bf460

3 files changed

Lines changed: 67 additions & 55 deletions

File tree

src/Syntax/SteamApi/Containers/Item.php

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,53 @@
44

55
class Item extends BaseContainer
66
{
7-
public $appId;
7+
public $id;
88

9-
public $name;
9+
public $originalId;
1010

11-
public $playtimeTwoWeeks;
11+
public $defIndex;
1212

13-
public $playtimeTwoWeeksReadable;
13+
public $level;
1414

15-
public $playtimeForever;
15+
public $quality;
1616

17-
public $playtimeForeverReadable;
17+
public $quantity;
1818

19-
public $icon;
19+
public $inventory;
2020

21-
public $logo;
21+
public $origin;
2222

23-
public $header;
23+
public $flags;
2424

25-
public $hasCommunityVisibleStats;
25+
public $containedItem;
2626

27-
public function __construct($app)
28-
{
29-
$this->appId = $app->appid;
30-
$this->name = $this->checkIssetField($app, 'name');
31-
$this->playtimeTwoWeeks = $this->checkIssetField($app, 'playtime_2weeks', 0);
32-
$this->playtimeTwoWeeksReadable = $this->convertFromMinutes($this->playtimeTwoWeeks);
33-
$this->playtimeForever = $this->checkIssetField($app, 'playtime_forever', 0);
34-
$this->playtimeForeverReadable = $this->convertFromMinutes($this->playtimeForever);
35-
$this->icon = $this->checkIssetImage($app, 'img_icon_url');
36-
$this->logo = $this->checkIssetImage($app, 'img_logo_url');
37-
$this->header = 'http://cdn.steampowered.com/v/gfx/apps/' . $this->appId . '/header.jpg';
38-
$this->hasCommunityVisibleStats = $this->checkIssetField($app, 'has_community_visible_stats', 0);
39-
}
27+
public $style;
4028

41-
/**
42-
* @param $app
43-
* @param string $field
44-
* @param string $value
45-
*
46-
* @return null|string
47-
*/
48-
protected function checkIssetImage($app, $field, $value = null)
49-
{
50-
return isset($app->$field) ? $this->getImageForGame($app->appid, $app->$field) : $value;
51-
}
29+
public $attributes;
5230

53-
protected function getImageForGame($appId, $hash)
54-
{
55-
if ($hash != null) {
56-
return 'http://media.steampowered.com/steamcommunity/public/images/apps/' . $appId . '/' . $hash . '.jpg';
57-
}
31+
public $custom;
5832

59-
return null;
33+
public function __construct($item)
34+
{
35+
$this->id = $item->id;
36+
$this->originalId = $item->original_id;
37+
$this->defIndex = $item->defindex;
38+
$this->level = $item->level;
39+
$this->quality = $item->quality;
40+
$this->quantity = $item->quantity;
41+
$this->inventory = $item->inventory;
42+
$this->origin = $this->checkIssetField($item, 'origin');
43+
$this->containedItem = $this->checkIssetField($item, 'contained_item');
44+
$this->style = $this->checkIssetField($item, 'style');
45+
$this->attributes = $this->checkIssetField($item, 'attributes');
46+
47+
$this->flags = [
48+
'trade' => (boolean)! $this->checkIssetField($item, 'flag_cannot_trade', false),
49+
'craft' => (boolean)! $this->checkIssetField($item, 'flag_cannot_craft', false),
50+
];
51+
$this->custom = [
52+
'name' => $this->checkIssetField($item, 'custom_name'),
53+
'description' => $this->checkIssetField($item, 'custom_desc'),
54+
];
6055
}
6156
}

src/Syntax/SteamApi/Inventory.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Syntax\SteamApi;
4+
5+
class Inventory
6+
{
7+
public $numberOfBackpackSlots;
8+
9+
public $items;
10+
11+
public function __construct($slots, $items)
12+
{
13+
$this->numberOfBackpackSlots = $slots;
14+
$this->items = $items;
15+
}
16+
}

src/Syntax/SteamApi/Steam/Item.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Syntax\SteamApi\Client;
66
use NukaCode\Database\Collection;
77
use Syntax\SteamApi\Containers\Item as ItemContainer;
8+
use Syntax\SteamApi\Exceptions\ApiCallFailedException;
9+
use Syntax\SteamApi\Inventory;
810

911
class Item extends Client
1012
{
@@ -26,38 +28,37 @@ public function GetPlayerItems($appId, $steamId)
2628

2729
$arguments = ['steamId' => $steamId];
2830

29-
// Get the client
30-
$client = $this->setUpClient($arguments);
31-
dd($client);
31+
try {
32+
// Get the client
33+
$client = $this->setUpClient($arguments);
34+
} catch (ApiCallFailedException $exception) {
35+
// No items exist for this game.
36+
return null;
37+
}
3238

3339
// Clean up the items
34-
$items = $this->convertToObjects($client->result->items, $client->result->qualities);
40+
$items = $this->convertToObjects($client->result->items);
3541

36-
return $items;
42+
// Return a full inventory
43+
return new Inventory($client->result->num_backpack_slots, $items);
3744
}
3845

39-
protected function convertToObjects($items, $qualities)
46+
protected function convertToObjects($items)
4047
{
41-
$convertedItems = $this->convertGames($items, $qualities);
42-
43-
$items = $this->sortObjects($convertedItems);
44-
45-
return $items;
48+
return $this->convertItems($items);
4649
}
4750

4851
/**
4952
* @param array $items
5053
*
5154
* @return Collection
5255
*/
53-
protected function convertItems($items, $qualities)
56+
protected function convertItems($items)
5457
{
5558
$convertedItems = new Collection();
5659

5760
foreach ($items as $item) {
58-
if (isset($item->data)) {
59-
$convertedItems->add(new ItemContainer($item->data, $qualities));
60-
}
61+
$convertedItems->add(new ItemContainer($item));
6162
}
6263

6364
return $convertedItems;

0 commit comments

Comments
 (0)