1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Syntax\SteamApi\Steam; |
4
|
|
|
|
5
|
|
|
use Syntax\SteamApi\Client; |
6
|
|
|
use Syntax\SteamApi\Containers\Player as PlayerContainer; |
7
|
|
|
use Syntax\SteamApi\Exceptions\UnrecognizedId; |
8
|
|
|
|
9
|
|
|
class User extends Client |
10
|
|
|
{ |
11
|
|
|
private $friendRelationships = [ |
12
|
|
|
'all', |
13
|
|
|
'friend', |
14
|
|
|
]; |
15
|
|
|
|
16
|
9 |
|
public function __construct($steamId) |
17
|
|
|
{ |
18
|
9 |
|
parent::__construct(); |
19
|
9 |
|
$this->interface = 'ISteamUser'; |
20
|
9 |
|
$this->steamId = $steamId; |
21
|
9 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the user_ids for a display name. |
25
|
|
|
* |
26
|
|
|
* @param null $displayName Custom name from steam profile link. |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
* |
30
|
|
|
* @throws UnrecognizedId |
31
|
|
|
*/ |
32
|
2 |
|
public function ResolveVanityURL($displayName = null) |
33
|
|
|
{ |
34
|
|
|
// This only works with a display name. Make sure we have one. |
35
|
2 |
|
if ($displayName == null) { |
36
|
1 |
|
throw new UnrecognizedId('You must pass a display name for this call.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Set up the api details |
40
|
1 |
|
$this->method = __FUNCTION__; |
41
|
1 |
|
$this->version = 'v0001'; |
42
|
|
|
|
43
|
1 |
|
$results = $this->setUpClient(['vanityurl' => $displayName])->response; |
44
|
|
|
|
45
|
|
|
// The message key is used when something goes wrong. If it exists, return it. |
46
|
1 |
|
if (isset($results->message)) { |
47
|
|
|
return $results->message; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Return the full steam ID object for the display name. |
51
|
1 |
|
return $this->convertId($results->steamid); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $steamId |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
3 |
|
public function GetPlayerSummaries($steamId = null) |
60
|
|
|
{ |
61
|
|
|
// Set up the api details |
62
|
3 |
|
$this->method = __FUNCTION__; |
63
|
3 |
|
$this->version = 'v0002'; |
64
|
|
|
|
65
|
3 |
|
if ($steamId == null) { |
|
|
|
|
66
|
1 |
|
$steamId = $this->steamId; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
$steamId = implode(',', (array)$steamId); |
70
|
|
|
|
71
|
3 |
|
$chunks = array_chunk(explode(',', $steamId), 100); |
72
|
|
|
|
73
|
3 |
|
$map = array_map([$this, 'getChunkedPlayerSummaries'], $chunks); |
74
|
|
|
|
75
|
3 |
|
$players = $this->compressPlayerSummaries($map); |
76
|
|
|
|
77
|
3 |
|
return $players; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
private function getChunkedPlayerSummaries($chunk) |
81
|
|
|
{ |
82
|
|
|
// Set up the arguments |
83
|
|
|
$arguments = [ |
84
|
3 |
|
'steamids' => implode(',', $chunk), |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
// Get the client |
88
|
3 |
|
$client = $this->setUpClient($arguments)->response; |
89
|
|
|
|
90
|
|
|
// Clean up the games |
91
|
3 |
|
$players = $this->convertToObjects($client->players); |
92
|
|
|
|
93
|
3 |
|
return $players; |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
private function compressPlayerSummaries($summaries) |
97
|
|
|
{ |
98
|
3 |
|
$result = []; |
99
|
3 |
|
$keys = array_keys($summaries); |
100
|
|
|
|
101
|
3 |
|
foreach ($keys as $key) { |
102
|
3 |
|
$result = array_merge($result, $summaries[$key]); |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
return $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
public function GetPlayerBans($steamId = null) |
109
|
|
|
{ |
110
|
|
|
// Set up the api details |
111
|
2 |
|
$this->method = __FUNCTION__; |
112
|
2 |
|
$this->version = 'v1'; |
113
|
|
|
|
114
|
2 |
|
if ($steamId == null) { |
115
|
1 |
|
$steamId = $this->steamId; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Set up the arguments |
119
|
|
|
$arguments = [ |
120
|
2 |
|
'steamids' => implode(',', (array)$steamId), |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
// Get the client |
124
|
2 |
|
$client = $this->setUpClient($arguments); |
125
|
|
|
|
126
|
2 |
|
return $client->players; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
public function GetFriendList($relationship = 'all', $summaries = true) |
130
|
|
|
{ |
131
|
|
|
// Set up the api details |
132
|
2 |
|
$this->method = __FUNCTION__; |
133
|
2 |
|
$this->version = 'v0001'; |
134
|
|
|
|
135
|
2 |
|
if (! in_array($relationship, $this->friendRelationships)) { |
136
|
1 |
|
throw new \InvalidArgumentException('Provided relationship [' . $relationship . '] is not valid. Please select from: ' . implode(', ', $this->friendRelationships)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Set up the arguments |
140
|
|
|
$arguments = [ |
141
|
1 |
|
'steamid' => $this->steamId, |
142
|
1 |
|
'relationship' => $relationship, |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
// Get the client |
146
|
1 |
|
$client = $this->setUpClient($arguments)->friendslist; |
147
|
|
|
|
148
|
|
|
// Clean up the games |
149
|
1 |
|
$steamIds = []; |
150
|
|
|
|
151
|
1 |
|
foreach ($client->friends as $friend) { |
152
|
1 |
|
$steamIds[] = $friend->steamid; |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
if($summaries) { |
156
|
1 |
|
$friends = $this->GetPlayerSummaries(implode(',', $steamIds)); |
157
|
|
|
} else { |
158
|
|
|
$friends = $steamIds; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $friends; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
protected function convertToObjects($players) |
165
|
|
|
{ |
166
|
3 |
|
$cleanedPlayers = []; |
167
|
|
|
|
168
|
3 |
|
foreach ($players as $player) { |
169
|
3 |
|
if(property_exists($player, 'steamid')) { |
170
|
3 |
|
$cleanedPlayers[] = new PlayerContainer($player); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
3 |
|
return $cleanedPlayers; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|