Schafkopf-Server/Sources/App/Management/TableManagement.swift

191 lines
6.6 KiB
Swift
Raw Normal View History

2021-12-01 22:47:19 +01:00
import Foundation
import WebSocketKit
import Vapor
let maximumPlayersPerTable = 4
typealias TableId = String
typealias TableName = String
final class TableManagement: DiskWriter {
2021-12-03 18:03:29 +01:00
/// All tables indexed by their id
private var tables = [TableId : Table]()
2021-12-01 22:47:19 +01:00
2021-12-03 18:03:29 +01:00
/// The handle to the file where the tables are persisted
2021-12-01 22:47:19 +01:00
let storageFile: FileHandle
2021-12-03 18:03:29 +01:00
/// The url to the file where the tables are persisted
2021-12-01 22:47:19 +01:00
let storageFileUrl: URL
2021-12-03 18:03:29 +01:00
/**
Load the tables from a file in the storage folder
- Parameter storageFolder: The url to the folder where the table file is stored
- Throws: Errors when the file could not be read
*/
2021-12-01 22:47:19 +01:00
init(storageFolder: URL) throws {
let url = storageFolder.appendingPathComponent("tables.txt")
storageFileUrl = url
storageFile = try Self.prepareFile(at: url)
2021-12-03 18:03:29 +01:00
var entries = [TableId : (name: TableName, isPublic: Bool, players: [PlayerName])]()
2021-12-01 22:47:19 +01:00
try readLinesFromDisk().forEach { line in
// Each line has parts: ID | NAME | PLAYER, PLAYER, ...
let parts = line.components(separatedBy: ":")
guard parts.count == 4 else {
print("Invalid line in table file")
return
}
let id = parts[0]
let name = parts[1]
let isPublic = parts[2] == "public"
let players = parts[3].components(separatedBy: ",")
if name == "" {
entries[id] = nil
} else {
entries[id] = (name, isPublic, players)
}
}
2021-12-03 18:03:29 +01:00
entries.forEach { id, tableData in
let table = Table(id: id, name: tableData.name, isPublic: tableData.isPublic)
tableData.players.forEach { _ = table.add(player: $0) }
tables[id] = table
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
print("Loaded \(tables.count) tables")
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
/**
Writes the table info to disk.
Currently only the id, name, visibility and players are stored, all other information is lost.
- Parameter table: The changed table information to persist
- Returns: `true`, if the entry was written, `false` on error
*/
2021-12-01 22:47:19 +01:00
@discardableResult
2021-12-03 18:03:29 +01:00
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: ":")
2021-12-01 22:47:19 +01:00
return writeToDisk(line: entry)
}
2021-12-03 18:03:29 +01:00
/**
Writes the deletion of a table to disk.
The deletion is written as a separate entry and appended to the file, in order to reduce disk I/O.
- Parameter tableId: The id of the deleted table
- Returns: `true`, if the entry was written, `false` on error
*/
2021-12-01 22:47:19 +01:00
@discardableResult
2021-12-03 18:03:29 +01:00
private func writeTableDeletionEntry(tableId: TableId) -> Bool {
2021-12-01 22:47:19 +01:00
let entry = [tableId, "", "", ""].joined(separator: ":")
return writeToDisk(line: entry)
}
/**
Create a new table with optional players.
- Parameter name: The name of the table
- Parameter players: The player creating the table
2021-12-03 18:03:29 +01:00
- Parameter isPublic: Indicates that this is a game joinable by everyone
2021-12-01 22:47:19 +01:00
- Returns: The table id
*/
2021-12-06 11:43:30 +01:00
func createTable(named name: TableName, player: PlayerName, isPublic: Bool) -> TableInfo {
2021-12-03 18:03:29 +01:00
let table = Table(newTable: name, isPublic: isPublic)
2021-12-06 11:43:30 +01:00
_ = table.add(player: player)
2021-12-03 18:03:29 +01:00
tables[table.id] = table
writeTableToDisk(table: table)
2021-12-06 11:43:30 +01:00
return table.compileInfo(for: player)!
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
/// A list of all public tables
var publicTableList: [PublicTableInfo] {
tables.values.filter { $0.isPublic }.map { $0.publicInfo }
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
/**
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)
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
private func currentTable(for player: PlayerName) -> Table? {
tables.values.first(where: { $0.contains(player: player) })
2021-12-01 22:47:19 +01:00
}
/**
Join a table.
2021-12-03 18:03:29 +01:00
- Parameter tableId: The table to join
- Parameter player: The name of the player who wants to join.
2021-12-01 22:47:19 +01:00
- Returns: The result of the join operation
*/
2021-12-03 18:03:29 +01:00
func join(tableId: TableId, player: PlayerName) -> Result<TableInfo, JoinTableResult> {
if let existing = currentTable(for: player) {
guard existing.id == tableId else {
return .failure(.alreadyJoinedOtherTable)
}
return .success(existing.compileInfo(for: player)!)
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
guard let table = tables[tableId] else {
return .failure(.tableNotFound)
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
guard table.add(player: player) else {
return .failure(.tableIsFull)
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
writeTableToDisk(table: table)
return .success(table.compileInfo(for: player)!)
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
/**
A player leaves the table it previously joined
- Parameter player: The name of the player
*/
func leaveTable(player: PlayerName) {
guard let table = currentTable(for: player) else {
2021-12-01 22:47:19 +01:00
return
}
2021-12-03 18:03:29 +01:00
table.remove(player: player)
writeTableToDisk(table: table)
2021-12-01 22:47:19 +01:00
}
func connect(player: PlayerName, using socket: WebSocket) -> Bool {
2021-12-03 18:03:29 +01:00
guard let table = currentTable(for: player) else {
2021-12-01 22:47:19 +01:00
return false
}
2021-12-03 18:03:29 +01:00
return table.connect(player: player, using: socket)
2021-12-01 22:47:19 +01:00
}
func disconnect(player: PlayerName) {
2021-12-03 18:03:29 +01:00
guard let table = currentTable(for: player) else {
2021-12-01 22:47:19 +01:00
return
}
2021-12-03 18:03:29 +01:00
table.disconnect(player: player)
2021-12-01 22:47:19 +01:00
}
2021-12-03 18:03:29 +01:00
func performAction(player: PlayerName, action: Player.Action) -> PlayerActionResult {
guard let table = currentTable(for: player) else {
print("Player \(player) wants to \(action.path), but no table joined")
2021-12-01 22:47:19 +01:00
return .noTableJoined
}
2021-12-03 18:03:29 +01:00
return table.perform(action: action, forPlayer: player)
2021-12-01 22:47:19 +01:00
}
2021-12-06 11:43:30 +01:00
func select(game: GameType, player: PlayerName) -> PlayerActionResult {
guard let table = 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)
}
2021-12-06 11:43:30 +01:00
func play(card: Card, player: PlayerName) -> PlayCardResult {
guard let table = currentTable(for: player) else {
return .noTableJoined
}
return table.play(card: card, player: player)
}
2021-12-01 22:47:19 +01:00
}