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

116 lines
3.8 KiB
Swift
Raw Normal View History

2021-11-27 11:59:13 +01:00
import Foundation
import Vapor
2021-11-27 11:59:13 +01:00
final class Database {
private let players: PlayerManagement
2021-11-27 11:59:13 +01:00
private let tables: TableManagement
2021-11-27 11:59:13 +01:00
2021-12-01 12:45:42 +01:00
init(storageFolder: URL) throws {
self.players = try PlayerManagement(storageFolder: storageFolder)
2021-12-01 22:47:19 +01:00
self.tables = try TableManagement(storageFolder: storageFolder)
}
2021-11-27 11:59:13 +01:00
// MARK: Players & Sessions
2021-11-27 11:59:13 +01:00
func registerPlayer(named name: PlayerName, hash: PasswordHash) -> SessionToken? {
players.registerPlayer(named: name, hash: hash)
}
2021-11-27 11:59:13 +01:00
func passwordHashForExistingPlayer(named name: PlayerName) -> PasswordHash? {
players.passwordHash(ofRegisteredPlayer: name)
}
2021-11-27 11:59:13 +01:00
func deletePlayer(named name: PlayerName) {
2021-11-29 11:54:50 +01:00
_ = players.deletePlayer(named: name)
2021-12-03 18:03:29 +01:00
tables.leaveTable(player: name)
2021-11-27 11:59:13 +01:00
}
func isValid(sessionToken token: SessionToken) -> Bool {
players.isValid(sessionToken: token)
2021-11-27 11:59:13 +01:00
}
2021-11-29 11:54:50 +01:00
func startSession(socket: WebSocket, sessionToken: SessionToken) -> Bool {
guard let player = players.registeredPlayerExists(withSessionToken: sessionToken) else {
return false
}
2021-11-29 11:54:50 +01:00
return tables.connect(player: player, using: socket)
2021-11-27 11:59:13 +01:00
}
private func didReceive(message: String, forSessionToken token: SessionToken) {
// TODO: Handle client requests
print("Session \(token.prefix(6)): \(message)")
}
2021-11-29 11:54:50 +01:00
func endSession(forSessionToken sessionToken: SessionToken) {
guard let player = players.endSession(forSessionToken: sessionToken) else {
return
}
tables.disconnect(player: player)
2021-11-27 11:59:13 +01:00
}
/**
Start a new session for an existing user.
- Parameter name: The user name
- Returns: The generated access token for the session
*/
func startNewSessionForRegisteredPlayer(named name: PlayerName) -> SessionToken {
players.startNewSessionForRegisteredPlayer(named: name)
2021-11-27 11:59:13 +01:00
}
func registeredPlayerExists(withSessionToken token: SessionToken) -> PlayerName? {
players.registeredPlayerExists(withSessionToken: token)
2021-11-27 11:59:13 +01:00
}
2021-12-03 18:03:29 +01:00
func currentTableOfPlayer(named player: PlayerName) -> TableInfo? {
tables.tableInfo(player: player)
2021-11-27 11:59:13 +01:00
}
2021-11-29 11:54:50 +01:00
// MARK: Tables
2021-11-27 11:59:13 +01:00
/**
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-11-27 11:59:13 +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
tables.createTable(named: name, player: player, isPublic: isPublic)
2021-11-27 11:59:13 +01:00
}
2021-12-03 18:03:29 +01:00
func getPublicTableInfos() -> [PublicTableInfo] {
tables.publicTableList
2021-11-27 11:59:13 +01:00
}
2021-12-03 18:03:29 +01:00
func join(tableId: TableId, playerToken: SessionToken) -> Result<TableInfo,JoinTableResult> {
2021-11-30 11:56:51 +01:00
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
2021-12-03 18:03:29 +01:00
return .failure(.invalidToken)
2021-11-30 11:56:51 +01:00
}
return tables.join(tableId: tableId, player: player)
}
func leaveTable(playerToken: SessionToken) -> Bool {
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
return false
}
2021-12-03 18:03:29 +01:00
tables.leaveTable(player: player)
2021-11-30 11:56:51 +01:00
return true
2021-11-27 11:59:13 +01:00
}
2021-12-01 22:47:19 +01:00
2021-12-03 18:03:29 +01:00
func performAction(playerToken: SessionToken, action: Player.Action) -> PlayerActionResult {
2021-12-01 22:47:19 +01:00
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
return .invalidToken
}
2021-12-03 18:03:29 +01:00
return tables.performAction(player: player, action: action)
2021-12-01 22:47:19 +01:00
}
2021-12-06 11:43:30 +01:00
func play(card: Card, playerToken: SessionToken) -> PlayCardResult {
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
return .invalidToken
}
return tables.play(card: card, player: player)
}
2021-11-27 11:59:13 +01:00
}