-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave.php
More file actions
160 lines (122 loc) · 4.85 KB
/
save.php
File metadata and controls
160 lines (122 loc) · 4.85 KB
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
<?php
function getid($token){
$url = "https://discord.com/api/users/@me";
$headers = array('Content-Type: application/x-www-form-urlencoded', 'Authorization: Bearer ' . $token);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
curl_close($curl);
$results = json_decode($response, true);
$id = $results['id'];
return $id;
}
function addplay($newpuzzles){
foreach ($newpuzzles as $key => $value) {
if (isset($value['completed']) && $value['completed'] == 0) {
$name = $key;
$diff = $value['difficulty'];
$jsonFile = 'setups.json';
$jsonData = file_get_contents($jsonFile);
$setups = json_decode($jsonData, true);
$setups[$diff][$name]['plays'] = $setups[$diff][$name]['plays']+1;
$newJsonData = json_encode($setups, JSON_PRETTY_PRINT);
file_put_contents($jsonFile, $newJsonData);
}
}
}
function completedpuzzles($hist,$oldhist){
$diffKeys = array();
foreach ($hist as $key => $value1) {
if (array_key_exists($key, $oldhist)) {
$value2 = $oldhist[$key];
if (($value1['completed'] != 0&&$value2['completed']==0)) {
$diffKeys[$key] = $value1;
}
}
}
foreach ($diffKeys as $key => $value) {
$name = $key;
$diff = $value['difficulty'];
$jsonFile = 'setups.json';
$jsonData = file_get_contents($jsonFile);
$setups = json_decode($jsonData, true);
$setups[$diff][$name]['completions'] = $setups[$diff][$name]['completions']+1;
$newJsonData = json_encode($setups, JSON_PRETTY_PRINT);
file_put_contents($jsonFile, $newJsonData);
}
}
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the raw JSON data from the request body
$jsonData = file_get_contents('php://input');
// Check if the JSON data is not empty
if (!empty($jsonData)) {
// Decode the JSON data into a PHP associative array
$data2 = json_decode($jsonData, true);
if ($data2 === null) {
// JSON parsing failed
http_response_code(400); // Bad Request
echo json_encode(["error" => "Invalid JSON data"]);
} else {
$token = $data2['accessToken'];
$id = getid($token);
if (isset($id)) {
// Read the JSON data from the file
$jsonString = file_get_contents('data.json');
// Decode the JSON data into a PHP associative array
$data = json_decode($jsonString, true);
// Get the 'userid' from the URL
$userId = $id;
// Check if the 'userid' exists in the JSON data
if (isset($data[$userId])) {
// Output the JSON data for the specified 'userid'
// 'userid' not found in the JSON data, create a new entry
$hist = $data2['history'];
$oldhist = json_decode($data[$userId]['history'],true);
$newpuzzles = array_diff_assoc($hist, $oldhist);
addplay($newpuzzles);
completedpuzzles($hist,$oldhist);
foreach ($hist as $key => $element) {
// Check if $oldhist has the corresponding key
if (isset($oldhist[$key])) {
// Determine the value for 'completed' based on the condition
$completedValue = ($element['completed'] == 0) ? $oldhist[$key]['completed'] : $element['completed'];
$element['completed'] = $completedValue;
$hist[$key] = $element;
}
}
// Merge $oldhist and $hist
$resultArray = array_merge($oldhist, $hist);
$newEntry = [
"userid" => $data[$userId]['userid'],
"history" => json_encode($resultArray),
"points" => $data[$userId]['points'],
"username" => $data[$userId]['username'],
"avatar" => $data[$userId]['avatar'],
"time" => $data[$userId]['time'], // Current time in milliseconds
"supporter" => $data[$userId]['supporter'],
"rush_score" => $data[$userId]['rush_score'],
"rush_time" => $data[$userId]['rush_time'],
"rush_access" => $data[$userId]['rush_access']
];
// Add the new entry to the data array
$data[$userId] = $newEntry;
// Save the updated JSON data back to the file
file_put_contents('data.json', json_encode($data));
echo json_encode($resultArray);
}
}
}
} else {
// JSON data is empty
http_response_code(400); // Bad Request
echo json_encode(["error" => "Empty JSON data"]);
}
} else {
// Request method is not POST
http_response_code(405); // Method Not Allowed
echo json_encode(["error" => "Method not allowed"]);
}
?>