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\Plugins\Plugin;
use ManiaControl\Settings\Setting; use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager; use ManiaControl\Settings\SettingManager;
use ManiaControl\Utils\WebReader;
use ManiaControl\Files\AsyncHttpRequest; use ManiaControl\Files\AsyncHttpRequest;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\TimerListener;
if (! class_exists('MatchManagerSuite\MatchManagerCore')) { if (! class_exists('MatchManagerSuite\MatchManagerCore')) {
$this->maniaControl->getChat()->sendErrorToAdmins('MatchManager Core is needed to use MatchManagerGSheet plugin. Install it and restart Maniacontrol'); $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 * @author Beu
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @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 * Constants
*/ */
const PLUGIN_ID = 156; const PLUGIN_ID = 156;
const PLUGIN_VERSION = 0.8; const PLUGIN_VERSION = 0.9;
const PLUGIN_NAME = 'MatchManager GSheet'; const PLUGIN_NAME = 'MatchManager GSheet';
const PLUGIN_AUTHOR = 'Beu'; const PLUGIN_AUTHOR = 'Beu';
@ -246,28 +246,33 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
$this->maniaControl->getChat()->sendError('Client ID empty', $player); $this->maniaControl->getChat()->sendError('Client ID empty', $player);
return; 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);
if (!$json) { $asyncHttpRequest->setContentType("application/x-www-form-urlencoded");
Logger::logError('Impossible to Google API: ' . $json); $asyncHttpRequest->setCallable(function ($json, $error) use ($player) {
$this->maniaControl->getChat()->sendError('Impossible to Google API: ' . $json, $player); if (!$json) {
return; Logger::logError('Impossible to Google API: ' . $json);
} $this->maniaControl->getChat()->sendError('Impossible to Google API: ' . $json, $player);
$data = json_decode($json); return;
if (!$data) { }
Logger::logError('Json parse error: ' . $json); $data = json_decode($json);
$this->maniaControl->getChat()->sendError('Json parse error: ' . $json, $player); if (!$data) {
return; Logger::logError('Json parse error: ' . $json);
} $this->maniaControl->getChat()->sendError('Json parse error: ' . $json, $player);
if (isset($data->device_code)) { return;
$this->device_code = $data->device_code; }
$this->maniaControl->getChat()->sendSuccess('Open $<$l['. $data->verification_url . ']this link$> and type this code: "' . $data->user_code .'"' , $player); if (isset($data->device_code)) {
$this->maniaControl->getChat()->sendSuccess('After have validate the App, type the commande "//matchgsheet step2"' , $player); $this->device_code = $data->device_code;
} elseif (isset($data->error_code)) { $this->maniaControl->getChat()->sendSuccess('Open $<$l['. $data->verification_url . ']this link$> and type this code: "' . $data->user_code .'"' , $player);
$this->maniaControl->getChat()->sendError('Google refused the request: ' . $data->error_code, $player); $this->maniaControl->getChat()->sendSuccess('After have validate the App, type the commande "//matchgsheet step2"' , $player);
} else { } elseif (isset($data->error_code)) {
$this->maniaControl->getChat()->sendError('Unkown error' , $player); $this->maniaControl->getChat()->sendError('Google refused the request: ' . $data->error_code, $player);
} } else {
$this->maniaControl->getChat()->sendError('Unkown error' , $player);
}
});
$asyncHttpRequest->postData(1000);
} }
private function OAuth2Step2(Player $player) { private function OAuth2Step2(Player $player) {
@ -291,32 +296,37 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
return; 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); $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);
$json = $response->getContent(); $asyncHttpRequest->setContentType("application/x-www-form-urlencoded");
if (!$json) { $asyncHttpRequest->setHeaders(array("Content-Length: 0"));
Logger::logError('Impossible to Google API: ' . $json); $asyncHttpRequest->setCallable(function ($json, $error) use ($player) {
$this->maniaControl->getChat()->sendError('Impossible to Google API: ' . $json, $player); if (!$json) {
return; Logger::logError('Impossible to Google API: ' . $json);
} $this->maniaControl->getChat()->sendError('Impossible to Google API: ' . $json, $player);
$data = json_decode($json); return;
if (!$data) { }
Logger::logError('Json parse error: ' . $json); $data = json_decode($json);
$this->maniaControl->getChat()->sendError('Json parse error: ' . $json, $player); if (!$data) {
return; Logger::logError('Json parse error: ' . $json);
} $this->maniaControl->getChat()->sendError('Json parse error: ' . $json, $player);
if (isset($data->access_token)) { return;
$this->access_token = $data->access_token; }
$this->saveSecretSetting("access_token", $data->access_token); if (isset($data->access_token)) {
$this->saveSecretSetting("expire", time() + $data->expires_in); $this->access_token = $data->access_token;
$this->saveSecretSetting("refresh_token", $data->refresh_token); $this->saveSecretSetting("access_token", $data->access_token);
$this->maniaControl->getChat()->sendSuccess('Maniacontrol is registered' , $player); $this->saveSecretSetting("expire", time() + $data->expires_in);
} elseif (isset($data->error_description)) { $this->saveSecretSetting("refresh_token", $data->refresh_token);
Logger::logError('Google refused the request: ' . $data->error_description); $this->maniaControl->getChat()->sendSuccess('Maniacontrol is registered' , $player);
$this->maniaControl->getChat()->sendError('Google refused the request: ' . $data->error_description , $player); } elseif (isset($data->error_description)) {
} else { Logger::logError('Google refused the request: ' . $data->error_description);
Logger::logError('Unkown error' . $data->error_description); $this->maniaControl->getChat()->sendError('Google refused the request: ' . $data->error_description , $player);
$this->maniaControl->getChat()->sendError('Unkown error' , $player); } else {
} Logger::logError('Unkown error' . $data->error_description);
$this->maniaControl->getChat()->sendError('Unkown error' , $player);
}
});
$asyncHttpRequest->postData(1000);
} }
private function refreshTokenIfNeeded() { private function refreshTokenIfNeeded() {
@ -329,26 +339,30 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
if (!empty($refreshtoken) && !empty($expire) && !empty($clientid) && !empty($clientsecret)) { if (!empty($refreshtoken) && !empty($expire) && !empty($clientid) && !empty($clientsecret)) {
if (time() >= $expire) { if (time() >= $expire) {
$response = WebReader::postUrl('https://oauth2.googleapis.com/token?grant_type=refresh_token&client_id=' . $clientid . '&client_secret=' . $clientsecret . '&refresh_token=' . $refreshtoken); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, 'https://oauth2.googleapis.com/token?grant_type=refresh_token&client_id=' . $clientid . '&client_secret=' . $clientsecret . '&refresh_token=' . $refreshtoken);
$json = $response->getContent(); $asyncHttpRequest->setContentType("application/x-www-form-urlencoded");
if (!$json) { $asyncHttpRequest->setCallable(function ($json, $error) use ($player) {
Logger::logError('Impossible to Google API: ' . $json); if (!$json) {
return; Logger::logError('Impossible to Google API: ' . $json);
} return;
$data = json_decode($json); }
if (!$data) { $data = json_decode($json);
Logger::logError('Json parse error: ' . $json); if (!$data) {
return; Logger::logError('Json parse error: ' . $json);
} return;
if (isset($data->access_token)) { }
$this->access_token = $data->access_token; if (isset($data->access_token)) {
$this->saveSecretSetting("access_token", $data->access_token); $this->access_token = $data->access_token;
$this->saveSecretSetting("expire", time() + $data->expires_in); $this->saveSecretSetting("access_token", $data->access_token);
} elseif (isset($data->error_description)) { $this->saveSecretSetting("expire", time() + $data->expires_in);
$this->maniaControl->getChat()->sendError('Google refused the request: ' . $data->error_description , $player); } elseif (isset($data->error_description)) {
} else { $this->maniaControl->getChat()->sendError('Google refused the request: ' . $data->error_description , $player);
$this->maniaControl->getChat()->sendError('Unkown error' , $player); } else {
} $this->maniaControl->getChat()->sendError('Unkown error' , $player);
}
});
$asyncHttpRequest->postData(1000);
} }
return true; return true;
} }
@ -445,7 +459,6 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
$data->data[1] = new \stdClass; $data->data[1] = new \stdClass;
$data->data[1]->range = "'" . $sheetname . "'!A9"; $data->data[1]->range = "'" . $sheetname . "'!A9";
foreach ($this->maniaControl->getPlayerManager()->getPlayers() as $player) { foreach ($this->maniaControl->getPlayerManager()->getPlayers() as $player) {
$this->playerlist[$player->login] = $player->nickname; $this->playerlist[$player->login] = $player->nickname;
} }
@ -548,7 +561,10 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin {
function onCallbackEndMatch(String $matchid, Array $currentscore, Array $currentteamsscore) { function onCallbackEndMatch(String $matchid, Array $currentscore, Array $currentteamsscore) {
Logger::Log('onCallbackEndMatch'); Logger::Log('onCallbackEndMatch');
$this->matchstatus = "ended"; $this->matchstatus = "ended";
$this->UpdateGSheetData($matchid, $currentscore, $currentteamsscore);
$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) { function onCallbackStopMatch(String $matchid, Array $currentscore, Array $currentteamsscore) {
Logger::Log('onCallbackStopMatch'); Logger::Log('onCallbackStopMatch');