Repository navigation
Expand file tree
/
Copy pathapi.php
More file actions
181 lines (156 loc) · 5.37 KB
/
Copy pathapi.php
File metadata and controls
181 lines (156 loc) · 5.37 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
header('Content-Type: application/json');
// Database connection
$host = 'localhost';
$dbname = 'benchmarks';
$username = 'root';
$password = 'orestislef';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo json_encode(['success' => false, 'error' => 'Database connection failed']);
exit;
}
// Get JSON input
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? '';
if ($action === 'save_result') {
// Generate unique share code
$share_code = generateShareCode($pdo);
$test_type = $input['test_type'] ?? 'gpu';
$score = $input['score'] ?? 0;
// CPU metrics
$cpu_digits_total = $input['cpu_digits_total'] ?? null;
$cpu_digits_per_sec = $input['cpu_digits_per_sec'] ?? null;
$cpu_threads = $input['cpu_threads'] ?? null;
// GPU metrics
$gpu_avg_fps = $input['gpu_avg_fps'] ?? null;
$gpu_min_fps = $input['gpu_min_fps'] ?? null;
$gpu_max_fps = $input['gpu_max_fps'] ?? null;
$gpu_total_frames = $input['gpu_total_frames'] ?? null;
// System info
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$platform = $input['platform'] ?? '';
try {
$stmt = $pdo->prepare("
INSERT INTO results (
share_code, test_type, score,
cpu_digits_total, cpu_digits_per_sec, cpu_threads,
gpu_avg_fps, gpu_min_fps, gpu_max_fps, gpu_total_frames,
user_agent, platform
) VALUES (
:share_code, :test_type, :score,
:cpu_digits_total, :cpu_digits_per_sec, :cpu_threads,
:gpu_avg_fps, :gpu_min_fps, :gpu_max_fps, :gpu_total_frames,
:user_agent, :platform
)
");
$stmt->execute([
':share_code' => $share_code,
':test_type' => $test_type,
':score' => $score,
':cpu_digits_total' => $cpu_digits_total,
':cpu_digits_per_sec' => $cpu_digits_per_sec,
':cpu_threads' => $cpu_threads,
':gpu_avg_fps' => $gpu_avg_fps,
':gpu_min_fps' => $gpu_min_fps,
':gpu_max_fps' => $gpu_max_fps,
':gpu_total_frames' => $gpu_total_frames,
':user_agent' => $user_agent,
':platform' => $platform
]);
echo json_encode([
'success' => true,
'share_code' => $share_code
]);
} catch(PDOException $e) {
echo json_encode([
'success' => false,
'error' => 'Failed to save result: ' . $e->getMessage()
]);
}
} elseif ($action === 'get_result') {
$share_code = $input['share_code'] ?? '';
try {
$stmt = $pdo->prepare("SELECT * FROM results WHERE share_code = :share_code");
$stmt->execute([':share_code' => $share_code]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result) {
echo json_encode([
'success' => true,
'result' => $result
]);
} else {
echo json_encode([
'success' => false,
'error' => 'Result not found'
]);
}
} catch(PDOException $e) {
echo json_encode([
'success' => false,
'error' => 'Failed to fetch result'
]);
}
} elseif ($action === 'get_leaderboard') {
$test_type = $input['test_type'] ?? 'all';
$limit = $input['limit'] ?? 10;
try {
if ($test_type === 'all') {
$stmt = $pdo->prepare("
SELECT share_code, test_type, score, created_at
FROM results
ORDER BY score DESC
LIMIT :limit
");
} else {
$stmt = $pdo->prepare("
SELECT share_code, test_type, score, created_at
FROM results
WHERE test_type = :test_type
ORDER BY score DESC
LIMIT :limit
");
$stmt->bindValue(':test_type', $test_type, PDO::PARAM_STR);
}
$stmt->bindValue(':limit', (int)$limit, PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode([
'success' => true,
'results' => $results
]);
} catch(PDOException $e) {
echo json_encode([
'success' => false,
'error' => 'Failed to fetch leaderboard'
]);
}
} else {
echo json_encode([
'success' => false,
'error' => 'Invalid action'
]);
}
function generateShareCode($pdo) {
$characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; // Removed ambiguous characters
$attempts = 0;
$maxAttempts = 10;
do {
$code = '';
for ($i = 0; $i < 8; $i++) {
$code .= $characters[random_int(0, strlen($characters) - 1)];
}
// Check if code already exists
$stmt = $pdo->prepare("SELECT COUNT(*) FROM results WHERE share_code = :code");
$stmt->execute([':code' => $code]);
$exists = $stmt->fetchColumn() > 0;
$attempts++;
} while ($exists && $attempts < $maxAttempts);
if ($exists) {
// Fallback to timestamp-based code
$code = strtoupper(substr(md5(microtime()), 0, 8));
}
return $code;
}