%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /data/www_bck/varak.net_bck/broot.varak.net/
Upload File :
Create Path :
Current File : //data/www_bck/varak.net_bck/broot.varak.net/receivePlext.php

<?php
die;
function getmicrotime()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

// MySql Database
$srv = "localhost";
$usr = "waritko";
$pwd = "blade666";
$dbname = "ingress";


$spojeni = mysql_connect($srv, $usr, $pwd);
mysql_select_db($dbname, $spojeni);
header("Content-type: text/plain");
$total_count = 0;

ob_start();
$tstart = getmicrotime();
$players = array();
$player_guids = array();
$res = mysql_query("select id, name, guid from players");
while ($r = mysql_fetch_array($res)) {
    $players[$r[1]] = $r[0];
    $player_guids[$r[0]] = trim($r[2]);
}

$rawdata = file_get_contents("php://input");


$maxtime_guids = array();
$cnt = 0;
$end = false;
$toInsert = array();
$guids = array();
$last = 0;
$iter = 0;
$msgs = array();
$duplicates = 0;
$maxtime = 0;
$res = mysql_query("select max(`when`) from actions where city='Brno'");
if ($res != false) {
    if ($r = mysql_fetch_array($res)) {
        $maxtime = $r[0];
        $res = mysql_query("select guid from messages where time >= " . $maxtime . "000 AND city='Brno'");
        while ($r = mysql_fetch_object($res)) {
            $maxtime_guids[] = trim($r->guid);
        }
    }
}
$maxtimems = -1;
$dashboard = json_decode($rawdata);
$last = $dashboard->result[0][1];
foreach ($dashboard->result as $res) {
    $time = (int)substr($res[1], 0, 10);
    if ($time < $maxtime) {
        $end = true;
        continue;
    }
    $mguid = trim($res[0]);
    if (in_array($mguid, $guids) || in_array($mguid, $maxtime_guids)) {
        $duplicates++;
        continue;
    }
    $guids[] = $mguid;
    $msg = "insert into messages(time, guid, message, city) values(" . $res[1] . ", '" . $res[0] . "', '" . str_replace("'", "\\'", json_encode($res)) . "', '" . 'Brno' . "')";
    $msgs[] = $msg;
    //print_r($msgs);
    $maxtimems = $res[1];
    if ($res[2]->plext->plextType == "PLAYER_GENERATED") continue;
    if ($res[2]->plext->team == "NEUTRAL") continue;
    if ($res[2]->plext->plextType == "SYSTEM_NARROWCAST") continue;
    if ($res[2]->plext->markup[4][1]->plain == 'has decayed') continue;
    if ($res[2]->plext->markup[0][0] != "PLAYER" && $res[2]->plext->markup[4][1]->plain != 'has decayed') {
        print_r($res);
        continue;
    }
    $level = 0;
    $who = $res[2]->plext->markup[0][1]->plain;
    $side = $res[2]->plext->markup[0][1]->team;
    $guid = $res[2]->plext->markup[0][1]->guid;
    // Get player ID or insert into DB
    if (!isset($players[$who])) {
        mysql_query("insert into players(name, side, guid) values('$who', '$side', '$guid')");
        $players[$who] = mysql_insert_id();
        $player_guids[$players[$who]] = $guid;
        echo "Inserting player $who (#" . $players[$who] . ", GUID: $guid)\n";
    }
    $who = $players[$who];
    if (!isset($player_guids[$who]) || $player_guids[$who] == "") {
        mysql_query("update players set guid='$guid' where id=$who");
        echo "Adding GUID $guid to player " . $res[2]->plext->markup[0][1]->plain . "\n";
        $player_guids[$who] = $guid;
    }
    $action = $res[2]->plext->markup[1][1]->plain;
    $ap = 0;
    switch (trim($action)) {
        case 'destroyed an':
        {
            $level = (int)substr($res[2]->plext->markup[2][1]->plain, 1);
            $action = 'resonator_destroy';
            $ap = 75;
            break;
        }
        case 'destroyed the Link':
        {
            $action = 'link_destroy';
            $ap = 187;
            break;
        }
        case 'destroyed a Control Field @':
        {
            $action = 'field_destroy';
            $ap = 750;
            break;
        }
        case 'deployed an':
        {
            $level = (int)substr($res[2]->plext->markup[2][1]->plain, 1);
            $action = 'resonator_create';
            $ap = 125;
            break;
        }
        case 'linked':
        {
            $action = 'link_create';
            $ap = 313;
            break;
        }
        case 'created a Control Field @':
        {
            $action = "field_create";
            $ap = 1250;
            break;
        }
        case 'captured':
        {
            $action = 'capture';
            $ap = 500;
            break;
        }
        default:
            {
            print_r($res);
            break;
            }
    }
    // Now get position of the portal
    $lat = 0;
    $lng = 0;
    foreach ($res[2]->plext->markup as $item) {
        if ($item[0] == 'PORTAL') {
            $lat = $item[1]->latE6;
            $lng = $item[1]->lngE6;
            break;
        }
    }
    $query = "insert into actions(who, `when`, action, level, ap, latitude, longitude, city) values($who, $time, '$action', $level, $ap, $lat, $lng, '" . 'Brno' . "')";
    //echo $query."\n";
    if ($maxtimems != $last) {
        //echo "aaa\n";
        $last = $maxtimems;
        foreach ($toInsert as $act) {
            mysql_query($act);
            $cnt++;
        }
        foreach ($msgs as $m) {
            mysql_query($m);
        }
        $toInsert = array();
        //$guids = array();
        $msgs = array();
        $toInsert[] = $query;
    } else {
        //echo "bb\n";
        $toInsert[] = $query;
    }
}
echo "Inserted $cnt actions ($duplicates duplicates)\n";
$total_count += $cnt;

mysql_close();
$tend = getmicrotime();
$tim = $tend - $tstart;
printf("Saving %d actions took %01.4f seconds\n", $total_count, $tim);
echo ob_get_clean();
?>

Zerion Mini Shell 1.0