用于对象收集游戏的 MongoDB 算法

标签 mongodb algorithm sorting nosql

我在做游戏。游戏的一部分是收集元素。您获得“多样性”的分数,因此类别中的每个对象都具有属性。如果您收集了具有特定属性的对象,则该属性的乘数将不再应用于具有该属性的任何其他对象。这些分数是通过组合您收集的每个属性的所有唯一值,然后乘以该属性的系数来计算的。例如:

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/

相关文章:

c# - 在 MongoDB 中计数不正确

node.js - mongoose.connection.collections.collection.drop() 每隔一段时间就会抛出错误

mongodb - 如何在 cursor.ForEach 中打印文档?

algorithm - 证明遍历 k-ary 树两次产生直径

node.js - Mongoose:错误找不到模块调试

php - 确定变量是否是 PHP 中一组变量中最小的

algorithm - 我如何解决这个动态规划问题?

linux - Unix 命令 "uniq"和 "sort"

php - php 中不同类型的不同排序

c++ - 为什么我的程序无法对整数字符串进行排序?