Add leave table server API
This commit is contained in:
parent
f81b8e4119
commit
775ac91cc8
@ -90,7 +90,18 @@ final class Database {
|
|||||||
tables.getPublicTableInfos()
|
tables.getPublicTableInfos()
|
||||||
}
|
}
|
||||||
|
|
||||||
func join(tableId: TableId, player: PlayerName) -> TableManagement.JoinTableResult {
|
func join(tableId: TableId, playerToken: SessionToken) -> JoinTableResult {
|
||||||
tables.join(tableId: tableId, player: player)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,8 +79,6 @@ final class TableManagement {
|
|||||||
players.append(player)
|
players.append(player)
|
||||||
if let oldTable = playerTables[tableId] {
|
if let oldTable = playerTables[tableId] {
|
||||||
remove(player: player, fromTable: oldTable)
|
remove(player: player, fromTable: oldTable)
|
||||||
// TODO: End game if needed
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
tablePlayers[tableId] = players
|
tablePlayers[tableId] = players
|
||||||
playerTables[tableId] = tableId
|
playerTables[tableId] = tableId
|
||||||
@ -89,10 +87,15 @@ final class TableManagement {
|
|||||||
|
|
||||||
func remove(player: PlayerName, fromTable tableId: TableId) {
|
func remove(player: PlayerName, fromTable tableId: TableId) {
|
||||||
tablePlayers[tableId] = tablePlayers[tableId]?.filter { $0 != player }
|
tablePlayers[tableId] = tablePlayers[tableId]?.filter { $0 != player }
|
||||||
|
// TODO: End connection for removed user
|
||||||
|
// TODO: End game if needed, send info to remaining players
|
||||||
}
|
}
|
||||||
|
|
||||||
func remove(player: PlayerName) {
|
func remove(player: PlayerName) {
|
||||||
fatalError()
|
guard let tableId = playerTables[player] else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
remove(player: player, fromTable: tableId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect(player: PlayerName, using socket: WebSocket) -> Bool {
|
func connect(player: PlayerName, using socket: WebSocket) -> Bool {
|
||||||
|
@ -235,10 +235,9 @@ func routes(_ app: Application) throws {
|
|||||||
let token = req.body.string else {
|
let token = req.body.string else {
|
||||||
throw Abort(.badRequest)
|
throw Abort(.badRequest)
|
||||||
}
|
}
|
||||||
guard let player = database.registeredPlayerExists(withSessionToken: token) else {
|
switch database.join(tableId: table, playerToken: token) {
|
||||||
|
case .invalidToken:
|
||||||
throw Abort(.unauthorized) // 401
|
throw Abort(.unauthorized) // 401
|
||||||
}
|
|
||||||
switch database.join(tableId: table, player: player) {
|
|
||||||
case .tableNotFound:
|
case .tableNotFound:
|
||||||
throw Abort(.gone) // 410
|
throw Abort(.gone) // 410
|
||||||
case .tableIsFull:
|
case .tableIsFull:
|
||||||
@ -247,4 +246,22 @@ func routes(_ app: Application) throws {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Leave the current table.
|
||||||
|
- Parameter token: The session token of the player, as a string in the request body
|
||||||
|
- Throws:
|
||||||
|
- 400: Missing token
|
||||||
|
- 401: The session token is invalid
|
||||||
|
- Returns: Nothing
|
||||||
|
*/
|
||||||
|
app.post("table", "leave") { req -> String in
|
||||||
|
guard let token = req.body.string else {
|
||||||
|
throw Abort(.badRequest)
|
||||||
|
}
|
||||||
|
guard database.leaveTable(playerToken: token) else {
|
||||||
|
throw Abort(.unauthorized) // 401
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user