Skip to content
Permalink
Browse files

0.74.15 Add threaded chat router

  • Loading branch information...
brakerb committed Sep 13, 2019
1 parent 25f8c3d commit 04ab7a3c163f673ddfc55cb3a9ace90cd9a6e6f9
Showing with 128 additions and 2 deletions.
  1. +99 −0 core/Commands/ChatRouter.php
  2. +14 −1 core/Commands/EscRun.php
  3. +5 −1 core/Controllers/ChatController.php
  4. +8 −0 core/Controllers/EventController.php
  5. +2 −0 esc
@@ -0,0 +1,99 @@
<?php
namespace esc\Commands;
use esc\Classes\Database;
use esc\Classes\Log;
use esc\Classes\Server;
use esc\Controllers\ConfigController;
use esc\Models\Player;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ChatRouter extends Command
{
protected function configure()
{
$this->setName('run:chat-router')
->setDescription('Run EvoSC chat-routing-service');
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
Log::setOutput($output);
ConfigController::init();
Database::init();
try {
$output->writeln("Connecting to server...");
Server::init(
config('server.ip'),
config('server.port'),
5,
config('server.rpc.login'),
config('server.rpc.password')
);
Server::chatEnableManualRouting(true, true);
$output->writeln("Connection established.");
} catch (Exception $e) {
$msg = $e->getMessage();
$output->writeln("<error>Connecting to server failed: $msg</error>");
exit(1);
}
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$players = collect();
while (true) {
foreach (Server::executeCallbacks() as $callback) {
if ($callback[0] == 'ManiaPlanet.PlayerChat') {
$login = $callback[1][1];
$text = $callback[1][2];
if (substr($text, 0, 1) == '/' || substr($text, 0, 2) == '/') {
continue;
}
if (!$players->has($login)) {
$players->put($login, Player::find($login));
}
$this->playerChat($players->get($login), $text);
} elseif ($callback[0] == 'ManiaPlanet.PlayerDisconnect') {
$players->forget($callback[1][0]);
}
}
usleep(100000);
}
}
public function playerChat(Player $player, $text)
{
$nick = $player->NickName;
if ($player->isSpectator()) {
$nick = '$eee📷 '.$nick;
}
$prefix = $player->group->chat_prefix;
$color = $player->group->color ?? config('colors.chat');
$chatText = sprintf('$%s[$z$s%s$z$s$%s] $%s$z$s%s', $color, $nick, $color, config('colors.chat'), $text);
if ($prefix) {
$chatText = '$'.$color.$prefix.' '.$chatText;
}
Server::ChatSendServerMessage($chatText);
}
}
@@ -34,7 +34,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function Sodium\version_string;
class EscRun extends Command
{
@@ -51,6 +50,20 @@ protected function initialize(InputInterface $input, OutputInterface $output)
{
global $serverName;
switch (pcntl_fork()) {
case -1:
$output->writeln('Starting char router failed.');
break;
case 0:
$output->writeln('Starting chat router.');
pcntl_exec('/usr/bin/php', ['esc', 'run:chat-router']);
exit(0);
default:
//parent
}
Log::setOutput($output);
ConfigController::init();
@@ -36,6 +36,7 @@ class ChatController implements ControllerInterface
public static function init()
{
self::$mutedPlayers = collect();
/*
self::$routingEnabled = config('server.enable-chat-routing');
if (self::$routingEnabled) {
@@ -44,7 +45,7 @@ public static function init()
while (!$routingEnabled){
try {
Server::chatEnableManualRouting(true, true);
Server::chatEnableManualRouting(false, false);
$routingEnabled = true;
} catch (FaultException $e) {
$msg = $e->getMessage();
@@ -55,6 +56,7 @@ public static function init()
} else {
Server::chatEnableManualRouting(false, false);
}
*/
AccessRight::createIfMissing('player_mute', 'Mute/unmute player.');
AccessRight::createIfMissing('admin_echoes', 'Receive admin messages.');
@@ -187,6 +189,8 @@ public static function playerChat(Player $player, $text)
Log::write('<fg=yellow>['.$player.'] '.$text.'</>', true);
return;
if (!self::$routingEnabled) {
return;
}
@@ -130,6 +130,14 @@ private static function mpPlayerChat($data)
return;
}
if (substr($text, 0, 1) == '/' || substr($text, 0, 2) == '/') {
warningMessage('Invalid chat-command entered. See ', secondary('/help'), ' for all commands.')->send(player($login));
return;
}
return;
if (ChatController::getRoutingEnabled()) {
try {
Hook::fire('PlayerChat', player($login), $text);
2 esc
@@ -13,6 +13,7 @@ use esc\Commands\ImportUaseco;
use esc\Commands\ImportPyplanet;
use esc\Commands\MakeMigration;
use esc\Commands\Migrate;
use esc\Commands\ChatRouter;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;

@@ -52,6 +53,7 @@ $application->add(new ImportPyplanet());
$application->add(new FixScores());
$application->add(new EscRun());
$application->add(new FakeLocals());
$application->add(new ChatRouter());
$application->add(new AddAdmin());
$application->setDefaultCommand("list");

0 comments on commit 04ab7a3

Please sign in to comment.
You can’t perform that action at this time.