-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.inc.php
executable file
·525 lines (469 loc) · 14.7 KB
/
user.inc.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
//LAST UPDATED 12-10-05
include_once ("conn.inc");
include_once ("inventory.inc.php");
/************************************************************************************
Object oriented user management
When a user logs in to the site, a new USER object is created and stored in their
session. This USER object contains all information about the user themselves - i.e.,
all information stored in the database is put into the object. This reduces the number
of database calls I'll need to make in the program.
I got this idea from a script I downloaded, modified it to suit my needs. Not sure
if object oriented was the way to go for user management, but wanted to exparament
with it, anyway.
Passwords are, right now, probably unsecure.
*************************************************************************************/
class user
{
//The user's password should not be stored in this object... if they passed
//the initial login, why would it need to be stored?
var $user_name = "";
var $logged_in = false;
var $permissions = 0;
var $email = "";
var $numericClass = 0;
var $PlayerClass = "";
var $gold = 0;
var $experience = 0;
var $str = 1;
var $dex = 1;
var $hp = 1;
var $maxHP = 1;
var $turns = 0;
var $timeOfLastTurn;
var $timeOfLastLogin;
var $isUnc = false;
var $numLoc = 0;
var $myInventory;
var $attacks = 1;
var $calculated = 0;
//Initialization function
function user($user,$pass)
{
if ($user != "" && $pass != "") $this->log_in($user,$pass);
return $this->logged_in;
}
function get_calculated()
{
return "Calculated " . $this->calculated . " times";
}
function get_username()
{
if($this->user_name != "")
{
return $this->user_name;
}
}
function get_Turns()
{
return $this->turns;
}
function get_Level()
{
//players earn a new level for every 100 experience points
return (floor($this->experience / 100) + 1);
}
function get_Experience()
{
return $this->experience;
}
function get_PlayerClass()
{
return $this->PlayerClass;
}
function get_Str()
{
return $this->str;
}
function get_Dex()
{
return $this->dex;
}
function get_lastTurnTime()
{
return $this->timeOfLastTurn;
}
function get_lastLoginTime()
{
return $this->timeOfLastLogin;
}
function get_CurrentHP()
{
return $this->hp;
}
function get_MaxHP()
{
return $this->maxHP;
}
function get_Location()
{
/*
1 = Town
2 = TheBlueRiver
3 = TheDarkForrest
4 = TheDeepCave
*/
switch ($this->numLoc)
{
case 1:
return "Town";
break;
case 2:
return "TheBlueRiver";
break;
case 3:
return "TheDarkForrest";
break;
case 4:
return "TheDeepCave";
break;
}
return "Town";
}
function get_PlayerInventory($whichInventory)
{
$toReturn = $this->myInventory->showInventory();
switch ($whichInventory)
{
case "all":
$toReturn = $this->myInventory->showInventory();
break;
case "weapon":
$toReturn = $this->myInventory->showWeaponInventory();
break;
case "armor":
$toReturn = $this->myInventory->showArmorInventory();
break;
case "accessory":
$toReturn = $this->myInventory->showAccessoryInventory();
break;
case "potionlist":
$toReturn = $this->myInventory->showPotionForm();
break;
case "selllist":
$toReturn = $this->myInventory->showSellList();
break;
}
return $toReturn;
}
function move($moveTo)
{
$moveTo = str_replace("_"," ",$moveTo);
if ($this->turns > 0) {
$result = mysql_query("SELECT location FROM places WHERE name = '{$moveTo}'") or die("error finding place");
$row = mysql_fetch_array($result);
$this->numLoc = $row['location'];
$this->turns --;
mysql_query("update users set turns={$this->turns},location={$this->numLoc} where username = '{$this->user_name}'") or die("error moving user");
return true;
}
else {return false;}
}
function rest()
{
//Resting allows the player to use a turn to regain some hitpoints.
//At first, they regain 50% of their max hp per rest, but, to encourage potion use,
//as the player levels up, resting is less usefull!
if ( $this->numLoc == 1 || $this->move("Town") )
{
$this->addHP( round( ($this->maxHP * .5) / $this->get_Level()) );
}
}
function calculateTurns()
{
//Give a player 40 turns every 24 hours
//first, calculate number of hours since last login
$timeDiff = $this->timeOfLastLogin - $this->timeOfLastTurn;
// $daysDiff = floor((($timeDiff / 60) / 60) / 24);
$daysDiff = floor(($timeDiff / 60) / 60); //for testing purposes, time to get turns lowered drastically
if ($daysDiff > 1)
{
$this->turns += $daysDiff * 10;
$this->timeOfLastTurn = $this->timeOfLastLogin;
$this->addHP($daysDiff * ($this->maxHP * .25));
}
if ($this->turns > 40)
$this->turns = 40;
}
function addHP ($hpToAdd)
{
$this->hp += $hpToAdd;
if ($this->hp > $this->maxHP)
{
$this->hp = $this->maxHP;
}
mysql_query("UPDATE users set hp={$this->hp} where username = '{$this->user_name}'") or die(" hp {this->hp}, {$this->user_name}");
}
function looseHP ($hpToLoose)
{
$this->hp -= $hpToLoose;
if ($this->hp < 0)
{
$this->hp = 0;
$this->playerDies();
}
mysql_query("UPDATE users set hp={$this->hp} where username = '{$this->user_name}'") or die(" hp");
}
function buy ($itemID)
{
$spent = 0;
$query = "SELECT itemID,itemname,itemtype,isWearable,strAdded,dexAdded,hpAdded FROM items WHERE itemID = {$itemID}";
$result = mysql_query( $query ) or die ("Error finding item; check item id");
if (mysql_num_rows($result) == 1)
{
$row = mysql_fetch_array($result);
$spent = round(( max($row['strAdded']*3,0) + max($row['dexAdded']*3,0) + max($row['hpAdded'],0) + max((isWearable*25),0)) * 1.15);
if ($this->get_Gold() > $spent)
{
$this->addItem($itemID);
$this->spendGold($spent);
}
else $spent = 0;
}
return $spent;
}
function addItem ($itemID)
{
$query = "SELECT itemID,itemname,itemtype,isWearable,strAdded,dexAdded,hpAdded FROM items WHERE itemID = {$itemID}";
$result = mysql_query( $query ) or die ("Error finding item; check item id");
if (mysql_num_rows($result) == 1)
{
$this->myInventory->addItem($itemID);
$toReturn = true;
}
else
$toReturn = false;
return $toReturn;
}
function equip ($itemID)
{
if ( $this->myInventory->equip($itemID) ) {
$this->calculateStats();
return true;
}
else {
$this->calculateStats();
return false;
}
}
function unequip ($itemID)
{
if ( $this->myInventory->unequip($itemID) ) {
$this->calculateStats();
return true;
}
else {
$this->calculateStats();
return false;
}
}
function addGold ($g)
{
$this->gold += $g;
mysql_query("UPDATE users set gold={$this->gold} where username = '{$this->user_name}'") or die("gold update error");
}
function spendGold ($g)
{
$this->gold -= $g;
mysql_query("UPDATE users set gold={$this->gold} where username = '{$this->user_name}'") or die("gold update error");
}
function get_Gold()
{
return $this->gold;
}
function addExp ($e)
{
$this->experience += $e;
mysql_query("UPDATE users set experience={$this->experience} where username = '{$this->user_name}'") or die("exp update error");
$this->calculateStats();
}
function playerDies()
{
//erm...
$this->isUnc = true;
}
function isPlayerUnc()
{
return $this->isUnc;
}
function isLoggedIn()
{
if ($this->logged_in == true) { return true; }
else { return false; }
}
function calculateStats()
{
//this function calculates the players statistics based on class and level
//eventually i should implement stats that grow naturally rather than just linearly....
$bonus = $this->myInventory->getBonus();
switch ($this->numericClass)
{
//1=zombie
//2=pirate
//3=ninja
case 1:
$this->maxHP = round(7 * $this->get_Level()) + $bonus['hp'] + 5;
$this->str = round(3.21*$this->get_Level()) + $bonus['str'] + 3;
$this->dex = round(1.12*$this->get_Level()) + $bonus['dex'] + 1;
break;
case 2:
$this->maxHP = round(4.8 * $this->get_Level()) + $bonus['hp'] + 4;
$this->str = round(2.53*$this->get_Level()) + $bonus['str'] + 3;
$this->dex = round(2.27*$this->get_Level()) + $bonus['dex'] + 3;
break;
case 3:
$this->attacks = 2;
$this->maxHP = round(3.56 * $this->get_Level()) + $bonus['hp'] + 2;
$this->str = round(1.23*$this->get_Level()) + $bonus['str'] + 1;
$this->dex = round(5.7*$this->get_Level()) + $bonus['dex'] + 6;
break;
}
if ($this->hp > $this->maxHP)
$this->hp = $this->maxHP;
}
function log_out()
{
//this function should eventually send a message to the database flagging the user as logged out
$this->user_name = "";
$this->permissions = 0;
$this->logged_in = false;
}
function log_in($user,$pass)
{
/*log_in
1. Query the database for the permissions of the user with the username and password supplied.
2. Check to be sure there is a row with that username and password(password).
3. Set user permissions.
4. Set username.
5. Set the user's logged_in status to 'true' on the database.
2a. Set everything to nothing.
*/
//Get permissions for user from users database.
$query = "SELECT now()+0 as now, email, class, permissions, experience, lastTurn, turns, location, hp, gold FROM users WHERE username = '{$user}' AND password = (password('{$pass}'))";
$result = mysql_query($query)
or die( "Problem with selecting permissions and email" );
//mysql_fetch_array should return an array of all information gathered
//in the above query.
$row = mysql_fetch_array( $result );
if ( mysql_num_rows( $result ) == 1)
{
$this->user_name = $user;
$this->numericClass = $row['class'];
switch ($row['class'])
{
case 1:
$this->PlayerClass = "Zombie";
break;
case 2:
$this->PlayerClass = "Pirate";
break;
case 3:
$this->PlayerClass = "Ninja";
break;
}
$this->permissions = $row['permissions'];
$this->email = $row['email'];
$this->logged_in = true;
if ($row['experience'] != null)
$this->experience = $row['experience'];
else $this->experience = 100;
$this->myInventory = new inventory($this->user_name, $this->numericClass);
$this->hp = $row['hp'];
$this->calculateStats();
$this->numLoc = $row['location'];
$this->gold = $row['gold'];
$this->turns = $row['turns'];
$this->timeOfLastTurn = $row['lastTurn'];
$this->timeOfLastLogin = $row['now'];
$this->calculateTurns();
if ($this->timeOfLastTurn == $this->timeOfLastLogin)
{
mysql_query("UPDATE users SET lastTurn = now(),turns={$this->turns} WHERE username = '{$user}'")
or die ("error updating lastturn");
}
}
else
{
//Just to be sure, set things to "" if the user fails the login.
$this->user_name = "";
$this->permissions = 0;
$this->email = "";
$this->logged_in = false;
}
}
function register($u,$p,$c,$e)
{
//check for existing users with that username
$query = "SELECT username FROM users WHERE username='{$u}'";
$result = mysql_query($query)
or die(mysql_error());
if (mysql_num_rows($result) <= 0)
{
$per = 1;
$g = 0;
$h = 20;
$ex = 0;
$l = 0;
$t = 40;
//username,password,email,permissions,class,gold,hp,experience,location,turns,lastTurn
$query = "INSERT INTO users VALUES ('{$u}', (password('{$p}')), '{$e}', {$per}, {$c}, {$g}, {$h}, {$ex}, {$l}, {$t}, now())";
if (mysql_query($query))
{
$this->log_in($u,$p);
return true;
}
}
return false;
}
function get_Stats()
{
$toReturn['str'] = $this->str;
$toReturn['dex'] = $this->dex;
$toReturn['hp'] = $this->hp;
$toReturn['class'] = $this->numericClass;
$toReturn['attacks'] = $this->attacks;
return $toReturn;
}
function getUserInfoTable()
{
$lv = $this->get_Level();
$toReturn = "<!--USER INFO TABLE--> <table id=\"character_sheet\" width=\"600\" border = \"1\" bordercolor=\"black\" noshadow> <tr> " .
"<th align=\"right\" width=\"125\">Character Name:</th> <td width=\"75\">{$this->user_name}</td><th align=\"right\" width=\"100\"> " .
"Level: </th><td width=\"100\">{$lv}</td><th align=\"right\" width=\"125\">Total Experience:</th><td width=\"75\">" .
"{$this->experience}</td></tr> <tr><th align=\"right\">Streingth:</th><td>{$this->str}" .
"</td><th align=\"right\">Dexterity:</th><td>{$this->dex}</td><th align=\"right\">Hit Points:</th>" .
"<td>{$this->hp} / {$this->maxHP}</td></tr><tr><th align=\"right\" colspan=\"3\">" .
"Turns Remaining:</th><td colspan=\"3\">{$this->turns}</td></tr></table>";
return $toReturn;
}
function usePotion($id)
{
//item must be decremented in inventory and any status effects calculated
$hpchange = $this->myInventory->usePotion($id);
$this->addHP($hpchange);
return $hpchange;
}
function sell($id)
{
//item must be decremented in inventory and any status effects calculated
$goldChange = $this->myInventory->sell($id);
$this->addGold($goldChange);
return $goldChange;
}
function get_PlayerBuyList()
{
$toreturn = "<div id=\"PlayerBuyList\">Buy something?<br/>";
$query = "SELECT itemID, itemname, itemtype, isWearable, strAdded, dexAdded, hpAdded
FROM items
WHERE itemtype <> 10
ORDER BY itemtype";
$result = mysql_query( $query ) or die("error getting playerbuylist");
while ( $row = mysql_fetch_array( $result ) )
{
$toreturn .= "<a href=\"shop.php?task=buy&itemid={$row['itemID']}\">buy {$row['itemname']}</a><br/>";
}
$toreturn .= "</div>";
return $toreturn;
}
}
?>