这是否足够,或者我有竞争条件吗?

发布于 2024-09-15 10:29:30 字数 551 浏览 5 评论 0原文

我正在为网络编写一种策略型多用户游戏。它有一个游戏场(X × Y 方块),我计划将其序列化并存储在 MySQL(innodb)数据库中的 BLOB 中,每个正在进行的游戏一行。

我现在尝试找出一种好方法,使数据库随着游戏场的任何更改而更新,同时找到一种方便的解决方案来处理加载页面和实际之间的时间范围内发生在游戏场上的事情采取行动。
我不使用 AJAX。

每场比赛最多有20名玩家,每个玩家在24小时内走1到10步,所以这是一场“慢”游戏。

我的计划(到目前为止)是在 blob 旁边存储一种游戏场的校验和,并在尝试对游戏场进行更改之前将数据库状态与加载的状态进行比较。

我担心的是如何防止竞争条件。
是否足以:

  1. 开始交易。
  2. 如果校验和不同,则从表中加载比赛场
  3. - 如果校验和未更改,则回滚并更新用户视图
  4. - 更新表并提交更改

BEGIN TRANSACTION 是否足以阻止比赛,或者我需要在步骤中做更多事情2 显示我更新表格的意图?

感谢所有的建议。

I'm writing a strategy-kind of multi user game for the web. It has a playfield (X by Y squares) that I plan on serialize and store in a BLOB in a MySQL (innodb) database, one row for each ongoing game.

I now try to figure out a good way of keeping the database updated with any changes to the playfield, and at the same time finding a convenient solution to how to handle things that happen to the playfield in the time frame between loading the page and actually making a move.
I don't use AJAX.

There will be at most 20 players in each game, each player making between 1 and 10 moves in 24 hours, so it is a "slow" game.

My plan (so far) is to also store a kind of checksum for the playfield next to the blob and compare the databases state with the state loaded before trying to make changes to the playfield.

What I worry about is how to prevent race conditions.
Is it enough to:

  1. Begin transaction.
  2. load playfield from table
  3. if checksum differs - rollback and update the users view
  4. if checksum unchanged - update table and commit changes

Is the BEGIN TRANSACTION enough to block the race, or do I need to do something more in step 2 to show my intent to update the table?

Thankful for all advice.

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

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

发布评论

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

评论(2

大姐,你呐 2024-09-22 10:29:30

如果您使用 SELECT ... FOR UPDATE 当您从数据库加载游戏场时,它将阻止其他选择,直到您提交或回滚事务。

If you use SELECT ... FOR UPDATE when you load the playfield from the database, it will block other selects until you commit or rollback the transaction.

街角卖回忆 2024-09-22 10:29:30

不可以。您需要对需要防止更新冲突的表发出 LOCK TABLES 命令。这看起来像...

LOCK TABLE my_table WRITE;

更多详细信息可以在这里找到... http://dev.mysql.com/doc/refman/5.1/en/lock-tables.html

不要忘记之后解锁它们!

No. You will need to issue a LOCK TABLES command for the tables you need to protect against conflicting updates. This would look something like...

LOCK TABLE my_table WRITE;

More details may be found here... http://dev.mysql.com/doc/refman/5.1/en/lock-tables.html

Don't forget to UNLOCK them afterwards!

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