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,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<Match> {
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<Stats>
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<Int> {
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<Stats>
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<CODDate>, allMonthlyStats: inout [MonthStats], eventLoop: EventLoop) -> EventLoopFuture<[MonthStats]> {
var remaining = remaining
@@ -687,4 +719,6 @@ extension Date {
return calanderDate.minute ?? 1
}
}