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

97 lines
2.9 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
init() {
self.players = PlayerManagement()
self.tables = TableManagement()
// TODO: Load server data from disk
// TODO: Save data to disk
}
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)
tables.remove(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
}
closeSession(for: player)
}
2021-11-29 11:54:50 +01:00
private func closeSession(for player: PlayerName) {
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-11-29 11:54:50 +01:00
func currentTableOfPlayer(named player: PlayerName) -> TableId {
tables.currentTableOfPlayer(named: 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
- Parameter visible: Indicates that this is a game joinable by everyone
- Returns: The table id
*/
func createTable(named name: TableName, player: PlayerName, visible: Bool) -> TableId {
tables.createTable(named: name, player: player, visible: visible)
2021-11-27 11:59:13 +01:00
}
func getPublicTableInfos() -> [TableInfo] {
tables.getPublicTableInfos()
2021-11-27 11:59:13 +01:00
}
2021-11-29 11:54:50 +01:00
func join(tableId: TableId, player: PlayerName) -> TableManagement.JoinTableResult {
tables.join(tableId: tableId, player: player)
2021-11-27 11:59:13 +01:00
}
}