Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use SQL and cache results for 30 minutes
  • Loading branch information
zaphodef committed Mar 5, 2019
commit 32d5449370fb3c7651bd5dcbb7703c644c9f0973
43 changes: 23 additions & 20 deletions root-me101.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
<th>Pseudo</th><th>nom IRL</th><th>SCORE</th><th>challenges complétés</th><th>points depuis le <?php echo($date); ?></th>
</tr>
<?php

$user = "rmichon";
$pass = "blablabla";
$dbh = new PDO('mysql:host=localhost;dbname=rootme', $user, $pass);

// la bdd contient une table `scores` qui contient les attributs suivants:
// UNIQUE INT id, BOOLEAN is_anonymous, VARCHAR(255) nickname, VARCHAR(255) realname, INT initial_score, INT new_score, VARCHAR(32) challs, DATETIME last_fetch

function get_RootMe_score($pseudo){
$url="https://www.root-me.org/".$pseudo;
Expand Down Expand Up @@ -47,29 +53,26 @@ function get_RootMe_challs($pseudo){
return $challs;
}

function load_profiles(){
$row = 1;
$profiles=array();
if (($handle = fopen("/home/rmichon/participants.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
array_push($profiles,$data);
$row++;
}
fclose($handle);
}
// maj
foreach($dbh->query('SELECT id, nickname from scores WHERE last_fetch < DATE_SUB(NOW(), 30 MINUTE)') as $row) {
$score = get_RootMe_score($row['nickname']);
$challs = get_RootMe_challs($row['nickname']);

return $profiles;
}
foreach(load_profiles() as $profile){
$score = get_RootMe_score($profile[0]);
echo('<tr><td>');
if($profile[2] !== 'anonyme'){
echo('<a href="https://www.root-me.org/'.$profile[0].'">'.$profile[2].'</a>');
}
echo('</td><td>'.$profile[1].'</td><td>'.$score.'</td><td>'.get_RootMe_challs($profile[0]).'</td><td>'.((int) $score - (int) $profile[3]).'</td></tr>');
$req = "UPDATE scores SET new_score = :score, challs = :challs, last_fetch = :last_fetch WHERE id = :id";
$sth = $dbh->prepare($req);
$sth->execute(array(":score" => $score, ":challs" => $challs, ":last_fetch" => "NOW()", ":id" => $row['id']));
}

// affichage
foreach($dbh->query('SELECT * from scores') as $row) {
echo "<tr>
<td>" . ($row['is_anonymous'] ? "" : "<a href=\"https://www.root-me.org/" . $row['nickname'] . "\">" . $row['realname'] . "</a>") . "</td>
<td> " . $row['new_score'] . "</td>
<td> " . $row['challs'] . "</td>
<td>" . ($row['new_score'] - ['initial_score']) . "</td>
</tr>";
}

?>
</table>
<br><br>
Expand Down