more stats tweaks

This commit is contained in:
Michael Simard
2021-02-26 18:30:18 -06:00
parent 15f9e0cf86
commit 41a7a7c5bc

View File

@@ -62,7 +62,7 @@ struct StatsController: RouteCollection {
return newMatch.save(on: req.db).map { newMatch} return newMatch.save(on: req.db).map { newMatch}
} }
func getStats(matches:[Match]) -> Stats{ func getCountedMatches(matches:[Match]) -> Stats{
let countedMatches = matches.filter { let countedMatches = matches.filter {
return self.shouldCountMatch(match: $0) return self.shouldCountMatch(match: $0)
@@ -84,13 +84,33 @@ struct StatsController: RouteCollection {
return Stats( totalWins: Int(winCount), totalLosses: Int(lossCount)) 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 { 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))")
let stats = getStats(matches: sortedMatches) let stats = getCountedMatches(matches: sortedMatches)
//print ("MRR STATS \(Date().timeIntervalSince(startTime))") //print ("MRR STATS \(Date().timeIntervalSince(startTime))")
let mostRecentDailyStats = self.mostRecentDailyStats(matches: sortedMatches, game: game) let mostRecentDailyStats = self.mostRecentDailyStats(matches: sortedMatches, game: game)
@@ -114,7 +134,7 @@ struct StatsController: RouteCollection {
//print ("MDD days played \(Date().timeIntervalSince(startTime))") //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 = var shouldInclude =
match.date.day == lastDayPlayed?.day && match.date.day == lastDayPlayed?.day &&
match.date.month == lastDayPlayed?.month && 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 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() { for (i, matchGroup) in dayMatches.enumerated() {
let stats = self.getStats(matches: matchGroup) let stats = self.getCountedMatches(matches: matchGroup)
cumulativeWins = cumulativeWins + stats.totalWins; cumulativeWins = cumulativeWins + stats.totalWins;
cumulativeLosses = cumulativeLosses + stats.totalLosses; cumulativeLosses = cumulativeLosses + stats.totalLosses;
@@ -570,21 +590,21 @@ struct StatsController: RouteCollection {
group.enter() group.enter()
queue.async { 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.leave()
} }
group.enter() group.enter()
queue.async { 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.leave()
} }
group.enter() group.enter()
queue.async { 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() group.leave()
} }
@@ -597,7 +617,7 @@ struct StatsController: RouteCollection {
DashboardItem(title: "MW Overall", content: mwStats!.record, title2: "Ratio", content2: mwStats!.winLossRatio), 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 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: "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: "Overall", content: overallStats!.record, title2: "Ratio", content2: overallStats!.winLossRatio),
DashboardItem(title: "2021 Overall", content: statsFor2021.record, title2: "Ratio", content2: statsFor2021.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: "2020 Overall", content: statsFor2020.record, title2: "Ratio", content2: statsFor2020.winLossRatio),
@@ -647,8 +667,11 @@ struct StatsController: RouteCollection {
}.reversed() }.reversed()
} }
func getStatsByPlayerCount(matches:[Match], playerCount:Int) -> Stats { func getCountedMatchesByPlayerCount(matches:[Match], playerCount:Int) -> Stats {
return getStats(matches: matches.filter{$0.playerList.count == playerCount}) 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 { func getBestMap (records :[ Int:Stats] ) -> Int {
@@ -699,7 +722,7 @@ struct StatsController: RouteCollection {
return Match.query(on: req.db) return Match.query(on: req.db)
.filter(\.$players ~~ "\(playerId)") .filter(\.$players ~~ "\(playerId)")
.all().map { (matches) -> (Stats) in .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) return Match.query(on: req.db)
.filter(\.$players !~ "\(playerId)") .filter(\.$players !~ "\(playerId)")
.all().map { (matches) -> (Stats) in .all().map { (matches) -> (Stats) in
return self.getStats(matches: matches) return self.getCountedMatches(matches: matches)
} }
} }