Refactor tables and players for clarity
This commit is contained in:
@ -99,7 +99,7 @@ final class Database {
|
||||
return true
|
||||
}
|
||||
|
||||
func performAction(playerToken: SessionToken, action: Player.Action) -> PlayerActionResult {
|
||||
func performAction(playerToken: SessionToken, action: PlayerAction) -> PlayerActionResult {
|
||||
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
|
||||
return .invalidToken
|
||||
}
|
||||
@ -113,7 +113,7 @@ final class Database {
|
||||
return tables.select(game: game, player: player)
|
||||
}
|
||||
|
||||
func play(card: Card, playerToken: SessionToken) -> PlayCardResult {
|
||||
func play(card: Card, playerToken: SessionToken) -> PlayerActionResult {
|
||||
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
|
||||
return .invalidToken
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ final class TableManagement: DiskWriter {
|
||||
}
|
||||
}
|
||||
entries.forEach { id, tableData in
|
||||
let table = Table(id: id, name: tableData.name, isPublic: tableData.isPublic)
|
||||
let table = WaitingTable(id: id, name: tableData.name, isPublic: tableData.isPublic)
|
||||
tableData.players.forEach { _ = table.add(player: $0) }
|
||||
tables[id] = table
|
||||
}
|
||||
@ -65,8 +65,10 @@ final class TableManagement: DiskWriter {
|
||||
@discardableResult
|
||||
private func writeTableToDisk(table: Table) -> Bool {
|
||||
let visible = table.isPublic ? "public" : "private"
|
||||
let players = table.playerNames.joined(separator: ",")
|
||||
let entry = [table.id, table.name, visible, players].joined(separator: ":")
|
||||
let players = table.playerNames
|
||||
.joined(separator: ",")
|
||||
let entry = [table.id, table.name, visible, players]
|
||||
.joined(separator: ":")
|
||||
return writeToDisk(line: entry)
|
||||
}
|
||||
|
||||
@ -79,7 +81,8 @@ final class TableManagement: DiskWriter {
|
||||
*/
|
||||
@discardableResult
|
||||
private func writeTableDeletionEntry(tableId: TableId) -> Bool {
|
||||
let entry = [tableId, "", "", ""].joined(separator: ":")
|
||||
let entry = [tableId, "", "", ""]
|
||||
.joined(separator: ":")
|
||||
return writeToDisk(line: entry)
|
||||
}
|
||||
|
||||
@ -91,27 +94,27 @@ final class TableManagement: DiskWriter {
|
||||
- Returns: The table id
|
||||
*/
|
||||
func createTable(named name: TableName, player: PlayerName, isPublic: Bool) -> TableInfo {
|
||||
let table = Table(newTable: name, isPublic: isPublic)
|
||||
let table = WaitingTable(newTable: name, isPublic: isPublic)
|
||||
_ = table.add(player: player)
|
||||
tables[table.id] = table
|
||||
writeTableToDisk(table: table)
|
||||
return table.compileInfo(for: player)!
|
||||
return table.tableInfo(forPlayer: player)
|
||||
}
|
||||
|
||||
/// A list of all public tables
|
||||
var publicTableList: [PublicTableInfo] {
|
||||
tables.values.filter { $0.isPublic }.map { $0.publicInfo }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Get the table info for a player
|
||||
- Parameter player: The name of the player
|
||||
- Returns: The table info, if the player has joined a table
|
||||
*/
|
||||
func tableInfo(player: PlayerName) -> TableInfo? {
|
||||
currentTable(for: player)?.compileInfo(for: player)
|
||||
currentTable(for: player)?.tableInfo(forPlayer: player)
|
||||
}
|
||||
|
||||
|
||||
private func currentTable(for player: PlayerName) -> Table? {
|
||||
tables.values.first(where: { $0.contains(player: player) })
|
||||
}
|
||||
@ -127,16 +130,20 @@ final class TableManagement: DiskWriter {
|
||||
guard existing.id == tableId else {
|
||||
return .failure(.alreadyJoinedOtherTable)
|
||||
}
|
||||
return .success(existing.compileInfo(for: player)!)
|
||||
return .success(existing.tableInfo(forPlayer: player))
|
||||
}
|
||||
guard let table = tables[tableId] else {
|
||||
return .failure(.tableNotFound)
|
||||
}
|
||||
guard table.add(player: player) else {
|
||||
guard let joinableTable = table as? WaitingTable else {
|
||||
return .failure(.tableIsFull)
|
||||
}
|
||||
guard joinableTable.add(player: player) else {
|
||||
return .failure(.tableIsFull)
|
||||
}
|
||||
writeTableToDisk(table: table)
|
||||
return .success(table.compileInfo(for: player)!)
|
||||
joinableTable.sendUpdateToAllPlayers()
|
||||
return .success(joinableTable.tableInfo(forPlayer: player))
|
||||
}
|
||||
|
||||
/**
|
||||
@ -144,10 +151,12 @@ final class TableManagement: DiskWriter {
|
||||
- Parameter player: The name of the player
|
||||
*/
|
||||
func leaveTable(player: PlayerName) {
|
||||
guard let table = currentTable(for: player) else {
|
||||
guard let oldTable = currentTable(for: player) else {
|
||||
return
|
||||
}
|
||||
table.remove(player: player)
|
||||
let table = WaitingTable(oldTable: oldTable, removing: player)
|
||||
tables[table.id] = table
|
||||
table.sendUpdateToAllPlayers()
|
||||
writeTableToDisk(table: table)
|
||||
}
|
||||
|
||||
@ -165,26 +174,60 @@ final class TableManagement: DiskWriter {
|
||||
table.disconnect(player: player)
|
||||
}
|
||||
|
||||
func performAction(player: PlayerName, action: Player.Action) -> PlayerActionResult {
|
||||
func performAction(player: PlayerName, action: PlayerAction) -> PlayerActionResult {
|
||||
guard let table = currentTable(for: player) else {
|
||||
print("Player \(player) wants to \(action.path), but no table joined")
|
||||
return .noTableJoined
|
||||
}
|
||||
return table.perform(action: action, forPlayer: player)
|
||||
let (result, newTable) = table.perform(action: action, forPlayer: player)
|
||||
guard result == .success else {
|
||||
return result
|
||||
}
|
||||
guard let newTable = newTable else {
|
||||
table.sendUpdateToAllPlayers()
|
||||
return .success
|
||||
}
|
||||
tables[newTable.id] = newTable
|
||||
newTable.sendUpdateToAllPlayers()
|
||||
return .success
|
||||
}
|
||||
|
||||
func select(game: GameType, player: PlayerName) -> PlayerActionResult {
|
||||
guard let table = currentTable(for: player) else {
|
||||
guard let aTable = currentTable(for: player) else {
|
||||
print("Player \(player) wants to play \(game.rawValue), but no table joined")
|
||||
return .noTableJoined
|
||||
}
|
||||
return table.select(game: game, player: player)
|
||||
guard let table = aTable as? BiddingTable else {
|
||||
return .tableStateInvalid
|
||||
}
|
||||
let (result, newTable) = table.select(game: game, player: player)
|
||||
guard result == .success else {
|
||||
return result
|
||||
}
|
||||
guard let newTable = newTable else {
|
||||
print("Game selected by \(player), but no playing table \(table.name) created")
|
||||
table.sendUpdateToAllPlayers()
|
||||
return result
|
||||
}
|
||||
tables[newTable.id] = newTable
|
||||
newTable.sendUpdateToAllPlayers()
|
||||
return .success
|
||||
}
|
||||
|
||||
func play(card: Card, player: PlayerName) -> PlayCardResult {
|
||||
func play(card: Card, player: PlayerName) -> PlayerActionResult {
|
||||
guard let table = currentTable(for: player) else {
|
||||
return .noTableJoined
|
||||
}
|
||||
return table.play(card: card, player: player)
|
||||
let (result, newTable) = table.play(card: card, player: player)
|
||||
guard result == .success else {
|
||||
return result
|
||||
}
|
||||
guard let newTable = newTable else {
|
||||
table.sendUpdateToAllPlayers()
|
||||
return .success
|
||||
}
|
||||
tables[newTable.id] = newTable
|
||||
newTable.sendUpdateToAllPlayers()
|
||||
return .success
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user