1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Burthorpe\Runescape\RS3; |
4
|
|
|
|
5
|
|
|
use Burthorpe\Exception\UnknownPlayerException; |
6
|
|
|
use Burthorpe\Runescape\RS3\Skills\Repository as SkillsRepository; |
7
|
|
|
use Burthorpe\Runescape\RS3\Stats\Repository as StatsRepository; |
8
|
|
|
use GuzzleHttp\Client as Guzzle; |
9
|
|
|
use GuzzleHttp\Exception\RequestException; |
10
|
|
|
use GuzzleHttp\Psr7\Request; |
11
|
|
|
|
12
|
|
|
class API |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Burthorpe\Runescape\RS3\Skills\Repository |
16
|
|
|
*/ |
17
|
|
|
protected $skills; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Guzzle HTTP client for making requests |
21
|
|
|
* |
22
|
|
|
* @var \GuzzleHttp\Client |
23
|
|
|
*/ |
24
|
|
|
protected $guzzle; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array of resource URLs |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $endpoints = [ |
32
|
|
|
'hiscores' => 'http://hiscore.runescape.com/index_lite.ws?player=%s', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class constructor |
37
|
|
|
*/ |
38
|
11 |
|
public function __construct() |
39
|
1 |
|
{ |
40
|
11 |
|
$this->skills = new SkillsRepository(); |
41
|
|
|
|
42
|
11 |
|
$this->guzzle = new Guzzle([ |
43
|
|
|
'headers' => [ |
44
|
11 |
|
'User-Agent' => 'Burthorpe Runescape API', |
45
|
11 |
|
], |
46
|
11 |
|
]); |
47
|
11 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get a players statistics from the hiscores API feed |
51
|
|
|
* |
52
|
|
|
* @return \Burthorpe\Runescape\RS3\Stats\Repository|bool |
53
|
|
|
* |
54
|
|
|
* @throws \Burthorpe\Exception\UnknownPlayerException |
55
|
|
|
*/ |
56
|
5 |
|
public function stats($rsn) |
57
|
|
|
{ |
58
|
5 |
|
$request = new Request('GET', sprintf($this->endpoints['hiscores'], $rsn)); |
59
|
|
|
|
60
|
|
|
try { |
61
|
5 |
|
$response = $this->guzzle->send($request); |
62
|
5 |
|
} catch (RequestException $e) { |
63
|
1 |
|
throw new UnknownPlayerException($rsn); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return StatsRepository::factory($response->getBody()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get access to the skills helper |
71
|
|
|
* |
72
|
|
|
* @return \Burthorpe\Runescape\RS3\Skills\Repository |
73
|
|
|
*/ |
74
|
1 |
|
public function getSkills() |
75
|
|
|
{ |
76
|
1 |
|
return $this->skills; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Calculates a players combat level |
81
|
|
|
* |
82
|
|
|
* @param int $attack |
83
|
|
|
* @param int $strength |
84
|
|
|
* @param int $magic |
85
|
|
|
* @param int $ranged |
86
|
|
|
* @param int $defence |
87
|
|
|
* @param int $constitution |
88
|
|
|
* @param int $prayer |
89
|
|
|
* @param int $summoning |
90
|
|
|
* @param bool $float |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
2 |
|
public function calculateCombatLevel($attack, $strength, $magic, $ranged, $defence, $constitution, $prayer, $summoning, $float = false) |
94
|
|
|
{ |
95
|
2 |
|
$highest = max(($attack + $strength), (2 * $magic), (2 * $ranged)); |
96
|
|
|
|
97
|
2 |
|
$cmb = floor(0.25 * ((1.3 * $highest) + $defence + $constitution + floor(0.5 * $prayer) + floor(0.5 * $summoning))); |
98
|
|
|
|
99
|
2 |
|
return $float ? $cmb : (int) $cmb; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|