I'm developing an flutter firestore app. Every user has a username. If the user press a button he should get returned another random online username. That other username is needed for further steps(that part is already working). After the two users connected, they shouldn't be no longer available.
I have a solution, but it isn't very satisfying:
For finding out if a user is online, I created in Firestore a new collection called "RandomUser". If the user press a button, the user is added with his username and a randomly created number(userid). As far, every user who is online and wants to be connected, is in that collection. RandomUser Firestore Path Then the program awaits 7 seconds. To look if a user gets connected with him. If there's a "match", both users get removed from the "RandomUser" collection, they are no longer "available" and connectable. If nothing happens within that 7 seconds. The program goes further and creates himself a random number. If that number fits to any uid in the collection, the to users get connected and removed from the collection.
This system has some very obvious bugs:
- The same randomnumber can given twice or more
- To many users could get the same values
- While two users are connecting a third one could get the same uid
- While deleting the two users, they could get rematched
I thought about order them and with every new user in the collection, the uid should increase +1. That would avoid creating the same uid twice. The problem with the get function would stay.
You see a lot of possibe errors. Especially when there are lots of users, which want to connect.
Summarizing: I need an function, which can return a random online user. After the two users matched, they should be no longer connectable.
Can somebody help me? Please
Here's my code:
ConnectWithRandomUser()async{
await addRandomUser();
await new Future.delayed(const Duration(seconds: 7));
await getRandomUserOption();
await databaseMethods.deleteRandomActiveUser(Constants.myName);}
add random user (Note: Constants.myname is just the own username):
addRandomUser()async{
Map<String, dynamic> RandomMap = {
"name" : Constants.myName,
"uid" : randomNumber(),
};
await databaseMethods.addReadyUser(RandomMap, Constants.myName);
setState(() {
ConWithRandUs = true;
});}
class databaseMethods{
addReadyUser(RandomMap, username){
Firestore.instance.collection("RandomUser")
.add(RandomMap)
.catchError((e){print(e.toString());})}
}
Get another username:
getRandomUserOption() {
databaseMethods.getUserbyId(randomNumberGetUser()).then((val){
randomUidSnapshot = val;
Constants.yourName = randomUidSnapshot.documents[0].data["name"];
print("Random name is:");
print(Constants.yourName);
});
}
class databaseMethods{
getUserbyId(RandomNumber)async{
return await Firestore.instance.collection("RandomUser")
.where("uid", isEqualTo: RandomNumber)
.getDocuments();
}}
randomNumberGetUser(){
var rng = new Random();
for (var i = 0; i < 10; i++) {
return(rng.nextInt(2));//TODO Change range of numbers
}}
Delete user from collection:
class databaseMethods{
deleteRandomActiveUser(username)async{
await Firestore.instance.collection("RandomUser")
.where("name", isEqualTo: username)
.getDocuments().then((snapshot) {
for (DocumentSnapshot ds in snapshot.documents){
ds.reference.delete();
}
});
}}
Aucun commentaire:
Enregistrer un commentaire