简体   繁体   English

特定时间后(用户不同)如何采取措施?

[英]How to take an action after certain time (different from user to user)?

I'm developing a web game (js php mysql) in which one clicks a button to start an action that takes time to complete (let's say 10 hours) and when it finishes some points are added to that player's total.. The problem is that I need those points to be added even if the player is not online at the time the action finishes.. for example I need to have the rankings updated, or an email sent to the player.. 我正在开发一个网络游戏(js php mysql),在该游戏中,单击一个按钮即可开始,该操作需要一些时间才能完成(例如10个小时),完成时会将一些积分添加到该玩家的总数中。问题是即使动作完成时玩家不在线上,我也需要添加这些分数。例如,我需要更新排名,或向玩家发送电子邮件。

I thought about a cron job checking constantly for ending actions, but I think that would kill the resources (contantly checking against actions of thousands of players..). 我想到了一个cron作业,要不断检查结束动作,但是我认为这会浪费资源(不断检查成千上万玩家的动作..)。

Is there a better solution to this problem? 有没有更好的解决方案来解决这个问题?

Thanks for your attention!! 感谢您的关注!!

You can just write into your database when it's finished and when the user logs in you add the earned points to his account. 您只需在数据库完成后以及用户登录时将其写入数据库,即可将赚取的积分添加到他的帐户中。 You can also check with a cronjob. 您也可以通过cronjob进行检查。 Even if you have millions of user this will not kill your server. 即使你有几百万的用户,这将不会杀了你的服务器。

Cron is perfect for this. Cron对此非常适合。 You could write your tasks in stored procedures, then have cron run an SQL script to call the stored procedure that would update the records of your players. 您可以在存储过程中编写任务,然后让cron运行SQL脚本来调用存储过程,这将更新播放器的记录。

Databases are designed to work with thousands and millions of pieces of information efficiently, so I don't think the idea that it will kill system resources is a valid one unless you hosting system is really constrained already. 数据库被设计为有效地处理成千上万条信息,因此,除非托管系统已经真正受到限制,否则我认为数据库将杀死系统资源的想法并不成立。

If you want to be safe against cheating you need to do the checking on the server anyway. 如果您想防止作弊,则无论如何都需要在服务器上进行检查。 If the "waiting" will happen within a Javascript on the client, one could easily decrease the remaing time. 如果“等待”将在客户端的Javascript中发生,则可以轻松地减少剩余时间。

So you need to send the job to the server (which is assumed to be safe against clock modifications) and the server will determine the end timestamp. 因此,您需要将作业发送到服务器(假定可以安全地进行时钟修改),然后服务器将确定结束时间戳记。 You could store your jobs in a queue. 您可以将作业存储在队列中。

If you only need this information for the user himself you can just look at the queue when the user logs in. Otherwise run a cron job every minute (or so). 如果您只需要用户自己使用此信息,则只需在用户登录时查看队列即可。否则,每分钟(或大约)运行一次cron作业。 This job will mark all jobs finished when their timestamp is in the past (and remove them from the database). 该作业将所有时间戳记为过去的作业标记为已完成(并将其从数据库中删除)。

If you need more precise checking you will need to come up with an alternative server side solution that is doing this more often (eg a simple program polling the database every few seconds). 如果需要更精确的检查,则需要提出一种替代的服务器端解决方案,该解决方案需要更频繁地执行此操作(例如,一个简单的程序每隔几秒钟轮询一次数据库)。

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

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