简体   繁体   English

PHP:在给定时间后执行操作

[英]PHP: execute an action after a given time

Good morning/afternoon/evening, Internet. 早上好/下午/晚上,上网。 I am currently developing a wap game and have encountered a problem: applying long-term effects on other players. 我目前正在开发wap游戏,遇到了一个问题:对其他玩家产生长期影响。 Let me explain. 让我解释。

If a user uses an 'instant' ability, it is just a normal command link that does executeAbility() ; 如果用户使用“即时”功能,则只是执行executeAbility()的普通命令链接; If a user casts a spell, the script does the following: 如果用户使用咒语,脚本将执行以下操作:

setPlayerState('casting');
sleep($cast_time);
executeAbility();

This is but a simple part, though. 不过,这只是一个简单的部分。 Now if we go to some more advanced techiques, like putting a curse on a player, I get lost. 现在,如果我们使用一些更高级的技术,例如对玩家施加诅咒,我会迷失方向。 I've already considered the following possibility: 我已经考虑过以下可能性:

$time=$_SERVER['REQUEST_TIME'];
$time_when_the_curse_finishes=$time+$curseDuration;
updateEnemyCurses($curseID,$endTime);
if ($time<$endTime) removeCurse();

This might look pretty and all, but it doesn't take two things into consideration: if the user logged off and logged back in in 2 hours, he would spend the whole two hours cursed and this method cannot support effect every x seconds for y seconds kind of curses. 这看起来很不错,但是并没有考虑两点:如果用户注销并在2小时后重新登录,他将花费整整两个小时, 该方法无法支持y的每x秒生效秒种诅咒。 For example: 例如:

Curse One. 诅咒一。 You take 1 damage every 2 seconds for 10 seconds. 每2秒受到1点伤害,持续10秒。

To summarise, I simply require a tip or two how I could make the line above possible. 总而言之,我只需要提示一两个即可使以上内容成为可能的技巧。 The curse itself would need to have its time checked globally (not with page reloads) and be able to complete queries (in this case damage ticks) every x seconds until it is removed in 10 seconds. 诅咒本身将需要全局检查其时间(而不是重新加载页面),并能够每x秒完成一次查询(在这种情况下为损坏滴答声),直到在10秒内将其删除。

I am thankful in advance for any tips offered here! 在此先感谢您提供的任何提示!

You might want to look into setting up a cron script for this type of "maintenance" work. 您可能需要研究为此类“维护”工作设置cron脚本。

It could run at a fixed interval (ie. every second) and handle curse etc. effects for all players 它可以以固定的间隔(即每秒)运行,并为所有玩家处理诅咒等效果

Start here :) http://en.wikipedia.org/wiki/Cron 从这里开始:) http://en.wikipedia.org/wiki/Cron

Edit : As the others have mentioned (@mark-b), it's best to clarify: Use a cronjob for a single script that manages all of the work, not multiple cronjobs for each piece of work. 编辑 :正如其他人提到的(@ mark-b)一样,最好澄清一下:对管理所有工作的单个脚本使用cronjob,而不对每个工作使用多个cronjob。

I fully understand your problem because here at work I had to deal with a feature that required a solution that might help you. 我完全理解您的问题,因为在这里我必须处理一项功能,该功能需要可能对您有帮助的解决方案。 What I did was setup a db table that stored all the required/pending actions and the owner of the action. 我所做的是设置了一个数据库表,该表存储了所有必需的/待处理的操作以及该操作的所有者。 This table had a timestamp and under a certain interval I checked what actions were required to perform for this user. 该表具有时间戳,在一定时间间隔内,我检查了对此用户执行哪些操作。 Other users checked also that table and removed past actions that were not retrieved (ie the owner of that action was logged out). 其他用户也检查了该表并删除了未检索的过去操作(即该操作的所有者已注销)。 The problem here was what to do if no users were logged at all and what I did was that when a user logs in, it checks the pending actions table first and cleans it if needed. 这里的问题是,如果根本没有用户登录,该怎么办?我要做的是,当用户登录时,它首先检查挂起的操作表,并在需要时进行清理。 I hope I can give you some insight. 希望我能给您一些见识。 Good Luck! 祝好运!

You'd be best to implement an event queue in a database. 最好在数据库中实现事件队列。 Rather than creating a cron job for every action that needs to be done on a user or by a user, you just add an entry to the DB job queue. 只需为数据库作业队列添加一个条目,而不是为需要对用户或用户执行的每个操作创建cron作业。

Then a SINGLE cron job can pull jobs out of that queue and process them. 然后,单个cron作业可以将作业从该队列中拉出并进行处理。 So for your 'curse' event, you'd set the curse flag on the user, and schedule a job to be executed at now + 5 minutes saying "remove curse X from user Y". 因此,对于您的“诅咒”事件,您将在用户上设置诅咒标志,并安排一个now + 5 minutes要执行的作业now + 5 minutes说“从用户Y删除诅咒X”。

The cron job would fire up periodically (at whatever rate a 'tick' is in your game, perhaps) and request the list of jobs that need to be executed in that particular time period. cron作业将定期启动(也许以某种“滴答声”打在您的游戏中),并请求在该特定时间段内需要执行的作业列表。

Do you know gearman? 你知道齿轮工吗? I think, you have to create asynchronous jobs. 我认为,您必须创建异步作业。

look at: http://php.net/manual/en/book.gearman.php 查看: http : //php.net/manual/zh/book.gearman.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM