From 707c6d03de83bdcf2bb3ee19807455b081d29939 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sun, 26 Dec 2021 16:38:16 +0100 Subject: [PATCH 1/2] Clean up code Update WeddingPlayer.swift --- Public/elements.js | 10 +--------- Sources/App/Infos/PlayerInfo.swift | 3 --- Sources/App/Model/Players/WeddingPlayer.swift | 6 ------ 3 files changed, 1 insertion(+), 18 deletions(-) diff --git a/Public/elements.js b/Public/elements.js index 8c0dbe6..8cb7267 100644 --- a/Public/elements.js +++ b/Public/elements.js @@ -293,21 +293,13 @@ function setInfoForPlayer(player, position, game) { const layer = player.position setTableCard(position, card, layer) setTablePlayerName(position, player.name, player.active) - // if (!player.connected) { - // showPlayerDisconnected(position) - // return - // } + var state = player.state if (game != null && state.indexOf("selects") > -1) { const i = state.indexOf("selects") state[i] = game } - // const double = doubleText(player.doubles) - // if (double) { - // state.push(double) - // } - const text = state.map(x => convertStateToString(x)).join("
") showPlayerState(position, text) } diff --git a/Sources/App/Infos/PlayerInfo.swift b/Sources/App/Infos/PlayerInfo.swift index e9fc826..8cdfcf3 100644 --- a/Sources/App/Infos/PlayerInfo.swift +++ b/Sources/App/Infos/PlayerInfo.swift @@ -22,8 +22,6 @@ struct PlayerInfo: Codable, Equatable { /// The number of times the player doubled the game cost (initial double and raises) var numberOfDoubles = 0 - var leadsGame = false - var state: [PlayerStateId] = [] init(name: PlayerName) { @@ -39,7 +37,6 @@ struct PlayerInfo: Codable, Equatable { case playedCard = "card" case positionInTrick = "position" case numberOfDoubles = "doubles" - case leadsGame = "leads" case state = "state" } } diff --git a/Sources/App/Model/Players/WeddingPlayer.swift b/Sources/App/Model/Players/WeddingPlayer.swift index 535576a..c626d5a 100644 --- a/Sources/App/Model/Players/WeddingPlayer.swift +++ b/Sources/App/Model/Players/WeddingPlayer.swift @@ -84,10 +84,4 @@ final class WeddingPlayer: CardHoldingPlayer { func replace(_ card: Card, with trumpCard: Card) { cards = (cards.filter { $0 != card } + [trumpCard]).sortedCards(forGame: .hochzeit) } - - override var info: PlayerInfo { - var info = super.info - info.leadsGame = offersWedding || selectsGame - return info - } } From 89007989e3b847cb8361998680e8bc7daa1a4b5e Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 24 Jan 2022 17:15:11 +0100 Subject: [PATCH 2/2] Close all sockets on shutdown --- Sources/App/Management/SQLiteDatabase.swift | 4 ++++ Sources/App/Management/TableManagement.swift | 4 ++++ Sources/App/Model/Tables/AbstractTable.swift | 4 ++++ Sources/App/Model/Tables/ManageableTable.swift | 2 ++ Sources/App/configure.swift | 7 +++++++ 5 files changed, 21 insertions(+) diff --git a/Sources/App/Management/SQLiteDatabase.swift b/Sources/App/Management/SQLiteDatabase.swift index b7e0c7f..293f948 100644 --- a/Sources/App/Management/SQLiteDatabase.swift +++ b/Sources/App/Management/SQLiteDatabase.swift @@ -174,4 +174,8 @@ final class SQLiteDatabase { } return tables.play(card: card, player: player) } + + func disconnectAllSockets() { + tables.disconnectAllSockets() + } } diff --git a/Sources/App/Management/TableManagement.swift b/Sources/App/Management/TableManagement.swift index aefea8e..586c380 100644 --- a/Sources/App/Management/TableManagement.swift +++ b/Sources/App/Management/TableManagement.swift @@ -199,4 +199,8 @@ final class TableManagement { // TODO: Save new table return .success } + + func disconnectAllSockets() { + tables.values.forEach { $0.disconnectAllPlayers() } + } } diff --git a/Sources/App/Model/Tables/AbstractTable.swift b/Sources/App/Model/Tables/AbstractTable.swift index fe3ce77..4e0dd28 100644 --- a/Sources/App/Model/Tables/AbstractTable.swift +++ b/Sources/App/Model/Tables/AbstractTable.swift @@ -123,6 +123,10 @@ extension AbstractTable: ManageableTable { sendUpdateToAllPlayers() return } + + func disconnectAllPlayers() { + players.forEach { $0.disconnect() } + } func sendUpdateToAllPlayers() { players.enumerated().forEach { playerIndex, player in diff --git a/Sources/App/Model/Tables/ManageableTable.swift b/Sources/App/Model/Tables/ManageableTable.swift index f1bb1e1..a2950dc 100644 --- a/Sources/App/Model/Tables/ManageableTable.swift +++ b/Sources/App/Model/Tables/ManageableTable.swift @@ -31,4 +31,6 @@ protocol ManageableTable { func disconnect(player name: PlayerName) func sendUpdateToAllPlayers() + + func disconnectAllPlayers() } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 47656d6..c8de8af 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -32,6 +32,13 @@ public func configure(_ app: Application) throws { let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())! server = try SQLiteDatabase(db: db) + + // Gracefully shut down by closing potentially open socket + DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) { + _ = app.server.onShutdown.always { _ in + server.disconnectAllSockets() + } + } // register routes try routes(app)