-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
014670f
commit 034525d
Showing
5 changed files
with
99 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
TowerForge/TowerForge/StorageAPI/StorageHandler+Conflict.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// StorageHandler+Conflict.swift | ||
// TowerForge | ||
// | ||
// Created by Rubesh on 21/4/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// This extension adds conflict resolution methods to StorageHandler | ||
extension StorageHandler { | ||
|
||
/// Returns the StatisticsDatabase from the location that corresponds to the most recent save. | ||
static func getLocationWithLatestMetadata(completion: @escaping (StorageLocation?) -> Void) { | ||
RemoteStorage.loadMetadataFromFirebase(player: Self.currentPlayerId) { remoteMetadata, remoteError in | ||
let localMetadata = LocalStorage.loadMetadataFromLocalStorage() | ||
|
||
// Handle errors or nil cases | ||
if let remoteError = remoteError { | ||
Logger.log("Error occurred retrieving metadata: \(remoteError)", self) | ||
} | ||
|
||
switch (remoteMetadata, localMetadata) { | ||
case (_?, nil): | ||
completion(.Remote) | ||
case (nil, _?): | ||
completion(.Local) | ||
case (let remote?, let local?): | ||
completion(remote > local ? .Remote : .Local) | ||
default: | ||
completion(nil) | ||
} | ||
} | ||
} | ||
|
||
static func loadLatest(completion: @escaping (StatisticsDatabase?) -> Void) { | ||
Self.getLocationWithLatestMetadata { location in | ||
switch location { | ||
|
||
case .Local: | ||
if let stats = LocalStorage.loadDatabaseFromLocalStorage() { | ||
completion(stats) | ||
} else { | ||
Logger.log("Error: Failed to load local database.", self) | ||
completion(nil) | ||
} | ||
|
||
case .Remote: | ||
RemoteStorageManager.loadDatabaseFromFirebase { statsData, error in | ||
if let error = error { | ||
Logger.log("Error occurred loading from database: \(error)", self) | ||
completion(nil) | ||
} else { | ||
completion(statsData) | ||
} | ||
} | ||
|
||
default: | ||
Logger.log("No valid metadata found, cannot determine latest storage.", self) | ||
completion(nil) | ||
} | ||
} | ||
} | ||
|
||
static func resolveConflict(this: StatisticsDatabase, | ||
that: StatisticsDatabase, | ||
completion: @escaping (StatisticsDatabase?) -> Void) { | ||
|
||
switch CONFLICT_RESOLUTION { | ||
case .MERGE: | ||
completion(StatisticsDatabase.merge(this: this, that: that)) | ||
case .KEEP_LATEST_ONLY: | ||
Self.loadLatest { completion($0) } | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters