From 9374175c5070afca27278e6393fbcfe485adc72b Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Sun, 31 Oct 2021 11:05:15 -0500 Subject: [PATCH] fixed bug with recording matches on older versions, added colour scheme models --- .../Content/Config/ColorSchemeConfig.swift | 21 ++++++++ Sources/App/Content/Config/GameConfig.swift | 3 +- .../App/Controllers/AppDataController.swift | 18 +++++-- Sources/App/Models/ColourScheme.swift | 52 +++++++++++++++++++ Sources/App/Models/Game.swift | 2 +- Sources/App/Models/Match.swift | 4 +- 6 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 Sources/App/Content/Config/ColorSchemeConfig.swift create mode 100644 Sources/App/Models/ColourScheme.swift diff --git a/Sources/App/Content/Config/ColorSchemeConfig.swift b/Sources/App/Content/Config/ColorSchemeConfig.swift new file mode 100644 index 0000000..151b95d --- /dev/null +++ b/Sources/App/Content/Config/ColorSchemeConfig.swift @@ -0,0 +1,21 @@ +// +// ColorSchemeConfig.swift +// App +// +// Created by Michael Simard on 10/31/21. +// + +import Foundation + + +import Foundation +import Fluent +import Vapor + +struct ColorSchemeConfig: Content { + var gameId: Int + var tileColour: String? + var winColour: String? + var lossColour: String? + +} diff --git a/Sources/App/Content/Config/GameConfig.swift b/Sources/App/Content/Config/GameConfig.swift index 25bf2c1..baa2f79 100644 --- a/Sources/App/Content/Config/GameConfig.swift +++ b/Sources/App/Content/Config/GameConfig.swift @@ -16,7 +16,8 @@ struct GameConfig: Content { var gameId: String var name: String var maps: [MapConfig] - var enabled:Bool + var enabled:Bool + var colours:ColorSchemeConfig? } diff --git a/Sources/App/Controllers/AppDataController.swift b/Sources/App/Controllers/AppDataController.swift index 2284937..6e04ec4 100644 --- a/Sources/App/Controllers/AppDataController.swift +++ b/Sources/App/Controllers/AppDataController.swift @@ -47,18 +47,30 @@ struct AppDataController: RouteCollection { } } - return (gameModeConfigs.and(gameModeGroupConfigs).and(games).and(maps).and(playerConfigs).and(lossReasonConfigs)).map { arg -> (Configuration) in + let colourSchemeConfigs = ColourScheme.query(on: req.db).all().map { colourScheme in + return colourScheme.map { cs in + return cs.colorSchemeConfig + } + } + + + return (gameModeConfigs.and(gameModeGroupConfigs).and(games).and(maps).and(playerConfigs).and(lossReasonConfigs).and(colourSchemeConfigs)).map { arg -> (Configuration) in - let (((((( gameModeConfigs), gameModeGroupConfigs),games),maps),playerConfigs), lossReasonConfigs) = arg + let ((((((( gameModeConfigs), gameModeGroupConfigs),games),maps),playerConfigs), lossReasonConfigs),colourSchemeConfigs) = 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 GameConfig(gameId: game.gameId, name: game.name, maps: mapConfigs, enabled: game.enabled, colours: colourSchemeConfigs.first{game.gameId == "\($0.gameId)"}) }) return Configuration(games:gameConfigs, gameModes: gameModeConfigs, gameModeGroups: gameModeGroupConfigs, players: playerConfigs, lossReasons: lossReasonConfigs) } diff --git a/Sources/App/Models/ColourScheme.swift b/Sources/App/Models/ColourScheme.swift new file mode 100644 index 0000000..2a01e4f --- /dev/null +++ b/Sources/App/Models/ColourScheme.swift @@ -0,0 +1,52 @@ +// +// ColourScheme.swift +// App +// +// Created by Michael Simard on 10/31/21. +// + +import Foundation + + +import Fluent +import Vapor + +final class ColourScheme: Model, Content { + // Name of the table or collection. + static let schema = "color_scheme" + + @ID(key: .id) + var id: UUID? + + @Field(key: "game_id") + var gameId: Int + + @Field(key: "tile_colour") + var tileColour: String? + + @Field(key: "win_colour") + var winColour: String? + + @Field(key: "loss_colour") + var lossColour: String? + + + // Creates a new, empty . + init() { } + + // Creates a new with all properties set. + init(id: UUID? = nil, gameId: Int, tileColour: String?,winColour: String?,lossColour: String? ) { + self.id = id + self.gameId = gameId + self.tileColour = tileColour + self.winColour = winColour + self.lossColour = lossColour + + } + + + var colorSchemeConfig: ColorSchemeConfig { + return ColorSchemeConfig(gameId: gameId, tileColour: tileColour, winColour:winColour, lossColour:lossColour) + + } +} diff --git a/Sources/App/Models/Game.swift b/Sources/App/Models/Game.swift index 15396f3..5f2bace 100644 --- a/Sources/App/Models/Game.swift +++ b/Sources/App/Models/Game.swift @@ -43,7 +43,7 @@ final class Game: Model, Content { self.gameId = gameId self.name = name self.maps = maps - self.enabled = enabled + self.enabled = enabled } diff --git a/Sources/App/Models/Match.swift b/Sources/App/Models/Match.swift index f615248..e187999 100644 --- a/Sources/App/Models/Match.swift +++ b/Sources/App/Models/Match.swift @@ -48,13 +48,13 @@ final class Match: Model, Content { @Field(key: "lossReason") - var lossReason: Int + var lossReason: Int? init() { } - init(id: UUID? = nil, map:String?, win:Bool, date:Date, roundsWon:Int?, roundsLost:Int?, codGame:String?, notes:String?, players:String?, sweaty:Bool?, finalKillRuinedPlayerId:Int?, numberOfFinalKillsRuined:Int?,competitive:Bool, gameMode:Int, lossReason:Int) { + init(id: UUID? = nil, map:String?, win:Bool, date:Date, roundsWon:Int?, roundsLost:Int?, codGame:String?, notes:String?, players:String?, sweaty:Bool?, finalKillRuinedPlayerId:Int?, numberOfFinalKillsRuined:Int?,competitive:Bool, gameMode:Int, lossReason:Int = 0) { self.id = id self.map = map self.win = win