added playeractions to communication listenings

This commit is contained in:
kremsy 2015-07-11 14:06:18 +02:00
parent a3cba8de76
commit 908e310fce
3 changed files with 112 additions and 1 deletions

View File

@ -14,6 +14,7 @@
- added Method getServerLoginByIndex to Server object - added Method getServerLoginByIndex to Server object
- added to PlayerManager's Method PlayerManager the Parameter "withoutBots" (default on true) - added to PlayerManager's Method PlayerManager the Parameter "withoutBots" (default on true)
- added Method getSpectators() in PlayerManager - added Method getSpectators() in PlayerManager
- added Method restartMap(), skipToMapByMxId(), skipToMapByUid() into MapActions
- added some missing PHP Docs - added some missing PHP Docs
- added some depency libraries as they are used by the Socket Handler - added some depency libraries as they are used by the Socket Handler
- added additional Callback which gets triggered on ManiaControl Restart - added additional Callback which gets triggered on ManiaControl Restart

View File

@ -16,6 +16,25 @@ interface CommunicationMethods {
*/ */
const RESTART_MANIA_CONTROL = "ManiaControl.Restart"; const RESTART_MANIA_CONTROL = "ManiaControl.Restart";
/** Restarts the Current Map
* no Parameters
*/
const RESTART_MAP = "MapActions.RestartMap";
/** Skips the Current Map
* no Parameters
*/
const SKIP_MAP = "MapActions.SkipMap";
/** Skips to a Specific Map by MxId or MapUid
* Required Parameters
* - mxId
* OR
* - mapUid
*/
const SKIP_TO_MAP = "MapActions.SkipToMap";
/** Adds a Map from Mania Exchange to the Server /** Adds a Map from Mania Exchange to the Server
* Required Parameters * Required Parameters
* - mxId * - mxId

View File

@ -2,6 +2,9 @@
namespace ManiaControl\Maps; namespace ManiaControl\Maps;
use ManiaControl\Communication\CommunicationAnswer;
use ManiaControl\Communication\CommunicationListener;
use ManiaControl\Communication\CommunicationMethods;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use Maniaplanet\DedicatedServer\Xmlrpc\ChangeInProgressException; use Maniaplanet\DedicatedServer\Xmlrpc\ChangeInProgressException;
@ -12,7 +15,7 @@ use Maniaplanet\DedicatedServer\Xmlrpc\ChangeInProgressException;
* @copyright 2014-2015 ManiaControl Team * @copyright 2014-2015 ManiaControl Team
* @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 MapActions { class MapActions implements CommunicationListener {
/* /*
* Private properties * Private properties
*/ */
@ -26,12 +29,79 @@ class MapActions {
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
//Communication Listenings
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::SKIP_MAP, $this, function ($data) {
$success = $this->skipMap();
return new CommunicationAnswer(array("success" => $success));
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::RESTART_MAP, $this, function ($data) {
$success = $this->restartMap();
return new CommunicationAnswer(array("success" => $success));
});
$this->maniaControl->getCommunicationManager()->registerCommunicationListener(CommunicationMethods::SKIP_TO_MAP, $this, function ($data) {
if (!is_object($data)) {
return new CommunicationAnswer("Error in provided Data", true);
}
if (property_exists($data, "mxId")) {
$success = $this->skipToMapByMxId($data->mxId);
} else if (property_exists($data, "mapUid")) {
$success = $this->skipToMapByUid($data->mapUid);
} else {
return new CommunicationAnswer("No mxId or mapUid provided.", true);
}
return new CommunicationAnswer(array("success" => $success));
});
}
/**
* Skips to a Map by its given UID
*
* @param String $uid
* @return bool
*/
public function skipToMapByUid($uid) {
//TODO message
//Check if Map exists
$map = $this->maniaControl->getMapManager()->getMapByUid($uid);
if (!$map) {
return false;
}
try {
$this->maniaControl->getClient()->jumpToMapIdent($uid);
} catch (ChangeInProgressException $e) {
return false;
}
return true;
}
/**
* Skips to a Map by its given MxId
*
* @param int $mxId
* @return bool
*/
public function skipToMapByMxId($mxId) {
$map = $this->maniaControl->getMapManager()->getMapByMxId($mxId);
if (!$map) {
return false;
}
return $this->skipToMapByUid($map->uid);
} }
/** /**
* Skip the current Map * Skip the current Map
*
* @return bool
*/ */
public function skipMap() { public function skipMap() {
//TODO message
// Force an EndMap on the MapQueue to set the next Map // Force an EndMap on the MapQueue to set the next Map
$this->maniaControl->getMapManager()->getMapQueue()->endMap(null); $this->maniaControl->getMapManager()->getMapQueue()->endMap(null);
@ -42,6 +112,27 @@ class MapActions {
try { try {
$this->maniaControl->getClient()->nextMap(); $this->maniaControl->getClient()->nextMap();
} catch (ChangeInProgressException $e) { } catch (ChangeInProgressException $e) {
return false;
} }
return true;
}
/**
* Restarts the Current Map
*
* @return bool
*/
public function restartMap() {
//TODO message
//Restarts the Current Map
try {
$this->maniaControl->getClient()->restartMap();
} catch (ChangeInProgressException $e) {
return false;
}
return true;
} }
} }