fwtrash/pages/doppelklinge.php
2024-03-29 15:47:35 +01:00

117 lines
3.3 KiB
PHP

<?php
require_once __DIR__ . '/../inc/db.inc.php';
$tpl['file'] = 'doppelklinge.twig';
$tpl['data'] = [];
$tasks = [
'auftrag' => [ 'name' => 'Aufträge / Vermessungen', 'max' => 15000 ],
'blut' => [ 'name' => 'Blutproben abgegeben', 'max' => 500 ],
'gewebe' => [ 'name' => 'Gewebeproben abgegeben', 'max' => 750 ],
'gnpc' => [ 'name' => 'Gruppen-NPCs getötet', 'max' => 5000 ],
'npc' => [ 'name' => 'NPCs getötet', 'max' => 100000 ],
'pflanzen' => [ 'name' => 'Pflanzen geerntet', 'max' => 1500 ],
'unique' => [ 'name' => 'Unique-NPCs getötet', 'max' => 2500 ]
];
if(isset($_GET['c'])) {
$res = $db->prepare('select * from ft_doppelklinge where uuid = :uuid limit 1');
$res->execute([ ':uuid' => $_GET['c'] ]);
$arr = $res->fetch($db::FETCH_ASSOC);
$i = 0;
foreach($tasks as $type => $data) {
$list[$i] = (object)[
'task' => $data['name'],
'act' => $arr[$type],
'max' => $data['max']
];
$list[$i]->percent = round($list[$i]->act / $list[$i]->max * 100, 2);
$list[$i]->left = $list[$i]->max - $list[$i]->act;
$i++;
}
usort($list, function($a, $b) { return $b->percent <=> $a->percent; });
$act = $max = $avg = 0;
for($i = 0; $i < 5; $i++) {
$act += $list[$i]->act;
$max += $list[$i]->max;
$avg += $list[$i]->percent;
}
$percent = round($act / $max * 100, 2);
$tpl['data'] = [
'progress' => $progress,
'list' => $list,
'percent' => $percent,
'avg' => round($avg / 5, 2),
'dt' => date('d.m.Y - H:i:s', strtotime($arr['created_at']))
];
return;
}
$progress = !empty($_POST['progress']) ? $_POST['progress'] : '';
if(!empty($progress)) {
$list = [];
preg_match_all("/(?<task>.*): (?<act>.*) \/ (?<max>.*)/m", $progress, $tmp);
for($i = 0; $i < count($tmp[0]); $i++) {
if(preg_match('/XP\:/', $tmp['task'][$i]))
continue;
$list[$i] = (object)[
'task' => trim($tmp['task'][$i]),
'act' => (int)str_replace('.', '', $tmp['act'][$i]),
'max' => (int)str_replace('.', '', $tmp['max'][$i])
];
$list[$i]->percent = round($list[$i]->act / $list[$i]->max * 100, 2);
$list[$i]->left = $list[$i]->max - $list[$i]->act;
}
$query = <<<SQL
replace into ft_doppelklinge
(`uuid`, `sessid`, `auftrag`, `blut`, `gewebe`, `gnpc`, `npc`, `pflanzen`, `unique`)
values
(substr(uuid(), 1, 8), :sessid, :auftrag, :blut, :gewebe, :gnpc, :npc, :pflanzen, :unique)
returning uuid;
SQL;
$insert = $db->prepare($query);
$insert->execute([
':sessid' => session_id(),
':auftrag' => $list[0]->act,
':blut' => $list[1]->act,
':gewebe' => $list[2]->act,
':gnpc' => $list[3]->act,
':npc' => $list[4]->act,
':pflanzen' => $list[5]->act,
':unique' =>$list[6]->act
]);
$uuid = $insert->fetch()->uuid;
usort($list, function($a, $b) { return $b->percent <=> $a->percent; });
$act = $max = $avg = 0;
for($i = 0; $i < 5; $i++) {
$act += $list[$i]->act;
$max += $list[$i]->max;
$avg += $list[$i]->percent;
}
$percent = round($act / $max * 100, 2);
$tpl['data'] = [
'progress' => $progress,
'list' => $list,
'percent' => $percent,
'avg' => round($avg / 5, 2),
'share' => $uuid
];
}
else {
$tpl['data'] = [
'progress' => ''
];
}