Update player points when game ends

This commit is contained in:
Christoph Hagen 2022-10-18 11:40:08 +02:00
parent df97271987
commit 3c4d1f0e29
5 changed files with 29 additions and 8 deletions

View File

@ -266,11 +266,11 @@ final class SQLiteDatabase {
return tables.select(game: game, player: player) return tables.select(game: game, player: player)
} }
func play(card: Card, playerToken: SessionToken) -> PlayerActionResult { func play(card: Card, playerToken: SessionToken, in database: Database) async throws -> PlayerActionResult {
guard let player = playerNameForToken[playerToken] else { guard let player = playerNameForToken[playerToken] else {
return .invalidToken return .invalidToken
} }
return tables.play(card: card, player: player) return try await tables.play(card: card, player: player, in: database)
} }
func disconnectAllSockets() { func disconnectAllSockets() {

View File

@ -114,8 +114,8 @@ final class TableManagement {
} }
/// `player.canStartGame` is automatically set to false, because table is not full /// `player.canStartGame` is automatically set to false, because table is not full
tables[table.id] = table tables[table.id] = table
#warning("Update points for all players, add penalty if running game")
table.sendUpdateToAllPlayers() table.sendUpdateToAllPlayers()
// TODO: Update points for all players
try await player.update(on: database) try await player.update(on: database)
} }
@ -177,7 +177,7 @@ final class TableManagement {
return .success return .success
} }
func play(card: Card, player: PlayerName) -> PlayerActionResult { func play(card: Card, player: PlayerName, in database: Database) async throws -> PlayerActionResult {
guard let table = currentTable(for: player) else { guard let table = currentTable(for: player) else {
return .noTableJoined return .noTableJoined
} }
@ -190,8 +190,10 @@ final class TableManagement {
return .success return .success
} }
tables[newTable.id] = newTable tables[newTable.id] = newTable
if let finished = newTable as? FinishedTable {
try await finished.updatePlayerPoints(in: database)
}
newTable.sendUpdateToAllPlayers() newTable.sendUpdateToAllPlayers()
// TODO: Save new table
return .success return .success
} }

View File

@ -7,7 +7,7 @@ class Player {
let name: PlayerName let name: PlayerName
let totalPoints: Int var totalPoints: Int
var socket: WebSocket? var socket: WebSocket?

View File

@ -1,4 +1,5 @@
import Foundation import Foundation
import Fluent
final class FinishedTable: AbstractTable<FinishedPlayer> { final class FinishedTable: AbstractTable<FinishedPlayer> {
@ -137,4 +138,22 @@ final class FinishedTable: AbstractTable<FinishedPlayer> {
override func cardStackPosition(ofPlayerAt index: Int) -> Int { override func cardStackPosition(ofPlayerAt index: Int) -> Int {
(4 + index - indexOfTrickStarter) % 4 (4 + index - indexOfTrickStarter) % 4
} }
func updatePlayerPoints(in database: Database) async throws {
let points = cost
let winnerNames = winners.map { $0.name }
for player in allPlayers {
guard let user = try await User.query(on: database).filter(\.$name == player.name).first() else {
continue
}
if winnerNames.contains(player.name) {
user.points += points
player.totalPoints += points
} else {
user.points -= points
player.totalPoints -= points
}
try await user.save(on: database)
}
}
} }

View File

@ -412,14 +412,14 @@ func performActionForPlayer(_ app: Application) {
- `412`: The action is not allowed - `412`: The action is not allowed
*/ */
func playCard(_ app: Application) { func playCard(_ app: Application) {
app.post("player", "card") { request -> HTTPResponseStatus in app.post("player", "card") { request async throws -> HTTPResponseStatus in
let token = try request.header(.token) let token = try request.header(.token)
let cardId = try request.header(.action) let cardId = try request.header(.action)
guard let card = Card(id: cardId) else { guard let card = Card(id: cardId) else {
throw Abort(.badRequest) throw Abort(.badRequest)
} }
switch server.play(card: card, playerToken: token) { switch try await server.play(card: card, playerToken: token, in: request.db) {
case .success: case .success:
return .ok return .ok
case .invalidToken: case .invalidToken: