TrackManiaControl/plugins/MCTeam/DynamicPointLimitPlugin.php

248 lines
8.2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace MCTeam;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Commands\CommandListener;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
/**
* Dynamic Point Limit Plugin
*
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
// TODO: test setpointlimit command
class DynamicPointLimitPlugin implements CallbackListener, CommandListener, Plugin {
/*
* Constants
*/
const ID = 21;
2017-05-10 13:00:53 +02:00
const VERSION = 0.4;
const NAME = 'Dynamic Point Limit Plugin';
const AUTHOR = 'MCTeam';
const SETTING_POINT_LIMIT_MULTIPLIER = 'Point Limit Multiplier';
const SETTING_POINT_LIMIT_OFFSET = 'Point Limit Offset';
const SETTING_MIN_POINT_LIMIT = 'Minimum Point Limit';
const SETTING_MAX_POINT_LIMIT = 'Maximum Point Limit';
const SETTING_ACCEPT_OTHER_MODES = 'Activate in other Modes than Royal';
const CACHE_SPEC_STATUS = 'SpecStatus';
2014-06-23 16:32:40 +02:00
const SCRIPT_SETTING_MAP_POINTS_LIMIT = 'S_MapPointsLimit';
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $lastPointLimit = null;
private $staticMode = null;
/**
* @see \ManiaControl\Plugins\Plugin::prepare()
*/
public static function prepare(ManiaControl $maniaControl) {
2014-08-13 11:14:29 +02:00
$maniaControl->getSettingManager()->initSetting(get_class(), self::SETTING_POINT_LIMIT_MULTIPLIER, 10);
$maniaControl->getSettingManager()->initSetting(get_class(), self::SETTING_POINT_LIMIT_OFFSET, 0);
$maniaControl->getSettingManager()->initSetting(get_class(), self::SETTING_MIN_POINT_LIMIT, 30);
$maniaControl->getSettingManager()->initSetting(get_class(), self::SETTING_MAX_POINT_LIMIT, 200);
$maniaControl->getSettingManager()->initSetting(get_class(), self::SETTING_ACCEPT_OTHER_MODES, false);
}
/**
* @see \ManiaControl\Plugins\Plugin::getId()
*/
public static function getId() {
return self::ID;
}
/**
* @see \ManiaControl\Plugins\Plugin::getName()
*/
public static function getName() {
return self::NAME;
}
/**
* @see \ManiaControl\Plugins\Plugin::getVersion()
*/
public static function getVersion() {
return self::VERSION;
}
/**
* @see \ManiaControl\Plugins\Plugin::getAuthor()
*/
public static function getAuthor() {
return self::AUTHOR;
}
/**
* @see \ManiaControl\Plugins\Plugin::getDescription()
*/
public static function getDescription() {
return 'Plugin offering a dynamic Point Limit according to the Number of Players on the Server.';
}
/**
* @see \ManiaControl\Plugins\Plugin::load()
*/
public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-08-13 11:14:29 +02:00
$allowOthers = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_ACCEPT_OTHER_MODES);
if (!$allowOthers && $this->maniaControl->getServer()->titleId !== 'SMStormRoyal@nadeolabs') {
$error = 'This plugin only supports Royal (check Settings)!';
throw new \Exception($error);
}
2014-08-13 11:14:29 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'updatePointLimit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'updatePointLimit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERINFOCHANGED, $this, 'handlePlayerInfoChangedCallback');
2017-05-10 13:00:53 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::MP_STARTROUNDSTART, $this, 'updatePointLimit');
2014-08-13 11:14:29 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::BEGINMAP, $this, 'handleBeginMap');
$this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'handleSettingChangedCallback');
2014-08-13 11:14:29 +02:00
$this->maniaControl->getCommandManager()->registerCommandListener('setpointlimit', $this, 'commandSetPointlimit', true, 'Setpointlimit XXX or auto');
$this->updatePointLimit();
2014-05-21 09:44:48 +02:00
}
/**
* Update Point Limit
*/
public function updatePointLimit() {
if ($this->staticMode) {
return;
}
2014-08-13 11:14:29 +02:00
$numberOfPlayers = $this->maniaControl->getPlayerManager()->getPlayerCount();
2014-08-13 11:14:29 +02:00
$multiplier = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_POINT_LIMIT_MULTIPLIER);
$offset = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_POINT_LIMIT_OFFSET);
$minValue = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MIN_POINT_LIMIT);
$maxValue = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MAX_POINT_LIMIT);
2014-05-20 22:56:09 +02:00
$pointLimit = $offset + $numberOfPlayers * $multiplier;
if ($pointLimit < $minValue) {
$pointLimit = $minValue;
}
if ($pointLimit > $maxValue) {
$pointLimit = $maxValue;
}
2014-05-20 22:56:09 +02:00
$pointLimit = (int)$pointLimit;
if ($this->lastPointLimit !== $pointLimit) {
try {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getClient()->setModeScriptSettings(array(self::SCRIPT_SETTING_MAP_POINTS_LIMIT => $pointLimit));
$message = "Dynamic PointLimit changed to: {$pointLimit}!";
if ($this->lastPointLimit !== null) {
$message .= " (From {$this->lastPointLimit})";
}
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendInformation($message);
$this->lastPointLimit = $pointLimit;
} catch (GameModeException $exception) {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendExceptionToAdmins($exception);
}
}
}
/**
* Handle SetPointLimit Command
*
* @param array $chatCallback
* @param Player $player
*/
public function commandSetPointlimit(array $chatCallback, Player $player) {
$commandParts = explode(' ', $chatCallback[1][2]);
if (count($commandParts) < 2) {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendUsageInfo('Example: //setpointlimit auto', $player);
return;
}
$value = strtolower($commandParts[1]);
if ($value === "auto") {
$this->staticMode = false;
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendInformation('Enabled Dynamic PointLimit!');
$this->updatePointLimit();
} else {
if (is_numeric($value)) {
$value = (int)$value;
if ($value <= 0) {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendError('PointLimit needs to be greater than Zero.', $player);
return;
}
try {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getClient()->setModeScriptSettings(array(self::SCRIPT_SETTING_MAP_POINTS_LIMIT => $value));
$this->staticMode = true;
$this->lastPointLimit = $value;
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendInformation("PointLimit changed to: {$value} (Fixed)");
} catch (GameModeException $exception) {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendException($exception, $player);
}
} else {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendUsageInfo('Example: //setpointlimit 150', $player);
}
}
}
/**
* @see \ManiaControl\Plugins\Plugin::unload()
*/
public function unload() {
}
/**
* Handle Setting Changed Callback
*
* @param Setting $setting
*/
public function handleSettingChangedCallback(Setting $setting) {
if (!$setting->belongsToClass($this)) {
return;
}
$this->updatePointLimit();
}
/**
* Handle BeginMap Callback
*/
public function handleBeginMap() {
2014-06-23 16:32:40 +02:00
if ($this->staticMode && !is_null($this->lastPointLimit)) {
// Refresh static point limit in case it has been reset
try {
2014-08-13 11:14:29 +02:00
$this->maniaControl->getClient()->setModeScriptSettings(array(self::SCRIPT_SETTING_MAP_POINTS_LIMIT => $this->lastPointLimit));
2014-06-23 16:32:40 +02:00
$message = "PointLimit fixed at {$this->lastPointLimit}.";
2014-08-13 11:14:29 +02:00
$this->maniaControl->getChat()->sendInformation($message);
2014-06-23 16:32:40 +02:00
} catch (GameModeException $e) {
$this->lastPointLimit = null;
}
}
}
/**
* Handle Player Info Changed Callback
*
* @param Player $player
*/
public function handlePlayerInfoChangedCallback(Player $player) {
$lastSpecStatus = $player->getCache($this, self::CACHE_SPEC_STATUS);
$newSpecStatus = $player->isSpectator;
if ($newSpecStatus === $lastSpecStatus && $lastSpecStatus !== null) {
return;
}
$player->setCache($this, self::CACHE_SPEC_STATUS, $newSpecStatus);
$this->updatePointLimit();
}
}