added most recent record
This commit is contained in:
@@ -72,7 +72,7 @@ struct StatsController: RouteCollection {
|
||||
}
|
||||
|
||||
func getStatsForDay(year:Int, month:Int, day:Int, req: Request) -> EventLoopFuture<Stats>{
|
||||
|
||||
|
||||
let winCount = Match.query(on: req.db)
|
||||
.filter(\.$date >= getStartOfDay(day:day, month: month, year: year))
|
||||
.filter(\.$date <= getEndOfDay(day: day, month: month, year: year))
|
||||
@@ -95,8 +95,6 @@ struct StatsController: RouteCollection {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func statsForRecent(numberGames:Int, req:Request) -> EventLoopFuture<Stats> {
|
||||
|
||||
let winCount = Match.query(on: req.db)
|
||||
@@ -152,7 +150,7 @@ struct StatsController: RouteCollection {
|
||||
let calendar = Calendar.current
|
||||
var components = DateComponents()
|
||||
components.timeZone = TimeZone(identifier: "GMT")
|
||||
|
||||
|
||||
components.day = day
|
||||
components.month = month
|
||||
components.year = year
|
||||
@@ -174,7 +172,7 @@ struct StatsController: RouteCollection {
|
||||
}
|
||||
|
||||
private func getStartDate() -> Date {
|
||||
|
||||
|
||||
let calendar = Calendar.current
|
||||
var components = DateComponents()
|
||||
components.timeZone = TimeZone(identifier: "GMT")
|
||||
@@ -188,29 +186,28 @@ struct StatsController: RouteCollection {
|
||||
|
||||
private func createDate(day:Int, month:Int, year:Int, hour:Int, minute:Int) -> Date {
|
||||
let calendar = Calendar.current
|
||||
|
||||
var components = DateComponents()
|
||||
components.timeZone = TimeZone(identifier: "GMT")
|
||||
components.day = day
|
||||
components.month = month
|
||||
components.year = year
|
||||
components.hour = hour
|
||||
components.minute = minute
|
||||
return calendar.date(from: components)!
|
||||
|
||||
var components = DateComponents()
|
||||
components.timeZone = TimeZone(identifier: "GMT")
|
||||
components.day = day
|
||||
components.month = month
|
||||
components.year = year
|
||||
components.hour = hour
|
||||
components.minute = minute
|
||||
return calendar.date(from: components)!
|
||||
}
|
||||
|
||||
func mostRecentDailyStats (req:Request) -> EventLoopFuture<Stats>{
|
||||
|
||||
let daysPlayed = getDaysPlayed()
|
||||
|
||||
return getStatsForDay(year: daysPlayed.last?.year ?? 0, month: daysPlayed.last?.month ?? 0, day: daysPlayed.last?.day ?? 0, req: req)
|
||||
return getDaysPlayed(req: req).flatMap { (days) -> (EventLoopFuture<Stats>) in
|
||||
return self.getStatsForDay(year: days.first?.year ?? 0, month: days.first?.month ?? 0, day: days.first?.day ?? 0, req: req)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func all(req: Request) throws -> EventLoopFuture<AllStats> {
|
||||
|
||||
|
||||
var date = getStartDate()
|
||||
var previousMonths:[CODDate] = []
|
||||
|
||||
@@ -251,7 +248,7 @@ struct StatsController: RouteCollection {
|
||||
}
|
||||
return highestRatio
|
||||
}
|
||||
|
||||
|
||||
return AllStats.init(overall: overall, byMonth: monthlyStats, highestWinLossRatio: highestWinLossRatio, dataPoints: cumulativeRatios, mostRecentRecord: "\(mostRecentDayStats.totalWins) - \(mostRecentDayStats.totalLosses)")
|
||||
}
|
||||
}
|
||||
@@ -286,7 +283,7 @@ struct StatsController: RouteCollection {
|
||||
let deno = (den != 0) ? den : 1
|
||||
|
||||
returnString = String((Double(num) / Double(deno)).truncate(places: 2))
|
||||
|
||||
|
||||
if den == 0 {
|
||||
returnString = returnString + "+"
|
||||
}
|
||||
@@ -294,108 +291,130 @@ struct StatsController: RouteCollection {
|
||||
|
||||
}
|
||||
|
||||
private func getDaysPlayed() -> [CODDate] {
|
||||
var date = getStartDate()
|
||||
var previousDays:[CODDate] = []
|
||||
private func getDaysPlayed(req:Request) -> EventLoopFuture<[CODDate]> {
|
||||
|
||||
repeat {
|
||||
previousDays.append(CODDate(month: date.month, year: date.year, day: date.day, hour: date.hour, minute: date.minute))
|
||||
date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
|
||||
} while (date < Date())
|
||||
|
||||
return previousDays
|
||||
return Match.query(on: req.db).sort(\.$date, .descending).all().map { (matches) -> ([CODDate]) in
|
||||
return matches.map { (match) -> CODDate in
|
||||
return CODDate(month: match.date.month, year: match.date.year, day: match.date.day, hour: match.date.hour, minute: match.date.minute)
|
||||
}.reduce([CODDate]()) { (datesPlayed, codDate) -> [CODDate] in
|
||||
|
||||
if datesPlayed.contains(where: { (existingDate) -> Bool in
|
||||
if codDate.month == existingDate.month && codDate.year == existingDate.year && existingDate.day == codDate.day{
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}){
|
||||
return datesPlayed
|
||||
}else {
|
||||
|
||||
return datesPlayed + [codDate]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// repeat {
|
||||
// previousDays.append(CODDate(month: date.month, year: date.year, day: date.day, hour: date.hour, minute: date.minute))
|
||||
// date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
|
||||
// } while (date < Date())
|
||||
//
|
||||
// return previousDays
|
||||
}
|
||||
|
||||
|
||||
func allDaily(req:Request) -> EventLoopFuture<AllDailyStats>{
|
||||
|
||||
let previousDays = getDaysPlayed().reversed()
|
||||
|
||||
func getDailyStats (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
|
||||
return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats] (stats) -> EventLoopFuture<[DailyStats]> in
|
||||
var allDailyStats = allDailyStats
|
||||
return getDaysPlayed(req: req).flatMap { (previousDays) -> (EventLoopFuture<AllDailyStats>) in
|
||||
|
||||
func getDailyStats (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], eventLoop: EventLoop) -> EventLoopFuture<[DailyStats]> {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
|
||||
let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalWins) }
|
||||
|
||||
let totalLosses = allDailyStats.reduce(Double(stats.totalLosses)) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalLosses)
|
||||
return self.getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats] (stats) -> EventLoopFuture<[DailyStats]> in
|
||||
var allDailyStats = allDailyStats
|
||||
|
||||
let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalWins) }
|
||||
|
||||
let totalLosses = allDailyStats.reduce(Double(stats.totalLosses)) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalLosses)
|
||||
}
|
||||
|
||||
allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses)))
|
||||
return getDailyStats(remaining, allDailyStats:&allDailyStats, eventLoop: eventLoop)
|
||||
}
|
||||
|
||||
allDailyStats.append(DailyStats(day: first.day, month: first.month, year: first.year, stats: stats, cumulativeRatio: self.getRatio(num: totalWins, den: totalLosses)))
|
||||
return getDailyStats(remaining, allDailyStats:&allDailyStats, eventLoop: eventLoop)
|
||||
} else {
|
||||
return req.eventLoop.makeSucceededFuture(allDailyStats)
|
||||
}
|
||||
|
||||
} else {
|
||||
return req.eventLoop.makeSucceededFuture(allDailyStats)
|
||||
}
|
||||
|
||||
|
||||
var stats:[DailyStats] = []
|
||||
let dailyStats = getDailyStats(Array(previousDays)[0..<previousDays.count], allDailyStats:&stats, eventLoop: req.eventLoop)
|
||||
|
||||
return dailyStats.map { (dailyStats) -> AllDailyStats in
|
||||
return AllDailyStats(dailyStats: dailyStats.filter({ (dailyStats) -> Bool in
|
||||
|
||||
if dailyStats.stats.totalWins == 0 && dailyStats.stats.totalLosses == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}).reversed()
|
||||
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var stats:[DailyStats] = []
|
||||
let dailyStats = getDailyStats(Array(previousDays)[0..<previousDays.count], allDailyStats:&stats, eventLoop: req.eventLoop)
|
||||
|
||||
return dailyStats.map { (dailyStats) -> AllDailyStats in
|
||||
return AllDailyStats(dailyStats: dailyStats.filter({ (dailyStats) -> Bool in
|
||||
|
||||
if dailyStats.stats.totalWins == 0 && dailyStats.stats.totalLosses == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}).reversed()
|
||||
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> {
|
||||
|
||||
let previousDays = getDaysPlayed().reversed()
|
||||
|
||||
func getRatios (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
|
||||
return getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats, cumulativeWinLossRatios] (stats) -> EventLoopFuture<[DataPoint]> in
|
||||
var allDailyStats = allDailyStats
|
||||
// let previousDays = getDaysPlayed().reversed()
|
||||
|
||||
|
||||
return getDaysPlayed(req: req).flatMap { (previousDays) -> (EventLoopFuture<[DataPoint]>) in
|
||||
|
||||
|
||||
|
||||
func getRatios (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> {
|
||||
var remaining = remaining
|
||||
if let first = remaining.popLast() {
|
||||
|
||||
let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalWins) }
|
||||
|
||||
let totalLosses = allDailyStats.reduce(Double(stats.totalLosses)) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalLosses)
|
||||
return self.getStatsForDay(year: first.year, month: first.month, day:first.day, req: req).flatMap { [remaining, allDailyStats, cumulativeWinLossRatios] (stats) -> EventLoopFuture<[DataPoint]> in
|
||||
var allDailyStats = allDailyStats
|
||||
|
||||
let totalWins = allDailyStats.reduce(Double(stats.totalWins)) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalWins) }
|
||||
|
||||
let totalLosses = allDailyStats.reduce(Double(stats.totalLosses)) { (total, dailyStats) -> Double in
|
||||
return total + Double(dailyStats.stats.totalLosses)
|
||||
}
|
||||
|
||||
var cumulativeWinLossRatios = cumulativeWinLossRatios
|
||||
if !(stats.totalWins == 0 && stats.totalLosses == 0) {
|
||||
|
||||
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)")
|
||||
let x = Double(cumulativeWinLossRatios.count)
|
||||
|
||||
let d = Date(timeIntervalSince1970: date.timeIntervalSince1970)
|
||||
cumulativeWinLossRatios.append(DataPoint(x:x , y: (totalWins/totalLosses).truncate(places: 2), label:("\(Utilities.monthToString(month: d.month)) \(d.day)")))
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
var cumulativeWinLossRatios = cumulativeWinLossRatios
|
||||
if !(stats.totalWins == 0 && stats.totalLosses == 0) {
|
||||
|
||||
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)")
|
||||
let x = Double(cumulativeWinLossRatios.count)
|
||||
|
||||
let d = Date(timeIntervalSince1970: date.timeIntervalSince1970)
|
||||
cumulativeWinLossRatios.append(DataPoint(x:x , y: (totalWins/totalLosses).truncate(places: 2), label:("\(Utilities.monthToString(month: d.month)) \(d.day)")))
|
||||
}
|
||||
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)
|
||||
} else {
|
||||
return req.eventLoop.makeSucceededFuture(cumulativeWinLossRatios)
|
||||
}
|
||||
|
||||
} else {
|
||||
return req.eventLoop.makeSucceededFuture(cumulativeWinLossRatios)
|
||||
}
|
||||
|
||||
var stats:[DailyStats] = []
|
||||
var cumulativeWinLossRatios:[DataPoint] = [DataPoint]()
|
||||
|
||||
|
||||
return getRatios(Array(previousDays)[0..<previousDays.count], allDailyStats: &stats, cumulativeWinLossRatios: &cumulativeWinLossRatios, eventLoop: req.eventLoop)
|
||||
}
|
||||
|
||||
var stats:[DailyStats] = []
|
||||
var cumulativeWinLossRatios:[DataPoint] = [DataPoint]()
|
||||
|
||||
|
||||
return getRatios(Array(previousDays)[0..<previousDays.count], allDailyStats: &stats, cumulativeWinLossRatios: &cumulativeWinLossRatios, eventLoop: req.eventLoop)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,7 +460,7 @@ extension Date {
|
||||
let calanderDate = calendar.dateComponents([.minute, .month, .year, .hour, .day], from: self)
|
||||
return calanderDate.hour ?? 1
|
||||
}
|
||||
|
||||
|
||||
|
||||
var minute:Int {
|
||||
var calendar = Calendar.current
|
||||
@@ -449,5 +468,5 @@ extension Date {
|
||||
let calanderDate = calendar.dateComponents([.minute, .month, .year, .hour, .day], from: self)
|
||||
return calanderDate.minute ?? 1
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user