private void timerAddResults() { try { lock(timerAddResultsRunningLock) { if (timerAddResultsRunning) return; timerAddResultsRunning = true; } Trace.WriteLine("timerAddResults_Tick on Thread \"" + Thread.CurrentThread.Name + "\" " + Thread.CurrentThread.ManagedThreadId.ToString()); bool retry = true; Structs.Competitor comp = new Structs.Competitor(); while (retry) { Structs.Competitor[] comps = CommonCode.GetCompetitors(); if (comps.Length == 0) return; comp = comps[rnd.Next(comps.Length)]; Structs.CompetitorResult[] results; try { results = CommonCode.GetCompetitorResults(comp.CompetitorId); if (results.Length < 2) retry = false; } catch(Exception exc) { Trace.WriteLine(exc.ToString()); retry = false; } } Structs.Station[] stations = CommonCode.GetStations(); foreach(Structs.Station station in stations) { Structs.CompetitorResult res = new Structs.CompetitorResult(); res.CompetitorId = comp.CompetitorId; res.FigureHits = rnd.Next(station.Figures); res.Hits = rnd.Next(station.Shoots); res.Points = 0; res.Station = station.StationId; res.StationFigureHits = res.Hits.ToString() + ";"; CommonCode.NewCompetitorResult(res); } } finally { timerAddResultsRunning = false; } }
internal Structs.CompetitorResult getCompetitorResult(int competitorsId, int stationNr) { Trace.WriteLine("CDatabase: Entering getCompetitorResults(" + competitorsId.ToString() + "," + stationNr.ToString() + ")"); foreach(DatabaseDataset.CompetitorResultsRow row in Database.CompetitorResults) { DatabaseDataset.StationsRow stationsRow = (DatabaseDataset.StationsRow)Database.Stations.Select("StationId=" + row.StationId.ToString())[0]; if (row.CompetitorId == competitorsId & stationsRow.StationNr == stationNr) { Structs.CompetitorResult compresult = new Structs.CompetitorResult(); compresult.CompetitorId = row.CompetitorId; compresult.Hits = row.Hits; compresult.ResultId = row.ResultId; compresult.Station = stationsRow.StationNr; compresult.FigureHits = row.FigureHits; compresult.Points = row.Points; compresult.StationFigureHits = row.StationFigureHits; return compresult; } } throw new CannotFindIdException("Could not find competitorid and station"); }
private void saveCurrent() { DateTime startTime = DateTime.Now; Trace.WriteLine("FResults: saveCurrent started from thread \"" + Thread.CurrentThread.Name + "\" " + " ( " + Thread.CurrentThread.ManagedThreadId.ToString() + " ) " + DateTime.Now.ToLongTimeString()); // If no current user, abort if (this.currentCompetitorId == -1) return; bool allHitsIsZero = true; // Get all values into hash tables for easy retreival Hashtable hitsTable = new Hashtable(); foreach(SafeTextBox safeTextBox in resultFigures.ToArray(typeof(SafeTextBox))) { int parse = 0; try { parse = int.Parse(safeTextBox.Text); } catch(Exception) { } hitsTable.Add(safeTextBox.Name, parse); if (parse > 0) allHitsIsZero = false; } Hashtable pointsTable = new Hashtable(); foreach(SafeTextBox safeTextBox in resultPoints.ToArray(new SafeTextBox().GetType())) { int parse = 0; try { parse = int.Parse(safeTextBox.Text); } catch(Exception) { } pointsTable.Add(safeTextBox.Name, parse); if (parse > 0) allHitsIsZero = false; } // go throw all stations, searching for values and adding // all together Structs.Station[] stations = CommonCode.GetStations(); foreach(Structs.Station station in stations) { int totHits = 0; int totFigureHits = 0; string stationFigureHits = ""; for(int figure=1; figure<=station.Figures ; figure++) { int thisHit = (int)hitsTable["Figure" + station.StationNr.ToString() + "-" + figure.ToString()]; totHits += thisHit; if (thisHit > 0) totFigureHits++; stationFigureHits += thisHit.ToString() + ";"; } // ok, we have all stuff for this station. // does this station already exist for this competitor? try { Structs.CompetitorResult res = CommonCode.GetCompetitorResult( currentCompetitorId, station.StationNr); // result already exist. Edit and return. if (!allHitsIsZero) { res.Hits = totHits; res.FigureHits = totFigureHits; res.Points = (int)pointsTable["Points" + station.StationNr.ToString()]; res.StationFigureHits = stationFigureHits; CommonCode.UpdateCompetitorResult(res, false); } else { CommonCode.DelCompetitorResult(res); } } catch (CannotFindIdException) { // Didnt exist. Create new Structs.CompetitorResult res = new Structs.CompetitorResult(); res.Hits = totHits; res.FigureHits = totFigureHits; res.Points = (int)pointsTable["Points" + station.StationNr.ToString()]; res.StationFigureHits = stationFigureHits; res.CompetitorId = currentCompetitorId; res.Station = station.StationNr; CommonCode.NewCompetitorResult(res); } } Structs.Competitor competitor = CommonCode.GetCompetitor(currentCompetitorId); if (this.txtFinalPlace.Text == "") competitor.FinalShootingPlace = 100; else { try { competitor.FinalShootingPlace = int.Parse(this.txtFinalPlace.Text); if (competitor.FinalShootingPlace == 0) competitor.FinalShootingPlace = 100; } catch(Exception exc) { Trace.WriteLine("FResults: saveCurrent Exception"+ " while parsing FinalShootingPlace: " + exc.ToString()); MessageBox.Show("Placering efter särskjutning " + "verkar inte vara en siffra. Avbryter.", "Felmeddelande", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } } CommonCode.UpdateCompetitor(competitor); resultIsChanged = false; TimeSpan span = DateTime.Now - startTime; Trace.WriteLine("FResults: saveCurrent ended after " + span.TotalMilliseconds.ToString() + " ms."); int patrolId = (int)ddPatrols.SelectedValue; Structs.Patrol patrol = CommonCode.GetPatrol(patrolId); int compsInPatrol = CommonCode.GetCompetitorsCountPatrol(patrol); int compsInPatrolWithResult = CommonCode.GetCompetitorsWithResultCountPatrol(patrol); if (compsInPatrol == compsInPatrolWithResult) { DialogResult res = MessageBox.Show( "Det finns nu resultat för alla skyttar i denna patrull." + "Vill du skriva ut resultat för denna patrull?", "Utskrift av resultat", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (res == DialogResult.Yes) { try { PrintPatrolResult(patrolId.ToString(), null); } catch (Exception exc) { Trace.WriteLine("Exception while printing current patrol:" + exc.ToString()); } } } }
internal Structs.CompetitorResult[] getCompetitorResults(int competitorsId) { Trace.WriteLine("CDatabase: Entering getCompetitorResults(" + competitorsId.ToString() + ")"); ArrayList compresults = new ArrayList(); Structs.CompetitorResult compresult = new Structs.CompetitorResult(); foreach(DatabaseDataset.CompetitorResultsRow row in Database.CompetitorResults.Select( "CompetitorId=" + competitorsId.ToString())) { if (row.CompetitorId == competitorsId) { DatabaseDataset.StationsRow stationsRow = (DatabaseDataset.StationsRow)Database.Stations.Select("StationId=" + row.StationId.ToString())[0]; compresult.CompetitorId = row.CompetitorId; compresult.Hits = row.Hits; compresult.ResultId = row.ResultId; compresult.Station = stationsRow.StationNr; compresult.FigureHits = row.FigureHits; compresult.Points = row.Points; compresult.StationFigureHits = row.StationFigureHits; compresults.Add(compresult); } } return (Structs.CompetitorResult[])compresults.ToArray(compresult.GetType()); }