-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.php
More file actions
437 lines (386 loc) · 10.4 KB
/
Game.php
File metadata and controls
437 lines (386 loc) · 10.4 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php //Bismillah
/*--------------------*\
| gamesModel Class |
\*--------------------*/
/*
DESCRIPTION:
Provides interface between gamesController and SOAP services
MVC MIGRATION:
- Copy to Models folder
- All migration steps are found within the class declaration line or the constructor
Migration Steps:
1. Extend a proper Model parent class (e.g. CI_Model or AppModel)
2. Uncomment call to parent constructor
*/
class Game // <- Step 1.) extend proper Model class
{
//Soap Service
protected $_soapURL = "http://xbox2.sierrabravo.net/xbox2.asmx?wsdl";
protected $_apiKey = "c18316f737b74b7636e06e24a99d6d6e";
protected $_soap; //SoapClient
protected $_soapEnv; //Soap Envelope
//Values to be returned to controllers
public $games = array();
public $error = false;
//Properties that must exist in the Games array for xbox.js to funciton properly
protected $_gameProperties = array('Id', 'Title', 'Status', 'Votes');
//Constructor - Connects to the SOAP service and validates api key
public function __construct()
{
//MVC: call parent constructor
//parent::__construct(); // <- Step 2.) UNCOMMENT when migrating to an MVC
//Attempt to connect to SOAP Service and create a SoapClient
try
{
@$this->_soap = new SoapClient($this->_soapURL); //Warning supressed, as invalid url triggers both warnings and catch();
}
catch (Exception $e)
{
$this->error = $e->faultstring;
return false;
}
//Insert apiKey into Soap Envelope Array
$this->_soapEnv = array('apiKey'=> $this->_apiKey);
//Validate API Key
return $this->_checkKey();
}
//SOAP: Validate Key
protected function _checkKey()
{
$client = $this->_soap;
try
{
$valid = $client->checkKey($this->_soapEnv);
if (!$valid->CheckKeyResult)
{
$this->error = "Error: Invalid API Key provided to SOAP Service.";
return false;
}
else
{
return true;
}
}
catch(Exception $e)
{
$this->error = $e->faultstring;
return false;
}
}
//MVC: Sends Response to Controller
protected function _respond()
{
//Ensure games is received by xbox.js as an array and not an object
$this->_arrGames();
//Ensure games objects have the necessary properties for xbox.js to function properly
$this->_checkGames();
//Controller expects an array containing a games array and an error string/bool
$response = array('games'=>$this->games, 'error'=>$this->error);
return $response;
}
//Checks if games object contains all required properties
protected function _checkGames()
{
//If there isn't already an error, and if games isn't empty
if (!$this->error && is_array($this->games) && count($this->games) > 0)
{
$errStr = "";
$sep = "";
//Go through each property and ensure it exists
foreach ($this->_gameProperties as $prop)
{
//Use first game as sample
$g = array_slice($this->games,0,1);
if (!property_exists($g[0], $prop))
{
//Add to errorstring if property is not present
$errStr .= $sep.$prop;
$sep = ", ";
}
}
//If at least on property is missing
if ($errStr!="")
{
//Send Xbox.js a warning that the games array may not function as expected
$this->error = "Warning: Games objects missing necessary properties: ".$errStr;
return false;
}
}
return true;
}
//Checks if data field exists, returns false if it doesn't
protected function _checkData($field = false)
{
if ($field)
{
$this->data[$field] = (isset($this->data[$field])) ? $this->data[$field] : '';
return $this->data[$field];
}
return false;
}
//Checks if games is in array format, wraps games in array if not (occurs when zero or one game exists)
protected function _arrGames()
{
if (!is_array($this->games))
{
$this->games = array($this->games);
}
return $this->games;
}
//Sorts Games during loadGames()
protected function sortGames($sort = false)
{
//If the games array isn't empty
if (is_array($this->games) && count($this->games) > 0)
{
//Set Default Sort to Title
$sort = (!$sort) ? "Title" : $sort;
//NOTE: I would generally use closures for usort instead of predefining callbacks, but am restricted by PHP 5.2
//Sort function callbacks
if (!function_exists('srtTitle')) //Prevent redeclaration
{
function srtTitle($a, $b)
{
return strcmp(strtolower($a->Title),strtolower($b->Title));
}
}
if (!function_exists('srtVotes')) //Prevent redeclaration
{
function srtVotes ($a, $b)
{
if ($a->Votes==$b->Votes)
{
return 0;
}
return ($a->Votes > $b->Votes) ? -1 : 1;
}
}
//Sort games
usort($this->games, "srt".$sort);
}
return $this->games;
}
//Filters Games during loadGames()
protected function filterGames($param = false, $value = false)
{
//NOTE: I would use an anonymous function in place of $fltStr if I weren't restricted by PHP 5.2
//Filter Callback: If parameter matches, return true, else return false
$fltStr = "return (strtolower(\$game->".$param.") == strtolower(\"".$value."\")) ? true : false;";
//Ensure games is an array
$this->_arrGames();
//If at least one game exists to filter
if (is_array($this->games) && count($this->games) > 0)
{
//If a parameter and a value are provided
if ($param&&$value)
{
//If parameter is valid (using first game as sample)
$g = array_slice($this->games,0,1);
if (property_exists($g[0], $param))
{
//Filter Games
$this->games = array_filter($this->games, create_function("\$game", $fltStr));
}
else
{
//Else send warning to xbox.js
$this->error = "Warning: invalid filter parameter '".$param."'";
}
}
}
return $this->games;
}
//SOAP: Retrieve All Games from SOAP service
protected function _getGames()
{
try
{
$client = $this->_soap;
$games = $client->getGames($this->_soapEnv);
//If any games are found
if (isset($games->GetGamesResult->XboxGame))
{
//Return games (else empty games array will be returned)
$this->games = $games->GetGamesResult->XboxGame;
}
}
catch (Exception $e)
{
$this->error = $e->faultstring;
}
return $this->_respond();
}
//Loads games and return to controller
public function loadGames($sort = false, $param = false, $value = false)
{
//Set sort and filter parameters, prioritizes non-false arguments over data array
$sort = (!$sort) ? $this->_checkData('sort') : $sort;
$param = (!$param) ? $this->_checkData('param') : $param;
$value = (!$value) ? $this->_checkData('value') : $value;
//Get games from gamesModel, and ensure it's an array
$rs = $this->_getGames();
$this->_arrGames();
//Check for errors during initialization
if ($this->error)
{
//ERROR: Send gamesModel's error to xbox.js
return $this->_respond();
}
//Filter Games
$this->filterGames($param, $value);
//Sort Games
$this->sortGames($sort);
//Respond to controller
return $this->_respond();
}
//SOAP: Add New Game to SOAP Service
protected function _addGame($title)
{
try
{
$client = $this->_soap;
$this->_soapEnv['title'] = $title;
$client->addGame($this->_soapEnv);
}
catch (Exception $e)
{
$this->error = $e->faultstring;
}
return $this->_respond();
}
//Add a Game
public function add()
{
$title = $this->_checkData('title');
$eligible = $this->_checkData('eligible');
//VALIDATION: check user eligibility
if (!$eligible)
{
//ERROR: user is ineligible
$this->error = "You are not eligible to add a game.";
return $this->_respond();
}
//VALIDATION: check for blank title
if (!$title)
{
//ERRROR: blank title
$this->error = "You must provide a title.";
return $this->_respond();
}
//BUGFIX: PHP automatically adds slashes to singlequotes, Fixed with stripslashes()
$title = stripslashes($title);
//VALIDATION: check for duplicate title
$this->loadGames(false, "Title", $title);
if (is_array($this->games) && count($this->games) > 0)
{
//Check if game is purchased (to help user locate game)
$pch = ".";
if ($this->games[0]->Status=="gotit")
{
$pch = " and has been purchased.";
}
//ERROR: Duplicate title
$this->error = "'".$this->games[0]->Title."' already exists".$pch;
return $this->_respond();
}
//Add game via model
$rs = $this->_addGame($title);
//Return ONLY new game to xbox.js
$this->loadGames(false, "Title", $title);
$this->error = $rs['error'];
return $this->_respond();
}
//SOAP: Vote for Game via SOAP
protected function _addVote($id)
{
try
{
$client = $this->_soap;
$this->_soapEnv['id'] = $id;
$client->addVote($this->_soapEnv);
}
catch (Exception $e)
{
$this->error = $e->faultstring;
}
return $this->_respond();
}
//Cast a Vote for a Title
public function vote()
{
$id = $this->_checkData('id');
$eligible = $this->_checkData('eligible');
//VALIDATION: Check to see if game exists
$this->loadGames(false, "Id", $id);
if (count($this->games) != 1)
{
//ERROR: Invalid Id
$this->error = "Error: attempted to vote for an invalid Id - ".$id.".";
return $this->_respond();
}
//VALIDATION: Check to see if user can vote
if (!$eligible)
{
//ERROR: User is Ineligible
$this->error = "Error: user is ineligible to vote.";
return $this->_respond();
}
//Add Vote via SOAP
$rs = $this->_addVote($id);
//Return soap's response to View
$this->loadGames();
$this->error = $rs['error'];
return $this->_respond();
}
//Purchase Game
protected function _setGotIt($id)
{
try
{
$client = $this->_soap;
$this->_soapEnv['id'] = $id;
$client->setGotIt($this->_soapEnv);
}
catch (Exception $e)
{
$this->error = $e->faultstring;
}
return $this->_respond();
}
//Purchase a game
public function purchase()
{
$id = $this->_checkData('id');
//VALIDATION: Check to see if game exists
$this->loadGames(false, "Id", $id);
if (count($this->games) != 1)
{
//ERROR: Invalid Id
$this->error = "Error: attempted to purchase with an invalid Id - ".$id.".";
return $this->_respond();
}
//Tell Model to purchase game
$rs = $this->_setGotIt($id);
//Return gamesModel's response to View
$this->loadGames();
$this->error = $rs['error'];
return $this->_respond();
}
//Clear All Games
public function clearGames()
{
try
{
$client = $this->_soap;
$client->clearGames($this->_soapEnv);
unset($this->games);
$this->games = array();
}
catch (Exception $e)
{
$this->error = $e->faultstring;
}
return $this->_respond();
}
}
?>