added most recent record

This commit is contained in:
Michael Simard
2020-06-21 00:53:06 -05:00
parent dae169f250
commit a5540fe844

View File

@@ -95,8 +95,6 @@ struct StatsController: RouteCollection {
} }
func statsForRecent(numberGames:Int, req:Request) -> EventLoopFuture<Stats> { func statsForRecent(numberGames:Int, req:Request) -> EventLoopFuture<Stats> {
let winCount = Match.query(on: req.db) let winCount = Match.query(on: req.db)
@@ -200,10 +198,9 @@ struct StatsController: RouteCollection {
} }
func mostRecentDailyStats (req:Request) -> EventLoopFuture<Stats>{ func mostRecentDailyStats (req:Request) -> EventLoopFuture<Stats>{
return getDaysPlayed(req: req).flatMap { (days) -> (EventLoopFuture<Stats>) in
let daysPlayed = getDaysPlayed() return self.getStatsForDay(year: days.first?.year ?? 0, month: days.first?.month ?? 0, day: days.first?.day ?? 0, req: req)
}
return getStatsForDay(year: daysPlayed.last?.year ?? 0, month: daysPlayed.last?.month ?? 0, day: daysPlayed.last?.day ?? 0, req: req)
} }
@@ -294,28 +291,45 @@ struct StatsController: RouteCollection {
} }
private func getDaysPlayed() -> [CODDate] { private func getDaysPlayed(req:Request) -> EventLoopFuture<[CODDate]> {
var date = getStartDate()
var previousDays:[CODDate] = []
repeat { return Match.query(on: req.db).sort(\.$date, .descending).all().map { (matches) -> ([CODDate]) in
previousDays.append(CODDate(month: date.month, year: date.year, day: date.day, hour: date.hour, minute: date.minute)) return matches.map { (match) -> CODDate in
date = Calendar.current.date(byAdding: .day, value: 1, to: date)! return CODDate(month: match.date.month, year: match.date.year, day: match.date.day, hour: match.date.hour, minute: match.date.minute)
} while (date < Date()) }.reduce([CODDate]()) { (datesPlayed, codDate) -> [CODDate] in
return previousDays 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<AllDailyStats>{ func allDaily(req:Request) -> EventLoopFuture<AllDailyStats>{
let previousDays = getDaysPlayed().reversed() return getDaysPlayed(req: req).flatMap { (previousDays) -> (EventLoopFuture<AllDailyStats>) in
func getDailyStats (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> { func getDailyStats (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> {
var remaining = remaining var remaining = remaining
if let first = remaining.popLast() { 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 return self.getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats] (stats) -> EventLoopFuture<[DailyStats]> in
var allDailyStats = allDailyStats var allDailyStats = allDailyStats
let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in
@@ -349,19 +363,24 @@ struct StatsController: RouteCollection {
) )
} }
}
} }
func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> { func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> {
let previousDays = getDaysPlayed().reversed() // let previousDays = getDaysPlayed().reversed()
return getDaysPlayed(req: req).flatMap { (previousDays) -> (EventLoopFuture<[DataPoint]>) in
func getRatios (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> { func getRatios (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> {
var remaining = remaining var remaining = remaining
if let first = remaining.popLast() { 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 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 var allDailyStats = allDailyStats
let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in
@@ -395,7 +414,7 @@ struct StatsController: RouteCollection {
return getRatios(Array(previousDays)[0..<previousDays.count], allDailyStats: &stats, cumulativeWinLossRatios: &cumulativeWinLossRatios, eventLoop: req.eventLoop) return getRatios(Array(previousDays)[0..<previousDays.count], allDailyStats: &stats, cumulativeWinLossRatios: &cumulativeWinLossRatios, eventLoop: req.eventLoop)
}
} }
} }