From 38453355ddbe976dfb0afd6f7a8e474910800dc8 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Mon, 11 Jan 2021 09:35:42 -0600 Subject: [PATCH] add map history --- Sources/App/Controllers/StatsController.swift | 41 +++++++++++++++++++ Sources/App/Models/Map.swift | 2 +- Sources/App/Models/MapRecord.swift | 16 ++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Sources/App/Models/MapRecord.swift diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index 765927c..2376f04 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -24,6 +24,8 @@ struct StatsController: RouteCollection { statsRoute.post("logMatch", use: logMatch) statsRoute.get("history","page",":page", use: history) statsRoute.get("history", use: history) + statsRoute.get("maps", use: mapRecords) + } @@ -626,6 +628,45 @@ struct StatsController: RouteCollection { // } } + + + func mapRecords(req: Request) throws -> EventLoopFuture<[MapRecord]> { + + return Match.query(on: req.db).all().map { (matches) -> [MapRecord] in + + let mapStats = self.getMapStats(matches: matches) + let sortedMaps = self.mapsSortedByBest(records: mapStats) + + let records = sortedMaps.map { (mapId) -> MapRecord in + return MapRecord(map: MapData.allMaps[mapId]!, stats: mapStats[mapId]!, ratio:mapStats[mapId]!.winLossRatio) + } + + var wins:Double = 0 + var loss:Double = 0 + + for record in records { + print("\(record.map.name) \(record.stats.record) \(record.ratio)") + wins = wins + Double(record.stats.totalWins) + loss = loss + Double(record.stats.totalLosses) + + } + let ratio = wins / loss + + return records + + } + + + } + + func mapsSortedByBest (records :[ Int:Stats] ) -> [ Int ] + { + return records.keys.sorted { (map1, map2) -> Bool in + return records[map1]?.getRatioDouble() ?? 0.0 < records[map2]?.getRatioDouble() ?? 0.0 + }.reversed() + } + + func getBestMap (records :[ Int:Stats] ) -> Int { let maps = records.keys.sorted { (map1, map2) -> Bool in diff --git a/Sources/App/Models/Map.swift b/Sources/App/Models/Map.swift index 99715b6..5a7796f 100644 --- a/Sources/App/Models/Map.swift +++ b/Sources/App/Models/Map.swift @@ -7,7 +7,7 @@ import Foundation -struct Map: Hashable { +struct Map: Hashable, Codable { var id: Int var name:String var imageName:String diff --git a/Sources/App/Models/MapRecord.swift b/Sources/App/Models/MapRecord.swift new file mode 100644 index 0000000..fbfd4cb --- /dev/null +++ b/Sources/App/Models/MapRecord.swift @@ -0,0 +1,16 @@ +// +// MapRecord.swift +// App +// +// Created by Michael Simard on 1/10/21. +// + +import Foundation +import Vapor +import Fluent + +struct MapRecord: Content { + var map:Map + var stats:Stats + var ratio:String +}