diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index 89ed6ca..c828169 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -90,8 +90,7 @@ struct StatsController: RouteCollection { 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(winLoss: self.getRatio(num: Double(winCount), den: Double(lossCount)), totalWins: winCount, totalLosses: lossCount) } } @@ -200,6 +199,12 @@ struct StatsController: RouteCollection { return calendar.date(from: components)! } + func mostRecentDailyStats (req:Request) -> EventLoopFuture{ + + let daysPlayed = getDaysPlayed() + + return getStatsForDay(year: daysPlayed.last?.year ?? 0, month: daysPlayed.last?.month ?? 0, day: daysPlayed.last?.day ?? 0, req: req) + } @@ -232,14 +237,14 @@ struct StatsController: RouteCollection { } } - var stats:[MonthStats] = [] let monthstats = getMonthStats(previousMonths[0.. AllStats in + return try overall(req: req).and(monthstats).and(cumulativeRatios).and(mostRecentDayStats).map { arg -> AllStats in - let ((overall, monthlyStats), cumulativeRatios) = arg + let (((overall, monthlyStats), cumulativeRatios), mostRecentDayStats) = arg let highestWinLossRatio = cumulativeRatios.reduce("0") { (highestRatio, dataPoint) -> String in if dataPoint.y > Double(highestRatio)!{ return String(dataPoint.y) @@ -247,7 +252,7 @@ struct StatsController: RouteCollection { return highestRatio } - return AllStats.init(overall: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios ) + return AllStats.init(overall: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios, mostRecentRecord: "\(mostRecentDayStats.totalWins) - \(mostRecentDayStats.totalLosses)") } } @@ -277,7 +282,15 @@ struct StatsController: RouteCollection { private func getRatio( num:Double, den:Double) -> String { - return String((Double(num) / Double(den)).truncate(places: 2)) + var returnString = "" + let deno = (den != 0) ? den : 1 + + returnString = String((Double(num) / Double(deno)).truncate(places: 2)) + + if den == 0 { + returnString = returnString + "+" + } + return returnString } @@ -363,7 +376,8 @@ struct StatsController: RouteCollection { let date = self.createDate(day: first.day, month: first.month, year: first.year, hour: first.hour + 6, minute:first.minute) // 6 hours to make sure we pick a time that isnt on borders of us time zones print ("p \(date.timeIntervalSince1970)") - cumulativeWinLossRatios.append(DataPoint(x:date.timeIntervalSince1970*1000 , y: (totalWins/totalLosses).truncate(places: 2))) + let x = Double(cumulativeWinLossRatios.count) + cumulativeWinLossRatios.append(DataPoint(x:x , y: (totalWins/totalLosses).truncate(places: 2), label:"\(date.timeIntervalSince1970*1000)")) } allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses))) return getRatios(remaining, allDailyStats:&allDailyStats, cumulativeWinLossRatios:&cumulativeWinLossRatios, eventLoop: eventLoop) diff --git a/Sources/App/Models/AllStats.swift b/Sources/App/Models/AllStats.swift index a9836b0..75fa503 100644 --- a/Sources/App/Models/AllStats.swift +++ b/Sources/App/Models/AllStats.swift @@ -14,10 +14,12 @@ import Vapor struct DataPoint : Content { var x:Double var y:Double + var label:String - init(x:Double, y:Double) { + init(x:Double, y:Double, label:String) { self.x = x self.y = y + self.label = label } } @@ -27,13 +29,14 @@ final class AllStats: Content { var byMonth: [MonthStats] var highestWinLossRatio:String var dataPoints:[DataPoint] + var mostRecentRecord:String - - init( overall:Stats, byMonth:[MonthStats], highestWinLossRatio:String, dataPoints:[DataPoint]) { + init( overall:Stats, byMonth:[MonthStats], highestWinLossRatio:String, dataPoints:[DataPoint], mostRecentRecord:String) { self.overall = overall self.byMonth = byMonth self.highestWinLossRatio = highestWinLossRatio self.dataPoints = dataPoints + self.mostRecentRecord = mostRecentRecord } }