TrackManiaControl/core/Maps/MapActions.php

143 lines
3.5 KiB
PHP
Raw Normal View History

2014-05-09 11:58:33 +02:00
<?php
namespace ManiaControl\Maps;
use ManiaControl\Communication\CommunicationAnswer;
use ManiaControl\Communication\CommunicationListener;
use ManiaControl\Communication\CommunicationMethods;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
use ManiaControl\ManiaControl;
use Maniaplanet\DedicatedServer\Xmlrpc\ChangeInProgressException;
2014-05-09 11:58:33 +02:00
/**
* ManiaControl Map Actions Class
2014-05-09 11:58:33 +02:00
*
* @author ManiaControl Team <mail@maniacontrol.com>
2017-02-04 11:49:23 +01:00
* @copyright 2014-2017 ManiaControl Team
2014-05-09 11:58:33 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class MapActions implements CommunicationListener, UsageInformationAble {
use UsageInformationTrait;
2014-05-09 11:58:33 +02:00
/*
* Private properties
2014-05-09 11:58:33 +02:00
*/
/** @var ManiaControl $maniaControl */
2014-05-09 11:58:33 +02:00
private $maniaControl = null;
/**
* Construct a map actions instance
2014-05-09 11:58:33 +02:00
*
* @param ManiaControl $maniaControl
*/
public function __construct(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);
2014-05-09 11:58:33 +02:00
}
/**
* Skip the current Map
*
* @return bool
2014-05-09 11:58:33 +02:00
*/
public function skipMap() {
//TODO message
// Force an EndMap on the MapQueue to set the next Map
2014-08-13 11:05:52 +02:00
$this->maniaControl->getMapManager()->getMapQueue()->endMap(null);
2014-05-09 11:58:33 +02:00
// Ignore EndMap on MapQueue
2014-08-13 11:05:52 +02:00
$this->maniaControl->getMapManager()->getMapQueue()->dontQueueNextMapChange();
2014-05-09 11:58:33 +02:00
// Switch The Map
2014-05-09 11:58:33 +02:00
try {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getClient()->nextMap();
2014-05-09 11:58:33 +02:00
} catch (ChangeInProgressException $e) {
return false;
2014-05-09 11:58:33 +02:00
}
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;
2014-05-09 11:58:33 +02:00
}
2014-06-23 16:04:00 +02:00
}