My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
<?php
/**********************************************************************//**\file
Armory Player Data

Description: Player character data model, provides a consistent interface
to player character information pulled from wowarmory.com
*******************************************************************************/

class playerdata
{
# variable declarations for each piece of player data, so it can be
# retrieved via object reference
public $data = array(
'id' => null,
'name' => null,
'added' => null,
'updated' => null,
'expired' => null,
'level' => null,
'class' => null,
'rank' => null,
'race' => null,
'gender' => null,
'url' => null,
'talents1' => null,
'talents2' => null,
'talents3' => null,
'talents4' => null,
'talents5' => null,
'talents6' => null,
'profname1' => null,
'profskill1' => null,
'profname2' => null,
'profskill2' => null,
'gearscore' => null,
'geardate' => null,
'gearsuggest' => null
);

private $query; #debug info

# Flag true ONLY if the profile is modified via updater script, sets
# "player_updated" time in query. Used to check for expired data on the
# next fresh pull from the armory.
protected $xml_update = false;

public function __construct($player = null, $xml_update = false)
{
$this->xml_update = $xml_update;

if(is_int($player))
{
# Player specified by record id
$this->get_player_id($player);
}
elseif(is_string($player))
{
# Player specified by name
$this->get_player_name($player);
}
elseif(is_null($player))
{
# This condition would be used to add a player profile via some
# other interface. This is currently not used for anything.
}
else
{
# The data provided was invalid, kill the script.
die();
}
}

# pull player's data from db by id
protected function get_player_id($id)
{
# pass id to get_player_data
$this->get_player_data('id', $id);
}

# pull player's data from db by name
protected function get_player_name($name)
{
# pass name to get_player_date
$this->get_player_data('name', $name);
}

# helper function for other get_player functions
protected function get_player_data($method, $value)
{
# build query based on id or name
if($method == 'id')
{
$where = "WHERE player_id = ".$value;
}
elseif($method == 'name')
{
$where = "WHERE player_name = '".$value."'";
}

# query data, lazy selection
$db = new armorydb();
$db->query("SELECT * FROM armory_player $where LIMIT 1");
$result_array = $db->result();

# Assign values from database query to data object's array
foreach($this->data as $key => $var)
{
$this->data[$key] = $result_array['player_'.$key];
}

# Close connection
unset($db);
}

# UPDATE FUNCTIONS - Update the player's information
public function update_general($class, $gender, $level, $race)
{
$this->data['class'] = $class;
$this->data['gender'] = $gender;
$this->data['level'] = $level;
$this->data['race'] = $race;
}

public function update_talents($talents1, $talents2, $talents3)
{
if(!is_null($this->data['talents1']))
{
$this->data['talents4'] = $talents1;
$this->data['talents5'] = $talents2;
$this->data['talents6'] = $talents3;
}
else
{
$this->data['talents1'] = $talents1;
$this->data['talents2'] = $talents2;
$this->data['talents3'] = $talents3;
}
}

public function update_professions($key, $value)
{
if(!is_null($this->data['profname1']))
{
$this->data['profname2'] = $key;
$this->data['profskill2'] = $value;
}
else
{
$this->data['profname1'] = $key;
$this->data['profskill1'] = $value;
}
}

# Store data in the object to the db
public function store()
{
$time = time();

# If fresh data is being pulled from the armory, update timestamp
# on the data so it will be pushed to the bottom of the update queue
if($this->xml_update)
{
$updated = ', player_updated = '.$time.' ';
}
else
{
$update = '';
}

# Build SET data for query
$set_data ='';
$first = true;
foreach($this->data as $key => $var)
{
if($key != 'id' && $key != 'name' && $key != 'updated' && !is_null($var))
{
if(!$first)
{
$set_data .= ',';
}

if(is_numeric($var) || $var == '0')
{
# If data is an integer, insert without quotes
$set_data .= " player_$key = $var";
}
else
{
# If data is non-integer, insert with quotes
$set_data .= " player_$key = '$var'";
}

$first = false;
}
}

$db = new armorydb();

# Check to see whether a record exists for this player
$db->query("SELECT player_name FROM armory_player WHERE player_name = '{$this->data['name']}'");

if(is_null($db->result()))
{
# Create a new record
$this->query = "INSERT INTO armory_player SET player_name = '{$this->data['name']}', player_added = $time, $set_data $updated";
$db->query($this->query);
}
else
{
# Update extant record
$this->query = "UPDATE armory_player SET $set_data $updated WHERE player_name = '{$this->data['name']}'";
$db->query($this->query);
}

}

public function debug()
{
var_dump($this->query);
var_dump($this->data);
}
}
?>

Change log

r19 by banzaimonkey on Nov 26, 2009   Diff
Bugfix:  Changed bad logic to check for
numbers using is_numeric instead of intval
Added query to debug information
Go to: 
Project members, sign in to write a code review

Older revisions

r16 by banzaimonkey on Oct 30, 2009   Diff
Update script ready for testing
Player data model saves to database
Minor fixes
r15 by banzaimonkey on Oct 30, 2009   Diff
Added multi-result to db abstraction
class
Added delay to network transfer to
prevent burst / ip ban
Some variable name tweaks for
...
r12 by banzaimonkey on Oct 28, 2009   Diff
Updated XML parsing functions
Simple database abstraction class
Implemented data object storage
functions
All revisions of this file

File info

Size: 5380 bytes, 223 lines
Powered by Google Project Hosting