0

我是制作游戏的新手,我正在使用 php 和 javascript 编写一个有趣的游戏。我正在使用 MySQL 来存储游戏用户的所有信息。游戏有点像管理一支运动队。你有一些变量(现金、资产、球员、员工等),你承担了体育经理的所有角色。我知道它存在,这只是个人挑战。

我的问题是,从数据库中获取信息到游戏中的最佳和最有效的方法是什么?1. 我必须在每个页面上运行一个 sql 查询吗?2. 每次更新某些内容时,我是否必须更新我的数据库?3.是否可以在用户登录时从数据库中获取所有信息,让他/她玩,然后仅在会话终止时使用新信息更新数据库?

抱歉缺少代码,只是寻找一个起点,因为在我开始编写大量游戏之前了解这一点对我很有帮助。

谢谢

4

1 回答 1

1
  1. No, you don't necessarily have to run a MySQL query on every page load. You could store the results of such queries in a cache system such as memcached, or keep necessary data in $_SESSION.
  2. No, you can use similar workarounds as before, but if the user disconnects you may end up with unsaved changes.
  3. Well, you could load the data relevant to the user and write your own session handler for saving the data when the session is destroyed, but although I haven't ever tried it I would say there's a very real risk of losing data if, for example, your server is restarted or PHP's garbage collector callback is not called for some reason.

Overall, I think you may perceive SQL queries as much heavier than they actually are. If your database structure and indexes are set up correctly, your queries and updates shouldn't take longer than about 0.01 seconds each to complete.

于 2013-07-14T21:30:15.987 回答