From a5540fe844343a9068c612ce85ac6b148ae3fb53 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Sun, 21 Jun 2020 00:53:06 -0500 Subject: [PATCH] added most recent record --- Sources/App/Controllers/StatsController.swift | 223 ++++++++++-------- 1 file changed, 121 insertions(+), 102 deletions(-) diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index 03c3e64..93b3b91 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -72,7 +72,7 @@ struct StatsController: RouteCollection { } func getStatsForDay(year:Int, month:Int, day:Int, req: Request) -> EventLoopFuture{ - + let winCount = Match.query(on: req.db) .filter(\.$date >= getStartOfDay(day:day, month: month, year: year)) .filter(\.$date <= getEndOfDay(day: day, month: month, year: year)) @@ -95,8 +95,6 @@ struct StatsController: RouteCollection { } - - func statsForRecent(numberGames:Int, req:Request) -> EventLoopFuture { let winCount = Match.query(on: req.db) @@ -152,7 +150,7 @@ struct StatsController: RouteCollection { let calendar = Calendar.current var components = DateComponents() components.timeZone = TimeZone(identifier: "GMT") - + components.day = day components.month = month components.year = year @@ -174,7 +172,7 @@ struct StatsController: RouteCollection { } private func getStartDate() -> Date { - + let calendar = Calendar.current var components = DateComponents() components.timeZone = TimeZone(identifier: "GMT") @@ -188,29 +186,28 @@ struct StatsController: RouteCollection { private func createDate(day:Int, month:Int, year:Int, hour:Int, minute:Int) -> Date { let calendar = Calendar.current - - var components = DateComponents() - components.timeZone = TimeZone(identifier: "GMT") - components.day = day - components.month = month - components.year = year - components.hour = hour - components.minute = minute - return calendar.date(from: components)! + + var components = DateComponents() + components.timeZone = TimeZone(identifier: "GMT") + components.day = day + components.month = month + components.year = year + components.hour = hour + components.minute = minute + return calendar.date(from: components)! } func mostRecentDailyStats (req:Request) -> EventLoopFuture{ - - let daysPlayed = getDaysPlayed() - - return getStatsForDay(year: daysPlayed.last?.year ?? 0, month: daysPlayed.last?.month ?? 0, day: daysPlayed.last?.day ?? 0, req: req) + return getDaysPlayed(req: req).flatMap { (days) -> (EventLoopFuture) in + return self.getStatsForDay(year: days.first?.year ?? 0, month: days.first?.month ?? 0, day: days.first?.day ?? 0, req: req) + } } func all(req: Request) throws -> EventLoopFuture { - + var date = getStartDate() var previousMonths:[CODDate] = [] @@ -251,7 +248,7 @@ struct StatsController: RouteCollection { } return highestRatio } - + return AllStats.init(overall: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios, mostRecentRecord: "\(mostRecentDayStats.totalWins) - \(mostRecentDayStats.totalLosses)") } } @@ -286,7 +283,7 @@ struct StatsController: RouteCollection { let deno = (den != 0) ? den : 1 returnString = String((Double(num) / Double(deno)).truncate(places: 2)) - + if den == 0 { returnString = returnString + "+" } @@ -294,108 +291,130 @@ struct StatsController: RouteCollection { } - private func getDaysPlayed() -> [CODDate] { - var date = getStartDate() - var previousDays:[CODDate] = [] + private func getDaysPlayed(req:Request) -> EventLoopFuture<[CODDate]> { - repeat { - previousDays.append(CODDate(month: date.month, year: date.year, day: date.day, hour: date.hour, minute: date.minute)) - date = Calendar.current.date(byAdding: .day, value: 1, to: date)! - } while (date < Date()) - - return previousDays + return Match.query(on: req.db).sort(\.$date, .descending).all().map { (matches) -> ([CODDate]) in + return matches.map { (match) -> CODDate in + return CODDate(month: match.date.month, year: match.date.year, day: match.date.day, hour: match.date.hour, minute: match.date.minute) + }.reduce([CODDate]()) { (datesPlayed, codDate) -> [CODDate] in + + if datesPlayed.contains(where: { (existingDate) -> Bool in + if codDate.month == existingDate.month && codDate.year == existingDate.year && existingDate.day == codDate.day{ + return true + } + return false + }){ + return datesPlayed + }else { + + return datesPlayed + [codDate] + } + } + } + + // repeat { + // previousDays.append(CODDate(month: date.month, year: date.year, day: date.day, hour: date.hour, minute: date.minute)) + // date = Calendar.current.date(byAdding: .day, value: 1, to: date)! + // } while (date < Date()) + // + // return previousDays } func allDaily(req:Request) -> EventLoopFuture{ - let previousDays = getDaysPlayed().reversed() - - func getDailyStats (_ remaining: ArraySlice, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> { - var remaining = remaining - if let first = remaining.popLast() { - - return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats] (stats) -> EventLoopFuture<[DailyStats]> in - var allDailyStats = allDailyStats + return getDaysPlayed(req: req).flatMap { (previousDays) -> (EventLoopFuture) in + + func getDailyStats (_ remaining: ArraySlice, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> { + var remaining = remaining + if let first = remaining.popLast() { - 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) + return self.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) + } + + allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses))) + return getDailyStats(remaining, allDailyStats:&allDailyStats, eventLoop: eventLoop) } - allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses))) - return getDailyStats(remaining, allDailyStats:&allDailyStats, eventLoop: eventLoop) + } else { + return req.eventLoop.makeSucceededFuture(allDailyStats) } - - } else { - return req.eventLoop.makeSucceededFuture(allDailyStats) + } + + + var stats:[DailyStats] = [] + let dailyStats = getDailyStats(Array(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() + + ) } } - - - var stats:[DailyStats] = [] - let dailyStats = getDailyStats(Array(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() - - ) - } - } func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> { - let previousDays = getDaysPlayed().reversed() - - func getRatios (_ remaining: ArraySlice, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> { - var remaining = remaining - if let first = remaining.popLast() { - - return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats, cumulativeWinLossRatios] (stats) -> EventLoopFuture<[DataPoint]> in - var allDailyStats = allDailyStats + // let previousDays = getDaysPlayed().reversed() + + + return getDaysPlayed(req: req).flatMap { (previousDays) -> (EventLoopFuture<[DataPoint]>) in + + + + func getRatios (_ remaining: ArraySlice, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> { + var remaining = remaining + if let first = remaining.popLast() { - 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) + return self.getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats, cumulativeWinLossRatios] (stats) -> EventLoopFuture<[DataPoint]> 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) + } + + var cumulativeWinLossRatios = cumulativeWinLossRatios + if !(stats.totalWins == 0 && stats.totalLosses == 0) { + + let date = self.createDate(day: first.day, month: first.month, year: first.year, hour: first.hour + 6, minute:first.minute) // 6 hours to make sure we pick a time that isnt on borders of us time zones + print ("p \(date.timeIntervalSince1970)") + let x = Double(cumulativeWinLossRatios.count) + + let d = Date(timeIntervalSince1970: date.timeIntervalSince1970) + cumulativeWinLossRatios.append(DataPoint(x:x , y: (totalWins/totalLosses).truncate(places: 2), label:("\(Utilities.monthToString(month: d.month)) \(d.day)"))) + } + allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses))) + return getRatios(remaining, allDailyStats:&allDailyStats, cumulativeWinLossRatios:&cumulativeWinLossRatios, eventLoop: eventLoop) } - var cumulativeWinLossRatios = cumulativeWinLossRatios - if !(stats.totalWins == 0 && stats.totalLosses == 0) { - - let date = self.createDate(day: first.day, month: first.month, year: first.year, hour: first.hour + 6, minute:first.minute) // 6 hours to make sure we pick a time that isnt on borders of us time zones - print ("p \(date.timeIntervalSince1970)") - let x = Double(cumulativeWinLossRatios.count) - - let d = Date(timeIntervalSince1970: date.timeIntervalSince1970) - cumulativeWinLossRatios.append(DataPoint(x:x , y: (totalWins/totalLosses).truncate(places: 2), label:("\(Utilities.monthToString(month: d.month)) \(d.day)"))) - } - allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses))) - return getRatios(remaining, allDailyStats:&allDailyStats, cumulativeWinLossRatios:&cumulativeWinLossRatios, eventLoop: eventLoop) + } else { + return req.eventLoop.makeSucceededFuture(cumulativeWinLossRatios) } - - } else { - return req.eventLoop.makeSucceededFuture(cumulativeWinLossRatios) } + + var stats:[DailyStats] = [] + var cumulativeWinLossRatios:[DataPoint] = [DataPoint]() + + + return getRatios(Array(previousDays)[0..