fixed issue with calculating dates, added api for posting a game to the server

This commit is contained in:
Michael Simard
2020-12-09 14:11:44 -06:00
parent 26fdc1e39a
commit f10a360556
4 changed files with 67 additions and 19 deletions

View File

@@ -18,26 +18,54 @@ struct StatsController: RouteCollection {
statsRoute.get("totalWins", use: totalWins) statsRoute.get("totalWins", use: totalWins)
statsRoute.get("totalLosses", use: totalLosses) statsRoute.get("totalLosses", use: totalLosses)
statsRoute.get("overall", use: overall) statsRoute.get("overall", use: overall)
statsRoute.get("all", use: test) statsRoute.get("all", use: all)
statsRoute.get("allDaily", use: allDaily) statsRoute.get("allDaily", use: allDaily)
statsRoute.get("test", use: test) 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<Match> {
let newMatch = try req.content.decode(Match.self)
return newMatch.save(on: req.db).map { newMatch}
}
func getStats(matches:[Match]) -> Stats{ 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 { if match.win == true {
winCount += 1; return [totals[0] + 1, totals[1]]
} }
if match.win == false { else {
lossCount += 1; 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)) let ratio = self.getRatio(num: Double(winCount), den: Double(lossCount))
@@ -89,7 +117,7 @@ struct StatsController: RouteCollection {
let startTime = Date() let startTime = Date()
//print ("c \(Date().timeIntervalSince(startTime))") print ("c \(Date().timeIntervalSince(startTime))")
let daysPlayed = getDaysPlayed(matches: matches) let daysPlayed = getDaysPlayed(matches: matches)
// print ("c \(Date().timeIntervalSince(startTime))") // print ("c \(Date().timeIntervalSince(startTime))")
@@ -151,7 +179,7 @@ struct StatsController: RouteCollection {
// print ("cc \(Date().timeIntervalSince(startTime))") // print ("cc \(Date().timeIntervalSince(startTime))")
// //
// } // }
// print ("c \(Date().timeIntervalSince(startTime))") print ("c \(Date().timeIntervalSince(startTime))")
return cumulativeRatios return cumulativeRatios
@@ -169,7 +197,9 @@ struct StatsController: RouteCollection {
//let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats> //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats>
previousMonths.append(CODDate(month: date.month, year: date.year, day: 15, hour:6, minute: 0)) 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)! 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)) // print (date - Date().timeIntervalSince(startTime))
@@ -259,6 +289,7 @@ struct StatsController: RouteCollection {
} }
func totalWins(req: Request) throws -> EventLoopFuture<Int> { func totalWins(req: Request) throws -> EventLoopFuture<Int> {
return Match.query(on: req.db) return Match.query(on: req.db)
.filter(\.$win == true) .filter(\.$win == true)
@@ -442,7 +473,8 @@ struct StatsController: RouteCollection {
//let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats> //let stats = getStatsByMonth(year: date.year, month: date.month, req: req) //returns eventloopfuture<Stats>
previousMonths.append(CODDate(month: date.month, year: date.year, day: 15, hour:6, minute: 0)) 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)! 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<CODDate>, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> { func getMonthStats (_ remaining: ArraySlice<CODDate>, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> {
var remaining = remaining var remaining = remaining
@@ -687,4 +719,6 @@ extension Date {
return calanderDate.minute ?? 1 return calanderDate.minute ?? 1
} }
} }

View File

@@ -18,15 +18,20 @@ import Fluent
// } // }
//} //}
struct AddCODGame: Migration { struct AddCODGame2: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> { func prepare(on database: Database) -> EventLoopFuture<Void> {
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<Void> { func revert(on database: Database) -> EventLoopFuture<Void> {
return database.schema("matches").delete() return database.schema("match23es").delete()
} }
} }

View File

@@ -23,11 +23,18 @@ final class Match: Model, Content {
var roundsLost: Int? var roundsLost: Int?
@Field(key: "codGame") @Field(key: "codGame")
var codGame: String var codGame: String?
@Field(key: "notes")
var notes: String?
@Field(key: "players")
var players: String?
init() { } 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.id = id
self.map = map self.map = map
self.win = win self.win = win
@@ -35,6 +42,8 @@ final class Match: Model, Content {
self.roundsWon = roundsWon self.roundsWon = roundsWon
self.roundsLost = roundsLost self.roundsLost = roundsLost
self.codGame = codGame; self.codGame = codGame;
self.notes = notes;
self.players = players
} }
} }

View File

@@ -20,7 +20,7 @@ public func configure(_ app: Application) throws {
), as: .psql) ), as: .psql)
//app.migrations.add(CreateMatch()) //app.migrations.add(CreateMatch())
app.migrations.add(AddCODGame()) app.migrations.add(AddCODGame2())
// register routes // register routes