Remove WebReader & and add a sec delay before sending EndMatch update

This commit is contained in:
Beu 2022-03-04 17:42:12 +01:00
parent e6dbc613ca
commit 848526b30f

View File

@ -11,10 +11,10 @@ use ManiaControl\Players\Player;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
use ManiaControl\Utils\WebReader;
use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\TimerListener;
if (! class_exists('MatchManagerSuite\MatchManagerCore')) {
$this->maniaControl->getChat()->sendErrorToAdmins('MatchManager Core is needed to use MatchManagerGSheet plugin. Install it and restart Maniacontrol');
@ -30,12 +30,12 @@ use MatchManagerSuite\MatchManagerCore;
* @author Beu
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
class MatchManagerGSheet implements CallbackListener, TimerListener, CommandListener, Plugin {
/*
* Constants
*/
const PLUGIN_ID = 156;
const PLUGIN_VERSION = 0.8;
const PLUGIN_VERSION = 0.9;
const PLUGIN_NAME = 'MatchManager GSheet';
const PLUGIN_AUTHOR = 'Beu';
@ -246,8 +246,10 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
$this->maniaControl->getChat()->sendError('Client ID empty', $player);
return;
}
$response = WebReader::postUrl('https://oauth2.googleapis.com/device/code?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&client_id=' . $clientid);
$json = $response->getContent();
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, 'https://oauth2.googleapis.com/device/code?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&client_id=' . $clientid);
$asyncHttpRequest->setContentType("application/x-www-form-urlencoded");
$asyncHttpRequest->setCallable(function ($json, $error) use ($player) {
if (!$json) {
Logger::logError('Impossible to Google API: ' . $json);
$this->maniaControl->getChat()->sendError('Impossible to Google API: ' . $json, $player);
@ -268,6 +270,9 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
} else {
$this->maniaControl->getChat()->sendError('Unkown error' , $player);
}
});
$asyncHttpRequest->postData(1000);
}
private function OAuth2Step2(Player $player) {
@ -291,8 +296,10 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
return;
}
$response = WebReader::postUrl('https://oauth2.googleapis.com/token?grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code&client_id=' . $clientid . '&client_secret=' . $clientsecret . '&device_code=' . $this->device_code);
$json = $response->getContent();
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, 'https://oauth2.googleapis.com/token?grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code&client_id=' . $clientid . '&client_secret=' . $clientsecret . '&device_code=' . $this->device_code);
$asyncHttpRequest->setContentType("application/x-www-form-urlencoded");
$asyncHttpRequest->setHeaders(array("Content-Length: 0"));
$asyncHttpRequest->setCallable(function ($json, $error) use ($player) {
if (!$json) {
Logger::logError('Impossible to Google API: ' . $json);
$this->maniaControl->getChat()->sendError('Impossible to Google API: ' . $json, $player);
@ -317,6 +324,9 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
Logger::logError('Unkown error' . $data->error_description);
$this->maniaControl->getChat()->sendError('Unkown error' , $player);
}
});
$asyncHttpRequest->postData(1000);
}
private function refreshTokenIfNeeded() {
@ -329,8 +339,9 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
if (!empty($refreshtoken) && !empty($expire) && !empty($clientid) && !empty($clientsecret)) {
if (time() >= $expire) {
$response = WebReader::postUrl('https://oauth2.googleapis.com/token?grant_type=refresh_token&client_id=' . $clientid . '&client_secret=' . $clientsecret . '&refresh_token=' . $refreshtoken);
$json = $response->getContent();
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, 'https://oauth2.googleapis.com/token?grant_type=refresh_token&client_id=' . $clientid . '&client_secret=' . $clientsecret . '&refresh_token=' . $refreshtoken);
$asyncHttpRequest->setContentType("application/x-www-form-urlencoded");
$asyncHttpRequest->setCallable(function ($json, $error) use ($player) {
if (!$json) {
Logger::logError('Impossible to Google API: ' . $json);
return;
@ -349,6 +360,9 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
} else {
$this->maniaControl->getChat()->sendError('Unkown error' , $player);
}
});
$asyncHttpRequest->postData(1000);
}
return true;
}
@ -445,7 +459,6 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
$data->data[1] = new \stdClass;
$data->data[1]->range = "'" . $sheetname . "'!A9";
foreach ($this->maniaControl->getPlayerManager()->getPlayers() as $player) {
$this->playerlist[$player->login] = $player->nickname;
}
@ -548,7 +561,10 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
function onCallbackEndMatch(String $matchid, Array $currentscore, Array $currentteamsscore) {
Logger::Log('onCallbackEndMatch');
$this->matchstatus = "ended";
$this->maniaControl->getTimerManager()->registerOneTimeListening($this, function () use ($matchid, $currentscore, $currentteamsscore) {
$this->UpdateGSheetData($matchid, $currentscore, $currentteamsscore);
}, 1000); // Wait a sec before sending last data to avoid collision
}
function onCallbackStopMatch(String $matchid, Array $currentscore, Array $currentteamsscore) {
Logger::Log('onCallbackStopMatch');