forked from r00telement/SmartHunter
-
Notifications
You must be signed in to change notification settings - Fork 140
/
smarthunter.php
172 lines (167 loc) · 7.15 KB
/
smarthunter.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
<?php
$_POST = json_decode(file_get_contents('php://input'), true);
$version = $_POST["version"];
$key = $_POST["key"];
$command = $_POST["command"];
$host = $_POST["host"] == "true";
$player = $_POST["player"];
$damage = $_POST["damage"];
$status = "error";
$obj = "";
$development = false;
$currentVersion = "1.2-1";
if ($development) {
$obj = "dev";
}else if (strcmp($currentVersion, $version) != 0) {
$obj = "v";
}else if ((isset($key) && strlen($key) == 28 && strcmp(substr($key, -1), "=") == 0) || strcmp($command, "alive") == 0) {
$servername = "<server of your db>";
$username = "<username of your db>";
$password = "<password of your db>";
$dbname = "<name of your db>";
$conn = new mysqli($servername, $username, $password, $dbname);
if (!$conn->connect_error) {
$status = "ok";
if (strcmp($command, "alive") == 0) {
//Nothing
}else if (strcmp($command, "damage") == 0 && strlen($player) > 0) {
$sql = "SELECT `damage`, `player_count` FROM `SmartHunter` WHERE `key` = '" . $key . "'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$damages = "";
$row = $result->fetch_assoc();
if ($row["damage"] != null) {
$damages = $row["damage"];
}
$damages = json_decode($damages, true);
$damages[$player] = $damage;
$sql = "UPDATE `SmartHunter` SET `damage` = '" . json_encode($damages) . "' WHERE `key` = '" . $key . "'";
if (!$conn->query($sql)) {
$status = "error";
$obj = $conn->error;
}else{
if ($row["player_count"] > 1) {
unset($damages[$player]);
$obj = json_encode($damages);
}else{
$status = "error";
$obj = "false";
}
}
}else{
$status = "error";
$obj = "0";
}
}else if (strcmp($command, "elevate") == 0) {
$sql = "SELECT * FROM `SmartHunter` WHERE `key` = '" . $key . "'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$sql = "UPDATE `SmartHunter` SET `host_connected` = 1, `last_modify` = CURRENT_TIMESTAMP WHERE `key` = '" . $key . "'";
if (!$conn->query($sql)) {
$status = "error";
$obj = $conn->error;
}else{
$obj = $key;
}
}else{
$status = "error";
$obj = "0";
}
}else if (strcmp($command, "hello") == 0) {
$sql = "SELECT * FROM `SmartHunter` WHERE `key` = '" . $key . "'";
$result = $conn->query($sql);
if ($result->num_rows == 0) {
$sql = "INSERT INTO `SmartHunter` (`key`, `host_connected`, `player_count`, `damage`, `data`, `last_modify`) VALUES ('" . $key . "', " . ($host ? "1" : "0") . ", 1, NULL, NULL, CURRENT_TIMESTAMP)";
if (!$conn->query($sql)) {
$status = "error";
$obj = $conn->error;
}else{
$obj = $key;
}
}else{
if ($host) {
$sql = "UPDATE `SmartHunter` SET `host_connected` = 1, `player_count` = `player_count` + 1, `last_modify` = CURRENT_TIMESTAMP WHERE `key` = '" . $key . "'";
}else{
$sql = "UPDATE `SmartHunter` SET `player_count` = `player_count` + 1, `last_modify` = CURRENT_TIMESTAMP WHERE `key` = '" . $key . "'";
}
if (!$conn->query($sql)) {
$status = "error";
$obj = $conn->error;
}else{
$obj = $key;
}
}
}else if (strcmp($command, "done") == 0) {
if ($host) {
$sql = "UPDATE `SmartHunter` SET `host_connected` = 0, `player_count` = `player_count` - 1, `last_modify` = CURRENT_TIMESTAMP WHERE `key` = '" . $key . "'; ";
}else{
$sql = "UPDATE `SmartHunter` SET `player_count` = `player_count` - 1, `last_modify` = CURRENT_TIMESTAMP WHERE `key` = '" . $key . "'; ";
}
$sql .= "DELETE FROM `SmartHunter` WHERE `player_count` <= 0 OR CURRENT_TIMESTAMP - `last_modify` > 60; ";
if (!$conn->multi_query($sql)) {
$status = "error";
$obj = $conn->error;
}else{
$obj = $key;
}
}else if (strcmp($command, "check") == 0) {
$sql = "SELECT `host_connected`, `player_count` FROM `SmartHunter` WHERE `key` = '" . $key . "'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$sql = "UPDATE `SmartHunter` SET `last_modify` = CURRENT_TIMESTAMP WHERE `key` = '" . $key . "'";
if ($conn->query($sql)) {
$obj = array("host" => $row["host_connected"] == 1 ? "true" : "false", "players" => $row["player_count"] > 1 ? "true" : "false");
}else{
$status = "error";
$obj = $conn->error;
}
}else{
$status = "error";
$obj = "0";
}
}else if (strcmp($command, "push") == 0) {
$data = $_POST["data"];
if (isset($data)) {
$sql = "UPDATE `SmartHunter` SET `data` = '" . $data . "', `last_modify` = CURRENT_TIMESTAMP WHERE `key` = '" . $key . "'";
if (!$conn->query($sql)) {
$status = "error";
$obj = $conn->error;
}else{
$sql = "SELECT `player_count` FROM `SmartHunter` WHERE `key` = '" . $key . "'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
if ($row["player_count"] > 1) {
$obj = "true";
}else{
$status = "error";
$obj = "false";
}
}else{
$status = "error";
$obj = "0";
}
}
}
}else if (strcmp($command, "pull") == 0) {
$sql = "SELECT `data`, `host_connected` FROM `SmartHunter` WHERE `key` = '" . $key . "'";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
if ($row["host_connected"] == 1) {
$obj = $row["data"];
}else{
$status = "error";
$obj = "false";
}
}else{
$status = "error";
$obj = "0";
}
}
$conn->close();
}
}
header('Content-type: application/json');
echo json_encode(['status' => $status, 'result' => $obj]);