public void UpdatePassingsGrid(TagReadEventArgs e, int addIndex = -1) { PassingsInfo pi = null; bool tagClash = false; CompetitorRace cr = null; List<CompetitorRace> crs = FindCompetitorRaceByTag(e.TagInfo.ID); if (crs.Count == 1) cr = crs[0]; else if(crs.Count > 1) tagClash = true; EventEntry eve = null; if (cr == null && !tagClash) { eve = isCompetitorInEventThreadSafe(e.TagInfo.ID); //the following may happen - for example, event entry has IDs of removed competitor; //then we should go as if there is not corresponding event entry if (eve != null) { if (DataManager.Instance.GetCompetitorByID(eve.competitorID) == null) eve = null; } } if (cr != null) { pi = new PassingsInfo(e.TagInfo, cr.competitorID, cr.firstName, cr.lastName, cr.bikeNumber); pi.CompetitorRace = cr; if (addIndex == -1 || addIndex >= cr.passings.Count) cr.passings.Add(pi); else cr.passings.Insert(addIndex, pi); } else if (eve != null) { CompetitorRace crNew = new CompetitorRace(eve.competitorID); //can competitor be null? probably not, as EventEntry is not null crNew.competitor = DataManager.Instance.GetCompetitorByID(eve.competitorID); crNew.EventEntry = eve; crNew.lastName = (crNew.competitor != null) ? crNew.competitor.LastName : ""; crNew.firstName = (crNew.competitor != null) ? crNew.competitor.FirstName : ""; crNew.competitorID = eve.competitorID; crNew.tagID = eve.tagNumber; crNew.tagID2 = eve.tagNumber2; crNew.bikeNumber = eve.bikeNumber; crNew.raceParent = this.GetCurrentSession(); if (currentRace != null)//should never happen { currentRace.competitorRaceList.Add(crNew); crNew.raceParent = currentRace; } pi = new PassingsInfo(e.TagInfo, crNew.competitorID, crNew.firstName, crNew.lastName, crNew.bikeNumber); pi.CompetitorRace = cr;//crNew??? if (addIndex == -1 || addIndex >= cr.passings.Count || addIndex >= crNew.passings.Count)// crNew.passings.Add(pi); else crNew.passings.Insert(addIndex, pi); } else { //have to create a new CompetitorRace: cr = new CompetitorRace(); cr.isNull = true; //make its competitor's ID negative: //this will prevent possible clash with real //competitors' IDs cr.competitorID = -1 * DataManager.getNextID(); cr.tagID = e.TagInfo.ID; cr.tagID2 = e.TagInfo.ID; cr.firstName = ""; cr.lastName = "Unassigned"; if (currentRace != null)//should never happen { //currentRace.competitorRaceList is the currentRaceList currentRace.competitorRaceList.Add(cr); cr.raceParent = currentRace; } pi = new PassingsInfo(e.TagInfo, cr.competitorID, cr.firstName, cr.lastName, cr.bikeNumber); pi.CompetitorRace = cr; if (addIndex == -1 || addIndex >= cr.passings.Count) cr.passings.Add(pi); else cr.passings.Insert(addIndex, pi); } if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate { if (addIndex == -1 || addIndex >= cr.passings.Count || addIndex >= (passingsDataGrid.DataSource as BindingList<PassingsInfo>).Count) (passingsDataGrid.DataSource as BindingList<PassingsInfo>).Add(pi); else (passingsDataGrid.DataSource as BindingList<PassingsInfo>).Insert(addIndex, pi); if (passingsDataGrid.RowCount > 0 && !passingsGridScrolled) { passingsDataGrid.Rows[passingsDataGrid.RowCount - 1].Selected = true; passingsDataGrid.CurrentCell = passingsDataGrid.Rows[passingsDataGrid.RowCount - 1].Cells[0]; } }); } else { if (addIndex == -1 || addIndex >= cr.passings.Count || addIndex >= (passingsDataGrid.DataSource as BindingList<PassingsInfo>).Count) (passingsDataGrid.DataSource as BindingList<PassingsInfo>).Add(pi); else (passingsDataGrid.DataSource as BindingList<PassingsInfo>).Insert(addIndex, pi); if (passingsDataGrid.RowCount > 0 && !passingsGridScrolled) { passingsDataGrid.Rows[passingsDataGrid.RowCount - 1].Selected = true; passingsDataGrid.CurrentCell = passingsDataGrid.Rows[passingsDataGrid.RowCount - 1].Cells[0]; } } if (tagClash) { //TODO: after the user has selected the Competitor, this should remain for the whole //run; need some kind of Dictionary for that; it should be looked up first in //the method findCRbyTag; //also, don't forget to reset everything for a new run; for instance, tagClashFormList if (!tagClashFormList.Contains(pi.ID)) { List<CompetitorRace> crsNew = new List<CompetitorRace>(); foreach (CompetitorRace cRace in crs) { if (cRace.competitorID >= 0)//don't need "Unassigned" CRs crsNew.Add(cRace); } if (crsNew.Count > 1) { tagClashFormList.Add(pi.ID); TagClashForm clashForm = new TagClashForm(); clashForm.SetCompetitors(crsNew, pi, tagClashFormList, currentRace, passingsDataGrid, disambiuationCRDict); clashForm.Show(this); } } } //check if pi.lapTime is smaller than min lap time, make it "DELETED" if true if (pi != null && pi.CompetitorRace != null) { if (pi.CompetitorRace.passings != null && pi.CompetitorRace.passings.Count > 1) { PassingsInfo piPrevious = null; //look for any previous valid passings for (int i = pi.CompetitorRace.passings.Count - 2; i >= 0; i--) { if (!pi.CompetitorRace.passings[i].Deleted.Equals("DELETED")) { piPrevious = pi.CompetitorRace.passings[i]; break; } } if (piPrevious != null) { long latestTime = pi.Time; long nextToLatestTime = piPrevious.Time; double lapTime = (latestTime - nextToLatestTime) / 1000.0; if (lapTime < DataManager.Instance.MinimumLapTime) { pi.Deleted = "DELETED"; pi.LapTime = GetTimeString(lapTime); } } } } }
/// <summary> /// This handler creates event entries for all selected competitors and adds /// them to the selected race (session, run). /// </summary> private void addCompetitorsToRunButton_Click(object sender, EventArgs e) { //if (MessageBox.Show("Are you sure you would like to add all the selected competitors to " + // "the selected run? This will create event entries for each of the competitors!", // "Please confirm", MessageBoxButtons.YesNo) == DialogResult.No) //{ // return; //} if (GetSelectedCompetitors().Count == 0) { MessageBox.Show("Please select at least one competitor in a list!"); return; } //if ((raceCompetitorsGridView.DataSource as BindingList<CompetitorRace>) == null) if ((raceCompetitorsGridView.DataSource as SortableBindingList<CompetitorRace>) == null) { MessageBox.Show("Please add a race to an event first!"); return; } Race selectedRace = null; int index = selectedRaceComboBox.SelectedIndex; if (index != -1) { selectedRace = (selectedRaceComboBox.DataSource as BindingList<Race>)[index]; } if (selectedRace == null) return; //for each competitor, create a new event entry; then add those event entries to the race Event ev = null; if (eventsComboBox.SelectedIndex != -1) ev = DataManager.Instance.Events[eventsComboBox.SelectedIndex]; else { MessageBox.Show("Please select an event!"); return; } try { foreach (Competitor c in GetSelectedCompetitors()) { String eventEntryClass = ""; if (classComboBox.SelectedIndex != -1 && classComboBox.Items != null && classComboBox.Items.Count > 0) { eventEntryClass = classComboBox.Items[classComboBox.SelectedIndex] as String; } EventEntry eventEntry = new EventEntry(); eventEntry.competitor = c; eventEntry.competitorID = c.ID; eventEntry.eventID = ev.ID; eventEntry.tagNumber = c.TagNumber; eventEntry.tagNumber2 = c.TagNumber2; eventEntry.bikeNumber = c.BikeNumber; eventEntry.bikeBrand = c.BikeBrand; eventEntry.className = eventEntryClass; DataManager.Instance.EventEntries.Add(eventEntry); CompetitorRace cr = new CompetitorRace(eventEntry.competitorID); cr.competitor = c; cr.EventEntry = eventEntry; cr.tagID = eventEntry.tagNumber; cr.tagID2 = eventEntry.tagNumber2; cr.bikeNumber = eventEntry.bikeNumber; cr.className = eventEntry.className; cr.raceParent = selectedRace; selectedRace.AddCompetitorRace(cr); raceCompetitorsGridView.Columns["competitor"].Visible = false; } //deselect competitors after adding competitorsDataGridControl.DataGridView.ClearSelection(); competitorDataInput.ClearControls();//sets focus to last name text box as well //DeselectEventEntriesUI(); } catch (Exception exc) { Console.WriteLine(exc.StackTrace); DataManager.Log(exc.StackTrace); MessageBox.Show("Sorry, there was a problem when adding competitors."); } }
//after adding/deleting/undeleting tags for a given CR, //re-select it in the competitor race grid private void SelectCompetitorRace(CompetitorRace cr) { if (cr == null) return; int selectedIndex = -1; int currentIndex = -1; foreach (CompetitorRace cmpR in (competitorRaceDataGrid.DataSource as SortableBindingList<CompetitorRace>)) { currentIndex++; if (cmpR.Equals(cr)) { selectedIndex = currentIndex; break; } } if (selectedIndex == -1) return; if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate { competitorRaceDataGrid.Rows[selectedIndex].Selected = true; }); } else { competitorRaceDataGrid.Rows[selectedIndex].Selected = true; } }
private void AddNewCompetitor(Competitor newCompetitor, bool addToCompetitorsList = true) { if(addToCompetitorsList) DataManager.Instance.Competitors.Add(newCompetitor); competitor = newCompetitor; EventEntry newEventEntry = new EventEntry(); newEventEntry.competitor = newCompetitor; Event currentEvent = raceInformationControl.GetSelectedEvent(); if (currentEvent != null) { newEventEntry.eventID = currentEvent.ID; newEventEntry.tagNumber = newCompetitor.TagNumber; newEventEntry.tagNumber2 = newCompetitor.TagNumber2; newEventEntry.bikeNumber = newCompetitor.BikeNumber; if (classComboBox.SelectedIndex != -1) { newEventEntry.className = classComboBox.Items[classComboBox.SelectedIndex] as String; } DataManager.Instance.EventEntries.Add(newEventEntry); } CompetitorRace cr = new CompetitorRace(newEventEntry.competitorID); cr.EventEntry = newEventEntry; cr.lastName = newCompetitor.LastName; cr.firstName = newCompetitor.FirstName; cr.competitorID = newCompetitor.ID; cr.tagID = newCompetitor.TagNumber; cr.tagID2 = newCompetitor.TagNumber2; cr.bikeNumber = newCompetitor.BikeNumber; cr.className = newEventEntry.className; cr.raceParent = raceInformationControl.GetCurrentSession(); raceInformationControl.GetCurrentSession().AddCompetitorRace(cr); UpdatePassings(); }
private void addCompetitorsToRaceButton_Click(object sender, EventArgs e) { if (GetSelectedCompetitors().Count == 0) { MessageBox.Show("Please select a competitor in a list"); return; } //if ((raceCompetitorsGridView.DataSource as BindingList<CompetitorRace>) == null) if ((raceCompetitorsGridView.DataSource as SortableBindingList<CompetitorRace>) == null) { MessageBox.Show("Please add a race to an event first!"); return; } Race selectedRace = null; int index = selectedRaceComboBox.SelectedIndex; if (index != -1) { selectedRace = (selectedRaceComboBox.DataSource as BindingList<Race>)[index]; } if (selectedRace == null) return; foreach (EventEntry eventEntry in GetSelectedEventEntries()) { CompetitorRace cr = new CompetitorRace(eventEntry.competitorID); cr.EventEntry = eventEntry; cr.tagID = eventEntry.tagNumber; cr.tagID2 = eventEntry.tagNumber2; cr.bikeNumber = eventEntry.bikeNumber; cr.className = eventEntry.className; cr.raceParent = selectedRace; //(raceCompetitorsGridView.DataSource as BindingList<CompetitorRace>).Add(cr); //instead, add cr to a corresponding race bool entryExists = false; foreach (CompetitorRace existingCR in selectedRace.competitorRaceList) { if (existingCR.isNull) continue; //ugly! maybe add a method like "isValid()" to TagId class, that checks for null, 0, and empty string? if ((existingCR.tagID.Value != null && !existingCR.tagID.Value.Equals("0") && !existingCR.tagID.Value.Equals("") && existingCR.tagID.Equals(cr.tagID)) || (existingCR.tagID.Value != null && !existingCR.tagID.Value.Equals("0") && !existingCR.tagID.Value.Equals("") && existingCR.tagID.Equals(cr.tagID2)) || (existingCR.tagID2.Value != null && !existingCR.tagID2.Value.Equals("0") && !existingCR.tagID2.Value.Equals("") && existingCR.tagID2.Equals(cr.tagID)) || (existingCR.tagID2.Value != null && !existingCR.tagID2.Value.Equals("0") && !existingCR.tagID2.Value.Equals("") && existingCR.tagID2.Equals(cr.tagID2))) { entryExists = true; break; } } //show dialog to confirm if (entryExists) { if (MessageBox.Show("Entry with the same tag id(s) already in the list. Are you sure still want to add it?", "Confirmation Dialog", MessageBoxButtons.YesNo) == DialogResult.No) { continue;//don't add } } //check if this competitor race already exists (check name, tag, etc) selectedRace.AddCompetitorRace(cr); raceCompetitorsGridView.Columns["competitor"].Visible = false; } }
private void okButton_Click(object sender, EventArgs e) { // //1. Unknown tag info came, no competitor is originally known for the incoming tag // //possible cases: //a) cancel (nothing done) //b) new competitor added //c) old competitor (existing in the competitor's list, but not racing list) // added to the race // if (competitor == null) { Competitor newCompetitor = new Competitor(); newCompetitor.ID = DataManager.getNextID();//important! otherwise will have id = 0 bool res = competitorDataInput.UpdateCompetitorData(ref newCompetitor); if (!res) { MessageBox.Show("There was a problem with the entered data."); return; } //Competitor existingCompetitor = DataManager.Instance.GetCompetitorByTagID(newCompetitor.TagNumber); //TODO: have to check as well for the name (assume the admin has entered John Doe, //but John Doe already exists somewhere in the competitors list, though with a different //or no tag. How to proceed there? Show the same warning dialog? Competitor existingCompetitor = null; List<Competitor> existingCmps = DataManager.Instance.GetCompetitorByName(newCompetitor.FirstName, newCompetitor.LastName); if (existingCmps.Count > 0) existingCompetitor = existingCmps[0]; if (existingCompetitor != null) { //AddNewCompetitor(existingCompetitor, false); //this.Dispose(); //return; //TEMPORARILY COMMENTED OUT Form userDialog = GetUserDialog("Warning!", "There already exists a competitor with the name " + existingCompetitor.FirstName + " " + existingCompetitor.LastName + ". Do you want to proceed with this user or create a new one?", "Proceed with current user", "Create a new one"); DialogResult dr = userDialog.ShowDialog(); if (dr == System.Windows.Forms.DialogResult.Cancel) { this.Dispose(); return; } else if (dr == System.Windows.Forms.DialogResult.Ignore) { if (newCompetitor.FirstName.Equals("") && newCompetitor.LastName.Equals("")) { if (MessageBox.Show("Are you sure you would like to add competitor with empty name fields?", "Please confirm", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } AddNewCompetitor(newCompetitor); this.Dispose(); return; } else if (dr == System.Windows.Forms.DialogResult.OK)//proceed { if (newCompetitor.FirstName.Equals("") && newCompetitor.LastName.Equals("")) { if (MessageBox.Show("Are you sure you would like to add competitor with empty name fields?", "Please confirm", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } //have to overwrite tags existingCompetitor.TagNumber = newCompetitor.TagNumber; existingCompetitor.TagNumber2 = newCompetitor.TagNumber2; AddNewCompetitor(existingCompetitor, false); this.Dispose(); return; } //AddNewCompetitor(newCompetitor, false); //this.Dispose(); //return; } else //just save new competitor to the competitors list { if (newCompetitor.FirstName.Equals("") && newCompetitor.LastName.Equals("")) { if (MessageBox.Show("Are you sure you would like to add competitor with empty name fields?", "Please confirm", MessageBoxButtons.YesNo) == DialogResult.No) { return; } } AddNewCompetitor(newCompetitor); this.Dispose(); return; } } // //2. some existing competitor was originally selected in the grid // //possible cases: //update or cancel // if (competitor != null && haveToAddEventEntry)// && !competitorDataInput.ChangesMade(competitor)) { if (MessageBox.Show("Are you sure you would like to proceed?", "Confirmation Dialog", MessageBoxButtons.YesNo) == DialogResult.No) { this.Dispose(); } EventEntry newEventEntry = new EventEntry(); newEventEntry.competitor = competitor; Event currentEvent = raceInformationControl.GetSelectedEvent(); if (currentEvent != null) { newEventEntry.eventID = currentEvent.ID; DataManager.Instance.EventEntries.Add(newEventEntry); } CompetitorRace cr = new CompetitorRace(newEventEntry.competitorID); cr.tagID = competitor.TagNumber; cr.tagID2 = competitor.TagNumber2; cr.bikeNumber = competitor.BikeNumber; //cr.className = newCompetitor.//newEventEntry.className; cr.raceParent = raceInformationControl.GetCurrentSession(); raceInformationControl.GetCurrentSession().AddCompetitorRace(cr); UpdatePassings(); this.Dispose(); } else if(competitor != null && !haveToAddEventEntry) { if (MessageBox.Show("Are you sure you would like to save the changes?", "Confirmation Dialog", MessageBoxButtons.YesNo) == DialogResult.No) { this.Dispose(); } else { bool updateResult = competitorDataInput.UpdateCompetitorData(ref competitor); if (!updateResult) { MessageBox.Show("There was a problem with the entered data."); return; } UpdatePassings(); this.Dispose(); } } }
/// <summary> /// This method should be used for adding competitor race /// from race instead of using competitorRaceList directly, /// because this method properly handles passings info objects /// that are associated with the race. /// </summary> public bool AddCompetitorRace(CompetitorRace cr) { if (competitorRaceList.Contains(cr)) return false; competitorRaceList.Add(cr); cr.raceParent = this; //List<PassingsInfo> toRemove = new List<PassingsInfo>(); //foreach (PassingsInfo pi in this.passings) //{ // if (cr.checkTag(pi.ID)) // { // pi.competitorID = cr.competitorID; // pi.firstName = cr.firstName; // pi.lastName = cr.lastName; // pi.CompetitorRace = cr; // toRemove.Add(pi); // } //} ////make sure passings are sorted before they are added //toRemove.Sort(delegate(PassingsInfo p1, PassingsInfo p2) //{ // if (p1.Time < p2.Time) return -1; // else if (p1.Time > p2.Time) return 1; // else return 0; //}); //foreach (PassingsInfo pi in toRemove) //{ // cr.passings.Add(pi); // this.passings.Remove(pi); //} //instead: CompetitorRace crNull = FindNullCR(cr.tagID);//, cr.tagID2); CompetitorRace crNull2 = FindNullCR(cr.tagID2); if (crNull != null) { //copy passings from crNull to cr List<PassingsInfo> toRemove = new List<PassingsInfo>(); foreach (PassingsInfo pi in crNull.passings) { if (cr.checkTag(pi.ID)) { pi.competitorID = cr.competitorID; pi.firstName = cr.firstName; pi.lastName = cr.lastName; pi.CompetitorRace = cr; if (!toRemove.Contains(pi)) toRemove.Add(pi); } } //make sure passings are sorted before they are added toRemove.Sort(delegate(PassingsInfo p1, PassingsInfo p2) { if (p1.Time < p2.Time) return -1; else if (p1.Time > p2.Time) return 1; else return 0; }); foreach (PassingsInfo pi in toRemove) { cr.passings.Add(pi); if(crNull.passings.Contains(pi)) crNull.passings.Remove(pi); } //finally, remove crNull altogether: this.competitorRaceList.Remove(crNull); } else { //do nothing } if (crNull2 != null) { //copy passings from crNull to cr List<PassingsInfo> toRemove = new List<PassingsInfo>(); foreach (PassingsInfo pi in crNull2.passings) { if (cr.checkTag(pi.ID)) { pi.competitorID = cr.competitorID; pi.firstName = cr.firstName; pi.lastName = cr.lastName; pi.CompetitorRace = cr; if(!toRemove.Contains(pi)) toRemove.Add(pi); } } //make sure passings are sorted before they are added toRemove.Sort(delegate(PassingsInfo p1, PassingsInfo p2) { if (p1.Time < p2.Time) return -1; else if (p1.Time > p2.Time) return 1; else return 0; }); foreach (PassingsInfo pi in toRemove) { cr.passings.Add(pi); if (crNull2.passings.Contains(pi)) crNull2.passings.Remove(pi); } //finally, remove crNull altogether: this.competitorRaceList.Remove(crNull2); } cr.SortPassings(); return true; }
//when a cr's tag id (s) change, try to re-match passings //1. if some nullCRs exist, look if their passings can be remapped to cr and remove nullCR //2. if passings in cr become unmapped (cr's tag id no longer matches passings'), create null CRs //and copy passings there; first check if such nullCR exists already public void ResetPassings(CompetitorRace cr) { //1. Find, whether exising nullCRs' passings can be reassociated with the new cr, //and if yes, perform this reassociation List<PassingsInfo> toRemove = new List<PassingsInfo>();//possibly removed from nullCRs foreach (CompetitorRace cmpRace in competitorRaceList) { if (!cmpRace.isNull) continue; foreach (PassingsInfo pi in cmpRace.passings) { if (cr.checkTag(pi.ID)) { toRemove.Add(pi); } } } //now we have a list of passings that can be re-associated with cr: foreach (PassingsInfo pi in toRemove) { if (!cr.passings.Contains(pi)) { pi.competitorID = cr.competitorID; pi.firstName = cr.firstName; pi.lastName = cr.lastName; pi.CompetitorRace = cr; cr.passings.Add(pi); } } //finally, remove those passings from nullCRs foreach (CompetitorRace cmpRace in competitorRaceList) { if (!cmpRace.isNull) continue; foreach (PassingsInfo pi in toRemove) { if (cmpRace.passings.Contains(pi)) cmpRace.passings.Remove(pi); } } //remove nullCRs if their passings info is empty - that means they are not needed any longer List<CompetitorRace> nullCRsToRemove = new List<CompetitorRace>(); foreach (CompetitorRace cmpRace in competitorRaceList) { if (!cmpRace.isNull) continue; if (cmpRace.passings.Count == 0) nullCRsToRemove.Add(cmpRace); } foreach (CompetitorRace cmpRace in nullCRsToRemove) { if(competitorRaceList.Contains(cmpRace)) competitorRaceList.Remove(cmpRace); } //2. on the other hand, if cr's tags no longer match its PassingsInfo, //create nullCRs for them and copy stuff over List<PassingsInfo> toAdd = new List<PassingsInfo>(); TagId tagID1 = new TagId("-1"); TagId tagID2 = new TagId("-1"); foreach (PassingsInfo pi in cr.passings) { if (!cr.checkTag(pi.ID)) { toAdd.Add(pi); //if there are two different ids in the tags for cr (which is possible), //have to get both of them if (tagID1.Value.Equals("-1")) tagID1 = pi.ID; else if (tagID1.Value.Equals(pi.ID.Value)) continue; else if (tagID2.Value.Equals("-1")) tagID2 = pi.ID; else if (tagID2.Value.Equals(pi.ID.Value)) continue; } } if (toAdd.Count == 0) return; //search for existing nullCRs that may be used CompetitorRace nullCR = null; CompetitorRace nullCR2 = null; //foreach (CompetitorRace cmpRace in competitorRaceList) //{ // if (!cmpRace.isNull) // continue; // if (cmpRace.tagID.Equals(tagID1)) // { // nullCR = cmpRace; // } //} if (nullCR == null) { nullCR = new CompetitorRace(); nullCR.isNull = true; nullCR.competitorID = (-1) * DataManager.getNextID(); nullCR.tagID = tagID1; nullCR.tagID2 = tagID1; nullCR.bikeNumber = "0"; nullCR.firstName = ""; nullCR.lastName = "Unassigned"; nullCR.raceParent = this; } if (nullCR2 == null) { nullCR2 = new CompetitorRace(); nullCR2.isNull = true; nullCR2.competitorID = (-1) * DataManager.getNextID(); nullCR2.tagID = tagID2; nullCR2.tagID2 = tagID2; nullCR2.bikeNumber = "0"; nullCR2.firstName = ""; nullCR2.lastName = "Unassigned"; nullCR2.raceParent = this; } foreach (PassingsInfo pi in toAdd) { if (!nullCR.passings.Contains(pi) && pi.ID.Equals(nullCR.tagID)) { nullCR.passings.Add(pi); pi.competitorID = nullCR.competitorID; pi.firstName = nullCR.firstName; pi.lastName = nullCR.lastName; pi.CompetitorRace = nullCR; } if (!nullCR2.passings.Contains(pi) && pi.ID.Equals(nullCR2.tagID)) { nullCR2.passings.Add(pi); pi.competitorID = nullCR2.competitorID; pi.firstName = nullCR2.firstName; pi.lastName = nullCR2.lastName; pi.CompetitorRace = nullCR2; } if (cr.passings.Contains(pi)) cr.passings.Remove(pi); } if (!competitorRaceList.Contains(nullCR) && nullCR.passings.Count > 0) competitorRaceList.Add(nullCR); if (!competitorRaceList.Contains(nullCR2) && nullCR2.passings.Count > 0) competitorRaceList.Add(nullCR2); }
/// <summary> /// This method should be used for removing competitor race /// from race instead of using competitorRaceList directly, /// because this method properly handles passings info objects /// that are associated with the race. /// </summary> public bool RemoveCompetitorRace(CompetitorRace cr) { if (!competitorRaceList.Contains(cr)) return false; //passings from the removed cr should //be copied to the passings list List<PassingsInfo> toRemove = new List<PassingsInfo>(); foreach (PassingsInfo pi in cr.passings) { pi.competitorID = -1; pi.firstName = ""; pi.lastName = ""; pi.CompetitorRace = null; if (!toRemove.Contains(pi)) toRemove.Add(pi); } //make sure passings are sorted before they are added to the race toRemove.Sort(delegate(PassingsInfo p1, PassingsInfo p2) { if (p1.Time < p2.Time) return -1; else if (p1.Time > p2.Time) return 1; else return 0; }); // CompetitorRace crNull = FindNullCR(cr.tagID);//, cr.tagID2); CompetitorRace crNull2 = FindNullCR(cr.tagID2); if (crNull == null) { crNull = new CompetitorRace(); crNull.isNull = true; crNull.competitorID = (-1) * DataManager.getNextID(); crNull.tagID = cr.tagID; crNull.tagID2 = cr.tagID; crNull.bikeNumber = cr.bikeNumber; crNull.firstName = ""; crNull.lastName = "Unassigned"; crNull.raceParent = this; } if (crNull2 == null) { crNull2 = new CompetitorRace(); crNull2.isNull = true; crNull2.competitorID = (-1) * DataManager.getNextID(); crNull2.tagID = cr.tagID2; crNull2.tagID2 = cr.tagID2; crNull2.bikeNumber = cr.bikeNumber; crNull2.firstName = ""; crNull2.lastName = "Unassigned"; crNull2.raceParent = this; } // foreach (PassingsInfo pi in toRemove) { cr.passings.Remove(pi); //OLD VERSION: copied to this.passings //if (!this.passings.Contains(pi)) // this.passings.Add(pi); //instead, add this to crNull: if (!crNull.passings.Contains(pi) && pi.ID.Equals(crNull.tagID)) { crNull.passings.Add(pi); pi.competitorID = crNull.competitorID; pi.firstName = crNull.firstName; pi.lastName = crNull.lastName; pi.CompetitorRace = crNull; } if (!crNull2.passings.Contains(pi) && pi.ID.Equals(crNull2.tagID)) { crNull2.passings.Add(pi); pi.competitorID = crNull2.competitorID; pi.firstName = crNull2.firstName; pi.lastName = crNull2.lastName; pi.CompetitorRace = crNull2; } } //add new crNull to the list of CRs: if(crNull.passings.Count > 0) competitorRaceList.Add(crNull); if(crNull2.passings.Count > 0) competitorRaceList.Add(crNull2); competitorRaceList.Remove(cr); if(disambiuationCRDict.Values.Contains(cr)) { TagId foundKey; foreach (TagId k in disambiuationCRDict.Keys) { if (disambiuationCRDict[k] == cr) foundKey = k; } if(disambiuationCRDict.ContainsKey(foundKey)) disambiuationCRDict.Remove(foundKey); } return true; }
public void ReassociatePassings(CompetitorRace newCR) { //use carefully! this should be used only when deambiguating //CRs with same tags, and the newCR is given all passings from //other (fully legitimate) CRs //this method should be called after resetPassings(), which //moves passings from null-CRs (if any of them exist) //newCR should be already in the list! //iterate through all non-null CRs and move their passings to newCR List<PassingsInfo> toAdd = new List<PassingsInfo>(); foreach (CompetitorRace cr in competitorRaceList) { if (cr.isNull || cr.Equals(newCR)) continue; if (newCR.checkTag(cr.tagID) || newCR.checkTag(cr.tagID2)) { foreach (PassingsInfo pi in cr.passings) { if (!newCR.passings.Contains(pi)) { toAdd.Add(pi); } } } } //remove from "old" CRs foreach (CompetitorRace cr in competitorRaceList) { if (cr.isNull || cr.Equals(newCR)) continue; foreach (PassingsInfo pi in toAdd) { if (cr.passings.Contains(pi)) cr.passings.Remove(pi); } } //finally, reassociate all "toAdd" passings foreach (PassingsInfo pi in toAdd) { pi.competitorID = newCR.competitorID; pi.firstName = newCR.firstName; pi.lastName = newCR.lastName; pi.CompetitorRace = newCR; } }