updated rules on what games count, also can determine most recent record based on game

This commit is contained in:
Michael Simard
2020-12-22 10:27:37 -06:00
parent 87726f6b99
commit 068932195b
2 changed files with 172 additions and 152 deletions

View File

@@ -22,8 +22,6 @@ struct StatsController: RouteCollection {
statsRoute.get("allDaily", use: allDaily)
statsRoute.get("test", use: test)
statsRoute.post("logMatch", use: logMatch)
statsRoute.get("history","page",":page", use: history)
statsRoute.get("history", use: history)
@@ -70,8 +68,11 @@ struct StatsController: RouteCollection {
func getStats(matches:[Match]) -> Stats{
let countedMatches = matches.filter {
return shouldCountMatch(match: $0)
}
let totals = matches.reduce([0,0]) { (totals, match) -> [Int] in
let totals = countedMatches.reduce([0,0]) { (totals, match) -> [Int] in
if match.win == true {
return [totals[0] + 1, totals[1]]
@@ -80,44 +81,70 @@ struct StatsController: RouteCollection {
return [totals[0], totals[1] + 1]
}
}
// for match in matches {
// if match.win == true {
// winCount += 1;
//
// }
// if match.win == false {
// lossCount += 1;
// }
// }
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))
}
func getStatsWithMostRecentDailyRecord(matches:[Match], game:String? = nil) -> StatsWithMostRecentDailyRecord {
func mostRecentDailyStats (matches:[Match]) -> Stats{
let stats = getStats(matches: matches)
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches, game: game)
let ret = StatsWithMostRecentDailyRecord(winLoss: stats.winLossRatio, totalWins: stats.totalWins, totalLosses: stats.totalLosses, mostRecentRecord:"\(mostRecentDailyStats.totalWins)-\(mostRecentDailyStats.totalLosses)")
return ret
}
func mostRecentDailyStats (matches:[Match], game:String? = nil) -> Stats{
let daysPlayed = getDaysPlayed(matches: matches)
let lastDayPlayed = daysPlayed.last
return getStats(matches: matches.filter({ (match) -> Bool in
return
var shouldInclude =
match.date.day == lastDayPlayed?.day &&
match.date.month == lastDayPlayed?.month &&
match.date.year == lastDayPlayed?.year
}))
match.date.year == lastDayPlayed?.year &&
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 {
//
// }
shouldInclude = shouldInclude && match.codGame == game
}
return shouldInclude
}))
}
private func shouldCountMatch (match:Match) -> Bool {
let isColdWar = match.codGame == "bocw"
let numberOfPlayers = self.numberOfPlayers(match: match)
return !isColdWar || (isColdWar && (numberOfPlayers == 0 || numberOfPlayers > 4 ))
}
private func numberOfPlayers(match:Match) -> Int {
return match.players?.components(separatedBy: ",").count ?? 0
}
private func getDaysPlayed(matches:[Match]) -> [CODDate] {
var sortedMatches = matches
@@ -147,12 +174,7 @@ struct StatsController: RouteCollection {
func getCumulativeWinLossRatios(matches:[Match]) -> [DataPoint] {
let startTime = Date()
//print ("c \(Date().timeIntervalSince(startTime))")
let daysPlayed = getDaysPlayed(matches: matches)
// print ("c \(Date().timeIntervalSince(startTime))")
var cumulativeRatios : [DataPoint] = []
var cumulativeWins:Int = 0
@@ -194,26 +216,6 @@ struct StatsController: RouteCollection {
}
// for (i, day) in daysPlayed.enumerated() {
// print ("ca \(Date().timeIntervalSince(startTime))")
//
//
// let stats = self.getStats(matches: matches.filter({ (match) -> Bool in
// return match.date.day == day.day && match.date.year == day.year && match.date.month == day.month
// }))
//
// print ("cb \(Date().timeIntervalSince(startTime))")
//
// cumulativeWins = cumulativeWins + stats.totalWins;
// cumulativeLosses = cumulativeLosses + stats.totalLosses;
// cumulativeRatios.append( DataPoint(x: Double(i), y: (Double(cumulativeWins) / (Double(cumulativeLosses) )), label: ("\(Utilities.monthToString(month: day.month)) \(day.day)")))
//
// print ("cc \(Date().timeIntervalSince(startTime))")
//
// }
// print ("c \(Date().timeIntervalSince(startTime))")
return cumulativeRatios
}
@@ -234,19 +236,21 @@ struct StatsController: RouteCollection {
// print (date - Date().timeIntervalSince(startTime))
return Match.query(on: req.db).all().map { (matches) -> AllStats in
let overallStats = self.getStats(matches: matches)
let overallStats = self.getStatsWithMostRecentDailyRecord(matches: matches)
// print ( Date().timeIntervalSince(startTime))
let mwStats = self.getStats(matches: matches.filter({ (match) -> Bool in
let mwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "mw"
}))
// print ( Date().timeIntervalSince(startTime))
let bocwStats = self.getStats(matches: matches.filter({ (match) -> Bool in
let bocwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "bocw"
}))
// print ( Date().timeIntervalSince(startTime))
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)
@@ -547,15 +551,15 @@ struct StatsController: RouteCollection {
func overall(req: Request) throws -> EventLoopFuture<OverallStats> {
return Match.query(on: req.db).all().map { (matches) -> OverallStats in
let overallStats = self.getStats(matches: matches)
let overallStats = self.getStatsWithMostRecentDailyRecord(matches: matches)
// print ( Date().timeIntervalSince(startTime))
let mwStats = self.getStats(matches: matches.filter({ (match) -> Bool in
return match.codGame == "mw"
let mwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "mw" && shouldCountMatch(match: match )
}))
let bocwStats = self.getStats(matches: matches.filter({ (match) -> Bool in
return match.codGame == "bocw"
let bocwStats = self.getStatsWithMostRecentDailyRecord(matches: matches.filter({ (match) -> Bool in
return match.codGame == "bocw" && shouldCountMatch(match: match )
}))
let mostRecentDailyStats = self.mostRecentDailyStats(matches: matches)

View File

@@ -25,15 +25,15 @@ struct DataPoint : Content {
final class AllStats: Content {
var overall:Stats
var mwStats:Stats
var bocwStats:Stats
var overall:StatsWithMostRecentDailyRecord
var mwStats:StatsWithMostRecentDailyRecord
var bocwStats:StatsWithMostRecentDailyRecord
var byMonth: [MonthStats]
var highestWinLossRatio:String
var dataPoints:[DataPoint]
var mostRecentRecord:String
init( overall:Stats, mwStats:Stats, bocwStats:Stats, byMonth:[MonthStats], highestWinLossRatio:String, dataPoints:[DataPoint], mostRecentRecord:String) {
init( overall:StatsWithMostRecentDailyRecord, mwStats:StatsWithMostRecentDailyRecord, bocwStats:StatsWithMostRecentDailyRecord, byMonth:[MonthStats], highestWinLossRatio:String, dataPoints:[DataPoint], mostRecentRecord:String) {
self.overall = overall
self.byMonth = byMonth
self.highestWinLossRatio = highestWinLossRatio
@@ -46,12 +46,12 @@ final class AllStats: Content {
}
final class OverallStats: Content {
var overall:Stats
var mwStats:Stats
var bocwStats:Stats
var overall:StatsWithMostRecentDailyRecord
var mwStats:StatsWithMostRecentDailyRecord
var bocwStats:StatsWithMostRecentDailyRecord
var mostRecentRecord:String
init( overall:Stats, mwStats:Stats, bocwStats:Stats, mostRecentRecord:String){
init( overall:StatsWithMostRecentDailyRecord, mwStats:StatsWithMostRecentDailyRecord, bocwStats:StatsWithMostRecentDailyRecord, mostRecentRecord:String){
self.overall = overall
self.mwStats = mwStats;
@@ -77,6 +77,7 @@ final class MonthStats: Content {
}
final class Stats: Content {
var winLossRatio:String
var totalWins:Int
@@ -86,10 +87,25 @@ final class Stats: Content {
self.winLossRatio = winLoss
self.totalWins = totalWins
self.totalLosses = totalLosses
}
}
final class StatsWithMostRecentDailyRecord: Content {
var winLossRatio:String
var totalWins:Int
var totalLosses:Int
var mostRecentRecord:String
init( winLoss:String, totalWins:Int, totalLosses:Int, mostRecentRecord:String ) {
self.winLossRatio = winLoss
self.totalWins = totalWins
self.totalLosses = totalLosses
self.mostRecentRecord = mostRecentRecord
}
}
final class AllDailyStats: Content {
var dailyStats : [DailyStats]