简体   繁体   English

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

[英]Designing databases: storing user stats in games(i.e. owned money) and 32bit integer overflow

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). 我正在创建一个小型的,基于数据库的基于浏览器的游戏,但我偶然发现了这个问题:我将用户拥有的钱存储为32位整数字段(准确地说是两个字段。一个存储在玩家手中,另一个存储在玩家手中)银行)。 We all know, that maximum value, which can be stored in 32 bits is 2^32-1. 众所周知,可以存储在32位中的最大值是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. 我绝对确定,95%的玩家将无法达到上限-但另一方面(在今天进行了一些计算之后),好的玩家将能够积聚这么多。

Having that in mind I came with the following ideas: 考虑到这一点,我提出了以下想法:

  • store money in 64bits, which doubles space of each record. 以64位存储金钱,这会使每条记录的空间增加一倍。
  • 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? 你会去哪一个?

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

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? 有5,000个用户在玩您的游戏吗? Changing from 32-bits to 64-bits will consume roughly 20k extra. 从32位更改为64位将额外消耗大约20k。 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. 最好的答案可能来自熟悉银行如何处理此类情况的人,尽管他们的要求可能比您需要的要复杂得多。

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? 但是认真的说是4,294,967,296卢比/西蒙/弗隆吗? Who are they? 他们是谁? Sim Gates? 辛·盖茨?

Why not store money the way it should be stored, as a Money data type? 为什么不以Money数据类型的方式存储货币呢? This is assume of course you are using SQL Server. 当然,这是假定您正在使用SQL Server。 The money data type won't have this limitation and won't be affected by rounding issues. money数据类型将没有此限制,并且不会受到四舍五入问题的影响。

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

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