-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.inc.php
610 lines (504 loc) · 18.5 KB
/
classes.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
class User
{
var $id;
var $name;
var $balance;
function __construct($new_id, $conn)
{
$this->id = $new_id;
$this->get_name($conn);
$this->get_balance($conn);
}
function set_balance($conn, $new_balance)
{
$this->balance = $new_balance;
//also need to update in the database
$query = "UPDATE users SET balance = $new_balance WHERE id = $this->id";
if(mysqli_query($conn, $query))
{
}
else
echo "Error balance update in db";
}
function get_id()
{
return $this->id;
}
function get_name($conn)
{
$query = "SELECT first_name FROM users WHERE id='$this->id'";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$name = $array['first_name'];
$this->name = $name;
}
return $name;
}
}
function get_balance($conn)
{
$query = "SELECT balance FROM users WHERE id='$this->id'";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$balance = $array['balance'];
$this->balance = $balance;
}
return floatval($balance);
}
}
//for updating the user's basic info
function set_basic_info($conn, $first_name, $last_name, $email)
{
$query_change_info = "UPDATE users SET first_name = '$first_name', last_name = '$last_name', email = '$email' WHERE id = '$this->id'";
if(mysqli_query($conn, $query_change_info))
return true;
}
//function to place the order
function placeOrder($conn, $type, $company_id, $quantity, $limit_or_market, $limit_price)
{
//first check if this is a valid order to be placed
//check if has enough balance to buy
if($type == "buy")
{
//first get price of the share
$company = new Company($company_id);
$price = floatval($company->get_company_price($conn));
if($this->balance < $quantity*$price)
{
echo "<script>alert('You do not have enough balance to place this order'); </script>";
return false;
}
}
elseif($type == "sell")
{
//check if has enough shares to sell
if($quantity > $this->get_user_quantity($conn, $company_id))
{
echo "<script>alert('You do not own enough shares to place this order'); </script>";
return false;
}
}
$query = "INSERT INTO orders(user_id, company_id, quantity, type, limit_or_market, limit_price) VALUES('$this->id','$company_id','$quantity','$type','$limit_or_market','$limit_price')";
if(mysqli_query($conn, $query))
{
return true;
}
else
echo "Error placing the order";
}
//get the quantity of shares for a company
function get_user_quantity($conn, $company_id)
{
$query = "SELECT quantity FROM shares WHERE user_id = '$this->id' AND company_id = '$company_id'";
if($run = mysqli_query($conn, $query))
{
if(mysqli_num_rows($run) < 1)
{
return 0;
}
while($array = mysqli_fetch_assoc($run))
{
$quantity = $array['quantity'];
}
return $quantity;
}
}
//execute the orders for this user
function executeOrders($conn)
{
$message_to_return = "";
//check with all the orders in the table orders
$query_get_orders = "SELECT * FROM orders WHERE user_id = '$this->id'";
if($run_get_orders = mysqli_query($conn, $query_get_orders))
{
if(mysqli_num_rows($run_get_orders) < 1)
{
return $message_to_return;
}
$count = 0;
//for eah order
while($array = mysqli_fetch_assoc($run_get_orders))
{
$order_id = $array['id'];
$company_id = $array['company_id'];
$type = $array['type'];
$quantity = $array['quantity'];
$limit_or_market = $array['limit_or_market'];
$limit_price = floatval($array['limit_price']);
//get price of the share
$company = new Company($company_id);
$price = floatval($company->get_company_price($conn));
//check validity for limit orders
if($limit_or_market == "limit" && (($type == "sell" && $price < $limit_price) || ($type == "buy" && $price >$limit_price)))
{
continue;
}
$total_price = $quantity*$price;
if($type == "buy")
{
$this->balance -= $total_price;
if($this->balance < 0)
{
$this->balance+=$total_price;
$message_to_return.="One order could not be executed due to: Insufficient Balance.";
continue;
}
}
if($type == "sell")
{
$this->balance += $total_price;
}
//insert into or update shares table
//check if some shares already there
$query = "SELECT * FROM shares WHERE company_id = '$company_id' AND user_id = '$this->id'";
if($run = mysqli_query($conn, $query))
{
if(mysqli_num_rows($run) == 1)
{
while($array = mysqli_fetch_assoc($run))
{
$owned = $array['quantity'];
}
//update shares quantity
if($type == "buy")
$query_update = "UPDATE shares SET quantity = quantity + '$quantity' WHERE company_id = '$company_id' AND user_id = '$this->id'";
elseif($type == "sell")
{
if($owned < $quantity)
{
$this->balance -= $total_price;
$message_to_return.="One order could not be executed due to: Insufficient Shares<br>";
continue;
}
$new_quantity = $owned - $quantity;
if($new_quantity > 0)
$query_update = "UPDATE shares SET quantity = '$new_quantity' WHERE company_id = '$company_id' AND user_id = '$this->id'";
elseif($new_quantity == 0)
$query_update = "DELETE FROM shares WHERE company_id = $company_id AND user_id = $this->id";
}
if(mysqli_query($conn, $query_update))
{
}
else
echo "Error updating shares";
}
elseif(mysqli_num_rows($run) == 0)
{
if($type == "sell")
{
$this->balance -= $total_price;
$message_to_return.="One order could not be executed due to: Insufficient Shares<br>";
continue;
}
//insert new entry
if($type == "buy")
{
$query = "INSERT INTO shares(user_id, company_id, quantity) VALUES ('$this->id', '$company_id', '$quantity')";
if(mysqli_query($conn, $query))
{
}
else
echo "Couldnt insert shares";
}
}
}
//update user balance
$this->set_balance($conn, $this->balance);
//insert into transactions table
if($type == "buy")
$query = "INSERT INTO transactions(user_id, company_id, quantity, price) VALUES ('$this->id', '$company_id', '$quantity', '$price')";
elseif($type == "sell")
$query = "INSERT INTO transactions(user_id, company_id, quantity, price) VALUES ('$this->id', '$company_id', '-$quantity', '$price')";
if(mysqli_query($conn, $query))
{
}
else
echo "Error transaction add";
//now delete from the orders table
$query_delete = "DELETE FROM orders WHERE id = '$order_id'";
if(mysqli_query($conn, $query_delete))
{
}
else
{
echo "Error deleting order";
}
$count++;
if($count == 1)
$message_to_return .= "One Order was executed. See <a href='trades.php'>Trade Book</a>";
elseif($count > 1)
$message_to_return .= "$count Orders were executed. See <a href='trades.php'>Trade Book</a>";
}
return $message_to_return;
}
}
//check for notifications (messages) for user
function checkMessages($conn)
{
$query = "SELECT message FROM users WHERE id = $this->id";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$message = $array['message'];
if($message != "")
{
echo "<div id='note'>$message<a id='close' class='pull-right'>[Close]</a></div>";
//this is for removing the message
$message = "";
}
$query_message = "UPDATE users SET message = '$message' WHERE id = '$this->id'";
mysqli_query($conn, $query_message);
}
}
}
//restart the game, delete/reset things
function restartGame($conn)
{
//delete all the things done by user, set user's balance to 500000
$query_all = "DELETE FROM shares WHERE user_id = $this->id; DELETE FROM transactions WHERE user_id = $this->id; DELETE FROM orders WHERE user_id = $this->id; UPDATE users SET balance = 500000, message='Reset Successfull.', highest_rank = '500' WHERE id = $this->id";
if(mysqli_multi_query($conn, $query_all))
{
$this->balance = 500000;
header("Location:index.php");
}
else
echo mysqli_error($conn);
return;
}
//returns valuation of all the shares
function get_valuation($conn)
{
$valuation_in_shares = 0;
$query_shares = "SELECT * FROM shares WHERE user_id = $this->id";
if($run_shares = mysqli_query($conn, $query_shares))
{
if(mysqli_num_rows($run_shares) < 1)
{
$valuation_in_shares = 0;
}
else
{
//for each company
while($array_shares = mysqli_fetch_assoc($run_shares))
{
$company_id = $array_shares['company_id'];
$quantity = $array_shares['quantity'];
$query_company_price = "SELECT price FROM companies WHERE id = $company_id";
$getPrice = mysqli_fetch_assoc(mysqli_query($conn, $query_company_price));
$company_price = $getPrice['price'];
$shares_value = $quantity*$company_price;
$valuation_in_shares += $shares_value;
}
}
}
return $valuation_in_shares;
}
//change user's consent about showing info of his stocks to others
function set_consent($conn, $consent)
{
if(mysqli_query($conn, "UPDATE users SET consent = '$consent' WHERE id = '$this->id'"))
return true;
else
return false;
}
}
class Company
{
var $id;
var $name;
var $price;
function __construct($new_id)
{
$this->id = $new_id;
}
//used only when the admin changes the price
function set_price($conn, $new_price)
{
$this->price = $new_price;
$query = "SELECT price, high, low FROM companies WHERE id = $this->id";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$price = $array['price'];
$high = $array['high'];
$low = $array['low'];
if($new_price > $high)
$high = $new_price;
elseif($new_price < $low)
$low = $new_price;
$query_update = "UPDATE companies SET price = $new_price, prev_price = $price, high = $high, low = $low WHERE id = $this->id";
if(mysqli_query($conn, $query_update))
{
}
else
echo "Err";
}
}
//insert into 'price_variation' table
$query_price = "INSERT INTO price_variation(company_id, price) VALUES('$this->id', '$new_price')";
if(mysqli_query($conn, $query_price))
{
}
else
echo "Error price table";
return true;
}
function get_company_name($conn)
{
$query = "SELECT name FROM companies WHERE id = $this->id";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$name = $array['name'];
}
return $name;
}
}
//get company price from its id
function get_company_price($conn)
{
$query = "SELECT price FROM companies WHERE id='$this->id'";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$price = $array['price'];
}
return floatval($price);
}
}
//get low price from its id
function get_low_price($conn)
{
$query = "SELECT low FROM companies WHERE id='$this->id'";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$low = $array['low'];
}
return $low;
}
}
//get high price from its id
function get_high_price($conn)
{
$query = "SELECT high FROM companies WHERE id='$this->id'";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$high = $array['high'];
}
return $high;
}
}
//get high price from its id
function get_abbr($conn)
{
$query = "SELECT abbr FROM companies WHERE id='$this->id'";
if($run = mysqli_query($conn, $query))
{
while($array = mysqli_fetch_assoc($run))
{
$abbr = $array['abbr'];
}
return $abbr;
}
}
}
class Order
{
var $id;
var $user_id;
var $company_id;
var $quantity;
var $type;
var $limit_price;
function __construct($new_id)
{
$this->id = $new_id;
}
function get_id()
{
return $this->id;
}
function get_company_id()
{
return $this->company_id;
}
function get_user_id()
{
return $this->user_id;
}
function get_limit_price()
{
return $this->limit_price;
}
function delete_order($conn)
{
$query_delete = "DELETE FROM orders WHERE id=$this->id";
if(mysqli_query($conn, $query_delete))
{
return true;
}
else
return false;
}
function edit_order($conn, $new_price, $new_quantity)
{
$query_update = "UPDATE orders SET limit_price = '$new_price', quantity = $new_quantity WHERE id=$this->id";
if(mysqli_query($conn, $query_update))
{
return true;
}
else
return false;
}
}
class Transaction
{
var $company_id;
var $id;
var $user_id;
var $quantity;
var $price;
function __construct($new_id, $new_user_id, $new_company_id, $new_quantity, $new_type, $new_price)
{
$this->id = $new_id;
$this->company_id = $new_company_id;
$this->user_id = $new_user_id;
$this->type = $new_type;
$this->price = $new_price;
$this->quantity = $new_quantity;
}
function get_id()
{
echo $this->id;
}
function get_company_id()
{
echo $this->company_id;
}
function get_user_id()
{
echo $this->user_id;
}
function getprice()
{
echo $this->price;
}
function get_quantity()
{
echo $this->quantity;
}
}
?>