首页 > 解决方案 > React Native 游戏、MongoDB 或 Firestore

问题描述

我正在为安卓开发一款多人反应原生游戏,它只是一个文本游戏。目前,我一直只在客户端工作,但我正在慢慢达到需要选择一种管理数据的方式的地步。因为我一直在尝试学习新东西——也是第一次使用 RN——所以我遇到了 Firestore,我很好奇它是否是我项目中的一个可行选择。

例如,在这个游戏中,您可以购买/开设公司。如果我要在 nodejs + MongoDB 中处理此操作,我会将 JWT 令牌发送到server/api/newcompany.

假设我有 2 个 MongoDB 模型,Users其中包含所有用户信息和Company游戏中所有公司的列表。首先,我会检查用户是否有足够的钱来开设公司,然后基于此返回一些消息,我的代码将是这样的:

// routes.js
...
router.post('/api/newcompany',AuthCheck, Company.new)


// controllers/company.js
...
new: async function (req, res, next) {
    if (!req.body.companyType || !validCompanyType) {
        return res.status(200).send('invalid/missing message and other failed checks')
    }
    //assuming token data would be passed from auth middleware and would have company prices somewhere
    const user =  await Users.find({id:token.userId})
    if (user.money < companyPrice) {
        res.status(200).send('no money')
    } else {
        const newCompany = new Company()
        newCompany.name = req.body.name
        ...
        new.Companny.save(function(){
            res.status(200).send('all good')
        })

    }
}

查看 Firestore,我将拥有与上述相同的 2 个文档。Users带有由其 ID 和文档标识的子文档(用户)列表Companies

我可以进行与上述相同的用户检查吗?我将如何处理?我会使用 Firestore 规则来检查用户是否有足够的钱,还是会使用我的服务器和管理 SDK 作为中介来处理更复杂的操作并使用直接 Firestore 连接来检索数据?

谢谢你的时间。

标签: mongodbfirebasereact-nativefirebase-realtime-databasegoogle-cloud-firestore

解决方案


推荐阅读