Skip to content

Commit

Permalink
Update EventStatisticLink
Browse files Browse the repository at this point in the history
  • Loading branch information
sp4ce-cowboy committed Apr 15, 2024
1 parent 7c18832 commit e228f26
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 87 deletions.
6 changes: 6 additions & 0 deletions TowerForge/TowerForge/GameModule/Events/TFEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ extension TFEvent {
ConcurrentEvent(self, otherEvent)
}
}

extension TFEvent {
static var asType: TFEventTypeWrapper {
TFEventTypeWrapper(type: Self.self)
}
}
18 changes: 8 additions & 10 deletions TowerForge/TowerForge/Metrics/Statistics/StatisticsEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ class StatisticsEngine {
}

/// Add statistics links manually
/// TODO: Consider a more elegant way to do this
func setUpLinks() {
eventStatisticLinks.addStatisticLink(for: KillEvent.self,
with: statistics.getStatistic(for: TotalKillsStatistic.asType))

eventStatisticLinks.addStatisticLink(for: GameStartEvent.self,
with: statistics.getStatistic(for: TotalGamesStatistic.asType))

eventStatisticLinks.addStatisticLink(for: DeathEvent.self,
with: statistics.getStatistic(for: TotalDeathsStatistic.asType))
private func setUpLinks() {
let links = StatisticsFactory.eventStatisticLinks
for key in links.keys {
links[key]?.forEach {
eventStatisticLinks.addStatisticLink(for: key.type,
with: statistics.getStatistic(for: $0))
}
}
}

private func initializeStatistics() {
Expand Down
16 changes: 8 additions & 8 deletions TowerForge/TowerForge/Metrics/Statistics/StatisticsFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import Foundation

class StatisticsFactory {

static var eventStatisticLinks: [TFEventTypeWrapper: [StatisticTypeWrapper]] =
[
KillEvent.asType: [TotalKillsStatistic.asType],
GameStartEvent.asType: [TotalGamesStatistic.asType],
DeathEvent.asType: [TotalDeathsStatistic.asType]
]

static var availableStatisticsTypes: [String: Statistic.Type] =
[
String(describing: TotalKillsStatistic.self): TotalKillsStatistic.self,
String(describing: TotalGamesStatistic.self): TotalGamesStatistic.self,
String(describing: TotalDeathsStatistic.self): TotalDeathsStatistic.self
]

static var defaultStatisticDecoder: [StatisticTypeWrapper: (JSONDecoder, Data) throws -> Statistic] =
[
TotalKillsStatistic.asType: { decoder, data in try decoder.decode(TotalKillsStatistic.self, from: data) },
TotalGamesStatistic.asType: { decoder, data in try decoder.decode(TotalGamesStatistic.self, from: data) },
TotalDeathsStatistic.asType: { decoder, data in try decoder.decode(TotalDeathsStatistic.self, from: data) }
]

static var statisticDecoder: [String: (Decoder) throws -> Statistic] =
[
TotalKillsStatistic.asType.asString: { decoder in try TotalKillsStatistic(from: decoder) },
Expand All @@ -39,7 +39,7 @@ class StatisticsFactory {

static func registerStatisticType<T: Statistic>(_ stat: T) {
availableStatisticsTypes[String(describing: T.self)] = T.self
defaultStatisticDecoder[T.asType] = { decoder, data in try decoder.decode(T.self, from: data) }
statisticDecoder[T.asType.asString] = { decoder in try T(from: decoder) }
defaultStatisticGenerator[T.asType] = { T() }
}

Expand Down
69 changes: 0 additions & 69 deletions TowerForge/TowerForge/Storage/RemoteStorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,72 +101,3 @@ class RemoteStorageManager {
}
}
}

extension RemoteStorageManager {
static func loadFromFirebaseOld(completion: @escaping (StatisticsDatabase?, Error?) -> Void) {
let databaseReference = FirebaseDatabaseReference(.Statistics)

databaseReference.child(currentPlayer).getData(completion: { error, snapshot in
if let error = error {
Logger.log(error.localizedDescription, self)
completion(nil, error)
return
}

guard let value = snapshot?.value as? [String: Any] else {
completion(nil, nil)
return
}

var statistics = [StatisticTypeWrapper: Statistic]()

for (key, statisticValue) in value {
guard let statisticDict = statisticValue as? [String: Any],
let statisticData = try? JSONSerialization.data(withJSONObject: statisticDict, options: []) else {
continue
}

do {
guard let statisticType = key.asTFClassFromString as? Statistic.Type else {
continue
}
let statisticName = statisticType.asType
let decoder = JSONDecoder()

let statistic: Statistic? = try StatisticsFactory
.defaultStatisticDecoder[statisticName]?(decoder, statisticData)
if let stat = statistic { statistics[statisticName] = stat }
} catch {
Logger.log("Error decoding from Firebase \(key):, \(error)", self)
}
}

let statsDatabase = StatisticsDatabase(statistics)
completion(statsDatabase, nil)
})
}

static func saveToFirebaseOld(_ stats: StatisticsDatabase, completion: @escaping (Error?) -> Void) {
let databaseReference = FirebaseDatabaseReference(.Statistics)
var statisticsDictionary = [String: Any]()

for (name, statistic) in stats.statistics {
do {
let data = try JSONEncoder().encode(statistic)
let dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
statisticsDictionary[name.asString] = dictionary
} catch {
Logger.log("Error encoding statistic \(name): \(error)", StatisticsDatabase.self)
}
}

databaseReference.child(currentPlayer).setValue(statisticsDictionary) { error, _ in
if let error = error {
Logger.log("Data could not be saved: \(error).", StatisticsDatabase.self)
} else {
Logger.log("StatisticsDatabase saved to Firebase successfully!", StatisticsDatabase.self)
}
completion(error)
}
}
}

0 comments on commit e228f26

Please sign in to comment.