From 550f986dd74ed012b27a113c47b926c677650f83 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Sun, 13 Dec 2020 09:40:01 -0600 Subject: [PATCH] updates --- Sources/App/Controllers/MatchController.swift | 30 ++++ Sources/App/Controllers/StatsController.swift | 146 ++++++++++-------- Sources/App/Models/AllStats.swift | 17 ++ Sources/App/routes.swift | 2 + 4 files changed, 127 insertions(+), 68 deletions(-) create mode 100644 Sources/App/Controllers/MatchController.swift diff --git a/Sources/App/Controllers/MatchController.swift b/Sources/App/Controllers/MatchController.swift new file mode 100644 index 0000000..e68b352 --- /dev/null +++ b/Sources/App/Controllers/MatchController.swift @@ -0,0 +1,30 @@ +// +// MatchController.swift +// App +// +// Created by Michael Simard on 12/13/20. +// + +import Foundation +import Fluent +import Vapor + + + +struct MatchController: RouteCollection { + func boot(routes: RoutesBuilder) throws { + let matchRoute = routes.grouped("cod-tracker","api", "match") + matchRoute.delete("delete", "id",":id", use: deleteMatch) + } + + func deleteMatch(req: Request) throws -> EventLoopFuture { + 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 } + } + +} diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index bd2696b..d21ac3b 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -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 { + 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 { +// func all(req: Request) throws -> EventLoopFuture { +// +// var date = getStartDate() +// var previousMonths:[CODDate] = [] +// +// repeat { +// //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture +// 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, 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.. 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 { - var date = getStartDate() - var previousMonths:[CODDate] = [] - - repeat { - //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture - 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, 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.. 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 { - - 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 = "" diff --git a/Sources/App/Models/AllStats.swift b/Sources/App/Models/AllStats.swift index f904bc9..e17e91e 100644 --- a/Sources/App/Models/AllStats.swift +++ b/Sources/App/Models/AllStats.swift @@ -45,6 +45,23 @@ final class AllStats: Content { } } +final class OverallStats: Content { + var overall:Stats + var mwStats:Stats + var bocwStats:Stats + var mostRecentRecord:String + + init( overall:Stats, mwStats:Stats, bocwStats:Stats, mostRecentRecord:String){ + + self.overall = overall + self.mwStats = mwStats; + self.bocwStats = bocwStats; + self.mostRecentRecord = mostRecentRecord + + } + +} + final class MonthStats: Content { var stats:Stats diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 7e01529..c2b800a 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -11,4 +11,6 @@ func routes(_ app: Application) throws { } try app.register(collection: StatsController()) + try app.register(collection: MatchController()) + }