/// <summary> /// Updates a weekly play. /// </summary> /// <param name="weeklyPlay">WeeklyPlay to be updated</param> /// <returns>WeeklyPlay</returns> public HttpResponseMessage PostWeeklyPlay(WeeklyPlay weeklyPlay) { if (ModelState.IsValid) { using (BowlingJackpotRepository rep = new BowlingJackpotRepository()) { var result = rep.UpdateWeeklyPlay(weeklyPlay); var statusCode = HttpStatusCode.Created; if (result == null) statusCode = HttpStatusCode.Forbidden; return this.Request.CreateResponse<WeeklyPlay>(statusCode, result); } } else return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); }
public WeeklyPlay AddWeeklyPlay(WeeklyPlay wp) { context.WeeklyPlays.Add(wp); Save(); return wp; }
private Decimal GetPreviousNonWinningPayouts(WeeklyPlay wp) { var lastWin = GetLastWeeklyPlayWin(wp); Decimal? payout; if (lastWin == null) { payout = context.WeeklyPlays.Where(i => i.Id != wp.Id && i.Play_Date < wp.Play_Date).Sum(i => i.Payout_Amount); } else { var lastWinStartDate = GetLastStartWeek(lastWin.Play_Date); var currentPlayStartDate = GetLastStartWeek(wp.Play_Date); payout = context.WeeklyPlays.Where(i => i.Id != wp.Id && i.Play_Date > lastWinStartDate && i.Play_Date < currentPlayStartDate).Sum(i => i.Payout_Amount); } if (payout.HasValue) return payout.Value; return (Decimal)0; }
private WeeklyPlay GetLastWeeklyPlayWin(WeeklyPlay wp) { return context.WeeklyPlays.Where(i => i.Pins == 10 && i.Id != wp.Id && i.Play_Date < wp.Play_Date).OrderByDescending(j => j.Play_Date).FirstOrDefault(); }
public WeeklyPlay UpdateWeeklyPlay(WeeklyPlay wp) { var playToUpdate = context.WeeklyPlays.Where(i => i.Id == wp.Id).FirstOrDefault(); playToUpdate.Pins = wp.Pins; if (playToUpdate != null) { //Strike Payout Full Amount if (playToUpdate.Pins == 10) playToUpdate.Payout_Amount = GetJackpotSinceLastWin(playToUpdate).JackpotBalance; //Pay 10% else playToUpdate.Payout_Amount = GetJackpotSinceLastWin(playToUpdate).JackpotBalance * (Decimal)0.1; Save(); //Update Payouts After playToUpdate var currentWeeklyPlay = GetCurrentWeeklyPlay(); var lastPlay = context.WeeklyPlays.OrderByDescending(i => i.Play_Date).FirstOrDefault(); if ((currentWeeklyPlay == null && playToUpdate.Id != lastPlay.Id) || (currentWeeklyPlay != null && playToUpdate.Id != currentWeeklyPlay.Id)) { var payoutsAfterPlay = context.WeeklyPlays.Where(i => i.Play_Date > playToUpdate.Play_Date).OrderBy(i => i.Play_Date); foreach (var p in payoutsAfterPlay) { if (p.Payout_Amount != null) { //Strike Payout Full Amount if (p.Pins.Value == 10) p.Payout_Amount = GetJackpotSinceLastWin(p).JackpotBalance; //Pay 10% else p.Payout_Amount = GetJackpotSinceLastWin(p).JackpotBalance * (Decimal)0.1; } } } } Save(); return playToUpdate; }
public WeeklyPlay PickWeeklyPlayer() { var currentWeeksPlay = GetCurrentWeeklyPlay(); if (currentWeeksPlay == null) { var currentWeeklyTickets = context.Tickets.Where(i => i.Purchase_Date > weekStartDate && i.Purchase_Date < weekEndDate); if (currentWeeklyTickets.Any()) { var randomTicket = GetRandomTicket(currentWeeklyTickets); var weeklyPlay = new WeeklyPlay() { Play_Date = DateTime.UtcNow, User_Id = randomTicket.User_Id, Pins = 0 }; context.WeeklyPlays.Add(weeklyPlay); Save(); return weeklyPlay; } return null; } else { return currentWeeksPlay; } }
public Jackpot GetJackpotSinceLastWin(WeeklyPlay wp) { var jackpot = new Jackpot(); WeeklyPlay lastWin = GetLastWeeklyPlayWin(wp); if (lastWin == null) { var lastStartWeek = GetLastStartWeek(wp.Play_Date); jackpot.JackpotBalance = (Decimal)context.Tickets.Where(i => i.Purchase_Date < lastStartWeek).Sum(i => i.TicketAmount.Ticket_Amount) - GetPreviousNonWinningPayouts(wp); } else { DateTime lastStartWeek = GetLastStartWeek(lastWin.Play_Date); DateTime currentPlayWeek = GetLastStartWeek(wp.Play_Date); var de = (Decimal)context.Tickets.Where(i => i.Purchase_Date > lastStartWeek && i.Purchase_Date < currentPlayWeek).Sum(i => i.TicketAmount.Ticket_Amount); jackpot.JackpotBalance = de - GetPreviousNonWinningPayouts(wp); } return jackpot; }