Added highest win loss ratio

This commit is contained in:
Michael Simard
2020-06-16 13:07:00 -05:00
parent 8c14a54781
commit 40e23960f1
2 changed files with 67 additions and 18 deletions

View File

@@ -205,9 +205,13 @@ struct StatsController: RouteCollection {
var stats:[MonthStats] = []
let monthstats = getMonthStats(previousMonths[0..<previousMonths.count], allMonthlyStats:&stats, eventLoop: req.eventLoop)
let highestRatio = getHighestWinLossRatio(req: req)
return try overall(req: req).and(monthstats).map { (overall, monthlyStats) -> 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<AllDailyStats>{
}
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<AllDailyStats>{
previousDays = previousDays.reversed()
let previousDays = getDaysPlayed().reversed()
func getDailyStats (_ remaining: ArraySlice<CODDate>, 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..<previousDays.count], allDailyStats:&stats, eventLoop: req.eventLoop)
let dailyStats = getDailyStats(Array(previousDays)[0..<previousDays.count], allDailyStats:&stats, eventLoop: req.eventLoop)
return dailyStats.map { (dailyStats) -> 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<AllRollingStats> {
//
//
// }
func getHighestWinLossRatio(req:Request) -> EventLoopFuture<String> {
var previousDays = getDaysPlayed().reversed()
func getHighestRatio (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], highestRatio: inout String, eventLoop: EventLoop) -> EventLoopFuture<String> {
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<String> 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..<previousDays.count], allDailyStats: &stats, highestRatio: &highestRatio, eventLoop: req.eventLoop)
}
private func getStartDate() -> Date {