public static void CalculateMatchPoints(this FishingMatch fishingMatch) { List <MatchEntry> sortedMatchEntries = fishingMatch.MatchEntries.OrderByDescending(matchEntry => matchEntry.Weight).ToList(); PositionToPointsMapping positionToPointsMapping = DataFileService.GetDataFile <PositionToPointsMapping>(DataFileType.PositionToPointsMapping) .SingleOrDefault(mapping => mapping.Id == fishingMatch.PositionToPointsMappingId); if (positionToPointsMapping == null) { throw new Exception("Position to points mapping not found"); } for (var index = 0; index < sortedMatchEntries.Count; index++) { sortedMatchEntries[index].Position = index + 1; if (Math.Abs(sortedMatchEntries[index].Weight) <= 0) { sortedMatchEntries[index].Points = positionToPointsMapping.DidNotWeighPoints; continue; } if (index > positionToPointsMapping.PositionToPoints.Count - 1) { sortedMatchEntries[index].Points = positionToPointsMapping.PositionToPoints.Last().Points; continue; } sortedMatchEntries[index].Points = positionToPointsMapping.PositionToPoints[index].Points; } }
public FishingMatch Get([FromUri] int id) { List <FishingMatch> matches = Get().ToList(); FishingMatch fishingMatchToReturn = matches.SingleOrDefault(match => match.Id == id); return(fishingMatchToReturn); }
public IHttpActionResult Post(int matchId, [FromBody] EntryToAdd entryToAdd) { List <FishingMatch> fishingMatches = DataFileService.GetDataFile <FishingMatch>(DataFileType.Matches).ToList(); FishingMatch fishingMatch = fishingMatches.SingleOrDefault(match => match.Id == matchId); if (fishingMatch == null) { return(NotFound()); } var matchEntry = new MatchEntry { AnglerName = entryToAdd.AnglerName, AnglerId = entryToAdd.AnglerId, Peg = entryToAdd.Peg, Weight = entryToAdd.Pounds + (entryToAdd.Ounces / OuncesInPound) }; fishingMatch.MatchEntries.Add(matchEntry); fishingMatch.CalculateMatchPoints(); DataFileService.WriteDataFile(DataFileType.Matches, fishingMatches); return(Created(string.Empty, entryToAdd)); }
public IHttpActionResult GetMatchEntries(int matchId) { FishingMatch fishingMatch = DataFileService.GetDataFile <FishingMatch>(DataFileType.Matches) .SingleOrDefault(match => match.Id == matchId); if (fishingMatch == null) { return(NotFound()); } return(Ok(fishingMatch.MatchEntries)); }
public IHttpActionResult CalculatePoints(int matchId) { List <FishingMatch> fishingMatches = Get().ToList(); FishingMatch fishingMatch = fishingMatches.SingleOrDefault(match => match.Id == matchId); if (fishingMatch == null) { return(NotFound()); } fishingMatch.CalculateMatchPoints(); DataFileService.WriteDataFile(DataFileType.Matches, fishingMatches); return(Ok()); }
public IEnumerable <PairResult> GetPairsMatch([FromUri] int id) { FishingMatch fishingMatch = Get(id); var pairResults = new List <PairResult>(); if (!fishingMatch.IsPairs) { return(pairResults); } foreach (MatchEntry matchEntry in fishingMatch.MatchEntries) { bool toAdd = !pairResults.Any(pair => pair.Peg1 == matchEntry.Peg || pair.Peg2 == matchEntry.Peg); if (!toAdd) { continue; } var matchEntryPair = new List <MatchEntry> { matchEntry }; MatchEntry pairedMatchEntry = fishingMatch.MatchEntries.Single(me => me.PairedWithPeg == matchEntry.Peg); matchEntryPair.Add(pairedMatchEntry); var pairResult = new PairResult { Angler1 = matchEntryPair[0].AnglerName, Angler2 = matchEntryPair[1].AnglerName, Peg1 = matchEntryPair[0].Peg, Peg2 = matchEntryPair[1].Peg, Weight = matchEntryPair[0].Weight + matchEntryPair[1].Weight }; if (pairResult.Peg1 == pairResult.Peg2) { pairResult.Peg2 = null; pairResult.Angler2 = "(weight doubled)"; } pairResults.Add(pairResult); } return(pairResults.OrderByDescending(result => result.Weight)); }