This commit is contained in:
Michael Simard
2021-01-22 11:22:04 -06:00
parent a6ddb534da
commit 2535e2817b

View File

@@ -85,7 +85,7 @@ struct StatsController: RouteCollection {
} }
func getStatsWithMostRecentDailyRecord(sortedMatches:[Match], game:String? = nil) -> StatsWithMostRecentDailyRecord { func getStatsWithMostRecentDailyRecord(sortedMatches:[Match], game:String? = nil) -> StatsWithMostRecentDailyRecord {
let startTime = Date() let startTime = Date()
//print ("MRR START \(Date().timeIntervalSince(startTime))") //print ("MRR START \(Date().timeIntervalSince(startTime))")
@@ -101,9 +101,11 @@ struct StatsController: RouteCollection {
return ret return ret
} }
func mostRecentDailyStats (matches:[Match], game:String? = nil) -> Stats{ func mostRecentDailyStats (matches:[Match], game:String? = nil) -> Stats{
let startTime = Date() let startTime = Date()
let daysPlayed = getDaysPlayed(sortedMatches: matches) let daysPlayed = getDaysPlayed(sortedMatches: matches)
@@ -136,6 +138,27 @@ struct StatsController: RouteCollection {
} }
func getStatsForYear(year:Int, req: Request) -> EventLoopFuture<Stats>{
// Specify date components
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = 1
dateComponents.day = 1
dateComponents.timeZone = TimeZone(abbreviation: "EST") // Japan Standard Time
dateComponents.hour = 8
dateComponents.minute = 0
// Create date from components
let userCalendar = Calendar(identifier: .gregorian) // since the components above (like year 1980) are for Gregorian
let startDate = userCalendar.date(from: dateComponents)
return Match.query(on: req.db).filter(\.$date > startDate!).all().map { (matches) -> (Stats) in
return getStats(matches: matches)
}
}
private func numberOfPlayers(match:Match) -> Int { private func numberOfPlayers(match:Match) -> Int {
return match.players?.components(separatedBy: ",").count ?? 0 return match.players?.components(separatedBy: ",").count ?? 0
@@ -241,6 +264,8 @@ struct StatsController: RouteCollection {
return getstatsForMonth(year: 2020, month: 03, req: req) return getstatsForMonth(year: 2020, month: 03, req: req)
} }
func getstatsForMonth(year:Int, month:Int, req: Request) -> EventLoopFuture<Stats>{ func getstatsForMonth(year:Int, month:Int, req: Request) -> EventLoopFuture<Stats>{
let winCount = Match.query(on: req.db) let winCount = Match.query(on: req.db)
@@ -404,6 +429,12 @@ struct StatsController: RouteCollection {
let statsWithoutHyder = statsWithoutPlayer(req: req, playerId: 5) let statsWithoutHyder = statsWithoutPlayer(req: req, playerId: 5)
let statsFor2020 = getStatsForYear(year: 2020, req: req)
let statsFor2021 = getStatsForYear(year: 2021, req: req)
let hyderFuture = statsWithHyder.and(statsWithoutHyder) let hyderFuture = statsWithHyder.and(statsWithoutHyder)
let hyderStats = hyderFuture.map { (withHyder, withoutHyder) -> [Stats] in let hyderStats = hyderFuture.map { (withHyder, withoutHyder) -> [Stats] in
@@ -415,7 +446,9 @@ struct StatsController: RouteCollection {
let matches = Match.query(on: req.db).sort( \.$date).all() let matches = Match.query(on: req.db).sort( \.$date).all()
return matches.and(hyderStats).map { (matches, hyderStats) -> (OverallStats) in return matches.and(hyderStats).and(statsFor2020).and(statsFor2021).map { arg -> (OverallStats) in
let (((matches, hyderStats), statsFor2020), statsFor2021) = arg
//print ("got matches \(Date().timeIntervalSince(startTime))") //print ("got matches \(Date().timeIntervalSince(startTime))")
@@ -424,6 +457,7 @@ struct StatsController: RouteCollection {
var overallStats:StatsWithMostRecentDailyRecord? var overallStats:StatsWithMostRecentDailyRecord?
var mwStats:StatsWithMostRecentDailyRecord? var mwStats:StatsWithMostRecentDailyRecord?
var bocwStats:StatsWithMostRecentDailyRecord? var bocwStats:StatsWithMostRecentDailyRecord?
var mostRecentStats:Stats? var mostRecentStats:Stats?
var mwSixPlayers:Stats? var mwSixPlayers:Stats?
@@ -441,6 +475,9 @@ struct StatsController: RouteCollection {
//print ("all stats done \(Date().timeIntervalSince(startTime))") //print ("all stats done \(Date().timeIntervalSince(startTime))")
} }
group.enter() group.enter()
queue.async { queue.async {
mwStats = self.getStatsWithMostRecentDailyRecord(sortedMatches: matches.filter({ (match) -> Bool in mwStats = self.getStatsWithMostRecentDailyRecord(sortedMatches: matches.filter({ (match) -> Bool in
@@ -524,6 +561,8 @@ struct StatsController: RouteCollection {
DashboardItem(title: "MW 5 Players ", content: mwFivePlayers!.record, title2: "Ratio", content2: mwFivePlayers!.winLossRatio), DashboardItem(title: "MW 5 Players ", content: mwFivePlayers!.record, title2: "Ratio", content2: mwFivePlayers!.winLossRatio),
DashboardItem(title: "MW 4 Players ", content: mwFourPlayers!.record, title2: "Ratio", content2: mwFourPlayers!.winLossRatio), DashboardItem(title: "MW 4 Players ", content: mwFourPlayers!.record, title2: "Ratio", content2: mwFourPlayers!.winLossRatio),
DashboardItem(title: "Overall", content: overallStats!.record, title2: "Ratio", content2: overallStats!.winLossRatio), DashboardItem(title: "Overall", content: overallStats!.record, title2: "Ratio", content2: overallStats!.winLossRatio),
DashboardItem(title: "2021 Overall", content: statsFor2021.record, title2: "Ratio", content2: statsFor2021.winLossRatio),
DashboardItem(title: "2020 Overall", content: statsFor2020.record, title2: "Ratio", content2: statsFor2020.winLossRatio),
DashboardItem(title: "Cold War Overall", content: bocwStats!.record, title2: "Ratio", content2: bocwStats!.winLossRatio), DashboardItem(title: "Cold War Overall", content: bocwStats!.record, title2: "Ratio", content2: bocwStats!.winLossRatio),
DashboardItem(title: "With Hyder", content: hyderStats[0].record, title2: "Ratio", content2: hyderStats[0].winLossRatio), DashboardItem(title: "With Hyder", content: hyderStats[0].record, title2: "Ratio", content2: hyderStats[0].winLossRatio),
DashboardItem(title: "No Hyder", content: hyderStats[1].record, title2: "Ratio", content2: hyderStats[1].winLossRatio), DashboardItem(title: "No Hyder", content: hyderStats[1].record, title2: "Ratio", content2: hyderStats[1].winLossRatio),
@@ -776,3 +815,4 @@ struct StatsController: RouteCollection {
} }