best and worst stats

This commit is contained in:
Michael Simard
2021-01-10 09:44:21 -06:00
parent a3977ac3ac
commit f974477106
5 changed files with 252 additions and 67 deletions

View File

@@ -24,8 +24,6 @@ struct StatsController: RouteCollection {
statsRoute.post("logMatch", use: logMatch)
statsRoute.get("history","page",":page", use: history)
statsRoute.get("history", use: history)
}
@@ -82,10 +80,9 @@ struct StatsController: RouteCollection {
let winCount = totals[0]
let lossCount = totals[1]
let ratio = self.getRatio(num: Double(winCount), den: Double(lossCount))
return Stats(winLoss: ratio, totalWins: Int(winCount), totalLosses: Int(lossCount))
return Stats( totalWins: Int(winCount), totalLosses: Int(lossCount))
}
func getStatsWithMostRecentDailyRecord(matches:[Match], game:String? = nil) -> StatsWithMostRecentDailyRecord {
let stats = getStats(matches: matches)
@@ -95,7 +92,7 @@ struct StatsController: RouteCollection {
return ret
}
func mostRecentDailyStats (matches:[Match], game:String? = nil) -> Stats{
let daysPlayed = getDaysPlayed(matches: matches)
@@ -109,15 +106,15 @@ struct StatsController: RouteCollection {
self.shouldCountMatch(match: match)
if let game = game {
// if game == "mw" {
// shouldInclude = shouldInclude && match.codGame == "mw"
// }
// else if game == "bocw" {
// shouldInclude = shouldInclude && match.codGame == "bocw"
//
// }else {
//
// }
// if game == "mw" {
// shouldInclude = shouldInclude && match.codGame == "mw"
// }
// else if game == "bocw" {
// shouldInclude = shouldInclude && match.codGame == "bocw"
//
// }else {
//
// }
shouldInclude = shouldInclude && match.codGame == game
}
@@ -246,7 +243,7 @@ struct StatsController: RouteCollection {
}))
// print ( Date().timeIntervalSince(startTime))
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)
@@ -260,16 +257,16 @@ struct StatsController: RouteCollection {
// print ( Date().timeIntervalSince(startTime))
// let cumulativeWinLossRatios = self.getCumulativeWinLossRatios(matches: matches)
// let cumulativeWinLossRatios = self.getCumulativeWinLossRatios(matches: matches)
// print ( Date().timeIntervalSince(startTime))
// let highestWinLossRatio = cumulativeWinLossRatios.reduce("0") { (highestRatio, dataPoint) -> String in
// if dataPoint.y > Double(highestRatio)!{
// return String(dataPoint.y)
// }
// return highestRatio
// }
// let highestWinLossRatio = cumulativeWinLossRatios.reduce("0") { (highestRatio, dataPoint) -> String in
// if dataPoint.y > Double(highestRatio)!{
// return String(dataPoint.y)
// }
// return highestRatio
// }
// print ( Date().timeIntervalSince(startTime))
@@ -356,8 +353,7 @@ struct StatsController: RouteCollection {
let combined = winCount.and(lossCount)
return combined.map { (winCount, lossCount) -> (Stats) in
let ratio:Double = (Double(winCount) / Double(lossCount)).truncate(places: 2)
return Stats.init(winLoss: String(ratio), totalWins: winCount, totalLosses: lossCount)
return Stats.init( totalWins: winCount, totalLosses: lossCount)
}
}
@@ -380,7 +376,7 @@ struct StatsController: RouteCollection {
return combined.map { (winCount, lossCount) -> (Stats) in
return Stats.init(winLoss: self.getRatio(num: Double(winCount), den: Double(lossCount)), totalWins: winCount, totalLosses: lossCount)
return Stats.init(totalWins: winCount, totalLosses: lossCount)
}
}
@@ -403,8 +399,7 @@ struct StatsController: RouteCollection {
let combined = winCount.and(lossCount)
return combined.map { (winCount, lossCount) -> (Stats) in
let ratio:Double = (Double(winCount) / Double(lossCount)).truncate(places: 2)
return Stats.init(winLoss: String(ratio), totalWins: winCount, totalLosses: lossCount)
return Stats.init(totalWins: winCount, totalLosses: lossCount)
}
}
@@ -547,17 +542,17 @@ struct StatsController: RouteCollection {
func overall(req: Request) throws -> EventLoopFuture<OverallStats> {
let statsWithHyder = statsWithPlayer(req: req, playerId: 5)
let statsWithoutHyder = statsWithoutPlayer(req: req, playerId: 5)
let hyderFuture = statsWithHyder.and(statsWithoutHyder)
let hyderStats = hyderFuture.map { (withHyder, withoutHyder) -> [Stats] in
return [withHyder, withoutHyder]
}
}
let matches = Match.query(on: req.db).all()
@@ -571,22 +566,22 @@ struct StatsController: RouteCollection {
var mwStats:StatsWithMostRecentDailyRecord?
var bocwStats:StatsWithMostRecentDailyRecord?
var mostRecentStats:Stats?
group.enter()
queue.async {
overallStats = self.getStatsWithMostRecentDailyRecord(matches: matches)
group.leave()
}
group.enter()
queue.async {
mwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "mw" && self.shouldCountMatch(match: match )
}))
group.leave()
}
group.enter()
queue.async {
@@ -594,33 +589,86 @@ struct StatsController: RouteCollection {
return match.codGame == "bocw" && self.shouldCountMatch(match: match )
}))
group.leave()
}
group.enter()
queue.async {
mostRecentStats = self.mostRecentDailyStats(matches: matches)
group.leave()
}
// group.enter()
// queue.async {
// mostRecentStats = self.mostRecentDailyStats(matches: matches)
// group.leave()
// }
group.wait()
let mapStats = getMapStats(matches: matches)
let bestMap = getBestMap(records: mapStats)
let worstMap = getWorstMap(records: mapStats)
let dashboardItems = [
DashboardItem(title: "Overall", content: overallStats!.record, title2: "Ratio", content2: overallStats!.winLossRatio),
DashboardItem(title: "MW Overall", content: mwStats!.record, title2: "Ratio", content2: mwStats!.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: "No Hyder", content: hyderStats[1].record, title2: "Ratio", content2: hyderStats[1].winLossRatio),
DashboardItem(title: "Best Map", content: MapData.allMaps[bestMap]?.name ?? "error", title2: "Ratio", content2: "\(mapStats[bestMap]!.winLossRatio) \(mapStats[bestMap]!.record)"),
DashboardItem(title: "Worst Map", content: MapData.allMaps[worstMap]?.name ?? "error", title2: "Ratio", content2: "\(mapStats[worstMap]!.winLossRatio) \(mapStats[worstMap]!.record)"),
]
return OverallStats(overall: overallStats!, mwStats: mwStats!, bocwStats: bocwStats!, mostRecentRecord: "\(mostRecentStats!.totalWins) - \(mostRecentStats!.totalLosses)", statsWithHyder:hyderStats[0], statsWithoutHyder: hyderStats[1])
return OverallStats(overall: overallStats!, mwStats: mwStats!, bocwStats: bocwStats!, mostRecentRecord: "Temporarily Unavailable", statsWithHyder:hyderStats[0], statsWithoutHyder: hyderStats[1], dashboardItems:dashboardItems)
}
// return Match.query(on: req.db).all().map { (matches) -> OverallStats in
//
//
// }
// return Match.query(on: req.db).all().map { (matches) -> OverallStats in
//
//
// }
}
func getBestMap (records :[ Int:Stats] ) -> Int {
let maps = records.keys.sorted { (map1, map2) -> Bool in
return records[map1]?.getRatioDouble() ?? 0.0 < records[map2]?.getRatioDouble() ?? 0.0
}
return maps.last ?? -1
}
func getWorstMap (records :[ Int:Stats] ) -> Int {
let maps = records.keys.sorted { (map1, map2) -> Bool in
return records[map1]?.getRatioDouble() ?? 0.0 < records[map2]?.getRatioDouble() ?? 0.0
}
return maps.first ?? -1
}
func getMapStats(matches:[Match]) -> [Int:Stats] {
var mapStats:[Int:Stats] = [Int:Stats]()
for match in matches {
if match.codGame == "mw" {
if let map = match.map, let mapInt = Int(map) {
if mapStats[mapInt] == nil {
mapStats[mapInt] = Stats(totalWins: 0, totalLosses: 0)
}
if match.win {
mapStats[mapInt]?.totalWins += 1
}
else{
mapStats[mapInt]?.totalLosses += 1
}
}
}
}
return mapStats
}
func statsWithPlayer(req: Request, playerId:Int) -> EventLoopFuture<Stats> {
return Match.query(on: req.db)
@@ -629,7 +677,7 @@ struct StatsController: RouteCollection {
return self.getStats(matches: matches)
}
}
}
func statsWithoutPlayer (req: Request, playerId:Int) -> EventLoopFuture<Stats> {
return Match.query(on: req.db)
@@ -640,19 +688,7 @@ struct StatsController: RouteCollection {
}
private func getRatio( num:Double, den:Double) -> String {
var returnString = ""
let deno = (den != 0) ? den : 1
returnString = String((Double(num) / Double(deno)).truncate(places: 2))
if den == 0 {
returnString = returnString + "+"
}
return returnString
}
private func getDaysPlayed(req:Request) -> EventLoopFuture<[CODDate]> {
@@ -722,6 +758,20 @@ struct StatsController: RouteCollection {
}
}
private func getRatio( num:Double, den:Double) -> String {
var returnString = ""
let deno = (den != 0) ? den : 1
returnString = String((Double(num) / Double(deno)).truncate(places: 2))
if den == 0 {
returnString = returnString + "+"
}
return returnString
}
func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> {