First working version

This commit is contained in:
Christoph Hagen
2021-12-18 15:08:43 +01:00
parent c9853dee28
commit 49787db1aa
32 changed files with 1416 additions and 415 deletions

View File

@ -10,7 +10,7 @@ typealias TableName = String
final class TableManagement: DiskWriter {
/// All tables indexed by their id
private var tables = [TableId : Table]()
private var tables = [TableId : ManageableTable]()
/// The handle to the file where the tables are persisted
let storageFile: FileHandle
@ -48,9 +48,7 @@ final class TableManagement: DiskWriter {
}
}
entries.forEach { id, tableData in
let table = WaitingTable(id: id, name: tableData.name, isPublic: tableData.isPublic)
tableData.players.forEach { _ = table.add(player: $0) }
tables[id] = table
tables[id] = WaitingTable(id: id, name: tableData.name, isPublic: tableData.isPublic, players: tableData.players)
}
print("Loaded \(tables.count) tables")
}
@ -63,7 +61,7 @@ final class TableManagement: DiskWriter {
- Returns: `true`, if the entry was written, `false` on error
*/
@discardableResult
private func writeTableToDisk(table: Table) -> Bool {
private func writeTableToDisk(table: ManageableTable) -> Bool {
let visible = table.isPublic ? "public" : "private"
let players = table.playerNames
.joined(separator: ",")
@ -94,8 +92,7 @@ final class TableManagement: DiskWriter {
- Returns: The table id
*/
func createTable(named name: TableName, player: PlayerName, isPublic: Bool) -> TableInfo {
let table = WaitingTable(newTable: name, isPublic: isPublic)
_ = table.add(player: player)
let table = WaitingTable(newTable: name, isPublic: isPublic, creator: player)
tables[table.id] = table
writeTableToDisk(table: table)
return table.tableInfo(forPlayer: player)
@ -115,8 +112,8 @@ final class TableManagement: DiskWriter {
currentTable(for: player)?.tableInfo(forPlayer: player)
}
private func currentTable(for player: PlayerName) -> Table? {
tables.values.first(where: { $0.contains(player: player) })
private func currentTable(for player: PlayerName) -> ManageableTable? {
tables.values.first(where: { $0.playerNames.contains(player) })
}
/**
@ -154,6 +151,7 @@ final class TableManagement: DiskWriter {
guard let oldTable = currentTable(for: player) else {
return
}
/// `player.canStartGame` is automatically set to false, because table is not full
let table = WaitingTable(oldTable: oldTable, removing: player)
tables[table.id] = table
table.sendUpdateToAllPlayers()