- fixed join/leave messages

- improved time formatter
- bugfix setting manager
- donation plugin
This commit is contained in:
Steffen Schröder 2013-11-12 19:33:25 +01:00
parent 414dcfee08
commit 6ce66e1640
9 changed files with 277 additions and 195 deletions

View File

@ -23,12 +23,11 @@ class CommandManager implements CallbackListener, CommandListener {
*/ */
private $maniaControl = null; private $maniaControl = null;
private $commandListeners = array(); private $commandListeners = array();
private $openBills = array();
private $serverShutdownTime = -1; private $serverShutdownTime = -1;
private $serverShutdownEmpty = false; private $serverShutdownEmpty = false;
/** /**
* Construct commands handler * Construct commands manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
@ -36,12 +35,10 @@ class CommandManager implements CallbackListener, CommandListener {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_5_SECOND, $this, 'each5Seconds'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_5_SECOND, $this, 'each5Seconds');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback');
// Register basic commands // Register basic commands
$commands = array('help', 'version', 'shutdown', 'shutdownserver', 'systeminfo', 'setservername', 'getplanets', 'donate', $commands = array('version', 'shutdown', 'shutdownserver', 'systeminfo', 'setservername', 'kick');
'pay', 'kick');
foreach ($commands as $command) { foreach ($commands as $command) {
$this->registerCommandListener($command, $this, 'command_' . $command); $this->registerCommandListener($command, $this, 'command_' . $command);
} }
@ -90,56 +87,16 @@ class CommandManager implements CallbackListener, CommandListener {
$command = explode(" ", substr($callback[1][2], 1)); $command = explode(" ", substr($callback[1][2], 1));
$command = strtolower($command[0]); $command = strtolower($command[0]);
if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) { if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
// No command handler registered // No command listener registered
return true; return true;
} }
// Inform command handlers // Inform command listeners
foreach ($this->commandListeners[$command] as $listener) { foreach ($this->commandListeners[$command] as $listener) {
call_user_func(array($listener[0], $listener[1]), $callback, $player); call_user_func(array($listener[0], $listener[1]), $callback, $player);
} }
return true; return true;
} }
/**
* Handle bill updated callback
*
* @param array $callback
* @return bool
*/
public function handleBillUpdated(array $callback) {
$bill = $callback[1];
if (!array_key_exists($bill[0], $this->openBills)) {
return false;
}
$login = $this->openBills[$bill[0]];
switch ($bill[1]) {
case 4:
{
// Payed
$message = 'Success! Thanks.';
$this->maniaControl->chat->sendSuccess($message, $login);
unset($this->openBills[$bill[0]]);
break;
}
case 5:
{
// Refused
$message = 'Transaction cancelled.';
$this->maniaControl->chat->sendError($message, $login);
unset($this->openBills[$bill[0]]);
break;
}
case 6:
{
// Error
$this->maniaControl->chat->sendError($bill[2], $login);
unset($this->openBills[$bill[0]]);
break;
}
}
return true;
}
/** /**
* Send ManiaControl version * Send ManiaControl version
* *
@ -152,138 +109,6 @@ class CommandManager implements CallbackListener, CommandListener {
return $this->maniaControl->chat->sendInformation($message, $login); return $this->maniaControl->chat->sendInformation($message, $login);
} }
/**
* Send help list
*
* @param array $chat
* @return bool
*/
private function command_help(array $chat) {
$login = $chat[1][1];
// TODO: improve help command
// TODO: enable help for specific commands
$list = 'Available commands: ';
$commands = array_keys($this->commandHandlers);
$count = count($commands);
for ($index = 0; $index < $count; $index++) {
if (!$this->maniaControl->authentication->checkRight($login, $this->getRightsLevel($commands[$index], 'superadmin'))) {
unset($commands[$index]);
}
}
$count = count($commands);
$index = 0;
foreach ($commands as $command) {
$list .= $command;
if ($index < $count - 1) {
$list .= ', ';
}
$index++;
}
return $this->maniaControl->chat->sendInformation($list, $login);
}
/**
* Handle getplanets command
*
* @param array $chat
* @param Player $player
* @return bool
*/
private function command_getplanets(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_ADMIN)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
if (!$this->maniaControl->client->query('GetServerPlanets')) {
trigger_error("Couldn't retrieve server planets. " . $this->maniaControl->getClientErrorText());
return false;
}
$planets = $this->maniaControl->client->getResponse();
$message = "This Server has {$planets} Planets!";
return $this->maniaControl->chat->sendInformation($message, $player->login);
}
/**
* Handle donate command
*
* @param array $chat
* @param Player $player
* @return bool
*/
private function command_donate(array $chat, Player $player) {
$params = explode(' ', $chat[1][2]);
if (count($params) < 2) {
// TODO: send usage information
return false;
}
$amount = (int) $params[1];
if (!$amount || $amount <= 0) {
// TODO: send usage information
return false;
}
if (count($params) >= 3) {
$receiver = $params[2];
$receiverPlayer = $this->maniaControl->playerHandler->getPlayer($receiver);
$receiverName = ($receiverPlayer ? $receiverPlayer['NickName'] : $receiver);
}
else {
$receiver = '';
$receiverName = $this->maniaControl->server->getName();
}
$message = 'Donate ' . $amount . ' Planets to $<' . $receiverName . '$>?';
if (!$this->maniaControl->client->query('SendBill', $pl, $amount, $message, $receiver)) {
trigger_error(
"Couldn't create donation of {$amount} planets from '{$player->login}' for '{$receiver}'. " .
$this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("Creating donation failed.", $player->login);
return false;
}
$bill = $this->maniaControl->client->getResponse();
$this->openBills[$bill] = $player->login;
return true;
}
/**
* Handle pay command
*
* @param array $chat
* @param Player $player
* @return bool
*/
private function command_pay(array $chat, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_SUPERADMIN)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
$params = explode(' ', $chat[1][2]);
if (count($params) < 2) {
// TODO: send usage information
return false;
}
$amount = (int) $params[1];
if (!$amount || $amount <= 0) {
// TODO: send usage information
return false;
}
if (count($params) >= 3) {
$receiver = $params[2];
}
else {
$receiver = $player->login;
}
$message = 'Payout from $<' . $this->maniaControl->server->getName() . '$>.';
if (!$this->maniaControl->client->query('Pay', $receiver, $amount, $message)) {
trigger_error(
"Couldn't create payout of {$amount} planets by '{$player->login}' for '{$receiver}'. " .
$this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("Creating payout failed.", $player->login);
return false;
}
$bill = $this->maniaControl->client->getResponse();
$this->openBills[$bill] = $player->login;
return true;
}
/** /**
* Handle systeminfo command * Handle systeminfo command
* *
@ -375,7 +200,7 @@ class CommandManager implements CallbackListener, CommandListener {
return false; return false;
} }
$target = $params[1]; $target = $params[1];
$target = $this->maniaControl->playerHandler->getPlayer($target); $target = $this->maniaControl->playerManager->getPlayer($target);
if (!$target) { if (!$target) {
$this->maniaControl->chat->sendError("Invalid player login.", $player->login); $this->maniaControl->chat->sendError("Invalid player login.", $player->login);
return false; return false;

View File

@ -35,21 +35,21 @@ abstract class Formatter {
/** /**
* Formats the given time (seconds) to hh:mm:ss * Formats the given time (seconds) to hh:mm:ss
* *
* @param int $time * @param int $seconds
* @return string * @return string
*/ */
public static function formatTimeH($time) { public static function formatTimeH($seconds) {
return gmdate("H:i:s", $seconds); return gmdate("H:i:s", $seconds);
} }
/** /**
* Convert the given time (seconds) to mysql timestamp * Convert the given time (seconds) to mysql timestamp
* *
* @param int $time * @param int $seconds
* @return string * @return string
*/ */
public static function formatTimestamp($time) { public static function formatTimestamp($seconds) {
return date("Y-m-d H:i:s", $time); return date("Y-m-d H:i:s", $seconds);
} }
/** /**

View File

@ -15,6 +15,7 @@ require_once __DIR__ . '/Chat.php';
require_once __DIR__ . '/Commands/CommandManager.php'; require_once __DIR__ . '/Commands/CommandManager.php';
require_once __DIR__ . '/Database.php'; require_once __DIR__ . '/Database.php';
require_once __DIR__ . '/FileUtil.php'; require_once __DIR__ . '/FileUtil.php';
require_once __DIR__ . '/Formatter.php';
require_once __DIR__ . '/Manialinks/ManialinkIdHandler.php'; require_once __DIR__ . '/Manialinks/ManialinkIdHandler.php';
require_once __DIR__ . '/Manialinks/ManialinkUtil.php'; require_once __DIR__ . '/Manialinks/ManialinkUtil.php';
require_once __DIR__ . '/Maps/Map.php'; require_once __DIR__ . '/Maps/Map.php';

View File

@ -4,6 +4,7 @@ namespace ManiaControl\Players;
require_once __DIR__ . '/Player.php'; require_once __DIR__ . '/Player.php';
use ManiaControl\Formatter;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
@ -18,7 +19,7 @@ class PlayerManager implements CallbackListener {
* Constants * Constants
*/ */
const TABLE_PLAYERS = 'mc_players'; const TABLE_PLAYERS = 'mc_players';
const SETTING_LEAVE_JOIN_MESSAGES = 'EnableLeaveJoinMessages'; const SETTING_JOIN_LEAVE_MESSAGES = 'Enable Join & Leave Messages';
/** /**
* Private properties * Private properties
@ -27,7 +28,7 @@ class PlayerManager implements CallbackListener {
private $playerList = array(); private $playerList = array();
/** /**
* Construct player handler * Construct player manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
@ -82,8 +83,9 @@ class PlayerManager implements CallbackListener {
*/ */
public function onInit(array $callback) { public function onInit(array $callback) {
// register settings // register settings
$this->maniaControl->settingManager->initSetting($this, "Leave Join Messages", true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES, false);
// Add all players
$this->maniaControl->client->query('GetPlayerList', 300, 0, 2); $this->maniaControl->client->query('GetPlayerList', 300, 0, 2);
$playerList = $this->maniaControl->client->getResponse(); $playerList = $this->maniaControl->client->getResponse();
foreach ($playerList as $playerItem) { foreach ($playerList as $playerItem) {
@ -109,7 +111,7 @@ class PlayerManager implements CallbackListener {
$player = new Player($playerInfo); $player = new Player($playerInfo);
$this->addPlayer($player); $this->addPlayer($player);
if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_LEAVE_JOIN_MESSAGES)) { if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) {
return; return;
} }
$string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); $string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin');
@ -131,10 +133,11 @@ class PlayerManager implements CallbackListener {
$login = $callback[1][0]; $login = $callback[1][0];
$player = $this->removePlayer($login); $player = $this->removePlayer($login);
if ($this->maniaControl->settingManager->getSetting($this, "Leave Join Messages")) { if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_JOIN_LEAVE_MESSAGES)) {
$played = TimeFormatter::formatTime(time() - $player->joinTime); return;
$this->maniaControl->chat->sendChat($player->nickname . '$z $ff0has left the game. Played:$fff ' . $played);
} }
$played = Formatter::formatTimeH(time() - $player->joinTime);
$this->maniaControl->chat->sendChat('$<'.$player->nickname . '$> $ff0has left the game. Played:$fff ' . $played);
} }
/** /**

View File

@ -27,6 +27,15 @@ abstract class Plugin {
*/ */
public abstract function __construct(ManiaControl $maniaControl); public abstract function __construct(ManiaControl $maniaControl);
/**
* Get class name as string
*
* @return string
*/
public static function getClass() {
return __CLASS__;
}
/** /**
* Get plugin author * Get plugin author
* *

View File

@ -25,7 +25,7 @@ class PluginManager {
private $pluginClasses = array(); private $pluginClasses = array();
/** /**
* Construct plugin handler * Construct plugin manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
@ -121,6 +121,7 @@ class PluginManager {
if ($pluginStatement->num_rows <= 0) { if ($pluginStatement->num_rows <= 0) {
$pluginStatement->free_result(); $pluginStatement->free_result();
$pluginStatement->close(); $pluginStatement->close();
$this->savePluginStatus($className, false);
return false; return false;
} }
$pluginStatement->bind_result($activeInt); $pluginStatement->bind_result($activeInt);
@ -149,7 +150,7 @@ class PluginManager {
$classesAfter = get_declared_classes(); $classesAfter = get_declared_classes();
$newClasses = array_diff($classesAfter, $classesBefore); $newClasses = array_diff($classesAfter, $classesBefore);
foreach ($newClasses as $className) { foreach ($newClasses as $className) {
if (!is_subclass_of($className, 'ManiaControl\Plugin')) { if (!is_subclass_of($className, Plugin::getClass())) {
continue; continue;
} }
array_push($this->pluginClasses, $className); array_push($this->pluginClasses, $className);

View File

@ -164,6 +164,9 @@ class SettingManager {
if ($type === self::TYPE_ARRAY) { if ($type === self::TYPE_ARRAY) {
return implode($this->arrayDelimiter, $value); return implode($this->arrayDelimiter, $value);
} }
if ($type === self::TYPE_BOOL) {
return ($value ? 1 : 0);
}
return $value; return $value;
} }

View File

@ -3,6 +3,7 @@
namespace ManiaControl; namespace ManiaControl;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
/** /**
* Class handling authentication levels * Class handling authentication levels
@ -101,7 +102,7 @@ class Authentication {
return false; return false;
} }
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$authQuery = "INSERT INTO `" . PlayerHandler::TABLE_PLAYERS . "` ( $authQuery = "INSERT INTO `" . PlayerManager::TABLE_PLAYERS . "` (
`login`, `login`,
`authLevel` `authLevel`
) VALUES ( ) VALUES (

View File

@ -0,0 +1,239 @@
<?php
use ManiaControl\Authentication;
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Players\Player;
/**
* Donation plugin
*
* @author steeffeen
*/
class DonationPlugin extends Plugin implements CallbackListener, CommandListener {
/**
* Constants
*/
const VERSION = '1.0';
const SETTING_ANNOUNCE_SERVERDONATION = 'Enable Server-Donation Announcements';
/**
* Private properties
*/
private $openBills = array();
/**
* Construct donation plugin
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->author = 'steeffeen';
$this->name = 'Donation Plugin';
$this->version = self::VERSION;
$this->description = 'DonationPlugin commands like /donate, /pay and /getplanets and a donation widget.';
$this->maniaControl->commandManager->registerCommandListener('donate', $this, 'command_Donate');
$this->maniaControl->commandManager->registerCommandListener('pay', $this, 'command_Pay');
$this->maniaControl->commandManager->registerCommandListener('getplanets', $this, 'command_GetPlanets');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated');
}
/**
* Handle /donate command
*
* @param array $chatCallback
* @param Player $player
* @return bool
*/
public function command_Donate(array $chatCallback, Player $player) {
$text = $chatCallback[1][2];
$params = explode(' ', $text);
if (count($params) < 2) {
$this->sendDonateUsageExample($player);
return false;
}
$amount = (int) $params[1];
if (!$amount || $amount <= 0) {
$this->sendDonateUsageExample($player);
return false;
}
if (count($params) >= 3) {
$receiver = $params[2];
$receiverPlayer = $this->maniaControl->playerManager->getPlayer($receiver);
$receiverName = ($receiverPlayer ? $receiverPlayer['NickName'] : $receiver);
}
else {
$receiver = '';
$receiverName = $this->maniaControl->server->getName();
}
$message = 'Donate ' . $amount . ' Planets to $<' . $receiverName . '$>?';
if (!$this->maniaControl->client->query('SendBill', $player->login, $amount, $message, $receiver)) {
trigger_error(
"Couldn't create donation of {$amount} planets from '{$player->login}' for '{$receiver}'. " .
$this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("Creating donation failed.", $player->login);
return false;
}
$bill = $this->maniaControl->client->getResponse();
$this->openBills[$bill] = array(true, $player->login, $receiver, $amount, time());
return true;
}
/**
* Handle /pay command
*
* @param array $chatCallback
* @param Player $player
* @return bool
*/
public function command_Pay(array $chatCallback, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_SUPERADMIN)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
$text = $chatCallback[1][2];
$params = explode(' ', $text);
if (count($params) < 2) {
$this->sendPayUsageExample($player);
return false;
}
$amount = (int) $params[1];
if (!$amount || $amount <= 0) {
$this->sendPayUsageExample($player);
return false;
}
if (count($params) >= 3) {
$receiver = $params[2];
}
else {
$receiver = $player->login;
}
$message = 'Payout from $<' . $this->maniaControl->server->getName() . '$>.';
if (!$this->maniaControl->client->query('Pay', $receiver, $amount, $message)) {
trigger_error(
"Couldn't create payout of {$amount} planets by '{$player->login}' for '{$receiver}'. " .
$this->maniaControl->getClientErrorText());
$this->maniaControl->chat->sendError("Creating payout failed.", $player->login);
return false;
}
$bill = $this->maniaControl->client->getResponse();
$this->openBills[$bill] = array(false, $player->login, $receiver, $amount, time());
return true;
}
/**
* Handle /getplanets command
*
* @param array $chat
* @param Player $player
* @return bool
*/
public function command_GetPlanets(array $chatCallback, Player $player) {
if (!$this->maniaControl->authentication->checkRight($player, Authentication::AUTH_LEVEL_ADMIN)) {
$this->maniaControl->authentication->sendNotAllowed($player);
return false;
}
if (!$this->maniaControl->client->query('GetServerPlanets')) {
trigger_error("Couldn't retrieve server planets. " . $this->maniaControl->getClientErrorText());
return false;
}
$planets = $this->maniaControl->client->getResponse();
$message = "This Server has {$planets} Planets!";
return $this->maniaControl->chat->sendInformation($message, $player->login);
}
/**
* Handle bill updated callback
*
* @param array $callback
* @return bool
*/
public function handleBillUpdated(array $callback) {
$billId = $callback[1][0];
if (!array_key_exists($billId, $this->openBills)) {
return false;
}
$billData = $this->openBills[$billId];
$login = $billData[1];
$receiver = $billData[2];
switch ($callback[1][1]) {
case 4:
{
// Payed
$donation = $billData[0];
$amount = $billData[3];
if ($donation) {
// Donation
if (strlen($receiver) > 0) {
// To player
$message = "Successfully donated {$amount} to '{$receiver}'!";
$this->maniaControl->chat->sendSuccess($message, $login);
}
else {
// To server
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_ANNOUNCE_SERVERDONATION, true)) {
$player = $this->maniaControl->playerManager->getPlayer($login);
$message = '$<' . ($player ? $player->nickname : $login) . '$> donated ' . $amount . ' Planets! Thanks.';
}
else {
$message = 'Donation successful! Thanks.';
}
$this->maniaControl->chat->sendSuccess($message, $login);
}
}
else {
// Payout
$message = "Successfully payed out {$amount} to '{$receiver}'!";
$this->maniaControl->chat->sendSuccess($message, $login);
}
unset($this->openBills[$billId]);
break;
}
case 5:
{
// Refused
$message = 'Transaction cancelled.';
$this->maniaControl->chat->sendError($message, $login);
unset($this->openBills[$billId]);
break;
}
case 6:
{
// Error
$this->maniaControl->chat->sendError($callback[1][2], $login);
unset($this->openBills[$billId]);
break;
}
}
return true;
}
/**
* Send an usage example for /donate to the player
*
* @param Player $player
* @return boolean
*/
private function sendDonateUsageExample(Player $player) {
$colorCode = '$f80';
return $this->maniaControl->chat->sendChat("{$colorCode}Usage Example: '/donate 100'", $player->login);
}
/**
* Send an usage example for /pay to the player
*
* @param Player $player
* @return boolean
*/
private function sendPayUsageExample(Player $player) {
$colorCode = '$f80';
return $this->maniaControl->chat->sendChat("{$colorCode}Usage Example: '/pay 100 login'", $player->login);
}
}
?>