From f10a360556f6d10b22eb3ca8bce4ac893ac0361f Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Wed, 9 Dec 2020 14:11:44 -0600 Subject: [PATCH] fixed issue with calculating dates, added api for posting a game to the server --- Sources/App/Controllers/StatsController.swift | 58 +++++++++++++++---- .../{AddCODGame.swift => AddCODGame2.swift} | 13 +++-- Sources/App/Models/Match.swift | 13 ++++- Sources/App/configure.swift | 2 +- 4 files changed, 67 insertions(+), 19 deletions(-) rename Sources/App/Migrations/{AddCODGame.swift => AddCODGame2.swift} (73%) diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index 9e64b7c..96a0a91 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -18,28 +18,56 @@ struct StatsController: RouteCollection { statsRoute.get("totalWins", use: totalWins) statsRoute.get("totalLosses", use: totalLosses) statsRoute.get("overall", use: overall) - statsRoute.get("all", use: test) + statsRoute.get("all", use: all) statsRoute.get("allDaily", use: allDaily) statsRoute.get("test", use: test) + statsRoute.post("logMatch", use: logMatch) + } + struct CreateMatchRequestBody: Content { + let match: Match + + func makeMatch(match:Match) -> Match { + return Match(id: match.id, map: match.map, win: match.win, date: match.date, roundsWon: match.roundsWon, roundsLost: match.roundsLost, codGame: match.codGame, notes: match.notes, players: match.players) + } + + } + func logMatch(req: Request) throws -> EventLoopFuture { + + let newMatch = try req.content.decode(Match.self) + + return newMatch.save(on: req.db).map { newMatch} + } + func getStats(matches:[Match]) -> Stats{ - var winCount = 0 - var lossCount = 0 - - for match in matches { + + let totals = matches.reduce([0,0]) { (totals, match) -> [Int] in if match.win == true { - winCount += 1; + return [totals[0] + 1, totals[1]] } - if match.win == false { - lossCount += 1; + else { + return [totals[0] + 1, totals[1] + 1] } } +// for match in matches { +// if match.win == true { +// winCount += 1; +// +// } +// if match.win == false { +// lossCount += 1; +// } +// } + let winCount = totals[0] + let lossCount = totals[1] + + let ratio = self.getRatio(num: Double(winCount), den: Double(lossCount)) return Stats(winLoss: ratio, totalWins: Int(winCount), totalLosses: Int(lossCount)) @@ -89,7 +117,7 @@ struct StatsController: RouteCollection { let startTime = Date() -//print ("c \(Date().timeIntervalSince(startTime))") +print ("c \(Date().timeIntervalSince(startTime))") let daysPlayed = getDaysPlayed(matches: matches) // print ("c \(Date().timeIntervalSince(startTime))") @@ -151,7 +179,7 @@ struct StatsController: RouteCollection { // print ("cc \(Date().timeIntervalSince(startTime))") // // } -// print ("c \(Date().timeIntervalSince(startTime))") + print ("c \(Date().timeIntervalSince(startTime))") return cumulativeRatios @@ -169,7 +197,9 @@ struct StatsController: RouteCollection { //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture previousMonths.append(CODDate(month: date.month, year: date.year, day: 15, hour:6, minute: 0)) date = Calendar.current.date(byAdding: .month, value: 1, to: date)! - } while (date.month != (Date().month + 1) || date.year != Date().year) + print (date.month) + print ((Date().month % 12) + 1) + } while ( date < Calendar.current.date(byAdding: .month, value: 1, to: Date())!) // print (date - Date().timeIntervalSince(startTime)) @@ -258,6 +288,7 @@ struct StatsController: RouteCollection { return Match.query(on: req.db).sort(\.$date).all() } + func totalWins(req: Request) throws -> EventLoopFuture { return Match.query(on: req.db) @@ -442,7 +473,8 @@ struct StatsController: RouteCollection { //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture previousMonths.append(CODDate(month: date.month, year: date.year, day: 15, hour:6, minute: 0)) date = Calendar.current.date(byAdding: .month, value: 1, to: date)! - } while (date.month != (Date().month + 1) || date.year != Date().year) + }while ( date < Calendar.current.date(byAdding: .month, value: 1, to: Date())!) + func getMonthStats (_ remaining: ArraySlice, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> { var remaining = remaining @@ -687,4 +719,6 @@ extension Date { return calanderDate.minute ?? 1 } + + } diff --git a/Sources/App/Migrations/AddCODGame.swift b/Sources/App/Migrations/AddCODGame2.swift similarity index 73% rename from Sources/App/Migrations/AddCODGame.swift rename to Sources/App/Migrations/AddCODGame2.swift index cf9f8f8..9e94c30 100644 --- a/Sources/App/Migrations/AddCODGame.swift +++ b/Sources/App/Migrations/AddCODGame2.swift @@ -18,15 +18,20 @@ import Fluent // } //} -struct AddCODGame: Migration { +struct AddCODGame2: Migration { func prepare(on database: Database) -> EventLoopFuture { - return database.schema("match").field("codGame",.string).create() - } + return database.schema("match") + .field("codGame",.string) + .field("notes",.string) + .field("players",.string) + .update() + + } func revert(on database: Database) -> EventLoopFuture { - return database.schema("matches").delete() + return database.schema("match23es").delete() } } diff --git a/Sources/App/Models/Match.swift b/Sources/App/Models/Match.swift index 6b0d482..28b7334 100644 --- a/Sources/App/Models/Match.swift +++ b/Sources/App/Models/Match.swift @@ -23,11 +23,18 @@ final class Match: Model, Content { var roundsLost: Int? @Field(key: "codGame") - var codGame: String + var codGame: String? + + @Field(key: "notes") + var notes: String? + + @Field(key: "players") + var players: String? + init() { } - init(id: UUID? = nil, map:String?, win:Bool, date:Date, roundsWon:Int?, roundsLost:Int?, codGame:String) { + init(id: UUID? = nil, map:String?, win:Bool, date:Date, roundsWon:Int?, roundsLost:Int?, codGame:String?, notes:String?, players:String?) { self.id = id self.map = map self.win = win @@ -35,6 +42,8 @@ final class Match: Model, Content { self.roundsWon = roundsWon self.roundsLost = roundsLost self.codGame = codGame; + self.notes = notes; + self.players = players } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 7d8d250..6bb68ce 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -20,7 +20,7 @@ public func configure(_ app: Application) throws { ), as: .psql) //app.migrations.add(CreateMatch()) - app.migrations.add(AddCODGame()) + app.migrations.add(AddCODGame2()) // register routes