【发布时间】:2021-08-30 06:36:40
【问题描述】:
我正在制作一个基于网络的小井字游戏,作为一个项目展示在我的投资组合网站上。我希望访问该网站的人能够创建游戏,然后另一个玩家加入该游戏。
创建游戏的人在我的 Firestore 数据库中的“游戏”集合下创建一个文档,该集合由该接口定义:
type Player = 'x' | 'o'
type GameFieldValue = null | Player;
type Coordinate = [number, number]
type Row = [GameFieldValue, GameFieldValue, GameFieldValue];
interface IGameDoc {
playerTurn: Player;
playerTwoUid: string | null
gameState: {
rowOne: Row
rowTwo: Row
rowThree: Row
};
}
文档与创建游戏的用户的 ID 一起存储,例如
因此,我的 firebase firestore 中创建游戏的规则就是您需要登录(我只是在有人访问该网站时匿名登录)
rules_version = '2';
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
match /databases/{database}/documents {
match /Games/{gameId} {
allow read, write: if request.auth != null;
}
}
}
现在玩家可以将 gameId 发送给其他可以加入游戏的人。然后两个玩家可以监听游戏状态的实时更新并相应地更新 UI。
现在我的问题是弄清楚如何让第二个玩家加入游戏。
第二个玩家最初需要能够更新文档并将他的 uid 插入到playerTwo 字段中。在后续请求更新文档时,应该只允许创建游戏的玩家以及第二个玩家这样做。
我希望我在这里表达清楚了。
谁能帮助我如何构建我的 Firestore 规则来实现这一点?
【问题讨论】:
标签: reactjs firebase google-cloud-firestore firebase-security