dedimania plugin upgrade + some upgrade on asynchttprequest

This commit is contained in:
kremsy 2017-05-08 18:53:56 +02:00
parent dce11990d5
commit b3707a47d1
2 changed files with 47 additions and 18 deletions

View File

@ -34,6 +34,7 @@ class AsyncHttpRequest implements UsageInformationAble {
private $content; private $content;
private $compression = false; private $compression = false;
private $contentType = 'text/xml; charset=UTF-8;'; private $contentType = 'text/xml; charset=UTF-8;';
private $timeout = 60;
private $headers = array(); private $headers = array();
public function __construct($maniaControl, $url) { public function __construct($maniaControl, $url) {
@ -45,11 +46,12 @@ class AsyncHttpRequest implements UsageInformationAble {
* Create a new cURL Request for the given URL * Create a new cURL Request for the given URL
* *
* @param string $url * @param string $url
* @return Request * @param int $timeout
* @return \cURL\Request
*/ */
private function newRequest($url) { private function newRequest($url, $timeout) {
$request = new Request($url); $request = new Request($url);
$request->getOptions()->set(CURLOPT_TIMEOUT, 60)->set(CURLOPT_HEADER, false)// don't display response header $request->getOptions()->set(CURLOPT_TIMEOUT, $timeout)->set(CURLOPT_HEADER, false)// don't display response header
->set(CURLOPT_CRLF, true)// linux line feed ->set(CURLOPT_CRLF, true)// linux line feed
->set(CURLOPT_ENCODING, '')// accept encoding ->set(CURLOPT_ENCODING, '')// accept encoding
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent ->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
@ -71,7 +73,7 @@ class AsyncHttpRequest implements UsageInformationAble {
array_push($this->headers, 'Connection: Keep-Alive'); array_push($this->headers, 'Connection: Keep-Alive');
} }
$request = $this->newRequest($this->url); $request = $this->newRequest($this->url, $this->timeout);
$request->getOptions()->set(CURLOPT_AUTOREFERER, true)// accept link reference $request->getOptions()->set(CURLOPT_AUTOREFERER, true)// accept link reference
->set(CURLOPT_HTTPHEADER, $this->headers); // headers ->set(CURLOPT_HTTPHEADER, $this->headers); // headers
@ -94,7 +96,7 @@ class AsyncHttpRequest implements UsageInformationAble {
} }
$request = $this->newRequest($this->url); $request = $this->newRequest($this->url, $this->timeout);
$request->getOptions()->set(CURLOPT_POST, true)// post method $request->getOptions()->set(CURLOPT_POST, true)// post method
->set(CURLOPT_POSTFIELDS, $content)// post content field ->set(CURLOPT_POSTFIELDS, $content)// post content field
->set(CURLOPT_HTTPHEADER, $this->headers) // headers ->set(CURLOPT_HTTPHEADER, $this->headers) // headers
@ -208,4 +210,22 @@ class AsyncHttpRequest implements UsageInformationAble {
$this->contentType = $contentType; $this->contentType = $contentType;
return $this; return $this;
} }
/**
* Gets the Timeout Time
*
* @return int
*/
public function getTimeout() {
return $this->timeout;
}
/**
* Sets the Timeout Time
*
* @param int $timeout
*/
public function setTimeout($timeout) {
$this->timeout = $timeout;
}
} }

View File

@ -40,7 +40,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
const NAME = 'Dedimania Plugin'; const NAME = 'Dedimania Plugin';
const MLID_DEDIMANIA = 'Dedimania.ManialinkId'; const MLID_DEDIMANIA = 'Dedimania.ManialinkId';
const XMLRPC_MULTICALL = 'system.multicall'; const XMLRPC_MULTICALL = 'system.multicall';
const DEDIMANIA_URL = 'http://dedimania.net:8081/Dedimania'; const DEDIMANIA_URL = 'http://dedimania.net:8082/Dedimania';
const DEDIMANIA_OPEN_SESSION = 'dedimania.OpenSession'; const DEDIMANIA_OPEN_SESSION = 'dedimania.OpenSession';
const DEDIMANIA_CHECK_SESSION = 'dedimania.CheckSession'; const DEDIMANIA_CHECK_SESSION = 'dedimania.CheckSession';
const DEDIMANIA_GET_RECORDS = 'dedimania.GetChallengeRecords'; const DEDIMANIA_GET_RECORDS = 'dedimania.GetChallengeRecords';
@ -152,7 +152,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$this->maniaControl->getCallbackManager()->registerCallbackListener(RecordCallback::FINISH, $this, 'handleFinishCallback'); $this->maniaControl->getCallbackManager()->registerCallbackListener(RecordCallback::FINISH, $this, 'handleFinishCallback');
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'updateEverySecond', 1000); $this->maniaControl->getTimerManager()->registerTimerListening($this, 'updateEverySecond', 1000);
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'handleEveryMinute', 1000 * 60); $this->maniaControl->getTimerManager()->registerTimerListening($this, 'handleEveryHalfMinute', 1000 * 30);
$this->maniaControl->getTimerManager()->registerTimerListening($this, 'updatePlayerList', 1000 * 60 * 3); $this->maniaControl->getTimerManager()->registerTimerListening($this, 'updatePlayerList', 1000 * 60 * 3);
$this->maniaControl->getCommandManager()->registerCommandListener(array('dedirecs', 'dedirecords'), $this, 'showDediRecordsList', false, 'Shows a list of Dedimania records of the current map.'); $this->maniaControl->getCommandManager()->registerCommandListener(array('dedirecs', 'dedirecords'), $this, 'showDediRecordsList', false, 'Shows a list of Dedimania records of the current map.');
@ -198,7 +198,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
Logger::log("Try to connect on Dedimania"); Logger::log("Try to connect on Dedimania");
if (!$data || $error) { if (!$data || $error) {
Logger::logError("Dedimania Error: '{$error}'"); Logger::logError("Dedimania Error while opening session: '{$error}'");
} }
$data = $this->decode($data); $data = $this->decode($data);
@ -233,6 +233,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);
$asyncHttpRequest->setCompression(true); $asyncHttpRequest->setCompression(true);
$asyncHttpRequest->setTimeout(500);
$asyncHttpRequest->postData(); $asyncHttpRequest->postData();
} }
@ -299,7 +300,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL);
$asyncHttpRequest->setCallable(function ($data, $error) { $asyncHttpRequest->setCallable(function ($data, $error) {
if ($error) { if ($error) {
Logger::logError('Dedimania Error: ' . $error); Logger::logError('Dedimania Error while fetching records: ' . $error);
} }
$data = $this->decode($data); $data = $this->decode($data);
@ -344,6 +345,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);
$asyncHttpRequest->setCompression(true); $asyncHttpRequest->setCompression(true);
$asyncHttpRequest->setTimeout(500);
$asyncHttpRequest->postData(); $asyncHttpRequest->postData();
return true; return true;
@ -561,7 +563,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
/** /**
* Handle 1 Minute Callback * Handle 1 Minute Callback
*/ */
public function handleEveryMinute() { public function handleEveryHalfMinute() {
if (!$this->init) { if (!$this->init) {
return; return;
} }
@ -583,13 +585,17 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL);
$asyncHttpRequest->setCallable(function ($data, $error) { $asyncHttpRequest->setCallable(function ($data, $error) {
if ($error) { if ($error) {
Logger::logError("Dedimania Error: " . $error); //Reopen session in Timeout case
$this->openDedimaniaSession();
//Logger::logError("Dedimania Error while checking session: " . $error);
} }
$data = $this->decode($data); $data = $this->decode($data);
if (!is_array($data) || empty($data)) { if (!is_array($data) || empty($data)) {
return; return;
} }
//var_dump("SESSION CHECK");
//var_dump($data);
$methodResponse = $data[0]; $methodResponse = $data[0];
if (xmlrpc_is_fault($methodResponse)) { if (xmlrpc_is_fault($methodResponse)) {
@ -611,6 +617,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}); });
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);
$asyncHttpRequest->setCompression(true); $asyncHttpRequest->setCompression(true);
$asyncHttpRequest->setTimeout(500);
$asyncHttpRequest->postData(); $asyncHttpRequest->postData();
} }
@ -631,7 +638,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL);
$asyncHttpRequest->setCallable(function ($data, $error) use (&$player) { $asyncHttpRequest->setCallable(function ($data, $error) use (&$player) {
if ($error) { if ($error) {
Logger::logError("Dedimania Error: " . $error); Logger::logError("Dedimania Error while player connect: " . $error);
} }
$data = $this->decode($data); $data = $this->decode($data);
@ -658,10 +665,11 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$manialink = $this->buildManialink(); $manialink = $this->buildManialink();
$this->maniaControl->getManialinkManager()->sendManialink($manialink, $player->login); $this->maniaControl->getManialinkManager()->sendManialink($manialink, $player->login);
} }
}, $content, true); });
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);
$asyncHttpRequest->setCompression(true); $asyncHttpRequest->setCompression(true);
$asyncHttpRequest->setTimeout(500);
$asyncHttpRequest->postData(); $asyncHttpRequest->postData();
} }
@ -683,7 +691,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL);
$asyncHttpRequest->setCallable(function ($data, $error) { $asyncHttpRequest->setCallable(function ($data, $error) {
if ($error) { if ($error) {
Logger::logError("Dedimania Error: " . $error); Logger::logError("Dedimania Error while player disconnect: " . $error);
} }
$data = $this->decode($data); $data = $this->decode($data);
@ -699,6 +707,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);
$asyncHttpRequest->setCompression(true); $asyncHttpRequest->setCompression(true);
$asyncHttpRequest->setTimeout(500);
$asyncHttpRequest->postData(); $asyncHttpRequest->postData();
} }
@ -761,7 +770,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL);
$asyncHttpRequest->setCallable(function ($data, $error) { $asyncHttpRequest->setCallable(function ($data, $error) {
if ($error) { if ($error) {
Logger::logError("Dedimania Error: " . $error); Logger::logError("Dedimania Error while submitting times: " . $error);
} }
if (self::DEDIMANIA_DEBUG) { if (self::DEDIMANIA_DEBUG) {
@ -794,6 +803,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}); });
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);
$asyncHttpRequest->setCompression(false); $asyncHttpRequest->setCompression(false);
$asyncHttpRequest->setTimeout(500);
$asyncHttpRequest->postData(); $asyncHttpRequest->postData();
} }
@ -815,7 +825,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL);
$asyncHttpRequest->setCallable(function ($data, $error) { $asyncHttpRequest->setCallable(function ($data, $error) {
if ($error) { if ($error) {
Logger::logError("Dedimania Error: " . $error); Logger::logError("Dedimania Error while update playerlist: " . $error);
} }
$data = $this->decode($data); $data = $this->decode($data);
@ -831,6 +841,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);
$asyncHttpRequest->setCompression(true); $asyncHttpRequest->setCompression(true);
$asyncHttpRequest->setTimeout(500);
$asyncHttpRequest->postData(); $asyncHttpRequest->postData();
} }
@ -996,8 +1007,6 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
return $string; return $string;
} }
//TODO some bug if no dedimania time is existing
/** /**
* Inserts the given new Dedimania record at the proper position * Inserts the given new Dedimania record at the proper position
* *