Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase Firestore多人游戏配对_Android_Firebase_Kotlin_Google Cloud Firestore - Fatal编程技术网

Android Firebase Firestore多人游戏配对

Android Firebase Firestore多人游戏配对,android,firebase,kotlin,google-cloud-firestore,Android,Firebase,Kotlin,Google Cloud Firestore,所以我想用Firebase Firestore制作一个多人游戏,包括两名玩家配对。我有一个游戏对象的集合,每个游戏都有一个创建者和加入者字段(只是用户的UID)。我正试图加入创建的游戏,并与之斗争。这是我目前拥有的 fun findOpenGame(onComplete: (gameFound: Boolean, gameObject: Game?, gameRef: DocumentReference?) -> Unit) { gamesCollectionRef

所以我想用Firebase Firestore制作一个多人游戏,包括两名玩家配对。我有一个游戏对象的集合,每个游戏都有一个创建者和加入者字段(只是用户的UID)。我正试图加入创建的游戏,并与之斗争。这是我目前拥有的

 fun findOpenGame(onComplete: (gameFound: Boolean, gameObject: Game?, gameRef: DocumentReference?) -> Unit) {
        gamesCollectionRef
                .whereEqualTo("joiner", null)
                .get()
                .addOnCompleteListener { task ->
                    if (task.isSuccessful) {
                        for (document: DocumentSnapshot in task.result) {
                            val game: Game = document.toObject(Game::class.java)!!
                            if (game.joiner == null) {
                                document.reference.update("joiner", FirebaseAuth.getInstance().uid)
                                        .addOnSuccessListener {
                                            onComplete(true, game, document.reference)
                                        }
                                        .addOnFailureListener {
                                            Log.e(FirestoreUtil.javaClass.simpleName, "Couldn't connect to game")
                                        }
                            }
                        }

                    } else {
                        //no games to join
                        onComplete(false, null, null)
                    }
                }
    }

顺便说一句,我对科特林很陌生,所以如果我遗漏了一些明显的东西,请随时告诉我。我还必须处理多个用户试图同时加入同一场比赛并把事情搞砸的问题。任何建议都有帮助:)

你说你想“加入创建的游戏”。什么意思?您还可以添加数据库结构以便更好地理解吗?如果没有打开的游戏,则用户将创建新的游戏对象并将其上载到游戏集合。每个文档(游戏)都有一个创建者和一个加入者。我的整个数据库由一个游戏集合和一个用户集合组成。