-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplace-bet.php
118 lines (105 loc) · 5.46 KB
/
place-bet.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
<html>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<body>
<?php
require 'connect-db.php';
include 'navbar.php';
$prof = $_GET['prof'];
$user = $_SESSION['name'];
// echo '<pre>';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// process form data and generate output
// echo "Post Request info\n";
// var_dump($_POST);
place_bet();
}
function updateBalance($db, $user, $val){
$val = floatval($val);
$sql = "UPDATE accounts SET balance = balance - $val WHERE username = '$user'";
$stmt = $db->prepare($sql);
$stmt->execute();
}
// function place_bet($db, $game_id, $bet_type, $wager){
function place_bet() {
global $db;
// Find user
$query_get_user = "SELECT id, balance FROM accounts WHERE username = :username";
$statement = $db->prepare($query_get_user);
$statement->bindValue(':username', $_SESSION['name']);
$statement->execute();
$user_tuple = $statement->fetchAll(PDO::FETCH_ASSOC);
// echo "User Attributes: ";
// echo var_dump($user_tuple);
$statement->closeCursor();
//Check if user already bet on this game
$query_check_already_bet = "SELECT IF(EXISTS (
SELECT * FROM bets
WHERE game_id = :game_id AND account_id = :account_id
),
1,
0
) AS result";
$statement = $db->prepare($query_check_already_bet);
$statement->bindValue(':account_id', $user_tuple['id']);
$statement->bindValue(':game_id', $_POST['game_id']);
$statement->execute();
$has_bet = $statement->fetchAll(PDO::FETCH_ASSOC);
// echo "User has bet?";
// echo var_dump($has_bet);
if ($has_bet[0]["result"] == 1) {
echo '<script>alert("Already bet on this game"); window.location.href = "/cachecash/games.php";</script>';
}
// Check if user has enough money
if ($user_tuple[0]['balance'] < floatval($_POST['wager_amount'])) {
echo '<script>alert("Insufficient CacheCash"); window.location.href = "/cachecash/games.php";</script>';
}
//Get the game the user want to bet on
$query_game = "SELECT home_team, away_team FROM games WHERE game_id = :game_id";
$statement = $db->prepare($query_game);
$statement->bindValue(':game_id', $_POST['game_id']);
$statement->execute();
$teams = $statement->fetchAll(PDO::FETCH_ASSOC);
$statement->closeCursor();
// echo "Teams: ";
// echo var_dump($teams);
$team = NULL; //default for OVER UNDER
$type = NULL;
if ($_POST['selectedOption'] == "Away Moneyline" OR $_POST['selectedOption'] == "Away Spread"){
$team = $teams[0]['away_team'];
if ($_POST['selectedOption'] == "Away Moneyline") {
$type = "Away_Moneyline";
} else {
$type = "Spread";
}
} else if ($_POST['selectedOption'] == "Home Moneyline" OR $_POST['selectedOption'] == "Home Spread") {
$team = $teams[0]['home_team'];
if ($_POST['selectedOption'] == "Home Moneyline") {
$type = "Home_Moneyline";
} else {
$type = "Spread";
}
}
if ($type == NULL) {
$type = "OverUnder";
}
//echo "Selected the team: " . $team . "\n";
//echo "Enum Bet Type: " . $type . "\n";
//Add new bet
$user_id = intval($user_tuple[0]['id']);
$game_id = intval($_POST['game_id']);
$wager = floatval($_POST['wager_amount']);
$sql = "INSERT INTO
bets(account_id, game_id, team, wager, bet_type, active)
VALUES ($user_id, $game_id, '$team', $wager, '$type', True)";
$db->exec($sql);
updateBalance($db, $_SESSION['name'], $wager);
header('Location: /cachecash/games.php?bet=yes');
//echo "Successful Bet placed!";
}
// echo '</pre>';
?>
</body>
</html>