map, player, game, gamemode data now database driven

This commit is contained in:
Michael Simard
2021-10-31 00:01:27 -05:00
parent b0b0b7bc60
commit 64a66e28b8
22 changed files with 711 additions and 143 deletions

View File

@@ -0,0 +1,66 @@
//
// AppDataController.swift
// App
//
// Created by Michael Simard on 10/29/21.
//
//
import Fluent
import Vapor
import Foundation
struct AppDataController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
let matchRoute = routes.grouped("cod-tracker","api")
matchRoute.get("config", use: config)
}
func config(req:Request) -> EventLoopFuture<Configuration>{
let gameModeConfigs = GameMode.query(on: req.db).all().map { gameMode in
return gameMode.map { gm in
return gm.gameModeConfig
}
}
let gameModeGroupConfigs = GameModeGroup.query(on: req.db).all().map { gameModeGroup in
return gameModeGroup.map { gmg in
return gmg.gameModeGroupConfig
}
}
let games = Game.query(on: req.db).all()
let maps = Map.query(on: req.db).all()
let playerConfigs = Player.query(on: req.db).all().map { player in
return player.map { p in
return p.playerConfig
}
}
let lossReasonConfigs = LossReason.query(on: req.db).all().map { lossReason in
return lossReason.map { lr in
return lr.lossReasonConfig
}
}
return (gameModeConfigs.and(gameModeGroupConfigs).and(games).and(maps).and(playerConfigs).and(lossReasonConfigs)).map { arg -> (Configuration) in
let (((((( gameModeConfigs), gameModeGroupConfigs),games),maps),playerConfigs), lossReasonConfigs) = arg
var gameConfigs:[GameConfig] = []
gameConfigs = games.map({ game in
let mapConfigs = game.mapIds.compactMap { mapId in
return maps.first { m in
m.mapId == mapId
}?.mapConfig
}
return GameConfig(gameId: game.gameId, name: game.name, maps: mapConfigs, enabled: game.enabled)
})
return Configuration(games:gameConfigs, gameModes: gameModeConfigs, gameModeGroups: gameModeGroupConfigs, players: playerConfigs, lossReasons: lossReasonConfigs)
}
}
}

View File

@@ -0,0 +1,35 @@
//
// MapsController.swift
// App
//
// Created by Michael Simard on 10/29/21.
//
import Fluent
import Vapor
import Foundation
struct MapsController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
let matchRoute = routes.grouped("cod-tracker","api", "maps")
matchRoute.get("all", use: maps)
matchRoute.get("game-modes", use: gameModes)
matchRoute.get("game-mode-groups", use: gameModeGroups)
}
func maps(req:Request) -> EventLoopFuture<[Map]>{
return Map.query(on: req.db).all()
}
func gameModes(req:Request) -> EventLoopFuture<[GameMode]>{
return GameMode.query(on: req.db).all()
}
func gameModeGroups(req:Request) -> EventLoopFuture<[GameModeGroup]>{
return GameModeGroup.query(on: req.db).all()
}
}

View File

@@ -28,7 +28,6 @@ struct StatsController: RouteCollection {
statsRoute.get("maps","game",":game","competitive",":competitive", "gamemode", ":gamemode",use: mapRecords)
statsRoute.get("dashboard", use: dashboard)
statsRoute.get("recalculate", use: recalc)
statsRoute.get("stats","q",":query", use: history)
}
@@ -236,11 +235,7 @@ struct StatsController: RouteCollection {
}
func getStatsWithMostRecentDailyRecord(sortedMatches:[Match], game:String? = nil) -> StatsWithMostRecentDailyRecord {
let startTime = Date()
//print ("MRR START \(Date().timeIntervalSince(startTime))")
let stats = getCountedMatches(matches: sortedMatches)
//print ("MRR STATS \(Date().timeIntervalSince(startTime))")
@@ -256,9 +251,7 @@ struct StatsController: RouteCollection {
func mostRecentDailyStats (matches:[Match], game:String? = nil) -> Stats{
let startTime = Date()
let daysPlayed = getDaysPlayed(sortedMatches: matches)
let lastDayPlayed = daysPlayed.last
@@ -359,10 +352,7 @@ struct StatsController: RouteCollection {
private func getDaysPlayed(sortedMatches:[Match]) -> [CODDate] {
let startTime = Date()
//print ("MDP Sort \(Date().timeIntervalSince(startTime))")
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)
}
@@ -618,11 +608,7 @@ struct StatsController: RouteCollection {
return self.getStatsForDay(year: days.first?.year ?? 0, month: days.first?.month ?? 0, day: days.first?.day ?? 0, req: req)
}
}
// func cachedStats(db: Database) -> EventLoopFuture<[String:Stats]> {
//
// }
func recalc(req:Request) -> EventLoopFuture<[String:Stats]> {
return forceCalculatedStats(db: req.db)
}
@@ -754,9 +740,12 @@ struct StatsController: RouteCollection {
let matches = Match.query(on: db).sort( \.$date).all()
return matches.and(dashboardStats).map { arg -> (OverallStats) in
let mapConfigs = mapConfigs(db: db)
return matches.and(dashboardStats).and(mapConfigs).map { arg -> (OverallStats) in
let (matches, dashboardStats) = arg
let ((matches, dashboardStats),mapConfigs) = arg
let queue = DispatchQueue(label: "com.sledsoft.cod-tracker.queue", attributes: .concurrent)
@@ -797,8 +786,8 @@ struct StatsController: RouteCollection {
(dashboardStats.dashboardItems +
[
DashboardItem(codTrackerId:"best_map_overall", title: "Best Map", content: MapData.allMaps[bestMap!]?.name ?? "error", title2: "Ratio", content2: "\(mapStats![bestMap!]!.winLossRatio) \(mapStats![bestMap!]!.record)", sortOrder: 12),
DashboardItem(codTrackerId:"worst_map_overall", title: "Worst Map", content: MapData.allMaps[worstMap!]?.name ?? "error", title2: "Ratio", content2: "\(mapStats![worstMap!]!.winLossRatio) \(mapStats![worstMap!]!.record)",sortOrder: 13),
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),
]).sorted{
$0.sortOrder < $1.sortOrder
@@ -815,11 +804,25 @@ struct StatsController: RouteCollection {
}
func mapConfigs(db: Database) -> EventLoopFuture<[MapConfig]> {
return Map.query(on: db).all().map { map in
return map.map { m in
return m.mapConfig
}
}
}
func mapRecords(req: Request) throws -> EventLoopFuture<[MapRecord]> {
return Match.query(on: req.db).all().map { (matches) -> [MapRecord] in
let matches = Match.query(on: req.db).all()
let mapConfigs = mapConfigs(db: req.db)
return matches.and(mapConfigs).map { matches, mapConfigs in
let mapStats:[Int:Stats]
if let game = req.parameters.get("game", as: String.self),
@@ -833,10 +836,11 @@ struct StatsController: RouteCollection {
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
return MapRecord(map: MapData.allMaps[mapId]!, stats: mapStats[mapId]!, ratio:mapStats[mapId]!.winLossRatio)
return MapRecord(map: mapConfigs.first{$0.mapId == mapId}!, stats: mapStats[mapId]!, ratio:mapStats[mapId]!.winLossRatio)
}
var wins:Double = 0
@@ -849,7 +853,48 @@ struct StatsController: RouteCollection {
}
return records
}
//
// return Match.query(on: req.db).all().map { (matches) -> [MapRecord] in
//
//
//
// let mapStats:[Int:Stats]
//
// if let game = req.parameters.get("game", as: String.self),
// let competitive = req.parameters.get("competitive", as:Bool.self) {
//
// let gameMode = req.parameters.get("gamemode", as:Int.self) ?? -2
// mapStats = self.getMapStats(matches: matches,game: game, competitive: competitive, gameMode: gameMode)
//
// }
// else {
// mapStats = self.getMapStats(matches: matches,game: "mw", competitive: true)
// }
//
//
// 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)
//
// }
// return records
// }
}
func mapsSortedByBest (records :[ Int:Stats] ) -> [ Int ] {