This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.php
276 lines (225 loc) · 9.04 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* @author Branko Wilhelm <branko.wilhelm@gmail.com>
* @link http://www.z-index.net
* @copyright (c) 2013 Branko Wilhelm
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die();
final class mod_wow_raid_progress_wotlk
{
private $params = null;
private $raids = array(
// The Ruby Sanctum
4987 => array(
'link' => '',
'stats' => array('kills' => 0, 'mode' => 'normal'),
'npcs' => array(
// Halion
39863 => array(
'link' => '',
'normal' => 0,
'heroic' => 0
)
),
)
);
public function __construct(JRegistry &$params)
{
$this->params = $params;
}
public function getRaids()
{
if ($this->params->get('mode') == 'auto') {
$url = 'http://' . $this->params->get('region') . '.battle.net/api/wow/guild/' . $this->params->get('realm') . '/' . $this->params->get('guild') . '?fields=members,achievements';
$result = $this->remoteContent($url);
if (!is_object($result)) {
return $result;
}
$this->checkNormal($result->achievements);
if ($this->params->get('heroic') && $this->params->get('ranks')) {
$this->checkHeroic($result->members);
}
}
if ($hidden = $this->params->get('hide')) {
foreach ($hidden as $hide) {
unset($this->raids[$hide]);
}
}
$this->adjustments();
// at last replace links and count mode-kills
foreach ($this->raids as $zoneId => &$zone) {
$zone['link'] = $this->link($zone['link'], $zoneId);
$heroic = $normal = 0;
foreach ($zone['npcs'] as $npcId => &$npc) {
$npc['link'] = $this->link($npc['link'], $npcId, true);
if ($npc['heroic'] === true) {
$heroic++;
}
if ($npc['normal'] === true) {
$normal++;
}
}
if ($normal > 0) {
$zone['stats']['kills'] = $normal;
}
if ($heroic > 0) {
$zone['stats']['kills'] = $heroic;
$zone['stats']['mode'] = 'heroic';
}
$zone['opened'] = in_array($zoneId, (array)$this->params->get('opened'));
$zone['stats']['bosses'] = count($zone['npcs']);
$zone['stats']['percent'] = round(($zone['stats']['kills'] / $zone['stats']['bosses']) * 100);
}
return $this->raids;
}
private function remoteContent($url)
{
$cache = JFactory::getCache('wow', 'output');
$cache->setCaching(1);
$cache->setLifeTime($this->params->get('cache_time', 24) * 60 + rand(0, 60)); // randomize cache time a little bit for each url
$key = md5($url);
if (!$result = $cache->get($key)) {
try {
$http = new JHttp(new JRegistry, new JHttpTransportCurl(new JRegistry));
$http->setOption('userAgent', 'Joomla! ' . JVERSION . '; WoW Raid Progress - WotLK; php/' . phpversion());
$result = $http->get($url, null, $this->params->get('timeout', 10));
} catch (Exception $e) {
return $e->getMessage();
}
$cache->store($result, $key);
}
if ($result->code != 200) {
return __CLASS__ . ' HTTP-Status ' . JHtml::_('link', 'http://wikipedia.org/wiki/List_of_HTTP_status_codes#' . $result->code, $result->code, array('target' => '_blank'));
}
return json_decode($result->body);
}
private function checkNormal(stdClass $achievements)
{
foreach ($this->raids as &$zone) {
foreach ($zone['npcs'] as &$npc) {
$npc['normal'] = in_array($npc['normal'], $achievements->criteria);
}
}
}
private function checkHeroic(array &$members)
{
$heroicIds = $this->getHeroicIDs();
foreach ($members as &$member) {
if (in_array($member->rank, $this->params->get('ranks'))) {
$member->achievements = $this->loadMember($member->character->name);
if ($member->achievements) {
foreach ($heroicIds as $id => $zoneNpc) {
list ($npc, $zone) = explode(':', $zoneNpc, 2);
if (in_array($id, $member->achievements->achievementsCompleted)) {
$this->raids[$zone]['npcs'][$npc]['heroic']++;
}
}
}
}
}
foreach ($this->raids as &$zone) {
foreach ($zone['npcs'] as &$npc) {
$npc['heroic'] = (bool)($npc['heroic'] >= $this->params->get('successful', 5));
}
}
}
private function getHeroicIDs()
{
$result = array();
foreach ($this->raids as $zoneId => &$zone) {
foreach ($zone['npcs'] as $npc => &$modes) {
$result[$modes['heroic']] = $npc . ':' . $zoneId;
$modes['heroic'] = 0;
}
}
return $result;
}
private function loadMember($name)
{
$url = 'http://' . $this->params->get('region') . '.battle.net/api/wow/character/' . $this->params->get('realm') . '/' . $name . '?fields=achievements';
$result = $this->remoteContent($url);
if (!is_object($result) || !isset($result->achievements)) {
return false;
}
return $result->achievements;
}
private function adjustments()
{
foreach ($this->raids as $zoneId => &$zone) {
foreach ($zone['npcs'] as $npcId => &$npc) {
if ($npc['heroic'] === true || $npc['normal'] === true) {
continue;
}
switch ($this->params->get('adjust_' . $npcId)) {
default:
continue;
break;
case 'no':
$npc['normal'] = false;
$npc['heroic'] = false;
break;
case 'normal':
$npc['normal'] = true;
break;
case 'heroic':
$npc['heroic'] = true;
break;
}
}
}
//$this->generateXML();
//$this->generateINI();
}
private function link($link, $id, $npc = false)
{
if ($npc) {
$sites['battle.net'] = 'http://' . $this->params->get('region') . '.battle.net/wow/' . $this->params->get('lang') . '/' . $link;
$sites['wowhead.com'] = 'http://' . $this->params->get('lang') . '.wowhead.com/npc=' . $id;
$sites['wowdb.com'] = 'http://www.wowdb.com/npcs/' . $id;
} else {
$sites['battle.net'] = 'http://' . $this->params->get('region') . '.battle.net/wow/' . $this->params->get('lang') . '/' . $link;
$sites['wowhead.com'] = 'http://' . $this->params->get('lang') . '.wowhead.com/zone=' . $id;
$sites['wowdb.com'] = 'http://www.wowdb.com/zones/' . $id;
}
return $sites[$this->params->get('link')];
}
private function generateINI()
{
header("Content-type: text/plain; charset=utf-8");
foreach ($this->raids as $zoneId => &$zone) {
echo strtoupper(__CLASS__) . '_ZONE_' . $zoneId . ' = ""' . PHP_EOL;
foreach ($zone['npcs'] as $npcId => &$npc) {
echo strtoupper(__CLASS__) . '_NPC_' . $npcId . ' = ""' . PHP_EOL;
}
echo PHP_EOL;
}
exit;
}
private function generateXML()
{
header("Content-type: text/xml; charset=utf-8");
$xml = new SimpleXMLElement('<fieldset />');
$xml->addAttribute('name', 'adjustments');
$options = array('auto', 'no', 'normal', 'heroic');
foreach ($this->raids as $zoneId => &$zone) {
$spacer = $xml->addChild('field');
$spacer->addAttribute('type', 'spacer');
$spacer->addAttribute('class', 'label');
$spacer->addAttribute('label', strtoupper(__CLASS__) . '_ZONE_' . $zoneId);
foreach ($zone['npcs'] as $npcId => &$npc) {
$adjust = $xml->addChild('field');
$adjust->addAttribute('name', 'adjust_' . $npcId);
$adjust->addAttribute('default', 'auto');
$adjust->addAttribute('type', 'radio');
$adjust->addAttribute('class', 'btn-group');
$adjust->addAttribute('label', strtoupper(__CLASS__) . '_NPC_' . $npcId);
foreach ($options as $option) {
$child = $adjust->addChild('option', strtoupper(__CLASS__ . '_RAID_' . $option));
$child->addAttribute('value', $option);
}
}
}
exit($xml->asXML());
}
}