-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBManager.cs
104 lines (87 loc) · 3.12 KB
/
DBManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Database;
using System;
public class DBManager : MonoBehaviour
{
private string userID;
public User newUser;
private DatabaseReference databaseReference;
public string[] FrndList;
private static DBManager _instance;
public static DBManager instance { get { return _instance; } }
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(this.gameObject);
}
else
{
_instance = this;
}
}
// Start is called before the first frame update
void Start()
{
userID = SystemInfo.deviceUniqueIdentifier;
databaseReference = FirebaseDatabase.DefaultInstance.RootReference;
Createuser();
//FrndList = new string[2] { "sahil", "mariyam" };
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.G))
{
GetUserInfo();
}
}
public void Createuser()
{
newUser = new User(AuthControl.instance.Name, 1.ToString(), FrndList);
string json = JsonUtility.ToJson(newUser);
databaseReference.Child("User").Child(AuthControl.instance.Name).SetRawJsonValueAsync(json);
}
public IEnumerator GetName(Action<string> onCallback)
{
var userNameData = databaseReference.Child("User").Child(AuthControl.instance.Name).Child("username").GetValueAsync();
yield return new WaitUntil(predicate: () => userNameData.IsCompleted);
if(userNameData != null)
{
DataSnapshot dataSnapshot = userNameData.Result;
onCallback.Invoke(dataSnapshot.Value.ToString());
}
}
public IEnumerator FriendList(Action<string> onCallback)
{
var userNameData = databaseReference.Child("User").Child(AuthControl.instance.Name).Child("FriendLists").GetValueAsync();
yield return new WaitUntil(predicate: () => userNameData.IsCompleted);
if (userNameData != null)
{
DataSnapshot dataSnapshot = userNameData.Result;
if (dataSnapshot != null && dataSnapshot.ChildrenCount > 0)
{
foreach (var childSnapshot in dataSnapshot.Children)
{
onCallback.Invoke(childSnapshot.Value.ToString());
// Debug.Log(frndlist + "LLLLLLLLLLLLLLL");/////////////>>>>>>>>>>>>>>>>>>>Get friendList Here<<<<<<<<<<<<<<<<<<<<
//text.text = childSnapshot.ToString();
}
}
}
}
public void GetUserInfo()
{
StartCoroutine(GetName((string name) => {
Debug.Log(name + " USERNAME");
}));
StartCoroutine(FriendList((string friendlist) => {
Debug.Log(friendlist + " Frnds");
}));
}
public void UpdateFrndList()
{
var userNameData = databaseReference.Child("User").Child(AuthControl.instance.Name).Child("FriendLists").SetValueAsync(FrndList);
}
}