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(); } } } }
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 check_home_prediction(fixture_class fix) { string pred_team = "home_prediction"; // Get match ID string match_id_str = "match_ID" + int.Parse(fix.match_id.Substring(6, 3)); string uid; auth = Firebase.Auth.FirebaseAuth.DefaultInstance; #if (UNITY_EDITOR) uid = "DESKTOP4"; #else var user = auth.CurrentUser; uid = auth.CurrentUser.UserId; #endif if (CommonData._database_predictions.Child(match_id_str).Child(uid).Child(pred_team).Value != null) { int score_out; if (int.TryParse(CommonData._database_predictions.Child(match_id_str).Child(uid).Child(pred_team).Value.ToString(), out score_out)) { fix.home_prediction = score_out; // Home prediction done, next up away prediction check_away_prediction(fix); } else { // Add to prediction not made count prediction_not_made++; } } else { // Add to prediction not made count prediction_not_made++; } }
private void check_score(fixture_class fix) { // Check scores are postive (have been updated) if (fix.home_prediction < 0 || fix.away_prediction < 0 || fix.home_result < 0 || fix.away_result < 0) { // If user has not made a prediction award 0 (should not get this far) fix.user_score = 0; } else { // if home score and away score exactly correct award 30 points if (fix.home_prediction == fix.home_result && fix.away_prediction == fix.away_result) { fix.user_score = 30; prediction_spot_on++; } // If the user didnt get it spot on see if the result was correct else { // Did home team win and did user predict that? if (fix.home_result > fix.away_result && fix.home_prediction > fix.away_prediction) { // award 10 points fix.user_score = 10; prediction_result_correct++; } // DId away team win and did user predict that? else if (fix.away_result > fix.home_result && fix.away_prediction > fix.home_prediction) { // award 10 points fix.user_score = 10; prediction_result_correct++; } // Was it a draw and did the user predict that? else if (fix.home_result == fix.away_result && fix.home_prediction == fix.away_prediction) { //draw - give 10 points fix.user_score = 10; prediction_result_correct++; } // Was the user wrong? else { // no points fix.user_score = 0; prediction_wrong++; } } } total_user_score = total_user_score + fix.user_score; // Add to matchday scores CommonData.user_matchday_scores[fix.matchday] = CommonData.user_matchday_scores[fix.matchday] + fix.user_score; if (fix.user_score == 30) { CommonData.user_pred_spoton[fix.matchday] = CommonData.user_pred_spoton[fix.matchday] + 1; } if (fix.user_score == 10) { CommonData.user_pred_correct[fix.matchday] = CommonData.user_pred_correct[fix.matchday] + 1; } if (fix.user_score == 0) { CommonData.user_pred_wrong[fix.matchday] = CommonData.user_pred_wrong[fix.matchday] + 1; } // TODO: MAKE SO DONE ONCE ALL SCORES CHECKED (ONLY DO ONCE) //save_score_player_prefs(); }
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(); } }