forked from nightmarez/Casino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplybets.php
76 lines (60 loc) · 2.35 KB
/
applybets.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
<?php
if (!isset($_COOKIE['login']) || !isset($_COOKIE['pass']))
{
echo 0;
die();
}
$login = htmlspecialchars($_COOKIE['login']);
$pass = htmlspecialchars($_COOKIE['pass']);
require_once('utils.php');
$request = json_decode($_POST['request']);
$gameid = $request->{'gameid'};
$token = $request->{'token'};
$data = $request->{'data'};
// =================================================================================================================
$totalBets = 0;
for ($i = 0; $i < count($data); ++$i)
{
$totalBets += $data[$i]->{'bet'};
}
// =================================================================================================================
$totalMoney = 0;
$userId = 0;
$db = new PdoDb();
$req = $db->prepare('SELECT * FROM `users` WHERE `login`=:login AND `pass`=:pass AND `level`=2;');
$req->bindParam(':login', $login, PDO::PARAM_STR);
$req->bindParam(':pass', $pass, PDO::PARAM_STR);
$req->execute();
while (list($id, $login, $pass, $level, $activated, $sms, $money) = $req->fetch(PDO::FETCH_NUM))
{
$totalMoney = $money;
$userId = $id;
break;
}
// =================================================================================================================
if ($totalBets > $totalMoney)
{
echo 'error: not enough money';
die();
}
// =================================================================================================================
$totalMoney -= $totalBets;
$req = $db->prepare('UPDATE `users` SET `money`=:totalMoney WHERE `id`=:userId;');
$req->bindParam(':totalMoney', $totalMoney, PDO::PARAM_INT);
$req->bindParam(':userId', $userId, PDO::PARAM_INT);
$req->execute();
// =================================================================================================================
for ($i = 0; $i < count($data); ++$i)
{
$bet = $data[$i]->{'bet'};
$balls = implode(',', $data[$i]->{'balls'});
$req = $db->prepare('INSERT INTO `bets` (`token`, `gameid`, `bet`, `balls`) VALUES (:token, :gameid, :bet, :balls);');
$req->bindParam(':token', $token, PDO::PARAM_STR);
$req->bindParam(':gameid', $gameid, PDO::PARAM_INT);
$req->bindParam(':bet', $bet, PDO::PARAM_INT);
$req->bindParam(':balls', $balls, PDO::PARAM_STR);
$req->execute();
}
// =================================================================================================================
echo 'OK';
?>