private bool check_if_user_name_in_use(string user_name) { bool is_user_name_taken = false; // Check realtime database for user name app = CommonData.app; Firebase.Database.FirebaseDatabase user_db = Firebase.Database.FirebaseDatabase.GetInstance(app); user_db.RootReference.Child("users").Child(user_name).GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; foreach (DataSnapshot user in snapshot.Children) { if ((string)user.Value == user_name) { is_user_name_taken = true; return; } } return; } }); return(is_user_name_taken); }
private void get_global_position() { // Just find rank of total score for all users // Would be nice if this makes a table in future for all users app = CommonData.app; _prediction_database = Firebase.Database.FirebaseDatabase.GetInstance(app); string path = "user_scores"; Query myquery = _prediction_database.RootReference.Child(path).Child("premier_league").OrderByChild("score"); myquery.GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { Debug.Log("task faulted"); } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; Debug.Log("SNAPSHOT value = " + snapshot.Value); Debug.Log("SNAPSHOT key = " + snapshot.Key); Debug.Log("SNAPSHOT reference = " + snapshot.Reference); Debug.Log("top score? = " + snapshot.Child("score").Value); } }); }
void get_fixtures_from_database() { Debug.Log("In: get_fixtures_from_database"); List <fixture_class> C_Fixtures = new List <fixture_class>(); // TODO: think of better name // Read from the database if user has set these scores before // (make them blue) app = CommonData.app; _prediction_database = Firebase.Database.FirebaseDatabase.GetInstance(app); // Get user from authentication auth = Firebase.Auth.FirebaseAuth.DefaultInstance; foreach (DataSnapshot child in CommonData._database_fixtures.Children) { //Debug.Log(child.Key); // Create temporary fixure class fixture_class fix_temp = new fixture_class(); // Get match id fix_temp.match_id = child.Key; // Get home team //Debug.Log("home_team??? " + child.Child("home_team").Value); fix_temp.home_team = child.Child("home_team").Value.ToString(); // Get away team //Debug.Log("away_team??? " + child.Child("away_team").Value); fix_temp.away_team = child.Child("away_team").Value.ToString(); // Get date //Debug.Log("home_team??? " + child.Child("time").Value); string time = child.Child("time").Value.ToString(); //Debug.Log("home_team??? " + child.Child("date").Value); string date = child.Child("date").Value.ToString(); DateTime fix_date; fix_date = new DateTime(Int32.Parse("20" + date.Substring(6, 2)), // year Int32.Parse(date.Substring(3, 2)), // month Int32.Parse(date.Substring(0, 2)), // day Int32.Parse(time.Substring(0, 2)), // hour Int32.Parse(time.Substring(2, 2)), // minute 0); // second // Convert to local user time //fix_date = ConvertTime_LondonToLocal(fix_date); fix_temp.fixture_date = fix_date; // Add fixture to list C_Fixtures.Add(fix_temp); // See if any fixtures within 7 days (includes past) TODO: link this value to a REMOTE CONFIG VALUE IN GOOGLE FIREBASE if (DateTime.Compare(DateTime.Now.AddDays(11), fix_date) > 0) { // Check fixture is not in the past if (DateTime.Compare(DateTime.Now, fix_date.AddMinutes(-30)) < 0) { // Create fixture UI create_fixture_UI(fix_temp); // If no fixtures going to be displayed then tell user any_predictions_in_seven_days = true; // In case error got displayed call again and it should be removed check_for_error_message(); } } } }
private void submit_prediciton(string match_id, float home_pred, float away_pred) { //Debug.Log("Submitting prediction for match id:" + match_id); string match_id_str = "match_ID" + match_id; string user_id; string name_name; app = CommonData.app; _prediction_database = Firebase.Database.FirebaseDatabase.GetInstance(app); // Get user from authentication auth = Firebase.Auth.FirebaseAuth.DefaultInstance; #if (UNITY_EDITOR) user_id = "DESKTOP4"; name_name = "DESKTOP4"; #else Debug.Log("in mobile"); var user = auth.CurrentUser; user_id = auth.CurrentUser.UserId; name_name = auth.CurrentUser.DisplayName; #endif // Set the values in the database _prediction_database.RootReference.Child("predictions").Child(match_id_str).Child(user_id).Child("match_id").SetRawJsonValueAsync(match_id); _prediction_database.RootReference.Child("predictions").Child(match_id_str).Child(user_id).Child("home_prediction").SetValueAsync(home_pred); _prediction_database.RootReference.Child("predictions").Child(match_id_str).Child(user_id).Child("user_name").SetValueAsync(name_name); _prediction_database.RootReference.Child("predictions").Child(match_id_str).Child(user_id).Child("away_prediction").SetValueAsync(away_pred); }
public void get_scores_from_database() { Debug.Log("Getting scores from database"); // Get app app = CommonData.app; // Get database instance _prediction_database = Firebase.Database.FirebaseDatabase.GetInstance(app); // Get user from authentication auth = Firebase.Auth.FirebaseAuth.DefaultInstance; // Seee if data exists at query Query score_query = _prediction_database.RootReference.Child("user_scores").Child("premier_league").OrderByChild("score"); score_query.GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // task faulted Debug.Log("task faulted"); } else if (task.IsCompleted) { Debug.Log(" get scores task completed"); // Get snapshot from task DataSnapshot snapshot = task.Result; // Set snapshot CommonData._database_scores = snapshot; CommonData._loaded_scores = true; } }); }
private void Awake() { FirebaseApp.DefaultInstance.SetEditorDatabaseUrl( "YOUR_URL"; fbref = Firebase.Database.FirebaseDatabase.DefaultInstance; fb = Firebase.Database.FirebaseDatabase.DefaultInstance.RootReference; }
private void add_user_to_database(string user_name, string email_address) { Debug.Log("adding user to database"); app = CommonData.app; _user_database = Firebase.Database.FirebaseDatabase.GetInstance(app); var user = auth.CurrentUser; var uid = user.UserId; _user_database.RootReference.Child("users").Child(uid).Child("user_name").SetRawJsonValueAsync(user_name); _user_database.RootReference.Child("users").Child(uid).Child("email").SetRawJsonValueAsync(email_address); }
// Use this for initialization void Start() { Debug.Log("STARTING PREDICTION"); app = CommonData.app; _prediction_database = Firebase.Database.FirebaseDatabase.GetInstance(app); // Get user from authentication auth = Firebase.Auth.FirebaseAuth.DefaultInstance; Debug.Log("app: " + app); // Starts the scene StartGame(); }
void get_fixtures_from_database(List <fixture_class> fixture_class_list) { Debug.Log("Getting fixtures from database"); // Get user home score prediction from database app = CommonData.app; _prediction_database = Firebase.Database.FirebaseDatabase.GetInstance(app); foreach (DataSnapshot child in CommonData._database_fixtures.Children) { fixture_class fix_temp = new fixture_class(); // If match is in the future then dont bother and skip string time = child.Child("time").Value.ToString(); string date = child.Child("date").Value.ToString(); DateTime fix_date; fix_date = new DateTime(Int32.Parse("20" + date.Substring(6, 2)), // year Int32.Parse(date.Substring(3, 2)), // month Int32.Parse(date.Substring(0, 2)), // day Int32.Parse(time.Substring(0, 2)), // hour Int32.Parse(time.Substring(2, 2)), // minute 0); // second fix_temp.fixture_date = fix_date; if (DateTime.Compare(DateTime.Now, fix_date) < 0) { continue; } fix_temp.match_id = child.Key; fix_temp.home_team = child.Child("home_team").Value.ToString(); fix_temp.away_team = child.Child("away_team").Value.ToString(); fix_temp.home_result = int.Parse(child.Child("home_score").Value.ToString()); fix_temp.away_result = int.Parse(child.Child("away_score").Value.ToString()); fix_temp.matchday = int.Parse(child.Child("matchday").Value.ToString()); //fixture_class_list.Add(fix_temp); if (fix_temp.home_result > -1 && fix_temp.away_result > -1) { // NOW CHECK RESULTS check_home_prediction(fix_temp); // Add to total results to check results_to_check++; } } }
private void add_score_to_db() { Debug.Log("ADDING SCORE TO DATABASE"); app = CommonData.app; _prediction_database = Firebase.Database.FirebaseDatabase.GetInstance(app); // Get user from authentication auth = Firebase.Auth.FirebaseAuth.DefaultInstance; string display_name; string user_id; #if (UNITY_EDITOR) user_id = "DESKTOP4"; display_name = "DESKTOP4"; #else Debug.Log("in mobile"); var user = auth.CurrentUser; user_id = auth.CurrentUser.UserId; display_name = auth.CurrentUser.DisplayName; #endif // Set the values in the database //_prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).SetValueAsync(total_user_score); _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("name").SetValueAsync(display_name); // total_user_score _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("score").SetValueAsync(PlayerPrefs.GetInt("HighScore", 0)); // User highest weekly score _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("highest_weekly_score").SetValueAsync(PlayerPrefs.GetInt("HighestWeeklyScore", 0)); // The matchday on which the users highest weekly score occured _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("highest_weekly_score_matchday").SetValueAsync(PlayerPrefs.GetInt("HighestWeeklyScoreMatchday", 0)); // _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("Prediction_SpotOn").SetValueAsync(PlayerPrefs.GetInt("Prediction_SpotOn", 0)); _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("Prediction_Correct_result").SetValueAsync(PlayerPrefs.GetInt("Prediction_Correct_result", 0)); _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("Prediction_Correct").SetValueAsync(PlayerPrefs.GetInt("Prediction_Correct", 0)); _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("Prediction_Wrong").SetValueAsync(PlayerPrefs.GetInt("Prediction_Wrong", 0)); _prediction_database.RootReference.Child("user_scores").Child("premier_league").Child(user_id).Child("Prediction_NotMade").SetValueAsync(PlayerPrefs.GetInt("Prediction_NotMade", 0)); // Is this needed? get_global_position(); }
void GetDataList() //Collects the player information from the tree { Firebase.Database.FirebaseDatabase dbInstance = Firebase.Database.FirebaseDatabase.DefaultInstance; dbInstance.GetReference("Game_01").GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { print("No data"); } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; foreach (DataSnapshot user in snapshot.Children) { IDictionary dictUser = (IDictionary)user.Value; playerIdList.Add(dictUser["userId"].ToString()); print(playerIdList.Count); } } }); }
//Fetches list of the player from lobby by their userID public void PlayerListCollector() { Firebase.Database.FirebaseDatabase dbInstance = Firebase.Database.FirebaseDatabase.DefaultInstance; dbInstance.GetReference(gameID + "/Lobby").GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) { DataSnapshot snapshot = task.Result; foreach (DataSnapshot user in snapshot.Children) { IDictionary dictUser = (IDictionary)user.Value; playerIDList.Add(dictUser["userId"].ToString()); } } }); }
public FirebaseDatabase(FirebaseApp app) { database = Firebase.Database.FirebaseDatabase.GetInstance(app); }
private void create_fixture_UI(fixture_class fix) { Debug.Log("Creating fixture UI: " + fix.home_team + "v" + fix.away_team); // Create a fixture UI element. With home & away teams with a match ID // This element is outlined in "Prediction_button.cs" // (which is not related to the add_prediciton_button() void in this class // Make instance of prediction GameObject prediction_instance = Instantiate(_prediction_score_template); // Create prediction as child of scroll view content object prediction_instance.transform.SetParent(_prediction_content.transform, false); prediction_instance.SetActive(true); // Add match id string[] split_fix_id = fix.match_id.Split('_'); fix.match_id = split_fix_id[1]; string match_id_str; if (int.Parse(fix.match_id) < 10) // SHOULD THIS BE FIXED ON DATABASE INPUT? NEXT YEAR { match_id_str = "match_ID" + int.Parse(fix.match_id).ToString("0"); } else if (int.Parse(fix.match_id) < 100) { match_id_str = "match_ID" + int.Parse(fix.match_id).ToString("00"); } else //(int.Parse(match_id) < 1000) // should be increased for 10000 if we ever get there { match_id_str = "match_ID" + int.Parse(fix.match_id).ToString("000"); } // Read from the database if user has set these scores before app = CommonData.app; _prediction_database = Firebase.Database.FirebaseDatabase.GetInstance(app); // Get user from authentication auth = Firebase.Auth.FirebaseAuth.DefaultInstance; string user_id; // TO DO - change to uid #if (UNITY_EDITOR) user_id = "DESKTOP4"; #else user_id = auth.CurrentUser.UserId; #endif Debug.Log("Starting to add to button, match ID: " + fix.match_id); prediction_instance.GetComponent <Prediction_button>().match_id = int.Parse(fix.match_id); // Set team names //Debug.Log("Starting to add to button, team names " + fix.home_team); prediction_instance.GetComponent <Prediction_button>().home_team = fix.home_team; prediction_instance.GetComponent <Prediction_button>().update_home_team_text(fix.home_team); //Debug.Log("Starting to add to button, team names " + fix.away_team); prediction_instance.GetComponent <Prediction_button>().away_team = fix.away_team; prediction_instance.GetComponent <Prediction_button>().update_away_team_text(fix.away_team); //Set ko time and date //Debug.Log("Starting to add to button, date " + fix.fixture_date); prediction_instance.GetComponent <Prediction_button>().ko_date = fix.fixture_date; prediction_instance.GetComponent <Prediction_button>().update_ko_time_text(fix.fixture_date); // Check if home prediction exists (might not have been made) if (CommonData._database_predictions.Child(match_id_str).Child(user_id).Child("home_prediction").Value != null) { prediction_instance.GetComponent <Prediction_button>().user_prediction_home_score_text.textComponent.color = Color.blue; DataSnapshot result = CommonData._database_predictions.Child(match_id_str).Child(user_id).Child("home_prediction"); Debug.Log("HOME PRED READ IN = " + int.Parse(result.Value.ToString())); prediction_instance.GetComponent <Prediction_button>().user_prediction_home_score = int.Parse(result.Value.ToString()); //prediction_instance.GetComponent<Prediction_button>().update_predicted_home_score(); } // Check if away prediction exists (might not have been made) if (CommonData._database_predictions.Child(match_id_str).Child(user_id).Child("away_prediction").Value != null) { prediction_instance.GetComponent <Prediction_button>().user_prediction_away_score_text.textComponent.color = Color.blue; DataSnapshot result = CommonData._database_predictions.Child(match_id_str).Child(user_id).Child("away_prediction"); Debug.Log("AWAY PRED READ IN = " + int.Parse(result.Value.ToString())); prediction_instance.GetComponent <Prediction_button>().user_prediction_away_score = int.Parse(result.Value.ToString()); //prediction_instance.GetComponent<Prediction_button>().update_predicted_away_score(); } }