private void Button_Import_Clicked(object sender, EventArgs e)
        {
            if (_isBusy)
            {
                return;
            }
            _isBusy = true;

            Label_Results.Text = "Importing data...";

            List <EventTeamMatch> matches = (List <EventTeamMatch>)SqlDataEventTeamMatches.GetItemsAsync().Result;

            string myDocumentsPath = App.GetMyDocumentsPath();

            string[] filenames = Directory.GetFiles(myDocumentsPath, $"{App.AppYear}_{App.currFRCEventKey}*.json");

            foreach (string path in filenames)
            {
                Label_Results.Text += $"\n\n{path}";

                string allMatchData = File.ReadAllText(path);

                JArray matchJsonData = JArray.Parse(allMatchData);

                Label_Results.Text += $"\n\nMatches found: {matchJsonData.Count}";

                int addedCount      = 0;
                int updatedCount    = 0;
                int notChangedCount = 0;

                foreach (JObject obj in matchJsonData)
                {
                    EventTeamMatch item    = EventTeamMatch.Parse(obj.ToString());
                    EventTeamMatch oldItem = matches.FirstOrDefault(p => p.Uuid == item.Uuid);

                    if (oldItem == null)
                    {
                        item.Changed = 0; // downloaded records are excluded from uploading
                        SqlDataEventTeamMatches.AddItemAsync(item);
                        addedCount++;
                    }
                    else if (oldItem.Changed > 0 && oldItem.Changed < item.Changed)
                    {
                        SqlDataEventTeamMatches.UpdateItemAsync(item);
                        updatedCount++;
                    }
                    else
                    {
                        notChangedCount++;
                    }
                }

                Label_Results.Text += $"\n\nAdded: {addedCount} - Updated: {updatedCount} - Not Changed: {notChangedCount}";
            }

            Label_Results.Text += "\n\nImport complete";

            _isBusy = false;
        }
 static void ShowEventTeamMatch(EventTeamMatch item)
 {
     Console.WriteLine(
         $"Id: {item.Id} - Uuid: {item.Uuid}" +
         $" - EventKey: {item.EventKey}" +
         $" - Team: {item.TeamNumber}" +
         $" - Match: {item.MatchNumber}" +
         $" - AllianceResult = {item.AllianceResult}");
 }
        private int CalculateMatchRP(EventTeamMatch match)
        {
            int rp = 0;

            rp += match.AllianceResult;
            rp += match.StageRankingPoint;
            rp += match.ClimbRankingPoint;
            return(rp);
        }
Exemple #4
0
        private int CalculateHatchCount(EventTeamMatch match)
        {
            int result = 0;

            result += match.SandstormHatches;
            result += match.CargoShipHatches;
            result += match.RocketHatches;
            return(result);
        }
        private int CalculateCargoCount(EventTeamMatch match)
        {
            int result = 0;

            result += match.AutoOuterCell;
            result += match.TeleBottomCell;
            result += match.TeleInnerCell;
            return(result);
        }
Exemple #6
0
        private int CalculateCargoCount(EventTeamMatch match)
        {
            int result = 0;

            result += match.SandstormCargo;
            result += match.CargoShipCargo;
            result += match.RocketCargo;
            return(result);
        }
        private int CalculateHatchCount(EventTeamMatch match)
        {
            int result = 0;

            result += match.AutoBottomCell;
            result += match.AutoInnerCell;
            result += match.TeleOuterCell;
            return(result);
        }
Exemple #8
0
        private int CalculateMatchRP(EventTeamMatch match)
        {
            int rp = 0;

            rp += match.AllianceResult;
            rp += match.RocketRankingPoint;
            rp += match.HabRankingPoint;
            return(rp);
        }
Exemple #9
0
        public HttpResponseMessage PostEventTeamMatch(EventTeamMatch item)
        {
            item.Id = null; // clear for autoincrement
            item    = repository.Add(item);
            var    response = Request.CreateResponse(HttpStatusCode.Created, item);
            string uri      = Url.Link("DefaultApi", new { uuid = item.Uuid });

            response.Headers.Location = new Uri(uri);
            return(response);
        }
Exemple #10
0
 public Task <int> SaveEventTeamMatchAsync(EventTeamMatch item)
 {
     // note: the caller must let this resolve before item.Id is first
     // available, using either "await" or "int x = ...().Result;"
     if (item.Uuid == null)
     {
         item.Uuid = Guid.NewGuid().ToString();
     }
     return(_database.InsertOrReplaceAsync(item));
 }
        static async Task <Uri> CreateEventTeamMatchAsync(EventTeamMatch item)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(
                "api/EventTeamMatches", item);

            response.EnsureSuccessStatusCode();

            // return URI of the created resource.
            return(response.Headers.Location);
        }
Exemple #12
0
        public EventTeamMatch GetEventTeamMatch(string uuid)
        {
            EventTeamMatch item = repository.GetByUuid(uuid);

            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return(item);
        }
        public EditMatchCommentPage(EventTeamMatch item)
        {
            InitializeComponent();

            if (item.Changed % 2 == 0) // change from even to odd, odd = must upload
            {
                item.Changed++;
            }

            BindingContext = viewModel = new EditEventTeamMatchViewModel(item);
        }
        private async void EventTeamsListMatchView_ItemSelected(object sender, SelectedItemChangedEventArgs args)
        {
            EventTeamMatch item = (EventTeamMatch)args.SelectedItem;

            if (item == null)
            {
                return;
            }
            App.currMatchNumber = item.MatchNumber;
            await Navigation.PushAsync(new EditEventTeamMatchPage(item));
        }
        static async Task <EventTeamMatch> GetEventTeamMatchAsync(string path)
        {
            EventTeamMatch      item     = null;
            HttpResponseMessage response = await client.GetAsync(path);

            if (response.IsSuccessStatusCode)
            {
                item = await response.Content.ReadAsAsync <EventTeamMatch>();
            }
            return(item);
        }
        static async Task <EventTeamMatch> UpdateEventTeamMatchAsync(EventTeamMatch item)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(
                $"api/EventTeamMatches?uuid={item.Uuid}", item);

            response.EnsureSuccessStatusCode();

            // Deserialize the updated EventTeamMatch from the response body.
            item = await response.Content.ReadAsAsync <EventTeamMatch>();

            return(item);
        }
Exemple #17
0
        private async void TeamMatchesListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            MatchResult item = (MatchResult)e.SelectedItem;

            if (item == null)
            {
                return;
            }
            // key = "EventKey|TeamNumber|MatchNumber"
            string         key       = $"{item.EventKey}|{item.TeamNumber}|{item.MatchNumber}";
            EventTeamMatch itemMatch = viewModel.DataStoreMatch.GetItemByKeyAsync(key).Result;
            await Navigation.PushAsync(new EditEventTeamMatchPage(itemMatch));
        }
Exemple #18
0
        public void PutEventTeamMatch(string uuid, EventTeamMatch item)
        {
            EventTeamMatch oldItem = repository.GetByUuid(uuid);

            // only update if new .Changed is greater
            if (oldItem != null && oldItem.Changed < item.Changed)
            {
                item.Uuid = uuid;
                if (!repository.Update(item))
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }
        }
        public EditEventTeamMatchPage(EventTeamMatch item)
        {
            InitializeComponent();

            if (item.Changed % 2 == 0) // change from even to odd, odd = must upload
            {
                item.Changed++;
            }

            BindingContext = viewModel = new EditEventTeamMatchViewModel(item);

#if DEBUG
            LabelChangeVersion.Text      = $"Change Version: {item.Changed}";
            LabelChangeVersion.IsVisible = true;
#endif

            SetButtons();
        }
Exemple #20
0
        async private Task <int> AirtableSendNewRecords(AirtableBase airtableBase,
                                                        List <Fields> newRecordList)
        {
            AirtableCreateUpdateReplaceMultipleRecordsResponse result;
            List <Fields> sendList   = new List <Fields>();
            int           finalCount = 0;

            while (newRecordList.Count > 0)
            {
                sendList.Clear();
                do
                {
                    sendList.Add(newRecordList[0]);
                    newRecordList.RemoveAt(0);
                } while (newRecordList.Count > 0 && sendList.Count < 10);
                result = await airtableBase.CreateMultipleRecords("Match", sendList.ToArray());

                if (!result.Success)
                {
                    Label_Results.Text  = "Error uploading:\r\n";
                    Label_Results.Text += $"{result.AirtableApiError.ErrorMessage}\r\n";
                    return(-1);
                }
                foreach (AirtableRecord rec in result.Records)
                {
                    EventTeamMatch match = App.Database.GetEventTeamMatchAsyncUuid(rec.GetField("Uuid").ToString());
                    match.Changed++; // mark as uploaded
                    if (match.Changed % 2 == 1)
                    {
                        match.Changed++; // make even so it doesn't send again
                    }
                    match.AirtableId = rec.Id;
                    await App.Database.SaveEventTeamMatchAsync(match);

                    finalCount++;
                }
                if (newRecordList.Count > 0)
                {
                    // can only send 5 batches per second, make sure that doesn't happen
                    System.Threading.Thread.Sleep(500);
                }
            }
            return(finalCount);
        }
Exemple #21
0
        private int CalculateMatchResult(EventTeamMatch match)
        {
            int score = 0;

            //not scoring movement type
            //score += match.SandstormMoveType;
            score += match.SandstormOffPlatform * 3;
            score += match.SandstormHatches * 2;
            score += match.SandstormCargo * 3;

            score += match.CargoShipHatches * 2;
            score += match.CargoShipCargo * 3;
            score += match.RocketHatches * 2;
            score += match.RocketCargo * 3;
            //not scoring highest platform
            //score += match.RocketHighestHatch;
            //score += match.RocketHighestCargo;

            //score += match.EndgamePlatform;
            switch (match.EndgamePlatform)
            {
            case 1:
                score += 3;
                break;

            case 2:
                score += 6;
                break;

            case 3:
                score += 12;
                break;
            }
            //not scoring buddy climb
            //score += match.EndgameBuddyClimb;

            //score += match.Defense;
            //score += match.Cooperation;
            score -= match.Fouls * 3;
            score -= match.TechFouls * 10;
            //score -= match.Broken*20;

            return(score);
        }
        private int CalculateMatchResult(EventTeamMatch match)
        {
            int score = 0;

            //not scoring movement type
            //score += match.AutoStartPos;
            score += match.AutoLeaveInitLine * 5;
            score += match.AutoBottomCell * 2;
            score += match.AutoOuterCell * 4;

            score += match.AutoInnerCell * 6;
            score += match.TeleBottomCell * 1;
            score += match.TeleOuterCell * 2;
            score += match.TeleInnerCell * 3;
            //not scoring highest platform
            score += match.RotationControl * 10;
            score += match.PositionControl * 20;

            //score += match.ClimbStatus;
            switch (match.ClimbStatus)
            {
            case 1:
                score += 5;
                break;

            case 2:
                score += 25;
                break;

            case 3:
                score += 25;
                break;
            }
            //not scoring buddy climb
            score += match.LevelSwitch * 15;

            //score += match.Defense;
            //score += match.Cooperation;
            score -= match.Fouls * 3;
            //score -= match.Broken*20;

            return(score);
        }
        public FixMatchPage(EventTeamMatch item)
        {
            InitializeComponent();
            match = item;
            Teams = App.Database.GetTeamsByEventAsync(App.currFRCEventKey).Result;
            int savePickerIndex = -1;

            for (int i = 0; i < Teams.Count; i++)
            {
                Team t = Teams[i];
                Picker_TeamNumber.Items.Add(t.TeamNumberDashName);
                if (t.TeamNumber == match.TeamNumber)
                {
                    savePickerIndex = i;
                }
            }
            Entry_ScouterName.Text          = match.ScouterName;
            Picker_TeamNumber.SelectedIndex = savePickerIndex;
            Entry_MatchNumber.Text          = match.MatchNumber.ToString();
        }
        private async void doAddNewMatch(int value)
        {
            foreach (EventTeamMatch oldMatch in viewModel.Matches)
            {
                if (oldMatch.MatchNumber == value)
                {
                    return;
                }
            }
            EventTeamMatch newMatch = new EventTeamMatch();

            newMatch.EventKey    = App.currFRCEventKey;
            newMatch.TeamNumber  = App.currTeamNumber;
            newMatch.MatchNumber = value;
            newMatch.Changed     = 1; // odd = must upload
            await App.Database.SaveEventTeamMatchAsync(newMatch);

            // add new match into list in proper order
            bool found = false;

            for (int i = 0; i < viewModel.Matches.Count; i++)
            {
                if (viewModel.Matches[i].MatchNumber > value)
                {
                    found = true;
                    viewModel.Matches.Insert(i, newMatch);
                    break;
                }
            }
            if (!found)
            {
                viewModel.Matches.Add(newMatch);
            }
            App.currMatchNumber = newMatch.MatchNumber;
            await Navigation.PushAsync(new EditEventTeamMatchPage(newMatch));
        }
 public EditEventTeamMatchViewModel(EventTeamMatch item)
 {
     this.item = item;
     Title     = $"Team {App.currTeamNumber} - Match {App.currMatchNumber}";
 }
Exemple #26
0
        async private void AirtableFetchRecords()
        {
            string offset       = null;
            string errorMessage = null;
            var    records      = new List <AirtableRecord>();

            using (AirtableBase airtableBase = new AirtableBase(App.AirtableKey, App.AirtableBase))
            {
                do
                {
                    if (offset != null)
                    {
                        // Sleep for half a second so we don't make requests too fast.
                        // The free Airtable plan only allows 5 requests per second.
                        // Each download is 100 records (pagesize).
                        System.Threading.Thread.Sleep(500);
                    }

                    AirtableListRecordsResponse response;
                    response = await airtableBase.ListRecords(
                        "Match",
                        offset,
                        null /*fieldsArray*/,
                        $"EventKey='{App.currFRCEventKey}'" /*filterByFormula*/,
                        null /*maxRecords*/,
                        100 /*pageSize*/,
                        null /*sort*/,
                        null /*view*/);

                    if (response.Success)
                    {
                        records.AddRange(response.Records.ToList());
                        offset = response.Offset;
                    }
                    else if (response.AirtableApiError is AirtableApiException)
                    {
                        errorMessage = response.AirtableApiError.ErrorMessage;
                        break;
                    }
                    else
                    {
                        errorMessage = "Unknown error";
                        break;
                    }
                } while (offset != null);
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                Label_Results.Text = errorMessage;
                return;
            }

            int RecordCount = 0;
            int NewCount    = 0;
            int UpdateCount = 0;
            int IgnoreCount = 0;

            foreach (AirtableRecord ar in records)
            {
                RecordCount++;
                JObject        jo         = new JObject();
                EventTeamMatch m          = null;
                string         uuid       = ar.Fields["Uuid"].ToString();
                string         airtableId = ar.Id;
                if (!string.IsNullOrEmpty(uuid))
                {
                    StringBuilder query = new StringBuilder();
                    query.Append("SELECT [EventTeamMatch].* FROM [EventTeamMatch]");
                    query.Append(" WHERE [EventTeamMatch].[Uuid] = '");
                    query.Append(uuid);
                    query.Append("'");
                    m = App.Database.GetEventTeamMatchAsyncUuid(uuid);
                    if (m != null && m.Changed >= (long)ar.Fields["Changed"])
                    {
                        // this record is newer, ignore airtable
                        IgnoreCount++;
                        continue;
                    }
                }
                if (m == null)
                {
                    m            = new EventTeamMatch();
                    m.AirtableId = ar.Id;
                    NewCount++;
                }
                else
                {
                    UpdateCount++;
                }
                // convert to JObject and back so individual match fields are not needed here
                jo = m.ToJson();
                foreach (KeyValuePair <string, object> kv in ar.Fields)
                {
                    if (kv.Key == "Id" || kv.Key == "AirtableId")
                    {
                        continue;
                    }
                    int intValue;
                    if (int.TryParse(kv.Value.ToString(), out intValue))
                    {
                        if (kv.Key == "Changed")
                        {
                            // only update if lower
                            if (m.Changed < intValue)
                            {
                                m.Changed = intValue;
                            }
                        }
                        else
                        {
                            jo.SetValue(kv.Key, intValue);
                        }
                    }
                    else
                    {
                        jo.SetValue(kv.Key, kv.Value.ToString());
                    }
                }
                // Rebuild the EventTeamMatch from the JObject data
                m = EventTeamMatch.FromJson(jo);
                if (m.Changed % 2 == 1)
                {
                    m.Changed++;
                }
                // save to the database
                await App.Database.SaveEventTeamMatchAsync(m);
            }

            Label_Results.Text  = "";
            Label_Results.Text += $"Records found: {RecordCount}\r\n";
            Label_Results.Text += $"Records added: {NewCount}\r\n";
            Label_Results.Text += $"Records updated: {UpdateCount}\r\n";
            Label_Results.Text += $"Records ignored: {IgnoreCount}";
        }
        private void Button_Download_Clicked(object sender, EventArgs e)
        {
            if (_isBusy)
            {
                return;
            }
            _isBusy = true;

            PrepareSync();

            int addedCount      = 0;
            int updatedCount    = 0;
            int notChangedCount = 0;
            int lastId          = 0;

            Label_Results.Text = "Downloading data...";

            List <EventTeamMatch> matches = (List <EventTeamMatch>)SqlDataEventTeamMatches.GetItemsAsync().Result;

            try
            {
                do
                {
                    addedCount      = 0;
                    updatedCount    = 0;
                    notChangedCount = 0;
                    string batchInfo             = $"{App.currFRCEventKey}|{lastId}|10";
                    HttpResponseMessage response = App.client.GetAsync($"api/EventTeamMatches?batchInfo={batchInfo}").Result;
                    response.EnsureSuccessStatusCode();
                    if (response.IsSuccessStatusCode)
                    {
                        string result  = response.Content.ReadAsStringAsync().Result;
                        JArray results = JArray.Parse(result);
                        foreach (JObject obj in results)
                        {
                            EventTeamMatch item = EventTeamMatch.Parse(obj.ToString());

                            if (lastId < item.Id.Value)
                            {
                                lastId = item.Id.Value;
                            }

                            EventTeamMatch oldItem = matches.FirstOrDefault(p => p.Uuid == item.Uuid);

                            if (oldItem == null)
                            {
                                item.Changed = 0; // downloaded records are excluded from sending
                                SqlDataEventTeamMatches.AddItemAsync(item);
                                addedCount++;
                            }
                            else if (oldItem.Changed > 0 && oldItem.Changed < item.Changed)
                            {
                                SqlDataEventTeamMatches.UpdateItemAsync(item);
                                updatedCount++;
                            }
                            else
                            {
                                notChangedCount++;
                            }
                        }
                    }

                    if (addedCount + updatedCount + notChangedCount > 0)
                    {
                        Label_Results.Text += $"\n\nAdded: {addedCount} - Updated: {updatedCount} - Not Changed: {notChangedCount}";
                    }
                }while (addedCount + updatedCount + notChangedCount > 0);

                Label_Results.Text += "\n\nDownload complete";
            }
            catch (Exception ex)
            {
                Label_Results.Text += $"\n\nError during transmission\n\n{ex.Message}\n\n{ex.InnerException}";
                _isBusy             = false;
                return;
            }

            _isBusy = false;
        }