From 41a7a7c5bcde22d35a2f8a5386379eee2ddeba37 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Fri, 26 Feb 2021 18:30:18 -0600 Subject: [PATCH] more stats tweaks --- Sources/App/Controllers/StatsController.swift | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index dc6b8e4..c0dbe8b 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -62,7 +62,7 @@ struct StatsController: RouteCollection { return newMatch.save(on: req.db).map { newMatch} } - func getStats(matches:[Match]) -> Stats{ + func getCountedMatches(matches:[Match]) -> Stats{ let countedMatches = matches.filter { return self.shouldCountMatch(match: $0) @@ -84,13 +84,33 @@ struct StatsController: RouteCollection { return Stats( totalWins: Int(winCount), totalLosses: Int(lossCount)) } + func getAllMatches(matches:[Match]) -> Stats{ + + + + let totals = matches.reduce([0,0]) { (totals, match) -> [Int] in + if match.win == true { + return [totals[0] + 1, totals[1]] + + } + else { + return [totals[0], totals[1] + 1] + } + } + + let winCount = totals[0] + let lossCount = totals[1] + + return Stats( totalWins: Int(winCount), totalLosses: Int(lossCount)) + } + func getStatsWithMostRecentDailyRecord(sortedMatches:[Match], game:String? = nil) -> StatsWithMostRecentDailyRecord { let startTime = Date() //print ("MRR START \(Date().timeIntervalSince(startTime))") - let stats = getStats(matches: sortedMatches) + let stats = getCountedMatches(matches: sortedMatches) //print ("MRR STATS \(Date().timeIntervalSince(startTime))") let mostRecentDailyStats = self.mostRecentDailyStats(matches: sortedMatches, game: game) @@ -114,7 +134,7 @@ struct StatsController: RouteCollection { //print ("MDD days played \(Date().timeIntervalSince(startTime))") - return getStats(matches: matches.filter({ (match) -> Bool in + return getCountedMatches(matches: matches.filter({ (match) -> Bool in var shouldInclude = match.date.day == lastDayPlayed?.day && match.date.month == lastDayPlayed?.month && @@ -195,7 +215,7 @@ struct StatsController: RouteCollection { return Match.query(on: req.db).filter(\.$date > startDate!).filter(\.$date < endDate!).all().map { (matches) -> (Stats) in - return self.getStats(matches: matches) + return self.getCountedMatches(matches: matches) } } @@ -271,7 +291,7 @@ struct StatsController: RouteCollection { for (i, matchGroup) in dayMatches.enumerated() { - let stats = self.getStats(matches: matchGroup) + let stats = self.getCountedMatches(matches: matchGroup) cumulativeWins = cumulativeWins + stats.totalWins; cumulativeLosses = cumulativeLosses + stats.totalLosses; @@ -570,21 +590,21 @@ struct StatsController: RouteCollection { group.enter() queue.async { - overallFourPlayers = self.getStatsByPlayerCount(matches: matches.filter{$0.date > Date(timeIntervalSince1970: 1612159200)}, playerCount: 4) + overallFourPlayers = self.getAllMatchesByPlayerCount(matches: matches.filter{$0.date > Date(timeIntervalSince1970: 1612159200)}, playerCount: 4) group.leave() } group.enter() queue.async { - mwFivePlayers = self.getStatsByPlayerCount(matches: matches.filter{$0.codGame == "mw"}, playerCount: 5) + mwFivePlayers = self.getCountedMatchesByPlayerCount(matches: matches.filter{$0.codGame == "mw"}, playerCount: 5) group.leave() } group.enter() queue.async { - mwSixPlayers = self.getStatsByPlayerCount(matches: matches.filter{$0.codGame == "mw"}, playerCount: 6) + mwSixPlayers = self.getCountedMatchesByPlayerCount(matches: matches.filter{$0.codGame == "mw"}, playerCount: 6) group.leave() } @@ -597,7 +617,7 @@ struct StatsController: RouteCollection { DashboardItem(title: "MW Overall", content: mwStats!.record, title2: "Ratio", content2: mwStats!.winLossRatio), DashboardItem(title: "MW 6 Players ", content: mwSixPlayers!.record, title2: "Ratio", content2: mwSixPlayers!.winLossRatio), DashboardItem(title: "MW 5 Players ", content: mwFivePlayers!.record, title2: "Ratio", content2: mwFivePlayers!.winLossRatio), - DashboardItem(title: "Overall 4 Players ", content: overallFourPlayers!.record, title2: "Ratio", content2: overallFourPlayers!.winLossRatio), + DashboardItem(title: "4 Player Crew", content: overallFourPlayers!.record, title2: "Ratio", content2: overallFourPlayers!.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), @@ -647,8 +667,11 @@ struct StatsController: RouteCollection { }.reversed() } - func getStatsByPlayerCount(matches:[Match], playerCount:Int) -> Stats { - return getStats(matches: matches.filter{$0.playerList.count == playerCount}) + func getCountedMatchesByPlayerCount(matches:[Match], playerCount:Int) -> Stats { + return getCountedMatches(matches: matches.filter{$0.playerList.count == playerCount}) + } + func getAllMatchesByPlayerCount(matches:[Match], playerCount:Int) -> Stats { + return getAllMatches(matches: matches.filter{$0.playerList.count == playerCount}) } func getBestMap (records :[ Int:Stats] ) -> Int { @@ -699,7 +722,7 @@ struct StatsController: RouteCollection { return Match.query(on: req.db) .filter(\.$players ~~ "\(playerId)") .all().map { (matches) -> (Stats) in - return self.getStats(matches: matches) + return self.getCountedMatches(matches: matches) } } @@ -708,7 +731,7 @@ struct StatsController: RouteCollection { return Match.query(on: req.db) .filter(\.$players !~ "\(playerId)") .all().map { (matches) -> (Stats) in - return self.getStats(matches: matches) + return self.getCountedMatches(matches: matches) } }