%PDF- %PDF-
Direktori : /data/www_bck/varak.net_bck/ingress.varak.net/api/ |
Current File : //data/www_bck/varak.net_bck/ingress.varak.net/api/actionsApi.php |
<?php ob_start(); function cmp($a, $b) { if ($a->ap == $b->ap) { return 0; } return ($a->ap > $b->ap) ? -1 : 1; } function verifyCity($city) { if($city == 'Brno' || $city == 'Kromeriz' || $city == 'Bratislava') return $city; return 'Brno'; } function getRangeStart($range) { switch($range) { case 'day': { return time()-3600*24; } case '2day': { return time() - 3600 * 48; } case 'week': { return time() - 3600 * 24 * 7; } case 'month': { return time() - 3600 * 24 * 30; } case 'all': { return 0; } case 'hour': { return time() - 3600; } default: { return 0; } } } function getPlayerResonatorDeploy($pid, $name, $side, $starttime = 0, $endtime = 0, $range = '') { global $city; $end = $start = ""; if($range != '') { $starttime = getRangeStart($range); $endtime = 0; } if($starttime != 0) { $start = "AND `when` >= ".mysql_real_escape_string($starttime); } if($endtime != 0) { $end = "AND `when` <= ".mysql_real_escape_string($endtime); } $query = "select distinct level, count(level) as `count` from actions where who=".mysql_real_escape_string($pid)." and action='resonator_create' AND city='$city' $start $end group by level order by level asc"; $res = mysql_query($query); if(!$res || mysql_num_rows($res) == 0) return false; $play = new stdClass(); $play->name = $name; $play->side = $side; $play->resonators = array(); $sum = 0; while($r = mysql_fetch_object($res)) { $play->resonators[] = $r; $sum += $r->count; } $total = new stdClass(); $total->level = 'Total'; $total->count = $sum; $play->resonators[] = $total; return $play; } function getPlayerResonatorDestroy($pid, $name, $side, $starttime = 0, $endtime = 0, $range = '') { global $city; $end = $start = ""; if($range != '') { $starttime = getRangeStart($range); $endtime = 0; } if($starttime != 0) { $start = "AND `when` >= ".mysql_real_escape_string($starttime); } if($endtime != 0) { $end = "AND `when` <= $endtime"; } $query = "select distinct level, count(level) as `count` from actions where who=".mysql_real_escape_string($pid)." and action='resonator_destroy' AND city='$city' $start $end group by level order by level asc"; $res = mysql_query($query); if(!$res || mysql_num_rows($res) == 0) return false; $play = new stdClass(); $play->name = $name; $play->side = $side; $play->resonators = array(); $sum = 0; while($r = mysql_fetch_object($res)) { $play->resonators[] = $r; $sum += $r->count; } $total = new stdClass(); $total->level = 'Total'; $total->count = $sum; $play->resonators[] = $total; return $play; } function getPlayerAPDetails($pid, $start = 0, $range = '') { global $city; $start = mysql_real_escape_string($start); if($range != '') { $start = getRangeStart($range); } $out = new stdClass(); $sum = 0; $res = mysql_query("select sum(ap) as created from actions where action in ('resonator_create', 'link_create', 'capture', 'field_create') AND `when` >= $start AND city='$city' AND who=".$pid); if($r = mysql_fetch_object($res)) { $out->created = (int)$r->created; $sum += $r->created; } else { $out->created = 0; } $res = mysql_query("select sum(ap) as destroyed from actions where action in ('resonator_destroy', 'link_destroy', 'field_destroy') AND `when` >= $start AND city='$city' AND who=".$pid); if($r = mysql_fetch_object($res)) { $out->destroyed = (int)$r->destroyed; $sum += $r->destroyed; } else { $out->destroyed = 0; } $out->total = $sum; $out->details = new stdClass(); // Funny details :D $res = mysql_query("select * from (select distinct action, sum(ap) as ap from actions where who=$pid and `when` > $start AND city='$city' group by action) as act right join (SELECT DISTINCT action FROM `actions`) as actlist on act.action=actlist.action"); print_r(mysql_error()); while($r = mysql_fetch_object($res)) { if($r->ap) { $out->details->{$r->action} = $r->ap; } else { $out->details->{$r->action} = 0; } } return $out; } require("config.php"); header("Content-type: text/plain"); $spojeni = mysql_connect($srv, $usr, $pwd); mysql_select_db($dbname, $spojeni); $city = 'Brno'; if(isset($_GET['city'])) { $city = verifyCity($_GET['city']); } while(true) { if($_GET['action'] == 'player_ap_week') { $res = mysql_query("select name, ap, side from (select distinct `actions`.`who` AS `player`,sum(`actions`.`ap`) AS `ap` from `actions` where (`actions`.`when` >= (unix_timestamp() - ((3600 * 24) * 7))) AND city='$city' group by `actions`.`who`) as player_ap_week join players on player_ap_week.player=players.id order by ap desc"); $out = array(); while($r = mysql_fetch_object($res)) { $out[] = $r; } } if($_GET['action'] == 'player_ap_overall' && (isset($_GET['start']) || isset($_GET['range']))) { if(isset($_GET['start'])) { $start = mysql_real_escape_string($_GET['start']); } if(isset($_GET['end'])) { $end = "AND `when` <= ".mysql_real_escape_string($_GET['end']); } if(isset($_GET['range'])) { $start = getRangeStart($_GET['range']); } $res = mysql_query("SELECT action, who, ap, `name`, side FROM ( SELECT action, who, ap FROM ( SELECT DISTINCT action, who, sum(ap) AS ap FROM actions WHERE `when` > $start $end AND city='$city' GROUP BY who, action ) AS act ORDER BY who ) AS ap_details JOIN players ON players.id = ap_details.who"); $out = array(); $helper = array(); while($r = mysql_fetch_object($res)) { if(!isset($helper[$r->who])) { $helper[$r->who] = new stdClass(); $helper[$r->who]->name = $r->name; $helper[$r->who]->side = $r->side; $helper[$r->who]->ap = 0; $helper[$r->who]->create = 0; $helper[$r->who]->destroy = 0; $helper[$r->who]->details = new stdClass(); $helper[$r->who]->details->resonator_destroy = 0; $helper[$r->who]->details->field_destroy = 0; $helper[$r->who]->details->link_destroy = 0; $helper[$r->who]->details->resonator_create = 0; $helper[$r->who]->details->field_create = 0; $helper[$r->who]->details->link_create = 0; $helper[$r->who]->details->capture = 0; } $helper[$r->who]->ap += $r->ap; if($r->action == 'resonator_destroy' || $r->action == 'field_destroy' || $r->action == 'link_destroy') { $helper[$r->who]->destroy += $r->ap; } if($r->action == 'resonator_create' || $r->action == 'field_create' || $r->action == 'link_create' || $r->action == 'capture') { $helper[$r->who]->create += $r->ap; } $helper[$r->who]->details->{$r->action} = $r->ap; } foreach($helper as $h) { $out[] = $h; } usort($out, 'cmp'); break; } if($_GET['action'] == 'player_ap' && (isset($_GET['start']) || isset($_GET['range'])) && isset($_GET['name'])) { $player = mysql_query("select id from players where name like '".mysql_real_escape_string($_GET['name'])."'"); if($player != false && mysql_num_rows($player) > 0) { if($pl = mysql_fetch_object($player)) { if(isset($_GET['start'])) { $out = getPlayerAPDetails($pl->id, $_GET['start']); } elseif(isset($_GET['range'])) { $out = getPlayerAPDetails($pl->id, 0, $_GET['range']); } } } break; } if($_GET['action'] == 'player_max_level') { $res = mysql_query("SELECT `name`, `level`, `side`, `when` FROM (select distinct `actions`.`who` AS `player`,max(`actions`.`level`) AS `level`, max(`actions`.`when`) as `when` from `actions` where (`actions`.`action` = 'resonator_create') group by `actions`.`who`) as `player_max_level` JOIN players ON player_max_level.player = players.id order by level desc, name"); $out = array(); while($r = mysql_fetch_object($res)) { $out[] = $r; } break; } if($_GET['action'] == 'player_resonator_deploy') { $starttime = 0; $endtime = 0; $range = ''; if(isset($_GET['start'])) { $starttime = $_GET['start']; } if(isset($_GET['end'])) { $endtime = $_GET['end']; } if(isset($_GET['range'])) { $range = $_GET['range']; } if((isset($_GET['all']) && $_GET['all'] == "true") || !isset($_GET['name'])) { $players = mysql_query("select distinct who, name, side from actions where city='$city' join players on actions.who = players.id order by name"); $out = array(); while($pl = mysql_fetch_object($players)) { $play = getPlayerResonatorDeploy($pl->who, $pl->name, $pl->side, $starttime, $endtime, $range); if($play != false) { $out[] = $play; } } } elseif(isset($_GET['name'])) { $player = mysql_query("select id, name, side from players where name like '".mysql_real_escape_string($_GET['name'])."'"); if($player != false && mysql_num_rows($player) > 0) { if($pl = mysql_fetch_object($player)) { $play = getPlayerResonatorDeploy($pl->id, $pl->name, $pl->side, $starttime, $endtime, $range); if($play != false) { $out = $play; } } } } break; } if($_GET['action'] == 'player_resonator_destroy') { $starttime = 0; $endtime = 0; $range = ''; if(isset($_GET['start'])) { $starttime = $_GET['start']; } if(isset($_GET['end'])) { $endtime = $_GET['end']; } if(isset($_GET['range'])) { $range = $_GET['range']; } if((isset($_GET['all']) && $_GET['all'] == "true") || !isset($_GET['name'])) { $players = mysql_query("select distinct who, name, side from actions where city='$city' join players on actions.who = players.id order by name"); $out = array(); while($pl = mysql_fetch_object($players)) { $play = getPlayerResonatorDestroy($pl->who, $pl->name, $pl->side, $starttime, $endtime, $range); if($play != false) { $out[] = $play; } } } elseif(isset($_GET['name'])) { $player = mysql_query("select id, name, side from players where name like '".mysql_real_escape_string($_GET['name'])."'"); if($player != false && mysql_num_rows($player) > 0) { if($pl = mysql_fetch_object($player)) { $play = getPlayerResonatorDestroy($pl->id, $pl->name, $pl->side, $starttime, $endtime, $range); if($play != false) { $out = $play; } } } } break; } if($_GET['action'] == 'player_last_actions' && (isset($_GET['start']) || isset($_GET['range'])) && isset($_GET['name'])) { if(isset($_GET['start'])) { $start = mysql_real_escape_string($_GET['start']); } elseif (isset($_GET['range'])) { $start = getRangeStart($_GET['range']); } $out = array(); $player = mysql_query("select id from players where name like '".mysql_real_escape_string($_GET['name'])."'"); if($pl = mysql_fetch_object($player)) { $res = mysql_query("select players.`name`, `action`, `when`, `level`, `ap`, `latitude`, `longitude` from actions join players on players.id=actions.who where `who`=".$pl->id." AND `when` >= $start AND city='$city' order by `when` desc"); while($r = mysql_fetch_object($res)) { $out[] = $r; } } break; } break; } echo json_encode($out, JSON_PRETTY_PRINT); mysql_close($spojeni); $ret = ob_get_clean(); // Gzip/Deflate/Bzip2 compression if(stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "bzip2")) { $ret = bzcompress($ret, 9); header("Content-Encoding: bzip2"); } elseif(stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")) { $ret = gzencode($ret, 9); header("Content-Encoding: gzip"); } elseif (stristr($_SERVER["HTTP_ACCEPT_ENCODING"], "deflate")) { $ret = gzdeflate($ret, 9); header("Content-Encoding: deflate"); } header("Content-length: ".strlen($ret)); echo $ret; ?>