- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个正在构建游戏的 websocket 应用程序,该应用程序构建在使用 React 事件循环的 Ratchet 上。在这个脚本的开头,我已经想出了如何实现一个周期性定时器,每秒向游戏发送一个脉冲,然后执行滴答和战斗回合。这很好用。
但是,我最近意识到我还需要添加“滞后”客户端或暂停函数执行的功能。例如,如果一个玩家被惊呆了,或者我想让一个 NPC 等待 1.5 秒再回复触发器以获得更“真实”的对话感觉。
这个功能是内置在 React 库中的,还是我必须通过其他方式实现的?经过一些研究,我可能正在寻找 pthreads,请参阅此问题/答案:How can one use multi threading in PHP applications
为了更清楚我想要实现的目标,以这段代码为例:
function onSay($string)
{
global $world;
$trigger_words = array(
'hi',
'hello',
'greetings'
);
$triggered = false;
foreach($trigger_words as $trigger_word)
{
if(stristr($string, $trigger_word))
{
$triggered = true;
}
}
if($triggered)
{
foreach($world->players as $player)
{
if($player->pData->in_room === $this->mobile->in_room)
{
sleep(1);
$this->toChar($player, $this->mobile->short . " says '`kOh, hello!``'");
}
}
}
}
显然,这不起作用,因为 sleep(1) 函数将暂停整个服务器进程。
任何见解将不胜感激。谢谢!
更新:我的服务器脚本:
require 'vendor/autoload.php';
require 'src/autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\Socket\Server as Reactor;
use React\EventLoop\Factory as LoopFactory;;
$world = new WorldInterface();
class Server implements MessageComponentInterface
{
public function __construct(React\EventLoop\LoopInterface $loop)
{
$update = new Update();
$update->doTick();
$loop->addPeriodicTimer(1, function()
{
$this->doBeat();
});
}
public function onOpen(ConnectionInterface $ch)
{
global $world;
$world->connecting[$ch->resourceId] = $ch;
$ch->CONN_STATE = "GET_NAME";
$ch->pData = new stdClass();
$ch->send("Who dares storm our wayward path? ");
}
public function onMessage(ConnectionInterface $ch, $args)
{
if($ch->CONN_STATE == "CONNECTED")
{
$ch->send("> " . $args . "\n");
$interpreter = new Interpreter($ch);
$interpreter->interpret($args);
}
else
{
$ch->send($args);
$login = new Login($ch, $args);
$login->start();
}
}
public function onClose(ConnectionInterface $ch)
{
global $world;
if(isset($ch->pData->name))
{
if(isset($world->players[$ch->pData->name]))
{
echo "Player {$ch->pData->name} has disconnected\n";
unset($world->players->{$ch->pData->name});
}
}
if(isset($world->connecting->{$ch->resourceId}))
{
echo "Connection " . $ch->resourceId . " has disconnected.";
unset($world->connecting->{$ch->resourceId});
}
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
public function doBeat()
{
global $world;
++$world->beats;
foreach($world->process_queue as $trigger_beat => $process_array)
{
// if the beat # that the function should fire on is less than,
// or equal to the current beat, fire the function.
if($trigger_beat <= $world->beats)
{
foreach($process_array as $process)
{
$class = new $process->class();
call_user_func_array(array($class, $process->function), $process->params);
}
// remove it from the queue
unset($world->process_queue[$trigger_beat]);
}
// else, the beat # the function should fire on is greater than the current beat,
// so break out of the loop.
else
{
break;
}
}
if($world->beats % 2 === 0)
{
$update = new Update();
$update->doBeat();
}
}
}
$loop = LoopFactory::create();
$socket = new Reactor($loop);
$socket->listen(9000, 'localhost');
$server = new IoServer(new HttpServer(new WsServer(new Server($loop))), $socket, $loop);
$server->run();
最佳答案
好吧,所以我假设因为这个问题仍然没有答案,所以没有“简单”的解决方案融入到 React 事件循环中,尽管我很乐意在这一点上是错误的。在那之前,我想我会发布我的解决方案。
注意:我不知道这样做的含义是什么。我不知道它的可扩展性如何。它未经在具有多个进程和播放器的实时环境中进行测试。
不过,我认为这是一个不错的解决方案。我的特定游戏适合 20 - 30 人的玩家群,所以我认为我可能面临的唯一问题是一堆排队的 Action 是否在同一秒触发。
到代码!
我(不久前)做的第一件事是在服务器启动时添加一个周期性计时器:
public function __construct(React\EventLoop\LoopInterface $loop)
{
$update = new Update();
$update->doTick();
$loop->addPeriodicTimer(1, function()
{
$this->doBeat();
});
}
我的“世界”类中也有一些全局变量:
// things in the world
public $beats = 0;
public $next_tick = 45;
public $connecting = array();
public $players = array();
public $mobiles = array();
public $objects = array();
public $mobs_in_rooms = array();
public $mobs_in_areas = array();
public $in_combat = array(
'mobiles' => array(),
'players' => array()
);
public $process_queue;
注意 beats 和 process_queue。
我的 doBeat() 函数如下所示:
public function doBeat()
{
global $world;
++$world->beats;
foreach($world->process_queue as $trigger_beat => $process_array)
{
// if the beat # that the function should fire on is less than,
// or equal to the current beat, fire the function.
if($trigger_beat <= $world->beats)
{
foreach($process_array as $process)
{
$class = new $process->class();
call_user_func_array(array($class, $process->function), $process->params);
}
// remove it from the queue
unset($world->process_queue[$trigger_beat]);
}
// else, the beat # the function should fire on is greater than the current beat,
// so break out of the loop.
else
{
break;
}
}
print_r(array_keys($world->process_queue));
if($world->beats % 2 === 0)
{
$update = new Update();
$update->doBeat();
}
}
现在,在我的全局“World”对象上,我有几个其他函数:
function addToProcessQueue($process_obj)
{
//adds the process object to an array of the beat #
//when it should be triggered on process_queue.
$this->process_queue[(int)$process_obj->trigger_beat][] = $process_obj;
ksort($this->process_queue);
}
function createProcessObject($array)
{
$process_obj = new stdClass();
if(isset($array['function']))
{
$process_obj->function = $array['function'];
}
else
{
echo "All process requests must define a function to call defined as a key named 'function' on the array you pass.";
}
if(isset($array['class']))
{
$process_obj->class = $array['class'];
}
else
{
echo "All process requests must define a class to call defined as a key named 'class' on the array you pass.";
}
if(isset($array['params']))
{
$process_obj->params = $array['params'];
}
else
{
$process_obj->params = array();
}
if(isset($array['char']))
{
$process_obj->char = $array['char'];
}
else
{
$process_obj->char = false;
}
if(isset($array['trigger_beat']) && is_numeric($array['trigger_beat']))
{
$process_obj->trigger_beat = $array['trigger_beat'];
}
else
{
echo "All process requests must define a trigger_beat. \n"
. "Use world->beats to get current beat and add your wait time onto it. \n"
. "Trigger beat MUST be an integer. \n";
}
$this->addToProcessQueue($process_obj);
}
现在要向队列中添加一个进程,这是我的新移动“onSay()”命令:
function onSay($string)
{
global $world;
$trigger_words = array(
'hi',
'hello',
'greetings'
);
$triggered = false;
foreach($trigger_words as $trigger_word)
{
if(stristr($string, $trigger_word))
{
$triggered = true;
}
}
if($triggered)
{
$process_array = array(
'trigger_beat' => $world->beats + 2,
'function' => 'toRoom',
'class' => 'PlayerInterface',
'params' => array($this->mobile->in_room, $this->mobile->short . " says '`kOh, hello!``'")
);
$world->createProcessObject($process_array);
}
}
因此,如果手机听到“hi”、“hello”或“greetings”,“toRoom”函数(向同一房间中的每个角色发送一个字符串)将被添加到进程队列中并触发 2从执行原始函数开始的秒数。
我希望所有这些都有意义,如果有人知道在 php 和事件循环中完成此类事情的更好方法,请回答/评论。我并没有像我上面所说的那样将其标记为“正确”,我不知道它在生产中的效率如何。
关于php - 我需要在 PHP React websocket 事件循环(也许是多线程?)的内部实现一个等待计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498952/
我是 PHP 新手。我一直在脚本中使用 for 循环、while 循环、foreach 循环。我想知道 哪个性能更好? 选择循环的标准是什么? 当我们在另一个循环中循环时应该使用哪个? 我一直想知道要
我在高中的编程课上,我的作业是制作一个基本的小计和顶级计算器,但我在一家餐馆工作,所以制作一个只能让你在一种食物中读到。因此,我尝试让它能够接收多种食品并将它们添加到一个价格变量中。抱歉,如果某些代码
这是我正在学习的一本教科书。 var ingredients = ["eggs", "milk", "flour", "sugar", "baking soda", "baking powder",
我正在从字符串中提取数字并将其传递给函数。我想给它加 1,然后返回字符串,同时保留前导零。我可以使用 while 循环来完成此操作,但不能使用 for 循环。 for 循环只是跳过零。 var add
编辑:我已经在程序的输出中进行了编辑。 该程序要求估计给定值 mu。用户给出一个值 mu,同时还提供了四个不等于 1 的不同数字(称为 w、x、y、z)。然后,程序尝试使用 de Jaeger 公式找
我正在编写一个算法,该算法对一个整数数组从末尾到开头执行一个大循环,其中包含一个 if 条件。第一次条件为假时,循环可以终止。 因此,对于 for 循环,如果条件为假,它会继续迭代并进行简单的变量更改
现在我已经习惯了在内存非常有限的情况下进行编程,但我没有答案的一个问题是:哪个内存效率更高;- for(;;) 或 while() ?还是它们可以平等互换?如果有的话,还要对效率问题发表评论! 最佳答
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 8 年前。 我正在尝试创建一个小程序,我可以在其中读取该程序的单词。如果单词有 6
这个问题在这里已经有了答案: python : list index out of range error while iteratively popping elements (12 个答案) 关
我正在尝试向用户请求 4 到 10 之间的整数。如果他们回答超出该范围,它将进入循环。当用户第一次正确输入数字时,它不会中断并继续执行 else 语句。如果用户在 else 语句中正确输入数字,它将正
我尝试创建一个带有嵌套 foreach 循环的列表。第一个循环是循环一些数字,第二个循环是循环日期。我想给一个日期写一个数字。所以还有另一个功能来检查它。但结果是数字多次写入日期。 Out 是这样的:
我想要做的事情是使用循环创建一个数组,然后在另一个类中调用该数组,这不会做,也可能永远不会做。解决这个问题最好的方法是什么?我已经寻找了所有解决方案,但它们无法编译。感谢您的帮助。 import ja
我尝试创建一个带有嵌套 foreach 循环的列表。第一个循环是循环一些数字,第二个循环是循环日期。我想给一个日期写一个数字。所以还有另一个功能来检查它。但结果是数字多次写入日期。 Out 是这样的:
我正在模拟一家快餐店三个多小时。这三个小时分为 18 个间隔,每个间隔 600 秒。每个间隔都会输出有关这 600 秒内发生的情况的统计信息。 我原来的结构是这样的: int i; for (i=0;
这个问题已经有答案了: IE8 for...in enumerator (3 个回答) How do I check if an object has a specific property in J
哪个对性能更好?这可能与其他编程语言不一致,所以如果它们不同,或者如果你能用你对特定语言的知识回答我的问题,请解释。 我将使用 c++ 作为示例,但我想知道它在 java、c 或任何其他主流语言中的工
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是 C 编程和编写代码的新手,以确定 M 测试用例的质因数分解。如果我一次只扫描一次,该功能本身就可以工作,但是当我尝试执行 M 次时却惨遭失败。 我不知道为什么 scanf() 循环有问题。 in
这个问题已经有答案了: JavaScript by reference vs. by value [duplicate] (4 个回答) 已关闭 3 年前。 我在使用 TSlint 时遇到问题,并且理
我尝试在下面的代码中添加 foreach 或 for 循环,以便为 Charts.js 创建多个数据集。这将允许我在此折线图上创建多条线。 我有一个 PHP 对象,我可以对其进行编码以稍后填充变量,但
我是一名优秀的程序员,十分优秀!