public static Dictionary <int, double> Price(this BasicCompetition competition) { var result = competition.Price(true); if (!CacheFile) { return(result); } string pricingResult; if (HttpContext.Current != null) { pricingResult = HttpContext.Current.Server.MapPath("~/Pricer/"); } else { pricingResult = "C:/Users/xavier/Documents/Pricer/"; } try { XmlSerializer xs = new XmlSerializer(typeof(PricerResult)); using (StreamWriter wr = new StreamWriter(pricingResult + competition.Name + ".xml")) { var prizes = competition.Prizes; var pricerResult = new PricerResult { Prizes = prizes.Skip(1).Aggregate(prizes.First().ToString(), (current, prize) => current + ("-" + prize)), Scenarios = competition.Simulation.Select( x => { var allTeams = x.Result.OrderByDescending(t => t.Value); return(new Scenario { Teams = allTeams.Skip(1) .Aggregate(allTeams.First().Key.ToString(), (current, t) => current + ("-" + t.Key)), Value = x.Value }); }).ToArray() }; xs.Serialize(wr, pricerResult); } } catch (Exception) { } return(result); }
private static BasicCompetition GetCompetion(string competitionName) { BasicCompetition competition = null; if (!Competitions.TryGetValue(competitionName, out competition)) { competition = new StrengthCompetition(competitionName); if (CacheFile && HttpContext.Current != null) { var pricingResult = HttpContext.Current.Server.MapPath("~/Pricer/"); if (File.Exists(pricingResult + competitionName + ".xml")) { try { XmlSerializer xs = new XmlSerializer(typeof(PricerResult)); using (StreamReader rd = new StreamReader(pricingResult + competitionName + ".xml")) { var pricerResult = (PricerResult)(xs.Deserialize(rd)); var prizes = pricerResult.Prizes.Split('-').Select(x => Convert.ToInt32(x)).ToList(); competition.Simulation = new HashSet <SimulationResult>(pricerResult.Scenarios.Select(x => { var teamsId = x.Teams.Split('-'); var length = teamsId.Length; var results = new Dictionary <int, int>(length); for (int i = 0; i < length; i++) { var teamId = teamsId[i]; results[Convert.ToInt32(teamId)] = prizes[i]; } var simulationResult = new SimulationResult(results, x.Value); return(simulationResult); })); competition.Simulation.TrimExcess(); } } catch (Exception) { } } } Competitions[competitionName] = competition; } return(competition); }