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] = [] var stats:[MonthStats] = []
let monthstats = getMonthStats(previousMonths[0..<previousMonths.count], allMonthlyStats:&stats, eventLoop: req.eventLoop) 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 try overall(req: req).and(monthstats).and(highestRatio).map { arg -> AllStats in
return AllStats.init(overall: overall, byMonth: monthlyStats)
let ((overall, monthlyStats), highestWinLoss) = arg
return AllStats.init(overall: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLoss)
} }
} }
@@ -240,8 +244,8 @@ struct StatsController: RouteCollection {
return String((Double(num) / Double(den)).truncate(places: 2)) return String((Double(num) / Double(den)).truncate(places: 2))
} }
func allDaily(req:Request) -> EventLoopFuture<AllDailyStats>{
private func getDaysPlayed() -> [CODDate] {
var date = getStartDate() var date = getStartDate()
var previousDays:[CODDate] = [] var previousDays:[CODDate] = []
@@ -252,7 +256,16 @@ struct StatsController: RouteCollection {
} while (date < Date()) } while (date < Date())
previousDays = previousDays.reversed() return previousDays
}
func allDaily(req:Request) -> EventLoopFuture<AllDailyStats>{
let previousDays = getDaysPlayed().reversed()
func getDailyStats (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> { func getDailyStats (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> {
@@ -280,12 +293,11 @@ struct StatsController: RouteCollection {
var stats:[DailyStats] = [] 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 dailyStats.map { (dailyStats) -> AllDailyStats in
return AllDailyStats(dailyStats: dailyStats.filter({ (dailyStats) -> Bool in return AllDailyStats(dailyStats: dailyStats.filter({ (dailyStats) -> Bool in
if dailyStats.stats.totalWins == 0 && dailyStats.stats.totalLosses == 0 { if dailyStats.stats.totalWins == 0 && dailyStats.stats.totalLosses == 0 {
return false return false
} }
@@ -298,10 +310,45 @@ struct StatsController: RouteCollection {
} }
// 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 { private func getStartDate() -> Date {

View File

@@ -14,10 +14,12 @@ import Vapor
final class AllStats: Content { final class AllStats: Content {
var overall:Stats var overall:Stats
var byMonth: [MonthStats] var byMonth: [MonthStats]
var highestWinLossRatio:String
init( overall:Stats, byMonth:[MonthStats]) { init( overall:Stats, byMonth:[MonthStats], highestWinLossRatio:String) {
self.overall = overall self.overall = overall
self.byMonth = byMonth self.byMonth = byMonth
self.highestWinLossRatio = highestWinLossRatio
} }
} }