Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Level10/GameViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ class GameViewModel: ObservableObject {
HapticManager.playWarning()
SoundManager.shared.playNotify()
let handCounts = Dictionary(uniqueKeysWithValues: game.players.map { ($0.id, 10) })
let remainingPlayers = Set(players.map { $0.id })
let remainingPlayers = Set(game.players.map { $0.id })

DispatchQueue.main.async { [self] in
completedLevel = false
Expand Down
34 changes: 21 additions & 13 deletions Level10/Utilities/Managers/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,16 @@ final class NetworkManager {

let params = DiscardParams(card: card, playerToSkip: playerToSkip)

channel.push(.discard, payload: params.toDict(), with: decoder) { (result: Result<HandUpdatedPayload, GameCreationError>) in
switch result {
case .success(let payload):
NotificationCenter.default.post(name: .handDidUpdate, object: nil, userInfo: ["hand": payload.hand])
case .failure(let error):
NotificationCenter.default.post(name: .didReceiveDiscardError, object: nil, userInfo: ["error": error])
try await withCheckedThrowingContinuation { continuation in
channel.push(.discard, payload: params.toDict(), with: decoder) { (result: Result<HandUpdatedPayload, DiscardError>) in
switch result {
case .success(let payload):
NotificationCenter.default.post(name: .handDidUpdate, object: nil, userInfo: ["hand": payload.hand])
continuation.resume()
case .failure(let error):
NotificationCenter.default.post(name: .didReceiveDiscardError, object: nil, userInfo: ["error": error])
continuation.resume(throwing: error)
}
}
}
}
Expand All @@ -226,17 +230,21 @@ final class NetworkManager {

- Throws: `NetworkError.socketNotConnected` if the socket wasn't properly created for some reason.
*/
func drawCard(source: DrawSource) async throws {
func drawCard(source: DrawSource) async throws -> Card {
guard let socket = socket, let channel = gameChannel else { throw NetworkError.socketNotConnected }
if !socket.isConnected { try await connectSocket() }
let params = DrawCardParams(source: source)

channel.push(.drawCard, payload: params.toDict(), with: decoder) { (result: Result<CardPayload, CardDrawError>) in
switch result {
case .success(let payload):
NotificationCenter.default.post(name: .didDrawCard, object: nil, userInfo: ["newCard": payload.card])
case .failure(let error):
NotificationCenter.default.post(name: .didReceiveCardDrawError, object: nil, userInfo: ["error": error])
return try await withCheckedThrowingContinuation { continuation in
channel.push(.drawCard, payload: params.toDict(), with: decoder) { (result: Result<CardPayload, CardDrawError>) in
switch result {
case .success(let payload):
NotificationCenter.default.post(name: .didDrawCard, object: nil, userInfo: ["newCard": payload.card])
continuation.resume(returning: payload.card)
case .failure(let error):
NotificationCenter.default.post(name: .didReceiveCardDrawError, object: nil, userInfo: ["error": error])
continuation.resume(throwing: error)
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion Level10/Utilities/Managers/UserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ final class UserManager {
} else {
let _ = removeToken()
}
} else {
removeId()
}
}

Expand Down Expand Up @@ -155,14 +157,21 @@ final class UserManager {

let status = SecItemDelete(removeQuery)

if status == errSecSuccess {
if status == errSecSuccess || status == errSecItemNotFound {
self.token = nil
removeId()
return true
} else {
return false
}
}

private func removeId() {
self.id = nil
var configuration = Configuration()
UserDefaults.standard.removeObject(forKey: configuration.environment.userIdKey)
}

private func setId(_ id: String) {
self.id = id
var configuration = Configuration()
Expand Down
Loading