#1470 monday's commit: update on parser (still incomplete); wip datasources; initial monitoring cronjob (done); various minor changes, fixes and so
--HG-- branch : gsoc2012-achievementshg/feature/gsoc2013-dfighter
parent
6d33124bc3
commit
8aeabcd247
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
error_reporting(E_ALL ^ E_NOTICE);
|
||||||
|
ini_set("display_errors","1");
|
||||||
|
|
||||||
|
if(file_exists("monitor.stop")) {
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once("class/mySQL_class.php");
|
||||||
|
require_once("conf.php");
|
||||||
|
|
||||||
|
//create database connection
|
||||||
|
$DBc = new mySQL($CONF['mysql_error']);
|
||||||
|
$DBc->connect($CONF['mysql_server'],$CONF['mysql_user'],$CONF['mysql_pass'],$CONF['mysql_database']);
|
||||||
|
|
||||||
|
//check status
|
||||||
|
$res = $DBc->sendSQL("SELECT * FROM ach_monitor_status ORDER by ams_start DESC LIMIT 0,1","ARRAY");
|
||||||
|
|
||||||
|
if(($res[0]['ams_start'] < (time()-$CONF['timeout']) && $res[0]['ams_end'] == 0) || ($res[0]['ams_end'] > 0 && $res[0]['ams_end'] < (time()-$CONF['timeout']))) {
|
||||||
|
$fp = fsockopen($CONF['self_host'], 80, $errno, $errstr, 30);
|
||||||
|
if(!$fp) {
|
||||||
|
logf("ERROR: self call; socket: ".$errstr." (."$errno.")");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$out = "GET ".$CONF['self_path']." HTTP/1.1\r\n";
|
||||||
|
$out .= "Host: ".$CONF['self_host']."\r\n";
|
||||||
|
$out .= "Connection: Close\r\n\r\n";
|
||||||
|
|
||||||
|
fwrite($fp, $out);
|
||||||
|
fclose($fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
?>
|
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 5.2 KiB After Width: | Height: | Size: 9.7 KiB |
@ -1,24 +1,25 @@
|
|||||||
<?php
|
<?php
|
||||||
abstract class DataSource {
|
abstract class DataSource {
|
||||||
private $types = array();
|
private $types = array();
|
||||||
private $priority = array();
|
|
||||||
private $write = false;
|
private $write = false;
|
||||||
#MISSING: offered values
|
|
||||||
|
|
||||||
function DataSource() {
|
function DataSource() {
|
||||||
|
require_once(dirname(__FILE__)."/conf.php");
|
||||||
|
|
||||||
|
$this->types = $CONF["types"];
|
||||||
|
$this->write = $CONF["write"];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTypes() {
|
function getTypes() {
|
||||||
return $this->types;
|
return $this->types;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPriority($type) {
|
function isWriteable() {
|
||||||
return $this->priority[$type];
|
return $this->write;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract function getData($type,$ident,$field = array());
|
abstract function getData($ident,$field,$type);
|
||||||
|
|
||||||
abstract function writeData($type,$ident,$field = array(),$value = array());
|
abstract function writeData($ident,$field,$data,$type);
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
class Logfile {
|
||||||
|
private $logfile;
|
||||||
|
private $buffer;
|
||||||
|
|
||||||
|
function Logfile($f) {
|
||||||
|
$this->logfile = $f;
|
||||||
|
$this->buffer = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function append($t) {
|
||||||
|
$this->buffer .= $t;
|
||||||
|
}
|
||||||
|
|
||||||
|
function write() {
|
||||||
|
$f = fopen($this->logfile.'.'.date("Ymd",time()).'.txt','a');
|
||||||
|
fwrite($f,$this->buffer);
|
||||||
|
fclose($f);
|
||||||
|
$this->buffer = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
@ -0,0 +1,160 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// This class is designed to make it easy to run multiple curl requests in parallel, rather than
|
||||||
|
// waiting for each one to finish before starting the next. Under the hood it uses curl_multi_exec
|
||||||
|
// but since I find that interface painfully confusing, I wanted one that corresponded to the tasks
|
||||||
|
// that I wanted to run.
|
||||||
|
//
|
||||||
|
// To use it, first create the ParallelCurl object:
|
||||||
|
//
|
||||||
|
// $parallelcurl = new ParallelCurl(10);
|
||||||
|
//
|
||||||
|
// The first argument to the constructor is the maximum number of outstanding fetches to allow
|
||||||
|
// before blocking to wait for one to finish. You can change this later using setMaxRequests()
|
||||||
|
// The second optional argument is an array of curl options in the format used by curl_setopt_array()
|
||||||
|
//
|
||||||
|
// Next, start a URL fetch:
|
||||||
|
//
|
||||||
|
// $parallelcurl->startRequest('http://example.com', 'on_request_done', array('something'));
|
||||||
|
//
|
||||||
|
// The first argument is the address that should be fetched
|
||||||
|
// The second is the callback function that will be run once the request is done
|
||||||
|
// The third is a 'cookie', that can contain arbitrary data to be passed to the callback
|
||||||
|
//
|
||||||
|
// This startRequest call will return immediately, as long as less than the maximum number of
|
||||||
|
// requests are outstanding. Once the request is done, the callback function will be called, eg:
|
||||||
|
//
|
||||||
|
// on_request_done($content, 'http://example.com', $ch, array('something'));
|
||||||
|
//
|
||||||
|
// The callback should take four arguments. The first is a string containing the content found at
|
||||||
|
// the URL. The second is the original URL requested, the third is the curl handle of the request that
|
||||||
|
// can be queried to get the results, and the fourth is the arbitrary 'cookie' value that you
|
||||||
|
// associated with this object. This cookie contains user-defined data.
|
||||||
|
//
|
||||||
|
// By Pete Warden <pete@petewarden.com>, freely reusable, see http://petewarden.typepad.com for more
|
||||||
|
|
||||||
|
class ParallelCurl {
|
||||||
|
|
||||||
|
public $max_requests;
|
||||||
|
public $options;
|
||||||
|
|
||||||
|
public $outstanding_requests;
|
||||||
|
public $multi_handle;
|
||||||
|
|
||||||
|
public function __construct($in_max_requests = 10, $in_options = array()) {
|
||||||
|
$this->max_requests = $in_max_requests;
|
||||||
|
$this->options = $in_options;
|
||||||
|
|
||||||
|
$this->outstanding_requests = array();
|
||||||
|
$this->multi_handle = curl_multi_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Ensure all the requests finish nicely
|
||||||
|
public function __destruct() {
|
||||||
|
$this->finishAllRequests();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sets how many requests can be outstanding at once before we block and wait for one to
|
||||||
|
// finish before starting the next one
|
||||||
|
public function setMaxRequests($in_max_requests) {
|
||||||
|
$this->max_requests = $in_max_requests;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sets the options to pass to curl, using the format of curl_setopt_array()
|
||||||
|
public function setOptions($in_options) {
|
||||||
|
|
||||||
|
$this->options = $in_options;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start a fetch from the $url address, calling the $callback function passing the optional
|
||||||
|
// $user_data value. The callback should accept 3 arguments, the url, curl handle and user
|
||||||
|
// data, eg on_request_done($url, $ch, $user_data);
|
||||||
|
public function startRequest($url, $callback, $user_data = array(), $post_fields=null) {
|
||||||
|
|
||||||
|
if( $this->max_requests > 0 )
|
||||||
|
$this->waitForOutstandingRequestsToDropBelow($this->max_requests);
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt_array($ch, $this->options);
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
||||||
|
|
||||||
|
if (isset($post_fields)) {
|
||||||
|
curl_setopt($ch, CURLOPT_POST, TRUE);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_multi_add_handle($this->multi_handle, $ch);
|
||||||
|
|
||||||
|
$ch_array_key = (int)$ch;
|
||||||
|
|
||||||
|
$this->outstanding_requests[$ch_array_key] = array(
|
||||||
|
'url' => $url,
|
||||||
|
'callback' => $callback,
|
||||||
|
'user_data' => $user_data,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->checkForCompletedRequests();
|
||||||
|
}
|
||||||
|
|
||||||
|
// You *MUST* call this function at the end of your script. It waits for any running requests
|
||||||
|
// to complete, and calls their callback functions
|
||||||
|
public function finishAllRequests() {
|
||||||
|
$this->waitForOutstandingRequestsToDropBelow(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks to see if any of the outstanding requests have finished
|
||||||
|
private function checkForCompletedRequests() {
|
||||||
|
|
||||||
|
// Call select to see if anything is waiting for us
|
||||||
|
if (curl_multi_select($this->multi_handle, 0.0) === -1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Since something's waiting, give curl a chance to process it
|
||||||
|
do {
|
||||||
|
$mrc = curl_multi_exec($this->multi_handle, $active);
|
||||||
|
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
|
||||||
|
|
||||||
|
// Now grab the information about the completed requests
|
||||||
|
while ($info = curl_multi_info_read($this->multi_handle)) {
|
||||||
|
|
||||||
|
$ch = $info['handle'];
|
||||||
|
$ch_array_key = (int)$ch;
|
||||||
|
|
||||||
|
if (!isset($this->outstanding_requests[$ch_array_key])) {
|
||||||
|
die("Error - handle wasn't found in requests: '$ch' in ".
|
||||||
|
print_r($this->outstanding_requests, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
$request = $this->outstanding_requests[$ch_array_key];
|
||||||
|
|
||||||
|
$url = $request['url'];
|
||||||
|
$content = curl_multi_getcontent($ch);
|
||||||
|
$callback = $request['callback'];
|
||||||
|
$user_data = $request['user_data'];
|
||||||
|
|
||||||
|
call_user_func($callback, $content, $url, $ch, $user_data);
|
||||||
|
|
||||||
|
unset($this->outstanding_requests[$ch_array_key]);
|
||||||
|
|
||||||
|
curl_multi_remove_handle($this->multi_handle, $ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blocks until there's less than the specified number of requests outstanding
|
||||||
|
private function waitForOutstandingRequestsToDropBelow($max)
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
$this->checkForCompletedRequests();
|
||||||
|
if (count($this->outstanding_requests)<$max)
|
||||||
|
break;
|
||||||
|
|
||||||
|
usleep(10000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
$CONF = array();
|
||||||
|
|
||||||
|
$CONF['logging'] = true;
|
||||||
|
$CONF['logfile'] = "log/AchWebParser.log";
|
||||||
|
|
||||||
|
$CONF['mysql_error'] = "";
|
||||||
|
$CONF['mysql_server'] = "localhost";
|
||||||
|
$CONF['mysql_user'] = "root";
|
||||||
|
$CONF['mysql_pass'] = "";
|
||||||
|
$CONF['mysql_database'] = "app_achievements";
|
||||||
|
|
||||||
|
$CONF['data_source'] = array();
|
||||||
|
|
||||||
|
$CONF['synch_chars'] = true
|
||||||
|
$CONF['char_mysql_server'] = "localhost";
|
||||||
|
$CONF['char_mysql_user'] = "root";
|
||||||
|
$CONF['char_mysql_pass'] = "";
|
||||||
|
$CONF['char_mysql_database'] = "ring_open";
|
||||||
|
|
||||||
|
$CONF['fork'] = true;
|
||||||
|
|
||||||
|
$CONF['self_host'] = "127.0.0.1";
|
||||||
|
$CONF['self_path'] = "/path/to/AchWebParser.php";
|
||||||
|
|
||||||
|
$CONF['sleep_time'] = 1500;
|
||||||
|
|
||||||
|
$CONF['enable_selfcall'] = true
|
||||||
|
|
||||||
|
$CONF['timeout'] = 60*60;
|
||||||
|
?>
|
@ -0,0 +1,132 @@
|
|||||||
|
private function parseRuleset() {
|
||||||
|
$this->ruleset_parsed = $this->ruleset;
|
||||||
|
#WORKPAD:####
|
||||||
|
/*
|
||||||
|
Trigger:
|
||||||
|
by value
|
||||||
|
(by event)
|
||||||
|
|
||||||
|
Sources:
|
||||||
|
XML
|
||||||
|
valuecache
|
||||||
|
ring_open
|
||||||
|
(Achievement Service)
|
||||||
|
(Mirror Service)
|
||||||
|
|
||||||
|
Keywords:
|
||||||
|
VALUE
|
||||||
|
GRANT:EVENT player_death
|
||||||
|
DENY:TIMER 3600
|
||||||
|
RESET
|
||||||
|
RESET_ALL
|
||||||
|
UNLOCK
|
||||||
|
UNLOCK_ALL
|
||||||
|
|
||||||
|
IF
|
||||||
|
SCRIPT
|
||||||
|
MSG
|
||||||
|
|
||||||
|
VALUE dappers = c_money
|
||||||
|
IF(dappers >= 5000) {
|
||||||
|
GRANT
|
||||||
|
}
|
||||||
|
|
||||||
|
VALUE sum = c_cache:sum
|
||||||
|
IF(sum > 1000) {
|
||||||
|
GRANT
|
||||||
|
}
|
||||||
|
|
||||||
|
VALUE tmp = c_fame[scorchers]
|
||||||
|
IF(tmp == 0) {
|
||||||
|
DENY:3600
|
||||||
|
}
|
||||||
|
|
||||||
|
VALUE x = c_pos_x
|
||||||
|
VALUE y = c_pos_y
|
||||||
|
SCRIPT inside(x,y) {
|
||||||
|
IF(MSG == "Majestic Garden") {
|
||||||
|
GRANT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EVENT player_death
|
||||||
|
ON player_death {
|
||||||
|
UNLOCK
|
||||||
|
}
|
||||||
|
|
||||||
|
EVENT region_changed
|
||||||
|
ON region_changed {
|
||||||
|
IF(MSG == "Majestic Garden") {
|
||||||
|
GRANT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
#############
|
||||||
|
|
||||||
|
|
||||||
|
#VALUE var = name;
|
||||||
|
$match = array();
|
||||||
|
preg_match_all("#VALUE ([a-zA-Z0-9_]) ?= ?([a-zA-Z0-9_]);#", $this->ruleset_parsed, $match,PREG_PATTERN_ORDER);
|
||||||
|
foreach($match[0] as $key=>$elem) {
|
||||||
|
$tmp = '$'.$match[1][$key].' = $_DATA->getData("VALUE","'.$match[2][$key].'",$user);\n';
|
||||||
|
$tmp .= 'if($'.$match[1][$key].' == ) {\n';
|
||||||
|
$tmp .= 'ERROR\n';
|
||||||
|
$tmp .= '}\n';
|
||||||
|
$this->ruleset_parsed = str_replace($elem,$tmp,$this->ruleset_parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#IF(statement) { }
|
||||||
|
$match = array();
|
||||||
|
preg_match_all("#IF ?\(([^\)]*)\) ?{#", $this->ruleset_parsed, $match,PREG_PATTERN_ORDER);
|
||||||
|
foreach($match[0] as $key=>$elem) {
|
||||||
|
$tmp = 'if() {\n';
|
||||||
|
$this->ruleset_parsed = str_replace($elem,$tmp,$this->ruleset_parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SCRIPT script(a,r,g,s) {
|
||||||
|
MSG
|
||||||
|
}
|
||||||
|
|
||||||
|
#EVENT name;
|
||||||
|
$match = array();
|
||||||
|
preg_match_all("#EVENT ([^;]*);#", $this->ruleset_parsed, $match,PREG_PATTERN_ORDER);
|
||||||
|
foreach($match[0] as $key=>$elem) {
|
||||||
|
$tmp = '';
|
||||||
|
$this->ruleset_parsed = str_replace($elem,$tmp,$this->ruleset_parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
ON name {
|
||||||
|
MSG
|
||||||
|
}
|
||||||
|
|
||||||
|
#GRANT;
|
||||||
|
#GRANT:EVENT name;
|
||||||
|
#GRANT:TIMER seconds;
|
||||||
|
$match = array();
|
||||||
|
preg_match_all("#GRANT:?([^;]*);#", $this->ruleset_parsed, $match,PREG_PATTERN_ORDER);
|
||||||
|
foreach($match[0] as $key=>$elem) {
|
||||||
|
$tmp = '$this->grant("'.$match[1][$key].'");';
|
||||||
|
$this->ruleset_parsed = str_replace($elem,$tmp,$this->ruleset_parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
#DENY;
|
||||||
|
#DENY:EVENT name;
|
||||||
|
#DENY:TIMER seconds;
|
||||||
|
$match = array();
|
||||||
|
preg_match_all("#DENY:?([^;]*);#", $this->ruleset_parsed, $match,PREG_PATTERN_ORDER);
|
||||||
|
foreach($match[0] as $key=>$elem) {
|
||||||
|
$tmp = '$this->deny("'.$match[1][$key].'");';
|
||||||
|
$this->ruleset_parsed = str_replace($elem,$tmp,$this->ruleset_parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
#RESET;
|
||||||
|
#RESET_ALL;
|
||||||
|
#UNLOCK;
|
||||||
|
#UNLOCK_ALL;
|
||||||
|
$this->ruleset_parsed = str_replace("RESET_ALL;",'$this->reset_all();',$this->ruleset_parsed);
|
||||||
|
$this->ruleset_parsed = str_replace("RESET;",'$this->reset_();',$this->ruleset_parsed);
|
||||||
|
$this->ruleset_parsed = str_replace("UNLOCK_ALL;",'$this->unlock_all();',$this->ruleset_parsed);
|
||||||
|
$this->ruleset_parsed = str_replace("UNLOCK;",'$this->unlock();',$this->ruleset_parsed);
|
||||||
|
}
|
@ -1,26 +1,19 @@
|
|||||||
<?php
|
<?php
|
||||||
class ValueCache extends DataSource {
|
class ValueCache extends DataSource {
|
||||||
function ValueCache() {
|
function ValueCache() {
|
||||||
$this->types[] = "c_cache";
|
parent::__construct();
|
||||||
|
|
||||||
$this->write = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getData($type,$ident,$field) {
|
function getData($ident,$field,$type) {
|
||||||
|
$res = $DBc->sendSQL("SELECT apv_value,apv_date FROM ach_player_valuecache WHERE apv_name='".$DBc->mre($field)."' AND apv_player='".$DBc->mre($ident)."'","ARRAY");
|
||||||
|
|
||||||
|
return array($res[0]['apv_value'],$res[0]['apv_date']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeData($type,$ident,$field = array(),$value = array()) {
|
function writeData($ident,$field,$data,$type) {
|
||||||
global $DBc;
|
global $DBc;
|
||||||
|
|
||||||
if($type == "c_cache") {
|
$DBc->sendSQL("INSERT INTO ach_player_valuecache (apv_name,apv_player,apv_value,apv_date) VALUES ('".$DBc->mre($field)."','".$DBc->mre($ident)."','".$DBc->mre($data)."','".time()."') ON DUPLICATE KEY UPDATE apv_value='".$DBc->mre($data)."', apv_date='".time()."'","NONE");
|
||||||
$DBc->sendSQL("INSERT INTO ach_player_valuecache () VALUES () ON DUPLICATE KEY UPDATE ");
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
$CONF = array();
|
||||||
|
|
||||||
|
$CONF['types'] = array("c_cache*");
|
||||||
|
$CONF['write'] = true;
|
||||||
|
|
||||||
|
?>
|
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
$CONF = array();
|
||||||
|
|
||||||
|
$CONF['types'] = array("c_stats*","c_items*");
|
||||||
|
$CONF['write'] = false;
|
||||||
|
|
||||||
|
$CONF['xml_path'] = "foo/bar/xml/"; // this is a dummy ^^
|
||||||
|
?>
|
Loading…
Reference in New Issue