Delete table when empty

This commit is contained in:
Christoph Hagen 2021-12-23 11:16:29 +01:00
parent 15d2b3d670
commit f97604e3bf
2 changed files with 12 additions and 4 deletions

View File

@ -110,11 +110,16 @@ final class TableManagement {
guard let oldTable = currentTable(for: player.name) else { guard let oldTable = currentTable(for: player.name) else {
return database.eventLoop.makeSucceededVoidFuture() return database.eventLoop.makeSucceededVoidFuture()
} }
player.$table.id = nil
guard let table = WaitingTable(oldTable: oldTable, removing: player.name) else {
tables[oldTable.id] = nil
return Table.query(on: database).filter(\.$id == oldTable.id).delete().flatMap {
player.update(on: database)
}
}
/// `player.canStartGame` is automatically set to false, because table is not full /// `player.canStartGame` is automatically set to false, because table is not full
let table = WaitingTable(oldTable: oldTable, removing: player.name)
tables[table.id] = table tables[table.id] = table
table.sendUpdateToAllPlayers() table.sendUpdateToAllPlayers()
player.$table.id = nil
// TODO: Update points for all players // TODO: Update points for all players
return player.update(on: database) return player.update(on: database)
} }

View File

@ -39,7 +39,7 @@ final class WaitingTable: AbstractTable<WaitingPlayer> {
- Parameter oldTable: The table to convert - Parameter oldTable: The table to convert
- Parameter player: The name of the player to remove from the table. - Parameter player: The name of the player to remove from the table.
*/ */
init(oldTable: ManageableTable, removing player: PlayerName) { init?(oldTable: ManageableTable, removing player: PlayerName) {
// TODO: End game and distribute points // TODO: End game and distribute points
let players = oldTable.allPlayers let players = oldTable.allPlayers
.filter { .filter {
@ -50,6 +50,9 @@ final class WaitingTable: AbstractTable<WaitingPlayer> {
return false return false
} }
.map { WaitingPlayer(name: $0.name, points: $0.totalPoints, socket: $0.socket) } .map { WaitingPlayer(name: $0.name, points: $0.totalPoints, socket: $0.socket) }
guard !players.isEmpty else {
return nil
}
players.first!.isNextActor = true players.first!.isNextActor = true
super.init(table: oldTable, players: players) super.init(table: oldTable, players: players)
} }
@ -58,7 +61,7 @@ final class WaitingTable: AbstractTable<WaitingPlayer> {
Convert another table to a waiting table. Convert another table to a waiting table.
This is needed when a player leaves an active table. This is needed when a player leaves an active table.
- Parameter oldTable: The table to convert - Parameter table: The table to convert
*/ */
init(oldTableAdvancedByOne table: ManageableTable) { init(oldTableAdvancedByOne table: ManageableTable) {
let players = table.allPlayers let players = table.allPlayers