2021-11-27 11:59:13 +01:00
|
|
|
import Foundation
|
2021-11-28 15:53:47 +01:00
|
|
|
import Vapor
|
2021-11-27 11:59:13 +01:00
|
|
|
|
|
|
|
final class Database {
|
|
|
|
|
2021-11-28 15:53:47 +01:00
|
|
|
private let players: PlayerManagement
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2021-11-28 15:53:47 +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-28 15:53:47 +01:00
|
|
|
}
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2021-11-28 15:53:47 +01:00
|
|
|
// MARK: Players & Sessions
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2021-11-28 15:53:47 +01:00
|
|
|
func registerPlayer(named name: PlayerName, hash: PasswordHash) -> SessionToken? {
|
|
|
|
players.registerPlayer(named: name, hash: hash)
|
|
|
|
}
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2021-11-28 15:53:47 +01:00
|
|
|
func passwordHashForExistingPlayer(named name: PlayerName) -> PasswordHash? {
|
|
|
|
players.passwordHash(ofRegisteredPlayer: name)
|
|
|
|
}
|
2021-11-27 11:59:13 +01:00
|
|
|
|
2021-11-28 15:53:47 +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
|
|
|
}
|
|
|
|
|
2021-11-28 15:53:47 +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-28 15:53:47 +01:00
|
|
|
}
|
2021-11-29 11:54:50 +01:00
|
|
|
return tables.connect(player: player, using: socket)
|
2021-11-27 11:59:13 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 15:53:47 +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
|
|
|
|
*/
|
2021-11-28 15:53:47 +01:00
|
|
|
func startNewSessionForRegisteredPlayer(named name: PlayerName) -> SessionToken {
|
|
|
|
players.startNewSessionForRegisteredPlayer(named: name)
|
2021-11-27 11:59:13 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 15:53:47 +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
|
|
|
|
*/
|
2021-11-28 15:53:47 +01:00
|
|
|
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] {
|
2021-11-28 15:53:47 +01:00
|
|
|
tables.getPublicTableInfos()
|
2021-11-27 11:59:13 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 11:56:51 +01:00
|
|
|
func join(tableId: TableId, playerToken: SessionToken) -> JoinTableResult {
|
|
|
|
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
|
|
|
|
return .invalidToken
|
|
|
|
}
|
|
|
|
return tables.join(tableId: tableId, player: player)
|
|
|
|
}
|
|
|
|
|
|
|
|
func leaveTable(playerToken: SessionToken) -> Bool {
|
|
|
|
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tables.remove(player: player)
|
|
|
|
return true
|
2021-11-27 11:59:13 +01:00
|
|
|
}
|
2021-12-01 22:47:19 +01:00
|
|
|
|
|
|
|
func dealCards(playerToken: SessionToken) -> DealCardResult {
|
|
|
|
guard let player = players.registeredPlayerExists(withSessionToken: playerToken) else {
|
|
|
|
return .invalidToken
|
|
|
|
}
|
|
|
|
return tables.dealCards(player: player)
|
|
|
|
}
|
2021-11-27 11:59:13 +01:00
|
|
|
}
|