如何实时更新浏览器游戏属性,如 Travian 游戏中的 4 个资源?

发布于 2024-08-18 12:14:29 字数 388 浏览 6 评论 0原文

我想制作一款类似 Travian(或类似 Ikariam)的网络游戏。游戏将以 PHP 和 PHP 语言编写。基于MySQL。我想知道如何实现游戏属性的实时更新。

对于前端,我可以通过使用 AJAX 调用(从数据库获取最新值),甚至伪造值更新(不与服务器通信)来实现。

对于后端,这是由 PHP cron 作业(每隔几秒运行一次)完成的吗?如果是的话,有人可以给我一些示例代码吗?

顺便说一句,我知道如果我使用 IIS + FastCGI 会很麻烦。

===版本信息===

PHP:5.2.3

IIS:带有 FastCGI 的 6.0

操作系统:Windows Server 2003 Standard R2

I would like to make a web-based game which is Travian-like (or Ikariam-like). The game will be in PHP & MySQL-based. I wonder how can I achieve the live updating of game attributes.

For frontend, I can achieve by using AJAX calls (fetch the latest values from database), or even fake update of values (not communicated with server).

For backend, is this done by a PHP cron job (which runs every few seconds)? If so, can anyone provide me some sample codes?

by the way, I know it would be a trouble if I use IIS + FastCGI.

=== Version Information ===

PHP : 5.2.3

IIS : 6.0 with FastCGI

OS : Windows Server 2003 Standard R2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

银河中√捞星星 2024-08-25 12:14:29

正确的答案取决于您的具体需求。

每个人总是以相同的速度获得资源吗?如果是这样,一个简单的解决方案是跟踪用户存在的时间,根据他们获得的速率计算资源量,并减去他们总共花费的资源数。但是,如果速率可能发生变化,这将是一个问题,因此,如果您使用此解决方案,您几乎会坚持您选择的速率,除非您完全重写处理(例如下面的处理) 。

如果人们获取资源的速度发生变化,您需要定期更新数据。 cronjob/计划任务可以很好地确保每个人都得到更新,但在某些情况下,最好简单地测量自从更新每个用户的资源以来已经过去了多长时间,然后在他们进行的每个页面加载时更新它们登录时,将他们离开的时间乘以他们获得资源的速率 - 这样,您就可以避免更新,直到您真正需要新值为止。

The correct answer depends on your exact needs.

Does everyone always get resources at the same rate? If so, a simple solution is to track how long their user has existed, calculate the amount of resources based on the rate they're getting, and subtract the number of resources they've spent in total. That's going to be a bit of a problem if the rate can ever change, though, so if you use this solution, you're pretty much stuck with the rate you pick unless you rewrite the handling entirely (for example to the one below).

If it varies how quickly people can get resources, you'll need to update the data periodically. A cronjob/scheduled task would work well to make sure everyone is updated, but in some situations, it might be better to simply measure how long it's been since you've updated each user's resources, and then update them on every page load they make while logged in by multiplying the time they've been away by the rate at which they gain resources - that way, you avoid updating until you actually need the new value.

套路撩心 2024-08-25 12:14:29

对于类似 Travian 的资源管理,您需要跟踪上次更新用户资源的时间。如果您读取资源值(用于页面刷新或其他操作),则需要添加自“上次更新时间”以来获得的资源量(取决于用户获得的资源字段和boni数量)并将该值发送到浏览器。您还可以让浏览器脚本计算这些金额。

您可能会考虑以某种方式缓存所有资源量,因为这些值需要很多,从而改善与数据库的通信。

如果用户完成资源字段的构建、使用市场、构建结构等,您需要更新资源量(以及“上次更新时间”),因为您无法简单地跟踪此类事件。

通过计算资源,可以减少数据库负载,因为您不需要每次用户刷新浏览器页面时都写入新值。它也更准确,因为舍入误差更少。

为了保持页面刷新之间资源的增加,您需要一种弗兰克·法默(Frank Farmer)所描述的方法。只需将资源量和“增益频率”嵌入到一些 JavaScript 中,并将每个“增益频率”的资源量增加 1 即可。

For a Travian like resource management you need to keep track when you updated the users resources for the last time. If you read the resource values (for a page refresh or something), you need to add the amount of resources gained since the 'last update time' (depending on the amount of resources fields and boni the user gets) and send that value to the browser. You could also the let browser script calculate these amounts.

You might to consider caching all resource amounts somehow, since these values are required a lot, improving the communication with your database.

If a user finishes building a resource field, uses the market, builds a structure, etc you need to update the amount of resources (and the 'last update time'), because you cannot keep track on these kind of events simply.

By calculating the resources the database load is reduced, since you do not need to write the new values every time when the user refreshes the browser page. It is also more accurate since you have less rounding errors.

To keep the resources increasing between page refreshes you need a method as Frank Farmer described. Just embed the resource amount and the 'gain frequency' in some javascript and increase the resource amount every 'gain frequency' by one.

戴着白色围巾的女孩 2024-08-25 12:14:29

您还可以在每次页面或 JavaScript 请求时计算资源。您需要存储上次更新时间。

You can also calculate the ressources each time a page or the javascript asks. You'd need to store the last updated time.

青衫负雪 2024-08-25 12:14:29

这可能是一篇旧帖子,但它立即出现在谷歌中,所以这里有另一个选项,这就是我一直在开发的游戏的做法。

我使用客户端 JavaScript,它使用闪存套接字从主机上运行的专用游戏服务器获取实时更新。

我使用 http://devpro.it/xmlsocket/ 中的 xmlsocket 套件

It may be an old post but it comes up right away in Google so here's another option which is how the game I've been developing does it.

I use a client side JavaScript that uses a flash socket to get live updates from a dedicated game server running on the host.

I use the xmlsocket kit from http://devpro.it/xmlsocket/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文