我在做游戏。游戏的一部分是收集元素。您获得“多样性”的分数,因此类别中的每个对象都具有属性。如果您收集了具有特定属性的对象,则该属性的乘数将不再应用于具有该属性的任何其他对象。这些分数是通过组合您收集的每个属性的所有唯一值,然后乘以该属性的系数来计算的。例如:
Points
Category: Toys
Base: 2
Shape: 7
Material: 10
Color: 5
Bouncy ball: Snow Globe:
category: sphere category: sphere
shape: round shape: round
material: rubber material: glass
color: swirls color: clear
Collected:
bases = my_objects.length * base = 2 * 2 = 4
shapes = array('round').length * 7
materials = array('rubber', 'glass') * 10
color = array('swirls', 'red') * 5
score = bases + shapes + materials + color = 4 + 7 + 20 + 10 = 41
对象存储为 nosql 文档 (mongodb)。我的问题是,从数据库中选择所有剩余对象的最佳方法是什么,按照未收集的对象将最多的分数添加到用户分数的顺序。
最佳答案
将总分存储在文档中,然后您可以按降序排序(如果需要,可以使用索引)
您还不能在 mongo 中“按 a+b+c 排序”,所以这可能是最好的
关于用于对象收集游戏的 MongoDB 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805224/