%PDF- %PDF-
| Direktori : /data/www_bck/varak.net_bck/epgp.varak.net/ |
| Current File : //data/www_bck/varak.net_bck/epgp.varak.net/parse.php |
<?php
header("Content-type: text/plain");
include "config.php";
include "lib.php";
include "dibi.phar";
set_time_limit(0);
error_reporting(E_ALL);
$hash = array();
$tzac = getmicrotime();
// Prace s uploadnutym souborem
$comp = false;
$tfname = uniqid("temp_");
if(strstr($_FILES['logfile']['name'], ".gz"))
{
// Soubor je gzipnuty
if(!move_uploaded_file($_FILES['logfile']['tmp_name'], $tfname.".gz"))
{
echo "There was an error uploading the file, please try again!";
die;
}
$gz = gzopen($tfname.".gz", "r");
$f = fopen($tfname, "w");
while(!gzeof($gz))
{
$txt = gzgets($gz, 8192);
fputs($f, $txt);
}
gzclose($gz);
fclose($f);
$comp = true;
unlink($tfname.".gz");
}
if(!$comp)
{
if(!move_uploaded_file($_FILES['logfile']['tmp_name'], $tfname))
{
echo "There was an error uploading the file, please try again!";
die;
}
}
//$spojeni = pg_connect("host=$sql port=5432 dbname=$db user=$usr");
dibi::connect([
'driver' => 'postgre',
'string' => "host=$sql port=5432 dbname=$db user=$usr password=$pass",
'persistent' => TRUE,
'profiler' => [
'run' => TRUE,
'file' => 'log.sql',
],
]);
//dibi::getProfiler()->setFile('log.sql');
//mysql_select_db($db, $spojeni);
//dibi::query("BEGIN");
dibi::begin();
$res = dibi::query("select [id], [name] from [characters] order by [name]");
if($res)
{
foreach($res as $r)
{
$hash[trim($r->name)] = $r->id;
}
}
//print_r($hash);
$count = 0;
$data = ParseLuaFile($tfname);
$info = $data['EPGP_DB']['namespaces']['log']['profiles'][$guild]['snapshot']['guild_info'];
$chars = $data['EPGP_DB']['namespaces']['log']['profiles'][$guild]['snapshot']['roster_info'];
$log = $data['EPGP_DB']['namespaces']['log']['profiles'][$guild]['log'];
// print_r($info);
// print_r($chars);
// print_r($log);
unset($data);
$base = substr($info, 9, strpos($info, '\n')-9);
//file_put_contents("epgp.base", $base);
dibi::query("update [characters] set [active]=0");
foreach($chars as $char)
{
if(isset($hash[$char[1]]))
{
// Update existujiciho
if(strstr($char[3], ','))
{
list($ep, $gp) = sscanf($char[3], "%d,%d");
dibi::query("update [characters] set [ep]=%i, [gp]=%i, [active]=1 where [id]=%i", $ep, $gp, $hash[$char[1]]);
}
}
else
{
// je treba vlozit novy
if(strstr($char[3], ','))
{
list($ep, $gp) = sscanf($char[3], "%d,%d");
//$query = sprintf("insert into \"characters\"(\"name\", \"class\", \"ep\", \"gp\", \"active\") values('%s', '%s', %d, %d, 1)", str_replace("'", "''", $char[1]), $char[2], $ep, $gp);
//echo $query . "\n";
//$res = q($query);
if(!$ep || !$gp) continue;
dibi::query("insert into [characters]", array(
'name' => $char[1],
'class' => $char[2],
"ep" => $ep,
'gp' => $gp
));
//$res = q("select max(\"id\") from \"characters\"");
$hash[$char[1]] = dibi::insertId();
}
}
}
//print_r($hash);
dibi::query("UPDATE [characters] SET [active]=1 where id in (select distinct [character] from log where [time] > extract(epoch from now()) - 24*3600*30)");
//print_r($log);
$max = dibi::query("select max([time]) from [log]")->fetchSingle();
//echo $max;
$decay = array();
foreach($log as $entry)
{
$time = $entry[1];
$who = $entry[3];
$ep = $entry[2] == 'EP' ? $entry[5] : 0;
$gp = $entry[2] == 'GP' ? $entry[5] : 0;
//$txt = str_replace("'", "\\'", $entry[4]);
$txt = $entry[4];
if(!isset($hash[$who]))
{
// Postava co uz nejspise neni v guilde, ale v logu se vyskytuje -> pridat
//$res = q("insert into \"characters\"(\"name\", \"class\", \"ep\", \"gp\") values('$who', 'UNKNOWN', 0, 0)");
dibi::query("INSERT INTO [characters]", array(
'name' => $who,
'class' => 'UNKNOWN',
'ep' => 0,
'gp' => 0
));
//$res = q("select max(\"id\") from \"characters\"");
$hash[$who] = dibi::insertId();
}
$who = $hash[$who];
//echo $time . "\n";
//echo $time <= $max . "\n";
if($time <= $max)
{
// Tady prichazi ta vesela cast
// Ignoruju vsechno starsi nez deset dni od posledniho zaznamu
if(time <= max - 3600*24*31) continue;
//$res = q("select count(*) from \"log\" where \"character\"='$who' and \"time\"=$time and \"desc\" like '%$txt%' and \"ep\"=$ep and \"gp\"=$gp");
dibi::query("SELECT sum(1) from [log] WHERE [character]=%s AND [time]=%i AND [desc] like %~like~ AND [ep]=%i AND [gp]=%i", $who, $time, $txt, $ep, $gp);
//echo $res->fetchSingle() . "\n";
if(!($res->fetchSingle())) continue;
}
if(strstr($txt, 'Decay'))
{
$decay[$who][0] = $who;
if($ep) $decay[$who][1] = $ep;
if($gp) $decay[$who][2] = $gp;
$decay[$who][3] = $txt;
$decay[$who][4] = $time;
$count++;
continue;
}
//q("insert into \"log\"(\"character\", \"time\", \"desc\", \"ep\", \"gp\") values($who, $time, '$txt', $ep, $gp)");
dibi::query("INSERT INTO [log]", array(
'character' => $who,
'time' => $time,
'desc' => $txt,
'ep' => $ep,
'gp' => $gp
));
$count++;
if(strstr($txt, 'Flasky'))
{
dibi::query('update [characters] set flask=1 where id=%i', $who);
}
}
foreach($decay as $d)
{
$time = $d[4];
$who = $d[0];
$txt = $d[3];
$ep = $d[1] ? $d[1] : 0;
$gp = $d[2] ? $d[2] : 0;
//q("insert into \"log\"(\"character\", \"time\", \"desc\", \"ep\", \"gp\") values($who, $time, '$txt', $ep, $gp)");
dibi::query("INSERT INTO [log]", array(
'character' => $who,
'time' => $time,
'desc' => $txt,
'ep' => $ep,
'gp' => $gp
));
}
dibi::commit();
//pg_close($spojeni);
//unlink($tfname);
$tkon = getmicrotime();
$mem = sprintf("%01.2f", memory_get_peak_usage(true)/(1024*2024));
$tkon=getmicrotime();
$time=$tkon-$tzac;
$time = sprintf("%01.4f", $time);
//echo "\n\n\n\nHotovo za $time sekund, pouzito $mem MB pameti pri zpracovani $count zaznamu";
header("Location: attend.php");
?>
<br /><br /><a href="./attend.php">Zpet</a> na index.