forked from cooperaj/wow-armory-character
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wow-armory-character.php
192 lines (160 loc) · 5.14 KB
/
class-wow-armory-character.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
<?php
/*
Copyright 2014 Adam Cooper (email : adam@networkpie.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Wraps the JSON returned by the WoW community API to provide some
* extra properties and methods on that data.
*
* @author Adam Cooper <adam@networkpie.co.uk>
*/
class WoW_Armory_Character {
public $region;
public $locale;
// Setting these in the object makes things a little easier.
public $last_checked;
public $cache_name;
public $notes;
private $_api_data;
public function __construct( $region, $locale, stdClass $api_data ) {
$this->region = $region;
$this->locale = $locale;
$this->_api_data = $api_data;
}
public function __get( $name ) {
if ( isset( $this->_api_data->$name ) ) {
return $this->_api_data->$name;
}
}
public function __isset( $name ) {
return isset( $this->_api_data->$name );
}
public function get_completed_achievement_data() {
$data = new stdClass();
if ( ! $this->_has_valid_achievement_data() ) {
return $data;
}
$achiev_data = WoW_Armory_Character_DAL::fetch_achievements( $this->region, $this->locale );
if ( is_wp_error( $achiev_data ) ) {
WoW_Armory_Character_DAL::persist_character_note(
$this,
__(
'The global achievement data is corrupt. Please clear the cache.',
'wow_armory_character'
)
);
return null;
}
$data->completed = count( $achiev_data->get_achievements_countable( $this->achievements->achievementsCompleted ) );
$data->total = $achiev_data->get_achievement_count();
$data->percent_complete = round( ( $data->completed / $data->total ) * 100 );
$data->percent_remaining = 100 - $data->percent_complete;
return $data;
}
public function get_latest_achievements( $no_to_fetch ) {
$achievs = array();
$count = 0;
if ( ! $this->_has_valid_achievement_data() ) {
return $achievs;
}
$achiev_data = WoW_Armory_Character_DAL::fetch_achievements( $this->region, $this->locale );
if ( is_wp_error( $achiev_data ) ) {
WoW_Armory_Character_DAL::persist_character_note(
$this,
__(
'The global achievement data is corrupt. Please clear the cache.',
'wow_armory_character'
)
);
} else {
arsort( $this->achievements->achievementsCompletedTimestamp );
foreach ( $this->achievements->achievementsCompletedTimestamp as $key => $timestamp ) {
if ( $count >= $no_to_fetch ) {
break;
}
$ach = $achiev_data->get_achievement_by_id( $this->achievements->achievementsCompleted[ $key ] );
// Our achievement data may not contain what we need so we skip achievements that don't get
// returned correctly. Come on Blizz...
if ( ! is_null( $ach ) ) {
$ach->completed = $timestamp;
$achievs[] = $ach;
$count ++;
} else {
WoW_Armory_Character_DAL::persist_character_note(
$this,
__(
'The achievement data does not contain a match for achievement id ' .
$this->achievements->achievementsCompleted[ $key ] . '.',
'wow_armory_character'
)
);
}
}
}
return $achievs;
}
public function get_activity_feed_items( $max_items = 5 ) {
// We want five of each item so this slightly confusing thing keeps track of
// what we've added to the return to ensure this happens.
$feed_items = array();
$item_count = 0;
$achiev_count = 0;
$criteria_count = 0;
foreach ( $this->feed as $feed_item ) {
$item = new WoW_Armory_Character_FeedItem( $this->region, $this->locale, $feed_item );
switch ( $item->type ) {
case 'ACHIEVEMENT' :
if ( $achiev_count < $max_items ) {
$feed_items[] = $item;
}
$achiev_count ++;
break;
case 'CRITERIA' :
if ( $criteria_count < $max_items ) {
$feed_items[] = $item;
}
$criteria_count ++;
break;
case 'LOOT' :
if ( $item_count < $max_items ) {
$feed_items[] = $item;
}
$item_count ++;
break;
}
}
return $feed_items;
}
/**
* Return whether or not our achievement data is valid.
*
* It appears that sometimes the api will return incorrect data for the achievements of a
* character. We now make sure to check before attempting any procession on that data.
* @return boolean
*/
private function _has_valid_achievement_data() {
if ( is_array( $this->achievements->achievementsCompleted ) &&
is_array( $this->achievements->achievementsCompletedTimestamp )
) {
return true;
}
WoW_Armory_Character_DAL::persist_character_note(
$this,
__(
'The achievement data for this character is not fully formed.',
'wow_armory_character'
)
);
return false;
}
}