updates
This commit is contained in:
30
Sources/App/Controllers/MatchController.swift
Normal file
30
Sources/App/Controllers/MatchController.swift
Normal file
@@ -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<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 }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,6 +26,18 @@ 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 }
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -246,8 +260,6 @@ 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,74 +483,72 @@ struct StatsController: RouteCollection {
|
||||
|
||||
|
||||
|
||||
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 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 getMonthStats (_ remaining: ArraySlice<CODDate>, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
func overall(req: Request) throws -> EventLoopFuture<OverallStats> {
|
||||
|
||||
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)
|
||||
}
|
||||
return Match.query(on: req.db).all().map { (matches) -> OverallStats in
|
||||
let overallStats = self.getStats(matches: matches)
|
||||
// print ( Date().timeIntervalSince(startTime))
|
||||
|
||||
} else {
|
||||
return req.eventLoop.makeSucceededFuture(allMonthlyStats)
|
||||
}
|
||||
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)
|
||||
|
||||
return OverallStats(overall: overallStats, mwStats: mwStats, bocwStats: bocwStats, mostRecentRecord: "\(mostRecentDailyStats.totalWins) - \(mostRecentDailyStats.totalLosses)")
|
||||
}
|
||||
|
||||
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<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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -11,4 +11,6 @@ func routes(_ app: Application) throws {
|
||||
}
|
||||
|
||||
try app.register(collection: StatsController())
|
||||
try app.register(collection: MatchController())
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user