-
Notifications
You must be signed in to change notification settings - Fork 0
/
scores.php
175 lines (162 loc) · 4.76 KB
/
scores.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
173
174
175
<?php
error_reporting(-1);
// 54.251.62.84
// QJ6T!yybCq
$original = $list = array();
$new_record = false;
if (($handle = fopen("datafile", "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
$list[] = $data;
}
fclose($handle);
}
if ($_REQUEST['fullname'] || $_REQUEST['email'] || $_REQUEST['wordcount'] || $_REQUEST['maxsize'] ||
$_REQUEST['letters'] || $_REQUEST['time'] || $_REQUEST['score']) {
$html_display = false;
} else {
$html_display = true;
}
if ($_REQUEST['fullname'] && $_REQUEST['email'] && $_REQUEST['wordcount'] && $_REQUEST['letters'] && $_REQUEST['time'] && $_REQUEST['score']) {
$_REQUEST['date'] = time();
$md5 = $_REQUEST['id'] = md5($_REQUEST['fullname'].$_REQUEST['email'].$_REQUEST['wordcount'].$_REQUEST['letters'].$_REQUEST['time'].$_REQUEST['score'].$_REQUEST['date']);
$score = $_REQUEST['score'];
$time = $_REQUEST['time'];
$wordcount = $_REQUEST['wordcount'];
$list[][] = json_encode($_REQUEST);
$new_record = $md5;
}
foreach ($list as $key => $item) {
$item = json_decode($item[0]);
$original[] = $item;
$data[$item->wordcount][] = $item;
}
if($data)
krsort($data);
const MAX_COUNT = 20;
if ($new_record) {
if (count($data[$wordcount]) > MAX_COUNT) {
// remove list item with id = $data[$wordcount][MAX_COUNT]['id']
usort($data[$wordcount], 'data_sort');
$remove_id = $data[$wordcount][MAX_COUNT]->id;
unset($data[$wordcount][MAX_COUNT]);
}
# code...
if(($fp = fopen('datafile', 'w')) !== FALSE) {
foreach ($list as $item) {
$item_de = json_decode($item[0]);
if (isset($remove_id) && $item_de->id == $remove_id) {
// ignore
} else {
fputcsv($fp, $item);
}
}
fclose($fp);
}
}
function data_sort($a, $b) {
if ($a->score > $b->score) return -1;
if ($a->score < $b->score) return 1;
// scores are the same
if ($a->time < $b->time) return -1;
if ($a->time > $b->time) return 1;
// equal times
if ($a->date < $b->date) return -1;
if ($a->date > $b->date) return 1;
// first person to score is higher
return +1;
}
function time_sort($a, $b) {
if ($a->time < $b->time) return -1;
if ($a->time > $b->time) return 1;
// equal times
if ($a->date < $b->date) return -1;
if ($a->date > $b->date) return 1;
// first person to score is higher
if ($a->score > $b->score) return -1;
if ($a->score < $b->score) return 1;
// scores are the same
return +1;
}
if ($html_display) {
?>
<html>
<head>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
</head>
<body>
<?php
}
function timeString($item) {
$time_data = $item->time;
$time_string = $time_data % 60;
if(strlen($time_string) < 2)
$time_string = '0'.$time_string;
if($time_data = floor($time_data/60)) {
$time_string = ($time_data % 60) . ':' .$time_string;
if(strlen($time_string) < 5)
$time_string = '0'.$time_string;
}
if ($time_data = floor($time_data/60)) {
$time_string = $time_data . ':' . $time_string;
if(strlen($time_string) < 7)
$time_string = '0'.$time_string;
}
return $time_string;
}
if($original) {
usort($original, 'time_sort');
if ($html_display) {
echo "<h3>Fastest Time</h3>";
}
foreach ($original as $counter => $item) {
if ($counter < MAX_COUNT) {
if ($html_display) {
echo ($counter+1).'- Time: '.timestring($item).", Words: {$item->wordcount}, Score: ".$item->score.', '.$item->fullname.'<br/>';
}
}
if ($new_record == $item->id ) {
$time_ranking = $counter + 1;
break;
}
}
}
if($data)
foreach ($data as $wordcount => $list) {
usort($list, 'data_sort');
$data[$wordcount] = $list;
if ($html_display)
echo "<h3>$wordcount word puzzles</h3>";
$counter = 0;
foreach ($list as $key => $item) {
# code...
$counter = $key+1;
if ($counter <= MAX_COUNT) {
if ($html_display) {
$time_string = timeString($item);
echo "$counter- Score: {$item->score}, Time: {$time_string} {$item->fullname}<br/>";
}
}
if ($new_record == $item->id ) {
$ranking = $counter;
}
}
}
if ($html_display) {
?>
</body>
<?php
}
if ($new_record) {
$msg = array();
if ($ranking) {
$msg[] = 'Vow, you ranked '.$ranking;
}
if($time_ranking <= 10) {
$msg[] = "Your time ranked in the top $time_ranking";
}
if(count($msg) == 0) {
$msg[] = 'Next time, be faster';
}
echo join("\r\n", $msg);
}
?>