Skip to content

Commit eaa5cc6

Browse files
Travis BlasingameTravis Blasingame
authored andcommitted
Refactored how I get the alternate ids. We can now convert back and forth between all 3.
1 parent b317863 commit eaa5cc6

File tree

9 files changed

+255
-32
lines changed

9 files changed

+255
-32
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,36 @@ Lastly, publish the config file. You can get your API key from [Steam](http://s
3636

3737
Each service from the steam api has it's own methods you can use.
3838

39+
- [Global](#global)
3940
- [News](#news)
4041
- [Player](#player)
4142
- [User](#user)
4243
- [User Stats](#user-stats)
4344
- [App](#app)
4445

46+
### Global
47+
These are methods that are available to each service
48+
49+
#### convertId
50+
This will convert the given steam ID to each type of steam ID (64 bit, 32 bit and steam ID3).
51+
52+
##### Arguments
53+
54+
Name | Type | Description | Required | Default
55+
-----|------|-------------|----------|---------
56+
id| string|int | The id you want to convert | Yes
57+
format | string | The format you want back. | No | null
58+
59+
> Possible formats are ID64, id64, 64, ID32, id32, 32, ID3, id3 and 3.
60+
61+
##### Example usage
62+
63+
```php
64+
Steam::convertId($id, $format);
65+
```
66+
67+
> Example Output: [convertId](./examples/global/convertId.txt)
68+
4569
### News
4670
The [Steam News](https://developer.valvesoftware.com/wiki/Steam_Web_API#GetNewsForApp_.28v0002.29) web api is used to get articles for games.
4771

examples/global/convertId.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
stdClass Object
2+
(
3+
[id32] => STEAM_1:1:9846342
4+
[id64] => 76561197979958413
5+
[id3] => [U:1:19692685]
6+
)

examples/user/GetPlayerSummaries.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ Array
33
[0] => Syntax\SteamApi\Containers\Player Object
44
(
55
[steamId] => 76561198022436617
6-
[communityId] => STEAM_0:1:9846342
6+
[steamIds] => Syntax\SteamApi\Containers\Id Object
7+
(
8+
[id32] => STEAM_1:1:31085444
9+
[id64] => 76561198022436617
10+
[id3] => [U:1:62170889]
11+
[communityId] => STEAM_1:1:31085444
12+
[steamId] => 76561198022436617
13+
)
714
[communityVisibilityState] => 3
815
[profileState] => 1
916
[personaName] => Stygian

src/Syntax/SteamApi/Client.php

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@
1010

1111
class Client {
1212

13-
public $validFormats = ['json', 'xml', 'vdf'];
13+
use SteamId;
1414

15-
protected $url = 'http://api.steampowered.com/';
15+
public $validFormats = ['json', 'xml', 'vdf'];
16+
17+
protected $url = 'http://api.steampowered.com/';
1618

1719
protected $interface;
1820

1921
protected $method;
2022

21-
protected $version = 'v0002';
23+
protected $version = 'v0002';
2224

2325
protected $apiKey;
2426

25-
protected $apiFormat = 'json';
27+
protected $apiFormat = 'json';
2628

2729
protected $steamId;
2830

29-
protected $isService = false;
31+
protected $isService = false;
3032

3133
public function __construct()
3234
{
@@ -39,6 +41,9 @@ public function __construct()
3941
$this->client = new GuzzleClient($this->url);
4042
$this->apiKey = $apiKey;
4143

44+
// Set up the Ids
45+
$this->setUpFormatted();
46+
4247
return $this;
4348
}
4449

@@ -47,29 +52,6 @@ public function get()
4752
return $this;
4853
}
4954

50-
/**
51-
* @param int $id
52-
*
53-
* @return string
54-
*/
55-
public function convertCommunityIdToSteamId($id)
56-
{
57-
$x = ($id - 76561197960265728) / 2;
58-
59-
return 'STEAM_0:' . is_float($x) . ':' . (int)$x;
60-
}
61-
62-
/**
63-
* @param string $id
64-
*
65-
* @return string
66-
*/
67-
public function convertSteamIdToCommunityId($id)
68-
{
69-
$x = explode(':', $id);
70-
return (string) ($x[2] * 2) + 76561197960265728 + $x[1];
71-
}
72-
7355
protected function setUpService($arguments = null)
7456
{
7557
// Services have a different url syntax
@@ -174,7 +156,8 @@ public function __call($name, $arguments)
174156
$this->steamId = $arguments[0];
175157

176158
if (strpos(':', $this->steamId) !== false) {
177-
$this->steamId = $this->convertSteamIdToCommunityId($this->steamId);
159+
// Convert the id to all types and grab the 64 bit version
160+
$this->steamId = $this->convertToAll($this->steamId)[2];
178161
}
179162
}
180163

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php namespace Syntax\SteamApi\Containers;
2+
3+
use Syntax\SteamApi\Client;
4+
5+
class Id {
6+
7+
public $id32;
8+
9+
public $id64;
10+
11+
public $id3;
12+
13+
public $communityId;
14+
15+
public $steamId;
16+
17+
function __construct($id)
18+
{
19+
$client = new Client;
20+
21+
$steamIds = $client->convertId($id);
22+
23+
$this->id32 = $steamIds->id32;
24+
$this->id64 = $steamIds->id64;
25+
$this->id3 = $steamIds->id3;
26+
27+
$this->steamId = $this->id64;
28+
$this->communityId = $this->id32;
29+
}
30+
31+
}

src/Syntax/SteamApi/Containers/Player.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Player {
66

77
public $steamId;
88

9-
public $communityId;
9+
public $steamIds;
1010

1111
public $communityVisibilityState;
1212

@@ -51,7 +51,7 @@ class Player {
5151
public function __construct($player)
5252
{
5353
$this->steamId = $player->steamid;
54-
$this->communityId = (new Client)->convertCommunityIdToSteamId((int) $this->steamId);
54+
$this->steamIds = (new Id((int)$this->steamId));
5555
$this->communityVisibilityState = $player->communityvisibilitystate;
5656
$this->profileState = $player->profilestate;
5757
$this->personaName = $player->personaname;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php namespace Syntax\SteamApi\Exceptions;
2+
3+
class InvalidIdType extends \Exception {
4+
5+
public function __construct($supplied, array $valid, $code = 0, $previous = null)
6+
{
7+
$selectFrom = $this->humanReadableImplode($valid);
8+
$message = 'Invalid steamId type passed [' . $supplied . ']. Select from: ' . $selectFrom;
9+
10+
parent::__construct($message, $code, $previous);
11+
}
12+
13+
private function humanReadableImplode($array)
14+
{
15+
$last = array_slice($array, -1);
16+
$first = join(', ', array_slice($array, 0, -1));
17+
$both = array_filter(array_merge([$first], $last));
18+
19+
return join(' or ', $both);
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php namespace Syntax\SteamApi\Exceptions;
2+
3+
class UnrecognizedId extends \Exception {
4+
5+
public function __construct($message, $code = 0, $previous = null)
6+
{
7+
parent::__construct($message, $code, $previous);
8+
}
9+
}

src/Syntax/SteamApi/SteamId.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php namespace Syntax\SteamApi;
2+
3+
use Syntax\SteamApi\Exceptions\InvalidIdType;
4+
use Syntax\SteamApi\Exceptions\UnrecognizedId;
5+
6+
trait SteamId {
7+
8+
public $validTypes = ['ID32', 'ID64', 'ID3'];
9+
10+
public $formatted;
11+
12+
private $rawValue;
13+
14+
private static $ID32 = 'id32';
15+
16+
private static $ID64 = 'id64';
17+
18+
private static $ID3 = 'id3';
19+
20+
private static $id64Base = '76561197960265728';
21+
22+
public function convertId($id, $format = null)
23+
{
24+
$this->convertToAll($id);
25+
26+
switch ($format) {
27+
case 'ID32':
28+
case 'id32':
29+
case 32:
30+
return $this->formatted->{self::$ID32};
31+
case 'ID64':
32+
case 'id64':
33+
case 64:
34+
return $this->formatted->{self::$ID64};
35+
case 'ID3':
36+
case 'id3':
37+
case 3:
38+
return $this->formatted->{self::$ID3};
39+
default:
40+
return $this->formatted;
41+
}
42+
}
43+
44+
protected function setUpFormatted()
45+
{
46+
$this->formatted = new \stdClass();
47+
$this->formatted->{self::$ID32} = null;
48+
$this->formatted->{self::$ID64} = null;
49+
$this->formatted->{self::$ID3} = null;
50+
}
51+
52+
private function convertToAll($id)
53+
{
54+
list($type, $matches) = $this->determineIDType($id);
55+
56+
$this->getRawValue($id, $type, $matches);
57+
58+
// Convert to each type
59+
$this->convertToID32();
60+
$this->convertToID64();
61+
$this->convertToID3();
62+
63+
return $this->formatted;
64+
}
65+
66+
private function convertToID32()
67+
{
68+
if (! isset($this->formatted->{self::$ID32})) {
69+
$z = bcdiv($this->rawValue, '2', 0);
70+
$y = bcmul($z, '2', 0);
71+
$y = bcsub($this->rawValue, $y, 0);
72+
$formatted = "STEAM_1:$y:$z";
73+
$this->formatted->{self::$ID32} = $formatted;
74+
}
75+
}
76+
77+
private function convertToID64()
78+
{
79+
if (! isset($this->formatted->{self::$ID64})) {
80+
$formatted = bcadd($this->rawValue, self::$id64Base, 0);
81+
$this->formatted->{self::$ID64} = $formatted;
82+
}
83+
}
84+
85+
private function convertToID3()
86+
{
87+
if (! isset($this->formatted->{self::$ID3})) {
88+
$formatted = "[U:1:$this->rawValue]";
89+
$this->formatted->{self::$ID3} = $formatted;
90+
}
91+
}
92+
93+
private function determineIDType($id)
94+
{
95+
$id = trim($id);
96+
97+
if (preg_match('/^STEAM_[0-1]:([0-1]):([0-9]+)$/', $id, $matches)) {
98+
return ['ID32', $matches];
99+
}
100+
if (preg_match('/^[0-9]+$/', $id)) {
101+
return ['ID64', null];
102+
}
103+
if (preg_match('/^\[U:1:([0-9]+)\]$/', $id, $matches)) {
104+
return ['ID3', $matches];
105+
}
106+
107+
throw new UnrecognizedId('Id [' . $id . '] is not recognized as a steam id.');
108+
}
109+
110+
/**
111+
* Get a raw value from any type of steam id
112+
*
113+
* @param $id
114+
* @param $type
115+
* @param $matches
116+
*/
117+
private function getRawValue($id, $type, $matches)
118+
{
119+
switch ($type) {
120+
case 'ID32':
121+
$this->rawValue = bcmul($matches[2], '2', 0);
122+
$this->rawValue = bcadd($this->rawValue, $matches[1], 0);
123+
124+
$this->formatted->{self::$ID32} = $id;
125+
126+
break;
127+
case 'ID64':
128+
$this->rawValue = bcsub($id, self::$id64Base, 0);
129+
130+
$this->formatted->{self::$ID64} = $id;
131+
132+
break;
133+
case 'ID3':
134+
$this->rawValue = $matches[1];
135+
136+
$this->formatted->{self::$ID3} = $id;
137+
138+
break;
139+
}
140+
}
141+
142+
}

0 commit comments

Comments
 (0)