Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

Commit

Permalink
Fetch killmails via STOMP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artefact2 committed Jan 8, 2015
1 parent b44f0b5 commit f3d3b03
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 8 deletions.
1 change: 1 addition & 0 deletions README-Install.md
Expand Up @@ -108,6 +108,7 @@ Dependencies
* (Optional) iconv extension (`iconv.so`)
* (Optional) intl extension (`intl.so`)
* (Optional) APC (or APCu for PHP >= 5.5) extension (`apc.so`)
* (Optional) Stomp extension (`stomp.so`)

* PostgreSQL >= 9.0

Expand Down
6 changes: 6 additions & 0 deletions UPDATING
@@ -1,3 +1,9 @@
staging
=======

The [eve_kill] section has been added to config.ini. If you want to
import Eve-Kill data, you need to install the Stomp extension.

v0.13.3, released 2014-12-13
============================

Expand Down
8 changes: 7 additions & 1 deletion bin/cache_top_kills
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php
/* Osmium
* Copyright (C) 2013 Romain "Artefact2" Dalmaso <artefact2@gmail.com>
* Copyright (C) 2013, 2015 Romain "Artefact2" Dalmaso <artefact2@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand All @@ -22,6 +22,12 @@ namespace Osmium\CLI\CacheTopKills;
require __DIR__.'/../inc/root.php';
require \Osmium\ROOT.'/inc/ajax-common.php';

echo "Purging old kills…\n";
\Osmium\Db\query_params(
'DELETE FROM osmium.recentkillsdna WHERE killtime < $1',
array(time() - 7*86400)
);

echo "Caching groupdna…\n";

$q = \Osmium\Db\query_params(
Expand Down
1 change: 0 additions & 1 deletion bin/cron.hourly
Expand Up @@ -11,7 +11,6 @@ require __DIR__.'/../inc/cron.php';
function() {
passthru('./bin/update_lscores');
passthru('./bin/prune_cache');
passthru('./bin/fetch_zkillboard_json');
passthru('./bin/cache_top_kills');
},
20 * 60,
Expand Down
136 changes: 136 additions & 0 deletions bin/eve_kill_stomp_client
@@ -0,0 +1,136 @@
#!/usr/bin/env php
<?php
/* Osmium
* Copyright (C) 2013, 2015 Romain "Artefact2" Dalmaso <artefact2@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Osmium\CLI\EveKillStompClient;

require __DIR__.'/../inc/root.php';

if(!\Osmium\get_ini_setting('enable_eve_kill')) {
echo "Eve-Kill support is not enabled in config.ini.\n";
die();
}

/* XXX: daemonize me! */
/* XXX: mention me in README-Install */

/* The zKillboard documentation doesn't say if the dates are in UTC,
* but they probably are */
date_default_timezone_set('UTC');

$d = \Osmium\Db\prepare('delkill', 'DELETE FROM recentkillsdna WHERE killid = $1');
$i = \Osmium\Db\prepare(
'inskill',
'INSERT INTO recentkillsdna (killid, killtime, dna, groupdna, solarsystemid,
characterid, charactername, corporationid, corporationname, allianceid, alliancename)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)'
);

if($d === false || $i === false) {
fprintf(STDERR, "pgsql error\n");
die(1);
}

/* Not a typo. This allows the script to continue running after read
* timeouts. */
while(true) try {
$stomp = new \Stomp(
\Osmium\get_ini_setting('eve_kill_stomp_uri'),
\Osmium\get_ini_setting('eve_kill_stomp_user'),
\Osmium\get_ini_setting('eve_kill_stomp_pass')
);

$stomp->setReadTimeout(5);
/* XXX: what do the options mean? */
$stomp->subscribe('/topic/kills', [
'id' => \Osmium\get_ini_setting('eve_kill_stomp_id').'-'.gethostname(),
'persistent' => 'true',
'ack' => 'client',
'prefetch-count' => 1,
]);

while($frame = $stomp->readFrame()) {
$stomp->ack($frame->headers['message-id']);

$k = json_decode($frame->body, true);
if(json_last_error() !== JSON_ERROR_NONE || $k === false) {
throw new \Exception((string)$frame->body);
continue;
}

if(!isset($k['killID']) || $k['killID'] <= 0) continue;

$dna = $k['victim']['shipTypeID'];

foreach($k['items'] as $item) {
$f = (int)$item['flag'];
if(!(
/* See invflags */
($f >= 11 && $f <= 34) /* Low, medium, high slots */
|| ($f >= 92 && $f <= 99) /* Rig slots */
|| ($f >= 125 && $f <= 132) /* Subsystem slots */
|| ($f === 87) /* Drone bay */
|| ($f === 88) /* Booster */
|| ($f === 89) /* Implant */
)) {
continue;
}

$typeid = (int)$item['typeID'];
$qty = (int)$item['qtyDropped'] + (int)$item['qtyDestroyed'];
if($qty === 0) continue;
$dna .= ':'.$typeid.';'.$qty;
}

$dna .= '::';

$dna = \Osmium\Fit\mangle_dna($dna);
$gdna = \Osmium\Fit\uniquify_dna($dna);

if($dna === false || $gdna === false) {
continue;
}

$d = \Osmium\Db\execute('delkill', array($k['killID']));
$i = \Osmium\Db\execute(
'inskill',
array(
$k['killID'],
strtotime($k['killTime']),
$dna,
$gdna,
$k['solarSystemID'],
$k['victim']['characterID'],
$k['victim']['characterName'],
$k['victim']['corporationID'],
$k['victim']['corporationName'],
$k['victim']['allianceID'] > 0 ? $k['victim']['allianceID'] : null,
$k['victim']['allianceID'] > 0 ? $k['victim']['allianceName'] : null,
)
);

if($d === false || $i === false) throw new \Exception('pgsql failure');

echo '.';
}
} catch(\Exception $e) {
ob_start();
var_dump($e);
fprintf(STDERR, ob_get_clean());
die(1);
}
17 changes: 11 additions & 6 deletions bin/sanity_check
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php
/* Osmium
* Copyright (C) 2013, 2014 Romain "Artefact2" Dalmaso <artefact2@gmail.com>
* Copyright (C) 2013, 2014, 2015 Romain "Artefact2" Dalmaso <artefact2@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -89,8 +89,11 @@ foreach(array(
check_func($func);
}

check_ext('dom');
check_method('DOMImplementation', 'createDocument');
check_ext('dom', CHECK_REQUIRED);
check_method('DOMImplementation', 'createDocument', CHECK_REQUIRED);

check_ext('stomp');
check_method('Stomp', 'ack');

check_ext('intl');
check_method('Normalizer', 'normalize');
Expand All @@ -99,9 +102,11 @@ check_method('Normalizer', 'normalize');
@include_once 'HTMLPurifier.auto.php';
check_method('HTMLPurifier', 'purify', CHECK_REQUIRED);

check('for HTMLPurifier >= '.MIN_HP_VER, function() {
return array(version_compare(HTMLPurifier::VERSION, MIN_HP_VER, '>='), HTMLPurifier::VERSION);
}, CHECK_REQUIRED);
if(class_exists('HTMLPurifier')) {
check('for HTMLPurifier >= '.MIN_HP_VER, function() {
return array(version_compare(HTMLPurifier::VERSION, MIN_HP_VER, '>='), HTMLPurifier::VERSION);
}, CHECK_REQUIRED);
}

check('whether config.ini exists', function() {
$ini = __DIR__.'/../config.ini';
Expand Down
11 changes: 11 additions & 0 deletions config-example.ini
Expand Up @@ -254,3 +254,14 @@ ccp_oauth_root="https://sisilogin.testeveonline.com"
; Given to you by CCP.
ccp_oauth_clientid=""
ccp_oauth_secret=""

[eve_kill]

; Use Eve-Kill data to display flavors of the week and popular
; alliance doctrines on the homepage.
enable_eve_kill=Off

eve_kill_stomp_id=osmium
eve_kill_stomp_uri=tcp://eve-kill.net:61613
eve_kill_stomp_user=guest
eve_kill_stomp_pass=guest

0 comments on commit f3d3b03

Please sign in to comment.