new stats

This commit is contained in:
Michael Simard
2021-11-01 22:29:07 -05:00
parent 2c5ea3a9d5
commit 2054181d49

View File

@@ -37,12 +37,19 @@ struct StatsController: RouteCollection {
let adamAffectedMatches = Match.query(on: db).filter(\.$finalKillRuinedPlayerId == 6).count() let adamAffectedMatches = Match.query(on: db).filter(\.$finalKillRuinedPlayerId == 6).count()
let totalMWGames = Match.query(on: db).filter(\.$codGame == "mw").count() let totalMWGames = Match.query(on: db).filter(\.$codGame == "mw").count()
return (statistics.and(adamAffectedMatches).and(totalMWGames)).map { arg -> (DashboardStats) in
let colourSchemeConfigs = ColourScheme.query(on: db).all().map { colourScheme in
return colourScheme.map { cs in
return cs.colorSchemeConfig
}
}
return (statistics.and(adamAffectedMatches).and(totalMWGames).and(colourSchemeConfigs)).map { arg -> (DashboardStats) in
let (((statistics, adamAffectedMatches), totalMWGames)) = arg let ((((statistics, adamAffectedMatches), totalMWGames),colors)) = arg
// return statistics.map { statistics in // return statistics.map { statistics in
return DashboardStats(dashboardItems: (statistics.map({ statisticItem in return DashboardStats(dashboardItems: (statistics.map({ statisticItem in
@@ -51,12 +58,14 @@ struct StatsController: RouteCollection {
var content1 = "" var content1 = ""
var content2 = "" var content2 = ""
var sortOrder = 0 var sortOrder = 0
var backgroundColor = "484848"
if statisticItem.codTrackerId == "no_hyder_overall"{ if statisticItem.codTrackerId == "no_hyder_overall"{
title2 = "Ratio" title2 = "Ratio"
content1 = "\(statisticItem.wins)-\(statisticItem.losses)" content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses) content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = 100 sortOrder = 100
} }
else if statisticItem.codTrackerId == "mw_overall"{ else if statisticItem.codTrackerId == "mw_overall"{
@@ -64,29 +73,33 @@ struct StatsController: RouteCollection {
content1 = "\(statisticItem.wins)-\(statisticItem.losses)" content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses) content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = 20 sortOrder = 20
backgroundColor = colors.first(where: {$0.gameId == "mw"})?.tileColour ?? backgroundColor
} }
else if statisticItem.codTrackerId == "mw_six_players"{ else if statisticItem.codTrackerId == "mw_six_players"{
title2 = "Ratio" title2 = "Ratio"
content1 = "\(statisticItem.wins)-\(statisticItem.losses)" content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses) content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = 60 sortOrder = 60
backgroundColor = colors.first(where: {$0.gameId == "mw"})?.tileColour ?? backgroundColor
} }
else if statisticItem.codTrackerId == "2021_overall"{ else if statisticItem.codTrackerId == "2021_overall"{
title2 = "Ratio" title2 = "Ratio"
content1 = "\(statisticItem.wins)-\(statisticItem.losses)" content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses) content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = 50 sortOrder = 50
} }
else if statisticItem.codTrackerId == "bocw_overall"{ else if statisticItem.codTrackerId == "bocw_overall"{
title2 = "Ratio" title2 = "Ratio"
content1 = "\(statisticItem.wins)-\(statisticItem.losses)" content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses) content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = 30 sortOrder = 30
backgroundColor = colors.first(where: {$0.gameId == "bocw"})?.tileColour ?? backgroundColor
} }
else if statisticItem.codTrackerId == "with_hyder_overall"{ else if statisticItem.codTrackerId == "with_hyder_overall"{
title2 = "Ratio" title2 = "Ratio"
@@ -99,15 +112,16 @@ struct StatsController: RouteCollection {
content1 = "\(statisticItem.wins)-\(statisticItem.losses)" content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses) content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = 70 sortOrder = 70
backgroundColor = colors.first(where: {$0.gameId == "mw"})?.tileColour ?? backgroundColor
} }
else if statisticItem.codTrackerId == "casual_overall"{ else if statisticItem.codTrackerId == "casual_overall"{
title2 = "Ratio" title2 = "Ratio"
content1 = "\(statisticItem.wins)-\(statisticItem.losses)" content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses) content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = 40 sortOrder = 40
} }
else if statisticItem.codTrackerId == "overall_four_players"{ else if statisticItem.codTrackerId == "overall_four_players"{
title2 = "Ratio" title2 = "Ratio"
@@ -128,35 +142,54 @@ struct StatsController: RouteCollection {
sortOrder = 90 sortOrder = 90
} }
else if statisticItem.codTrackerId == "bocw_nuketown_halloween"{ else if statisticItem.codTrackerId == "bocw_nuketown_halloween"{
title2 = "Ratio" title2 = "Ratio"
content1 = "\(statisticItem.wins)-\(statisticItem.losses)" content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses) content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = 41 sortOrder = 41
backgroundColor = "CC5500"
} }
else if statisticItem.codTrackerId == "vg_casual"{
title2 = "Ratio"
content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = -80
backgroundColor = colors.first(where: {$0.gameId == "vg"})?.tileColour ?? backgroundColor
}
else if statisticItem.codTrackerId == "bocw_casual"{
title2 = "Ratio"
content1 = "\(statisticItem.wins)-\(statisticItem.losses)"
content2 = Utils.getRatio(wins: statisticItem.wins, losses: statisticItem.losses)
sortOrder = -79
backgroundColor = colors.first(where: {$0.gameId == "bocw"})?.tileColour ?? backgroundColor
}
else { else {
} }
return return DashboardItem(codTrackerId:statisticItem.codTrackerId, title: title, content: content1, title2: title2, content2: content2, sortOrder: sortOrder, backgroundColor:backgroundColor)
DashboardItem(codTrackerId:statisticItem.codTrackerId, title: title, content: content1, title2: title2, content2: content2, sortOrder: sortOrder, backgroundColor: statisticItem.codTrackerId == "bocw_nuketown_halloween" ? "FFA500" : "004999")
} ) + } ) +
[ [
DashboardItem(codTrackerId: "adam_ruined_final_kills", title: "Final Kills Ruined by Adam", content: "\(adamAffectedMatches + 7)", title2: "", content2: "",sortOrder: 1000), DashboardItem(codTrackerId: "adam_ruined_final_kills", title: "Final Kills Ruined by Adam", content: "\(adamAffectedMatches + 7)", title2: "", content2: "",sortOrder: 1000, backgroundColor: "484848"),
DashboardItem(codTrackerId:"total_mw_games", title: "Total MW Games", content: "\(totalMWGames)", title2: "", content2: "", sortOrder: -10), DashboardItem(codTrackerId:"total_mw_games", title: "Total MW Games", content: "\(totalMWGames)", title2: "", content2: "", sortOrder: -10, backgroundColor: colors.first(where: {$0.gameId == "mw"})?.tileColour ?? "484848"
]
).sorted{$0.sortOrder < $1.sortOrder}
),
]
).sorted{$0.sortOrder < $1.sortOrder}
) )
} }
} }
func dashboard(req: Request) throws -> EventLoopFuture<DashboardStats> { func dashboard(req: Request) throws -> EventLoopFuture<DashboardStats> {
return try dashboard(db: req.db) return try dashboard(db: req.db)
} }
@@ -217,7 +250,7 @@ struct StatsController: RouteCollection {
} }
func getAllMatches(matches:[Match]) -> Stats{ func getAllMatches(matches:[Match]) -> Stats{
let totals = matches.reduce([0,0]) { (totals, match) -> [Int] in let totals = matches.reduce([0,0]) { (totals, match) -> [Int] in
if match.win == true { if match.win == true {
return [totals[0] + 1, totals[1]] return [totals[0] + 1, totals[1]]
@@ -235,35 +268,35 @@ struct StatsController: RouteCollection {
} }
func getStatsWithMostRecentDailyRecord(sortedMatches:[Match], game:String? = nil) -> StatsWithMostRecentDailyRecord { func getStatsWithMostRecentDailyRecord(sortedMatches:[Match], game:String? = nil) -> StatsWithMostRecentDailyRecord {
let stats = getCountedMatches(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)
//print ("MRR DAILY \(Date().timeIntervalSince(startTime))") //print ("MRR DAILY \(Date().timeIntervalSince(startTime))")
let ret = StatsWithMostRecentDailyRecord(winLoss: stats.winLossRatio, totalWins: stats.totalWins, totalLosses: stats.totalLosses, mostRecentRecord:"\(mostRecentDailyStats.totalWins)-\(mostRecentDailyStats.totalLosses)") let ret = StatsWithMostRecentDailyRecord(winLoss: stats.winLossRatio, totalWins: stats.totalWins, totalLosses: stats.totalLosses, mostRecentRecord:"\(mostRecentDailyStats.totalWins)-\(mostRecentDailyStats.totalLosses)")
return ret return ret
} }
func mostRecentDailyStats (matches:[Match], game:String? = nil) -> Stats{ func mostRecentDailyStats (matches:[Match], game:String? = nil) -> Stats{
let daysPlayed = getDaysPlayed(sortedMatches: matches) let daysPlayed = getDaysPlayed(sortedMatches: matches)
let lastDayPlayed = daysPlayed.last let lastDayPlayed = daysPlayed.last
//print ("MDD days played \(Date().timeIntervalSince(startTime))") //print ("MDD days played \(Date().timeIntervalSince(startTime))")
return getCountedMatches(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 &&
match.date.year == lastDayPlayed?.year && match.date.year == lastDayPlayed?.year &&
self.shouldCountMatch(match: match) self.shouldCountMatch(match: match)
if let game = game { if let game = game {
shouldInclude = shouldInclude && match.codGame == game shouldInclude = shouldInclude && match.codGame == game
@@ -278,7 +311,7 @@ struct StatsController: RouteCollection {
let isColdWar = match.codGame == "bocw" let isColdWar = match.codGame == "bocw"
let numberOfPlayers = self.numberOfPlayers(match: match) let numberOfPlayers = self.numberOfPlayers(match: match)
if match.competitive == false { if match.competitive == false {
return false return false
} }
@@ -295,7 +328,7 @@ struct StatsController: RouteCollection {
} }
} }
else { else {
if numberOfPlayers > 4 || numberOfPlayers == 0 { if numberOfPlayers > 4 || numberOfPlayers == 0 {
return true return true
} }
@@ -308,7 +341,7 @@ struct StatsController: RouteCollection {
} }
} }
} }
func getStatsForYear(year:Int, db: Database) -> EventLoopFuture<Stats>{ func getStatsForYear(year:Int, db: Database) -> EventLoopFuture<Stats>{
@@ -321,7 +354,7 @@ struct StatsController: RouteCollection {
dateComponents.timeZone = TimeZone(abbreviation: "EST") // Japan Standard Time dateComponents.timeZone = TimeZone(abbreviation: "EST") // Japan Standard Time
dateComponents.hour = 8 dateComponents.hour = 8
dateComponents.minute = 0 dateComponents.minute = 0
// Create date from components // Create date from components
let userCalendar = Calendar(identifier: .gregorian) // since the components above (like year 1980) are for Gregorian let userCalendar = Calendar(identifier: .gregorian) // since the components above (like year 1980) are for Gregorian
let startDate = userCalendar.date(from: dateComponents) let startDate = userCalendar.date(from: dateComponents)
@@ -335,10 +368,10 @@ struct StatsController: RouteCollection {
endDateComponenents.timeZone = TimeZone(abbreviation: "EST") // Japan Standard Time endDateComponenents.timeZone = TimeZone(abbreviation: "EST") // Japan Standard Time
endDateComponenents.hour = 8 endDateComponenents.hour = 8
endDateComponenents.minute = 0 endDateComponenents.minute = 0
let endDate = userCalendar.date(from: endDateComponenents) let endDate = userCalendar.date(from: endDateComponenents)
return Match.query(on: db).filter(\.$date > startDate!).filter(\.$date < endDate!).all().map { (matches) -> (Stats) in return Match.query(on: db).filter(\.$date > startDate!).filter(\.$date < endDate!).all().map { (matches) -> (Stats) in
return self.getCountedMatches(matches: matches) return self.getCountedMatches(matches: matches)
@@ -352,7 +385,7 @@ struct StatsController: RouteCollection {
private func getDaysPlayed(sortedMatches:[Match]) -> [CODDate] { private func getDaysPlayed(sortedMatches:[Match]) -> [CODDate] {
let dates = sortedMatches.suffix(30).map { (match) -> CODDate in let dates = sortedMatches.suffix(30).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) return CODDate(month: match.date.month, year: match.date.year, day: match.date.day, hour: match.date.hour, minute: match.date.minute)
} }
@@ -441,7 +474,7 @@ struct StatsController: RouteCollection {
return getstatsForMonth(year: 2020, month: 03, req: req) return getstatsForMonth(year: 2020, month: 03, req: req)
} }
func getstatsForMonth(year:Int, month:Int, req: Request) -> EventLoopFuture<Stats>{ func getstatsForMonth(year:Int, month:Int, req: Request) -> EventLoopFuture<Stats>{
@@ -467,14 +500,19 @@ struct StatsController: RouteCollection {
func getCasualStats( db: Database) -> EventLoopFuture<Stats>{ func getCasualStats( db: Database) -> EventLoopFuture<Stats>{
return Match.query(on: db).filter(\.$competitive == false).all().map { matches in return Match.query(on: db).filter(\.$competitive == false).all().map { matches in
return Stats(totalWins: matches.filter{$0.win == true}.count, totalLosses: matches.filter{$0.win == false}.count) return Stats(totalWins: matches.filter{$0.win == true}.count, totalLosses: matches.filter{$0.win == false}.count)
} }
} }
func getCasualStatsForGame( db: Database, gameId:String, afterDate:Date = Date(timeIntervalSince1970: 0)) -> EventLoopFuture<Stats>{
return Match.query(on: db).filter(\.$competitive == false).filter(\.$codGame == gameId).filter(\.$date > afterDate ).all().map { matches in
return Stats(totalWins: matches.filter{$0.win == true}.count, totalLosses: matches.filter{$0.win == false}.count)
}
}
@@ -608,18 +646,18 @@ struct StatsController: RouteCollection {
return self.getStatsForDay(year: days.first?.year ?? 0, month: days.first?.month ?? 0, day: days.first?.day ?? 0, req: req) return self.getStatsForDay(year: days.first?.year ?? 0, month: days.first?.month ?? 0, day: days.first?.day ?? 0, req: req)
} }
} }
func recalc(req:Request) -> EventLoopFuture<[String:Stats]> { func recalc(req:Request) -> EventLoopFuture<[String:Stats]> {
return forceCalculatedStats(db: req.db) return forceCalculatedStats(db: req.db)
} }
func forceCalculatedStats(db: Database) -> EventLoopFuture<[String:Stats]> { func forceCalculatedStats(db: Database) -> EventLoopFuture<[String:Stats]> {
let statsWithHyder = statsWithPlayer(db: db, playerId: 5) let statsWithHyder = statsWithPlayer(db: db, playerId: 5)
let statsWithoutHyder = statsWithoutPlayer(db: db, playerId: 5) let statsWithoutHyder = statsWithoutPlayer(db: db, playerId: 5)
let statsFor2020 = getStatsForYear(year: 2020, db: db) let statsFor2020 = getStatsForYear(year: 2020, db: db)
let statsFor2021 = getStatsForYear(year: 2021, db: db) let statsFor2021 = getStatsForYear(year: 2021, db: db)
let hyderFuture = statsWithHyder.and(statsWithoutHyder) let hyderFuture = statsWithHyder.and(statsWithoutHyder)
@@ -629,32 +667,37 @@ struct StatsController: RouteCollection {
let casualOverall = getCasualStats(db: db) let casualOverall = getCasualStats(db: db)
let bocwCasual = getCasualStatsForGame(db: db, gameId: "bocw")
let vgCasual = getCasualStatsForGame(db: db, gameId: "vg", afterDate: Date(timeIntervalSince1970: 1635823473)) //nov 21 to exlude beta time in sept
let matches = Match.query(on: db).sort( \.$date).all() let matches = Match.query(on: db).sort( \.$date).all()
return matches.and(hyderStats).and(statsFor2020).and(statsFor2021).and(casualOverall).map { arg -> ([String:Stats]) in return matches.and(hyderStats).and(statsFor2020).and(statsFor2021).and(casualOverall).and(bocwCasual).and(vgCasual).map { arg -> ([String:Stats]) in
let ((((matches, hyderStats), statsFor2020), statsFor2021), casualOverall) = arg let ((((((matches, hyderStats), statsFor2020), statsFor2021), casualOverall), bocwCasual), vgCasual) = arg
//print ("got matches \(Date().timeIntervalSince(startTime))") //print ("got matches \(Date().timeIntervalSince(startTime))")
let queue = DispatchQueue(label: "com.sledsoft.cod-tracker.queue", attributes: .concurrent) let queue = DispatchQueue(label: "com.sledsoft.cod-tracker.queue", attributes: .concurrent)
let group = DispatchGroup() let group = DispatchGroup()
var overallStats:StatsWithMostRecentDailyRecord? var overallStats:StatsWithMostRecentDailyRecord?
var mwStats:StatsWithMostRecentDailyRecord? var mwStats:StatsWithMostRecentDailyRecord?
var bocwStats:StatsWithMostRecentDailyRecord? var bocwStats:StatsWithMostRecentDailyRecord?
var mostRecentStats:Stats? var mostRecentStats:Stats?
var mwSixPlayers:Stats? var mwSixPlayers:Stats?
var mwFivePlayers:Stats? var mwFivePlayers:Stats?
var overallFourPlayers:Stats? var overallFourPlayers:Stats?
var blackOpsColdWarNuketownHalloween:Stats? var blackOpsColdWarNuketownHalloween:Stats?
// var mapStats:[Int:Stats]?
// var worstMap:Int? // var mapStats:[Int:Stats]?
// var bestMap:Int? // var worstMap:Int?
// // var bestMap:Int?
//
group.enter() group.enter()
queue.async { queue.async {
overallStats = self.getStatsWithMostRecentDailyRecord(sortedMatches: matches) overallStats = self.getStatsWithMostRecentDailyRecord(sortedMatches: matches)
@@ -678,7 +721,7 @@ struct StatsController: RouteCollection {
return match.codGame == "bocw" && self.shouldCountMatch(match: match ) return match.codGame == "bocw" && self.shouldCountMatch(match: match )
})) }))
group.leave() group.leave()
} }
@@ -694,7 +737,7 @@ struct StatsController: RouteCollection {
mwFivePlayers = self.getCountedMatchesByPlayerCount(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 {
@@ -707,10 +750,10 @@ struct StatsController: RouteCollection {
blackOpsColdWarNuketownHalloween = self.getRecordForMapId(matches: matches.filter{$0.codGame == "bocw"}, mapId: "69") blackOpsColdWarNuketownHalloween = self.getRecordForMapId(matches: matches.filter{$0.codGame == "bocw"}, mapId: "69")
group.leave() group.leave()
} }
group.wait() group.wait()
return [ return [
"bocw_nuketown_halloween":Stats(totalWins: blackOpsColdWarNuketownHalloween!.totalWins, totalLosses: blackOpsColdWarNuketownHalloween!.totalLosses), "bocw_nuketown_halloween":Stats(totalWins: blackOpsColdWarNuketownHalloween!.totalWins, totalLosses: blackOpsColdWarNuketownHalloween!.totalLosses),
"mw_overall":Stats(totalWins: mwStats!.totalWins, totalLosses: mwStats!.totalLosses), "mw_overall":Stats(totalWins: mwStats!.totalWins, totalLosses: mwStats!.totalLosses),
@@ -724,30 +767,29 @@ struct StatsController: RouteCollection {
"mw_five_players":Stats(totalWins: mwFivePlayers!.totalWins, totalLosses: mwFivePlayers!.totalLosses), "mw_five_players":Stats(totalWins: mwFivePlayers!.totalWins, totalLosses: mwFivePlayers!.totalLosses),
"overall":Stats(totalWins: overallStats!.totalWins, totalLosses: overallStats!.totalLosses), "overall":Stats(totalWins: overallStats!.totalWins, totalLosses: overallStats!.totalLosses),
"casual_overall":Stats(totalWins: casualOverall.totalWins, totalLosses: casualOverall.totalLosses), "casual_overall":Stats(totalWins: casualOverall.totalWins, totalLosses: casualOverall.totalLosses),
"bocw_casual":Stats(totalWins: bocwCasual.totalWins, totalLosses: bocwCasual.totalLosses),
"vg_casual":Stats(totalWins: vgCasual.totalWins, totalLosses: vgCasual.totalLosses),
] ]
} }
} }
func overall(db: Database) throws -> EventLoopFuture<OverallStats> { func overall(db: Database) throws -> EventLoopFuture<OverallStats> {
let dashboardStats = try dashboard(db:db) let dashboardStats = try dashboard(db:db)
let matches = Match.query(on: db).sort( \.$date).all() let matches = Match.query(on: db).sort( \.$date).all()
let mapConfigs = getMapConfigs(db: db) let mapConfigs = getMapConfigs(db: db)
return matches.and(dashboardStats).and(mapConfigs).map { arg -> (OverallStats) in return matches.and(dashboardStats).and(mapConfigs).map { arg -> (OverallStats) in
let ((matches, dashboardStats),mapConfigs) = arg let ((matches, dashboardStats),mapConfigs) = arg
let queue = DispatchQueue(label: "com.sledsoft.cod-tracker.queue", attributes: .concurrent) let queue = DispatchQueue(label: "com.sledsoft.cod-tracker.queue", attributes: .concurrent)
let group = DispatchGroup() let group = DispatchGroup()
@@ -755,43 +797,43 @@ struct StatsController: RouteCollection {
var worstMap:Int? var worstMap:Int?
var bestMap:Int? var bestMap:Int?
// //
group.enter() group.enter()
queue.async { queue.async {
mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true) mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true)
//print ("maps done \(Date().timeIntervalSince(startTime))") //print ("maps done \(Date().timeIntervalSince(startTime))")
group.leave() group.leave()
}
//
group.enter()
queue.async {
let mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true)
bestMap = self.getBestMap(records: mapStats)
group.leave()
}
group.enter()
queue.async {
let mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true)
worstMap = self.getWorstMap(records: mapStats)
group.leave()
}
group.wait()
let dashboardItems:[DashboardItem] =
(dashboardStats.dashboardItems + }
[ //
DashboardItem(codTrackerId:"best_map_overall", title: "Best Map", content: mapConfigs.first{$0.mapId == bestMap}?.name ?? "error", title2: "Ratio", content2: "\(mapStats![bestMap!]!.winLossRatio) \(mapStats![bestMap!]!.record)", sortOrder: 12), group.enter()
queue.async {
let mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true)
bestMap = self.getBestMap(records: mapStats)
group.leave()
}
group.enter()
queue.async {
let mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true)
worstMap = self.getWorstMap(records: mapStats)
group.leave()
}
group.wait()
let dashboardItems:[DashboardItem] =
(dashboardStats.dashboardItems +
[
DashboardItem(codTrackerId:"best_map_overall", title: "Best Map", content: mapConfigs.first{$0.mapId == bestMap}?.name ?? "error", title2: "Ratio", content2: "\(mapStats![bestMap!]!.winLossRatio) \(mapStats![bestMap!]!.record)", sortOrder: 12),
DashboardItem(codTrackerId:"worst_map_overall", title: "Worst Map", content: mapConfigs.first{$0.mapId == worstMap}?.name ?? "error", title2: "Ratio", content2: "\(mapStats![worstMap!]!.winLossRatio) \(mapStats![worstMap!]!.record)",sortOrder: 13), DashboardItem(codTrackerId:"worst_map_overall", title: "Worst Map", content: mapConfigs.first{$0.mapId == worstMap}?.name ?? "error", title2: "Ratio", content2: "\(mapStats![worstMap!]!.winLossRatio) \(mapStats![worstMap!]!.record)",sortOrder: 13),
]).sorted{ ]).sorted{
$0.sortOrder < $1.sortOrder $0.sortOrder < $1.sortOrder
} }
return OverallStats(dashboardItems: dashboardItems) return OverallStats(dashboardItems: dashboardItems)
} }
} }
@@ -801,7 +843,7 @@ struct StatsController: RouteCollection {
return try overall(db: req.db) return try overall(db: req.db)
} }
func getMapConfigs(db: Database) -> EventLoopFuture<[MapConfig]> { func getMapConfigs(db: Database) -> EventLoopFuture<[MapConfig]> {
@@ -817,26 +859,26 @@ struct StatsController: RouteCollection {
func mapRecords(req: Request) throws -> EventLoopFuture<[MapRecord]> { func mapRecords(req: Request) throws -> EventLoopFuture<[MapRecord]> {
let matches = Match.query(on: req.db).all() let matches = Match.query(on: req.db).all()
let mapConfigs = getMapConfigs(db: req.db) let mapConfigs = getMapConfigs(db: req.db)
return matches.and(mapConfigs).map { matches, mapConfigs in return matches.and(mapConfigs).map { matches, mapConfigs in
let mapStats:[Int:Stats] let mapStats:[Int:Stats]
if let game = req.parameters.get("game", as: String.self), if let game = req.parameters.get("game", as: String.self),
let competitive = req.parameters.get("competitive", as:Bool.self) { let competitive = req.parameters.get("competitive", as:Bool.self) {
let gameMode = req.parameters.get("gamemode", as:Int.self) ?? -2 let gameMode = req.parameters.get("gamemode", as:Int.self) ?? -2
mapStats = self.getMapStats(matches: matches,game: game, competitive: competitive, gameMode: gameMode) mapStats = self.getMapStats(matches: matches,game: game, competitive: competitive, gameMode: gameMode)
} }
else { else {
mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true) mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true)
} }
let sortedMaps = self.mapsSortedByBest(records: mapStats) let sortedMaps = self.mapsSortedByBest(records: mapStats)
let records = sortedMaps.map { (mapId) -> MapRecord in let records = sortedMaps.map { (mapId) -> MapRecord in
@@ -850,51 +892,51 @@ struct StatsController: RouteCollection {
//print("\(record.map.name) \(record.stats.record) \(record.ratio)") //print("\(record.map.name) \(record.stats.record) \(record.ratio)")
wins = wins + Double(record.stats.totalWins) wins = wins + Double(record.stats.totalWins)
loss = loss + Double(record.stats.totalLosses) loss = loss + Double(record.stats.totalLosses)
} }
return records return records
} }
// //
// return Match.query(on: req.db).all().map { (matches) -> [MapRecord] in // return Match.query(on: req.db).all().map { (matches) -> [MapRecord] in
// //
// //
// //
// let mapStats:[Int:Stats] // let mapStats:[Int:Stats]
// //
// if let game = req.parameters.get("game", as: String.self), // if let game = req.parameters.get("game", as: String.self),
// let competitive = req.parameters.get("competitive", as:Bool.self) { // let competitive = req.parameters.get("competitive", as:Bool.self) {
// //
// let gameMode = req.parameters.get("gamemode", as:Int.self) ?? -2 // let gameMode = req.parameters.get("gamemode", as:Int.self) ?? -2
// mapStats = self.getMapStats(matches: matches,game: game, competitive: competitive, gameMode: gameMode) // mapStats = self.getMapStats(matches: matches,game: game, competitive: competitive, gameMode: gameMode)
// //
// } // }
// else { // else {
// mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true) // mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true)
// } // }
// //
// //
// let sortedMaps = self.mapsSortedByBest(records: mapStats) // let sortedMaps = self.mapsSortedByBest(records: mapStats)
// //
// let records = sortedMaps.map { (mapId) -> MapRecord in // let records = sortedMaps.map { (mapId) -> MapRecord in
// return MapRecord(map: MapData.allMaps[mapId]!, stats: mapStats[mapId]!, ratio:mapStats[mapId]!.winLossRatio) // return MapRecord(map: MapData.allMaps[mapId]!, stats: mapStats[mapId]!, ratio:mapStats[mapId]!.winLossRatio)
// } // }
// //
// var wins:Double = 0 // var wins:Double = 0
// var loss:Double = 0 // var loss:Double = 0
// //
// for record in records { // for record in records {
// //print("\(record.map.name) \(record.stats.record) \(record.ratio)") // //print("\(record.map.name) \(record.stats.record) \(record.ratio)")
// wins = wins + Double(record.stats.totalWins) // wins = wins + Double(record.stats.totalWins)
// loss = loss + Double(record.stats.totalLosses) // loss = loss + Double(record.stats.totalLosses)
// //
// } // }
// return records // return records
// } // }
} }
func mapsSortedByBest (records :[ Int:Stats] ) -> [ Int ] { func mapsSortedByBest (records :[ Int:Stats] ) -> [ Int ] {
@@ -904,16 +946,16 @@ struct StatsController: RouteCollection {
} }
func getCountedMatchesByPlayerCount(matches:[Match], playerCount:Int) -> Stats { func getCountedMatchesByPlayerCount(matches:[Match], playerCount:Int) -> Stats {
return getCountedMatches(matches: matches.filter{$0.playerList.count == playerCount}) return getCountedMatches(matches: matches.filter{$0.playerList.count == playerCount})
} }
func getRecordForMapId(matches:[Match], mapId:String) -> Stats { func getRecordForMapId(matches:[Match], mapId:String) -> Stats {
return getAllMatches(matches: matches.filter{$0.map == mapId}) return getAllMatches(matches: matches.filter{$0.map == mapId})
} }
func getAllMatchesByPlayerCount(matches:[Match], playerCount:Int) -> Stats { func getAllMatchesByPlayerCount(matches:[Match], playerCount:Int) -> Stats {
return getAllMatches(matches: matches.filter{$0.playerList.count == playerCount}) return getAllMatches(matches: matches.filter{$0.playerList.count == playerCount})
} }
func getBestMap (records :[ Int:Stats] ) -> Int { func getBestMap (records :[ Int:Stats] ) -> Int {
@@ -952,7 +994,7 @@ struct StatsController: RouteCollection {
return true return true
} }
} }
for match in filteredMatches { for match in filteredMatches {
@@ -966,18 +1008,18 @@ struct StatsController: RouteCollection {
if let map = match.map, let mapInt = Int(map) { if let map = match.map, let mapInt = Int(map) {
if mapStats[mapInt] == nil { if mapStats[mapInt] == nil {
mapStats[mapInt] = Stats(totalWins: 0, totalLosses: 0) mapStats[mapInt] = Stats(totalWins: 0, totalLosses: 0)
} }
if match.win { if match.win {
mapStats[mapInt]?.totalWins += 1 mapStats[mapInt]?.totalWins += 1
} }
else{ else{
mapStats[mapInt]?.totalLosses += 1 mapStats[mapInt]?.totalLosses += 1
} }
} }
} }
return mapStats return mapStats
} }
@@ -1064,7 +1106,7 @@ struct StatsController: RouteCollection {
} }
return true return true
}).reversed() }).reversed()
) )
} }
} }
@@ -1086,9 +1128,9 @@ struct StatsController: RouteCollection {
func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> { func getCumulativeWinLossRatios(req:Request) -> EventLoopFuture<[DataPoint]> {
return getDaysPlayed(req: req).flatMap { (previousDays) -> (EventLoopFuture<[DataPoint]>) in
return getDaysPlayed(req: req).flatMap { (previousDays) -> (EventLoopFuture<[DataPoint]>) in
func getRatios (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> { func getRatios (_ remaining: ArraySlice<CODDate>, allDailyStats: inout [DailyStats], cumulativeWinLossRatios: inout [DataPoint], eventLoop: EventLoop) -> EventLoopFuture<[DataPoint]> {
var remaining = remaining var remaining = remaining
if let first = remaining.popLast() { if let first = remaining.popLast() {