Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为所有用户每秒运行一个PHP脚本_Php_Mysql - Fatal编程技术网

为所有用户每秒运行一个PHP脚本

为所有用户每秒运行一个PHP脚本,php,mysql,Php,Mysql,我知道有好几个关于这个题目的问题,但到目前为止似乎没有一个对我有帮助 我正在用jQuery/Ajax/PHP制作一个浏览器游戏,我想创建一个NPC,它有一个循环的特定路径查找(例如,5步向下、5步向右、5步向上和5步向左)。但是任何连接的玩家都应该随时看到NPC的当前位置,这就是为什么我需要在数据库中存储NPC的当前X和Y位置。全国人大每秒钟都会迈出一步 我试过这样的方法: <?php /* Here I have a string like this : "dddddrrrrruuuuu

我知道有好几个关于这个题目的问题,但到目前为止似乎没有一个对我有帮助

我正在用jQuery/Ajax/PHP制作一个浏览器游戏,我想创建一个NPC,它有一个循环的特定路径查找(例如,5步向下、5步向右、5步向上和5步向左)。但是任何连接的玩家都应该随时看到NPC的当前位置,这就是为什么我需要在数据库中存储NPC的当前X和Y位置。全国人大每秒钟都会迈出一步

我试过这样的方法:

<?php
/* Here I have a string like this : "dddddrrrrruuuuulllll" where "d" : down,
"r" : right, etc. And I check the characters of this string one after
another every second this script is called in index.php */   

// I store the new NPC position in the database :

$newNPC = $db->prepare('update npc set x = :x, y = :y, map = :map where id = :id');
$newNPC->execute(array(
        'x' => $posX,
        'y' => $posY,
        'map' => $map,
        'id' => $row['id']
        ));

// and here I just "echo" the image of the NPC with its new X and Y positions, 
// which I append to $('.map') in the index.php code
index.php:

function loopNPC(){

    $(document).load('getNPC.php', function(npc){
        $('.npc').remove();       // I remove the NPC's image with previous position
        $('.map').append(npc);    // I add the new one
    });

}

setInterval(loopNPC, 1000);
在getNPC.php文件中,我有如下内容:

<?php
/* Here I have a string like this : "dddddrrrrruuuuulllll" where "d" : down,
"r" : right, etc. And I check the characters of this string one after
another every second this script is called in index.php */   

// I store the new NPC position in the database :

$newNPC = $db->prepare('update npc set x = :x, y = :y, map = :map where id = :id');
$newNPC->execute(array(
        'x' => $posX,
        'y' => $posY,
        'map' => $map,
        'id' => $row['id']
        ));

// and here I just "echo" the image of the NPC with its new X and Y positions, 
// which I append to $('.map') in the index.php code

对于PHP的请求/响应方式,您试图做的事情不起作用

您应该考虑其他选项,要么在服务器端运行PHP CLI应用程序,要么使用“NodeJS”之类的“游戏服务器”的其他选项,也可以考虑WebSoCube用于通信而不是正常的GET/POST请求。


Websockets允许您在客户端和服务器之间保持双向通信,然后实际的服务器端应用程序将允许您保持这些通信并对其进行操作。

当前代码“按请求执行操作”;没有任何东西与时间有明确的联系。如果您确实想这样做(我不会使用SQL,因为在使用持久化服务时不需要SQL),那么请运行一个持久化的[PHP]“游戏引擎”服务,该服务每秒执行一次操作(例如,使用循环和计时),不管有多少请求,一个PHP网站将代理这个始终运行/始终更新的服务。也可能值得研究Web套接字或长轮询/COMET。要扩展@user2864740的观点,如果没有玩家连接,世界会冻结吗?游戏引擎有非常特定的需求(资源、调度、备份状态等),这些需求不能很好地转化为web架构。当然,客户机-服务器模式有利于连接客户机,因此最好的方法是混合模式。一个长期运行的服务器端进程,它维护游戏状态和查询当前状态并将其显示给用户的网站。(为了帮助游戏服务本身,考虑类似或类似的事情。例如,定时事件处理和处理来自前端的请求。)有了这种方法,这种行为是不可能实现的,或者至少,如果不是噩梦,是的!谢谢,我用PHPCLI实现了这一点(只是一个运行脚本的.bat文件,我在其中添加了一个while(1){…}循环)。