设计数据库:存储游戏中的用户统计信息(即拥有的资金)和 32 位整数溢出

发布于 2024-08-17 18:15:07 字数 421 浏览 3 评论 0原文

我正在创建一个小爱好数据库驱动的基于浏览器的游戏,我偶然发现了这个问题:我将用户拥有的钱存储为 32 位整数字段(准确地说:两个字段。一个将钱存储在玩家手中,另一个将钱存储在银行)。我们都知道,32位可以存储的最大值是2^32-1。

我绝对确定,95% 的玩家将无法达到上限 - 但另一方面(在今天做了一些计算之后)优秀的玩家将能够积累那么多。

考虑到这一点,我提出了以下想法:

  • 将钱存储在 64 位中,这使得每条记录的空间加倍。
  • 将钱存储为字符串并在运行时与 long long 相互转换。
  • 改变游戏机制,这样玩家将无法获得那么多的财富。

我知道可达到的上限对于某些玩家来说是相当有限的,所以对我来说,第三个选项是建议的选项中最差的。

还有其他方法可以处理此类问题吗?您会选择哪一个?

I am creating an little hobby database driven browser based game and I stumbled across this problem: I store money owned by users as an 32bit integer field (to be precise: two fields. One stores money in players hand, the other - money stored in bank). We all know, that maximum value, which can be stored in 32 bits is 2^32-1.

I am absolutelly sure, that 95% of players will not be able to reach the upper limit - but on the other hand (and after doing some calculations today) good players will be able to accumulate that much.

Having that in mind I came with the following ideas:

  • store money in 64bits, which doubles space of each record.
  • store money as string and convert to/from long long in the runtime.
  • change game mechanics so players will not be able to gain that amount of wealth.

I know that existence of reachable upper limit is rather limiting for some players, so for me the third option is worst from the proposed ones.

Are there any other ways of dealing with this kind of problems? Which one would You go for?

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

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

发布评论

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

评论(4

乱了心跳 2024-08-24 18:15:07

以现实世界为例,为什么不使用不同类型的硬币,例如一列可容纳一百万单位的货币。

Taking an example from the real world, why not have different types of coins e.g a column for a million units of the currency.

夜灵血窟げ 2024-08-24 18:15:07

更改为更大的数据类型可能是最简单的解决方案,并且除非您的游戏规模很大,否则磁盘空间/内存的考虑因素可能并不重要。有 5,000 位用户在玩您的游戏吗?从 32 位更改为 64 位将额外消耗大约 20k。这还不足以让人失眠。

最好的答案可能来自熟悉银行如何处理此类情况的人,尽管他们的要求可能比您需要的复杂得多。

Changing to a larger datatype is likely the easiest solution and considerations of disk space/memory aren't likely to be significant unless your game is huge in scale. Have 5,000 users playing your game? Changing from 32-bits to 64-bits will consume roughly 20k extra. That's not enough to lose any sleep over.

The best answer would likely come from someone familiar with how banks handle these types of situations, though their requirements may be far more complicated than what you need.

橘亓 2024-08-24 18:15:07

内存空间应该不是问题,具体取决于同时拥有的玩家数量,但存储为字符串肯定会使用更多磁盘空间。

但说真的,4 294 967 296 卢比/模拟币/弗隆?他们是谁?西姆·盖茨?

Space on memory shouldn't be a problem depending on the amount of players you'll have simultaneously, but storing as string will definitely use more disk space.

But seriously, 4 294 967 296 rupees/simoleons/furlongs? Who are they? Sim Gates?

躲猫猫 2024-08-24 18:15:07

为什么不以货币数据类型的形式存储货币呢?当然,这是假设您使用的是 SQL Server。货币数据类型不会有此限制,也不会受到舍入问题的影响。

Why not store money the way it should be stored, as a Money data type? This is assume of course you are using SQL Server. The money data type won't have this limitation and won't be affected by rounding issues.

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