<?php
class TopPvp {
const PREFIX = 'top_pvp_';
function getPrefix() {
return self::PREFIX;
}
function getPath() {
return "./cache";
}
private function getOrSave() {
$path = $this->getPath();
$files = glob($path.'/'. $this->getPrefix() . '*');
if(count($files) > 0 && file_exists($files[0])) {
$a = explode("_", $files[0]);
$time = intval($a[count($a) - 1]);
//echo "diff: " . (time() - $time);
if(time() - $time >= 180) {
unlink( $files[0] );
return $this->loadDb();
} else {
return file_get_contents($files[0]);
}
} else {
return $this->loadDb();
}
}
private function loadDb() {
require "db.php";
$sql = 'SELECT characters.char_name, characters.pvpkills, characters.pkkills, characters.onlinetime, clan_subpledges.name, char_templates.ClassName, character_subclasses.class_id, clan_data.crest, ally_data.crest as ally_crest, clan_data.ally_id, clan_data.clan_id '.
'FROM characters '.
'LEFT JOIN `clan_subpledges` ON characters.clanid = clan_subpledges.clan_id AND clan_subpledges.type = 0 '.
'LEFT JOIN character_subclasses ON character_subclasses.char_obj_id = characters.obj_Id AND character_subclasses.active = 1 '.
'LEFT JOIN `char_templates` ON char_templates.ClassId = character_subclasses.class_id '.
'LEFT JOIN `clan_data` ON clan_data.clan_id = characters.clanid '.
'LEFT JOIN `ally_data` ON ally_data.ally_id = clan_data.ally_id '.
'ORDER BY pvpkills DESC LIMIT 10;';
$result = mysqli_query($db, $sql);
$content = "";
if(mysqli_num_rows($result) > 0) {
foreach($result as $row) {
//var_dump($row);
$content .= "<tr>";
$content .= '<td>' . $row['char_name'] . '</td>';
$content .= '<td style="font-weight: bold;">' . $row['pvpkills'] . '</td>';
$content .= '<td>' . $row['pkkills'] . '</td>';
if(empty($row['name'])) {
$content .= '<td>-</td>';
} else {
$crest1 = base64_encode($row['crest']);
$crest2 = base64_encode($row['ally_crest']);
$content .= '<td><img src="/php/dds.php?data=' . $crest2 . '&id=' . $row['ally_id'] . '"><img src="/php/dds.php?data=' . $crest1 . '&id=' . $row['clan_id'] . '"> ' . $row['name'] . '</td>';
}
$content .= '<td>' . $row['ClassName'] . '</td>';
$online = intval($row['onlinetime']);
$content .= '<td>';
$hours = floor($online / 3600);
$minutes = floor(($online / 60) % 60);
$seconds = $online % 60;
$content .= "$hours ч. $minutes мин. $seconds сек.";
$content .= '</td>';
$content .= "</tr>";
//$content .= '<div class="quote">' . $row['char_name'] . ' - ' . $row['pvpkills'] . ' PVP</div>' . "\n";
}
} else {
$content .= "<tr><td>-</td><td>-</td><td>-</td><td>-</td></tr>";
}
$result->close();
file_put_contents($this->getPath() . "/" . $this->getPrefix() . "" . time(), $content);
return $content;
}
function get() {
$data = $this->getOrSave();
//foreach (explode("\n", $data) as $s) {
// echo $s;
//}
return $data;
}
}
?>