-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.php
115 lines (106 loc) · 3.34 KB
/
database.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
<?php
/**
* User: michele
* Date: 03/09/17
* Time: 22.09
*
* Database main class
*
*/
class Database
{
private $connection;
/**
* Database constructor.
* @param $connection String
*/
public function __construct($connection)
{
$this->connection = $connection;
}
/**
* @param $username String
* @return true or false
*/
public function getUser($username)
{
$prepare = $this->connection->prepare("SELECT * FROM Users WHERE username = ?");
$prepare->execute([$username]);
$fetch = $prepare->fetch();
if ($fetch){
return true;
} else {
return false;
}
}
/**
* @param $user_id INT
* @param $username String
*/
public function addUser($user_id, $username)
{
$prepare = $this->connection->prepare("SELECT * FROM Users WHERE user_id = ?");
$prepare->execute([$user_id]);
$fetch = $prepare->fetch();
if (!$fetch){
$prepare = $this->connection->prepare("INSERT INTO Users(user_id, username) VALUES (?, ?)");
$prepare->execute([$user_id, '@'.$username]);
}
}
/**
* @param $chat_id INT
* @param $username String
*/
public function addPointToUser($chat_id, $username)
{
$prepare = $this->connection->prepare("SELECT * FROM Users_vote WHERE chat_id = ? AND username = ?");
$prepare->execute([$chat_id, $username]);
$fetch = $prepare->fetch();
if ($fetch) {
$points = $fetch['votes'];
$points += 1;
$prepare = $this->connection->prepare("UPDATE Users_vote SET votes = " . $points . " WHERE chat_id = ? AND username = ?");
$prepare->execute([$chat_id, $username]);
} else {
$prepare = $this->connection->prepare("INSERT INTO Users_vote(chat_id, username, votes) VALUES (?, ?, 1)");
$prepare->execute([$chat_id, $username]);
}
}
/**
* @param $chat_id INT
* @param $username String
*/
public function deductPointToUser($chat_id, $username)
{
$prepare = $this->connection->prepare("SELECT * FROM Users_vote WHERE chat_id = ? AND username = ?");
$prepare->execute([$chat_id, $username]);
$fetch = $prepare->fetch();
if ($fetch) {
$points = $fetch['votes'];
$points -= 1;
$prepare = $this->connection->prepare("UPDATE Users_vote SET votes = " . $points . " WHERE chat_id = ? AND username = ?");
$prepare->execute([$chat_id, $username]);
} else {
$prepare = $this->connection->prepare("INSERT INTO Users_vote(chat_id, username, votes) VALUES (?, ?, -1)");
$prepare->execute([$chat_id, $username]);
}
}
/**
* Get a chat's leaderboard
* @param $chat_id INT
* @return String
*/
public function getLeaderboard($chat_id)
{
$prepare = $this->connection->prepare("SELECT * FROM Users_vote WHERE chat_id = ? ORDER BY votes DESC");
$prepare->execute([$chat_id]);
$fetchAll = $prepare->fetchAll();
$text = "Chat leaderboard \n";
$i = 1;
foreach ($fetchAll as $fetch) {
$text .= $i." - ".$fetch['username']." with ".$fetch['votes']." points \n";
$i++;
}
return $text;
}
}