This commit is contained in:
Michael Simard
2020-12-13 09:40:01 -06:00
parent 172b4fed06
commit 550f986dd7
4 changed files with 127 additions and 68 deletions

View File

@@ -18,7 +18,7 @@ struct StatsController: RouteCollection {
statsRoute.get("totalWins", use: totalWins)
statsRoute.get("totalLosses", use: totalLosses)
statsRoute.get("overall", use: overall)
statsRoute.get("all", use: all)
statsRoute.get("all", use: test)
statsRoute.get("allDaily", use: allDaily)
statsRoute.get("test", use: test)
statsRoute.post("logMatch", use: logMatch)
@@ -26,10 +26,22 @@ struct StatsController: RouteCollection {
statsRoute.get("history", use: history)
}
func deleteMatch(req: Request) throws -> EventLoopFuture<HTTPStatus> {
guard let id = req.parameters.get("id", as: UUID.self) else {
throw Abort(.badRequest)
}
return Match.find(id, on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { $0.delete(on: req.db) }
.map { .ok }
}
func history(req: Request) throws -> EventLoopFuture<[Match]> {
return Match.query(on: req.db).sort(\.$date, .descending).limit(20).all().map { (matches) -> ([Match]) in
return matches
@@ -223,6 +235,8 @@ struct StatsController: RouteCollection {
}))
// print ( Date().timeIntervalSince(startTime))
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)
let monthlyStats = previousMonths.reversed().map { (codDate) -> MonthStats in
let relevantMatches = matches.filter { (match) -> Bool in
@@ -245,9 +259,7 @@ struct StatsController: RouteCollection {
}
// print ( Date().timeIntervalSince(startTime))
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)
return AllStats.init(overall: overallStats,mwStats: mwStats, bocwStats: bocwStats, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeWinLossRatios, mostRecentRecord: "\(mostRecentDailyStats.totalWins) - \(mostRecentDailyStats.totalLosses)")
}
@@ -471,76 +483,74 @@ struct StatsController: RouteCollection {
func all(req: Request) throws -> EventLoopFuture<AllStats> {
// func all(req: Request) throws -> EventLoopFuture<AllStats> {
//
// var date = getStartDate()
// var previousMonths:[CODDate] = []
//
// repeat {
// //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats>
// previousMonths.append(CODDate(month: date.month, year: date.year, day: 15, hour:6, minute: 0))
// date = Calendar.current.date(byAdding: .month, value: 1, to: date)!
// }while ( date < Calendar.current.date(byAdding: .month, value: 1, to: Date())!)
//
//
// func getMonthStats (_ remaining: ArraySlice<CODDate>, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> {
// var remaining = remaining
// if let first = remaining.popLast() {
//
// return getstatsForMonth(year: first.year, month: first.month, req: req).flatMap { [remaining, allMonthlyStats] (stats) -> EventLoopFuture<[MonthStats]> in
// var allMonthlyStats = allMonthlyStats
// allMonthlyStats.append(MonthStats(month: first.month, year: first.year, stats:stats ))
// return getMonthStats(remaining, allMonthlyStats:&allMonthlyStats, eventLoop: eventLoop)
// }
//
// } else {
// return req.eventLoop.makeSucceededFuture(allMonthlyStats)
// }
// }
//
// var stats:[MonthStats] = []
// let monthstats = getMonthStats(previousMonths[0..<previousMonths.count], allMonthlyStats:&stats, eventLoop: req.eventLoop)
// let cumulativeRatios = getCumulativeWinLossRatios(req: req)
// let mostRecentDayStats = mostRecentDailyStats(req: req)
//
// return try overall(req: req).and(monthstats).and(cumulativeRatios).and(mostRecentDayStats).map { arg -> AllStats in
//
// let (((overall, monthlyStats), cumulativeRatios), mostRecentDayStats) = arg
// let highestWinLossRatio = cumulativeRatios.reduce("0") { (highestRatio, dataPoint) -> String in
// if dataPoint.y > Double(highestRatio)!{
// return String(dataPoint.y)
// }
// return highestRatio
// }
//
//
// return AllStats.init(overall: overall,mwStats: overall, bocwStats: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios, mostRecentRecord: "\(mostRecentDayStats.totalWins) - \(mostRecentDayStats.totalLosses)")
// }
// }
//
func overall(req: Request) throws -> EventLoopFuture<OverallStats> {
var date = getStartDate()
var previousMonths:[CODDate] = []
repeat {
//let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats>
previousMonths.append(CODDate(month: date.month, year: date.year, day: 15, hour:6, minute: 0))
date = Calendar.current.date(byAdding: .month, value: 1, to: date)!
}while ( date < Calendar.current.date(byAdding: .month, value: 1, to: Date())!)
return Match.query(on: req.db).all().map { (matches) -> OverallStats in
let overallStats = self.getStats(matches: matches)
// print ( Date().timeIntervalSince(startTime))
func getMonthStats (_ remaining: ArraySlice<CODDate>, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> {
var remaining = remaining
if let first = remaining.popLast() {
return getstatsForMonth(year: first.year, month: first.month, req: req).flatMap { [remaining, allMonthlyStats] (stats) -> EventLoopFuture<[MonthStats]> in
var allMonthlyStats = allMonthlyStats
allMonthlyStats.append(MonthStats(month: first.month, year: first.year, stats:stats ))
return getMonthStats(remaining, allMonthlyStats:&allMonthlyStats, eventLoop: eventLoop)
}
} else {
return req.eventLoop.makeSucceededFuture(allMonthlyStats)
}
}
var stats:[MonthStats] = []
let monthstats = getMonthStats(previousMonths[0..<previousMonths.count], allMonthlyStats:&stats, eventLoop: req.eventLoop)
let cumulativeRatios = getCumulativeWinLossRatios(req: req)
let mostRecentDayStats = mostRecentDailyStats(req: req)
return try overall(req: req).and(monthstats).and(cumulativeRatios).and(mostRecentDayStats).map { arg -> AllStats in
let mwStats = self.getStats(matches: matches.filter({ (match) -> Bool in
return match.codGame == "mw"
}))
let bocwStats = self.getStats(matches: matches.filter({ (match) -> Bool in
return match.codGame == "bocw"
}))
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)
let (((overall, monthlyStats), cumulativeRatios), mostRecentDayStats) = arg
let highestWinLossRatio = cumulativeRatios.reduce("0") { (highestRatio, dataPoint) -> String in
if dataPoint.y > Double(highestRatio)!{
return String(dataPoint.y)
}
return highestRatio
}
return AllStats.init(overall: overall,mwStats: overall, bocwStats: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios, mostRecentRecord: "\(mostRecentDayStats.totalWins) - \(mostRecentDayStats.totalLosses)")
return OverallStats(overall: overallStats, mwStats: mwStats, bocwStats: bocwStats, mostRecentRecord: "\(mostRecentDailyStats.totalWins) - \(mostRecentDailyStats.totalLosses)")
}
}
func overall(req: Request) throws -> EventLoopFuture<Stats> {
let lossCount = Match.query(on: req.db)
.filter(\.$win == false)
.count()
let winCount = Match.query(on: req.db)
.filter(\.$win == true )
.count()
let combined = winCount.and(lossCount)
return combined.map { (winCount, lossCount) -> (Stats) in
return Stats.init(winLoss: self.getRatio(num: Double(winCount), den: Double(lossCount)), totalWins: winCount, totalLosses: lossCount)
}
}
private func getRatio( num:Double, den:Double) -> String {
var returnString = ""