From 40e23960f13163051f1ec2aa8ae6494667010e59 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Tue, 16 Jun 2020 13:07:00 -0500 Subject: [PATCH] Added highest win loss ratio --- Sources/App/Controllers/StatsController.swift | 81 +++++++++++++++---- Sources/App/Models/AllStats.swift | 4 +- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index b84bd4f..b3fdf68 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -205,9 +205,13 @@ struct StatsController: RouteCollection { var stats:[MonthStats] = [] let monthstats = getMonthStats(previousMonths[0.. AllStats in - return AllStats.init(overall: overall, byMonth: monthlyStats) + return try overall(req: req).and(monthstats).and(highestRatio).map { arg -> AllStats in + + let ((overall, monthlyStats), highestWinLoss) = arg + + return AllStats.init(overall: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLoss) } } @@ -238,10 +242,10 @@ struct StatsController: RouteCollection { private func getRatio( num:Double, den:Double) -> String { return String((Double(num) / Double(den)).truncate(places: 2)) - - } - func allDaily(req:Request) -> EventLoopFuture{ + } + + private func getDaysPlayed() -> [CODDate] { var date = getStartDate() var previousDays:[CODDate] = [] @@ -251,8 +255,17 @@ struct StatsController: RouteCollection { date = Calendar.current.date(byAdding: .day, value: 1, to: date)! } while (date < Date()) + + return previousDays + } + + + func allDaily(req:Request) -> EventLoopFuture{ - previousDays = previousDays.reversed() + + + + let previousDays = getDaysPlayed().reversed() func getDailyStats (_ remaining: ArraySlice, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> { @@ -261,10 +274,10 @@ struct StatsController: RouteCollection { return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats] (stats) -> EventLoopFuture<[DailyStats]> in var allDailyStats = allDailyStats - + let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in return total + Double(dailyStats.stats.totalWins) } - + let totalLosses = allDailyStats.reduce(Double(stats.totalLosses)) { (total, dailyStats) -> Double in return total + Double(dailyStats.stats.totalLosses) } @@ -280,28 +293,62 @@ struct StatsController: RouteCollection { var stats:[DailyStats] = [] - let dailyStats = getDailyStats(previousDays[0.. AllDailyStats in return AllDailyStats(dailyStats: dailyStats.filter({ (dailyStats) -> Bool in - if dailyStats.stats.totalWins == 0 && dailyStats.stats.totalLosses == 0 { return false } return true - }).reversed() - + }).reversed() + ) } } - // func allCumulativeStats(req:Request) -> EventLoopFuture { - // - // - // } + func getHighestWinLossRatio(req:Request) -> EventLoopFuture { + + var previousDays = getDaysPlayed().reversed() + + func getHighestRatio (_ remaining: ArraySlice, allDailyStats: inout [DailyStats], highestRatio: inout String, eventLoop: EventLoop) -> EventLoopFuture { + var remaining = remaining + if let first = remaining.popLast() { + + return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats, highestRatio] (stats) -> EventLoopFuture in + var allDailyStats = allDailyStats + + let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in + return total + Double(dailyStats.stats.totalWins) } + + let totalLosses = allDailyStats.reduce(Double(stats.totalLosses)) { (total, dailyStats) -> Double in + return total + Double(dailyStats.stats.totalLosses) + } + + let ratio = self.getRatio(num: totalWins, den: totalLosses) + var highestRatio = highestRatio + if Double(ratio)! > Double(highestRatio)! { + highestRatio = ratio + } + allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses))) + return getHighestRatio(remaining, allDailyStats:&allDailyStats, highestRatio:&highestRatio, eventLoop: eventLoop) + } + + } else { + return req.eventLoop.makeSucceededFuture(highestRatio) + } + } + + var stats:[DailyStats] = [] + var highestRatio = "0" + + + return getHighestRatio(Array(previousDays)[0.. Date { diff --git a/Sources/App/Models/AllStats.swift b/Sources/App/Models/AllStats.swift index 4cb0aa2..7216772 100644 --- a/Sources/App/Models/AllStats.swift +++ b/Sources/App/Models/AllStats.swift @@ -14,10 +14,12 @@ import Vapor final class AllStats: Content { var overall:Stats var byMonth: [MonthStats] + var highestWinLossRatio:String - init( overall:Stats, byMonth:[MonthStats]) { + init( overall:Stats, byMonth:[MonthStats], highestWinLossRatio:String) { self.overall = overall self.byMonth = byMonth + self.highestWinLossRatio = highestWinLossRatio } }