-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTOR: Separated the functionality that's shared across GPEvent ty…
…pe, into their own protocols If applied, this commit will: - [T] Introduce tests for event router - [C] Add extra functionality of GPEventRouter, to easily stop the routing process - [C] Extend the Foundation's Data type, to add a convenience toString method
- Loading branch information
Showing
15 changed files
with
344 additions
and
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
public protocol GPEvent { | ||
|
||
var purpose : String { get } | ||
var purpose : String { get } | ||
|
||
var time : Date { get } | ||
var time : Date { get } | ||
|
||
var payload : [String: Any]? { get } | ||
var payload : [String: Any] { get } | ||
|
||
} |
46 changes: 32 additions & 14 deletions
46
Sources/GamePantry/Events/Implementations/GPAcquaintanceEvent.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 |
---|---|---|
@@ -1,21 +1,39 @@ | ||
import MultipeerConnectivity | ||
|
||
public struct GPAcquaintanceEvent : GPEvent { | ||
|
||
public let who : MCPeerID | ||
public let newState: MCSessionState | ||
public class GPAcquaintanceEvent : GPEvent { | ||
|
||
public var purpose : String | ||
public var time : Date | ||
public var payload : [String: Any]? | ||
public var payload : [String: Any] | ||
|
||
public init ( _ payload: [String: Any] ) { | ||
self.purpose = "An event which indicates that the state of one's acquaintance has changed" | ||
self.time = .now | ||
self.payload = payload | ||
} | ||
|
||
} | ||
|
||
extension GPAcquaintanceEvent : GPEasilyReadableEventPayloadKeys { | ||
|
||
public enum PayloadKeys : String, CaseIterable { | ||
case subject = "subject", | ||
acquaintanceState = "acquaintanceState", | ||
updatedAt = "updatedAt" | ||
} | ||
|
||
public func value ( for key: PayloadKeys ) -> Any { | ||
return self.payload[key.rawValue]! | ||
} | ||
|
||
} | ||
|
||
public init ( who: MCPeerID, newState: MCSessionState, payload: [String: Any]? ) { | ||
self.who = who | ||
self.newState = newState | ||
|
||
self.purpose = "An event which indicates that the state of one's acquaintance has changed." | ||
self.time = .now | ||
self.payload = payload | ||
extension GPAcquaintanceEvent : GPRepresentableAsData { | ||
|
||
public func representedAsData () -> Data { | ||
return dataFrom { | ||
PayloadKeys.allCases.reduce(into: [String: String]()) { (result, key) in | ||
result[key.rawValue] = self.payload[key.rawValue] as? String ?? "" | ||
} | ||
} ?? Data() | ||
} | ||
|
||
} |
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
56 changes: 26 additions & 30 deletions
56
Sources/GamePantry/Events/Implementations/GPBlacklistedEvent.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 |
---|---|---|
@@ -1,43 +1,39 @@ | ||
import MultipeerConnectivity | ||
|
||
public struct GPBlacklistedEvent : GPEvent { | ||
|
||
public let who : MCPeerID | ||
public let reason : String | ||
public class GPBlacklistedEvent : GPEvent { | ||
|
||
public let purpose : String | ||
public let time : Date | ||
public let payload : [String: Any]? | ||
|
||
public init ( who: MCPeerID, reason: String, payload: [String: Any]? ) { | ||
self.who = who | ||
self.reason = reason | ||
|
||
self.purpose = "An event that marks a peer as blacklisted." | ||
self.time = .now | ||
self.payload = payload | ||
public let payload : [String: Any] | ||
|
||
public init ( _ payload: [String: Any] ) { | ||
self.purpose = "An event that marks a peer as blacklisted" | ||
self.time = .now | ||
self.payload = payload | ||
} | ||
|
||
} | ||
|
||
extension GPBlacklistedEvent : GPEasilyReadableEventPayloadKeys { | ||
|
||
public enum PayloadKeys : String, CaseIterable { | ||
case subject = "subject", | ||
reason = "causeOfBlacklist", | ||
effectiveTime = "effectiveTime" | ||
} | ||
|
||
public func representation () -> Data { | ||
return ( | ||
try? JSONSerialization.data ( | ||
withJSONObject: [ | ||
"who" : who, | ||
"purpose" : purpose, | ||
"time" : time, | ||
"payload" : payload ?? [:] | ||
], | ||
options: .prettyPrinted | ||
) | ||
) ?? Data() | ||
public func value ( for key: PayloadKeys ) -> Any { | ||
return self.payload[key.rawValue]! | ||
} | ||
|
||
} | ||
|
||
extension GPBlacklistedEvent { | ||
extension GPBlacklistedEvent : GPRepresentableAsData { | ||
|
||
public enum payloadKeys : String { | ||
case reason = "causeOfBlacklist" | ||
public func representedAsData () -> Data { | ||
return dataFrom { | ||
PayloadKeys.allCases.reduce(into: [String: String]()) { (result, key) in | ||
result[key.rawValue] = self.payload[key.rawValue] as? String ?? "" | ||
} | ||
} ?? Data() | ||
} | ||
|
||
} |
55 changes: 25 additions & 30 deletions
55
Sources/GamePantry/Events/Implementations/GPTerminationEvent.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 |
---|---|---|
@@ -1,44 +1,39 @@ | ||
import MultipeerConnectivity | ||
|
||
public struct GPTerminationEvent : GPEvent { | ||
public class GPTerminationEvent : GPEvent { | ||
|
||
public let who : MCPeerID | ||
public let validOn : Date | ||
|
||
public let purpose : String | ||
public let time : Date | ||
public let payload : [String: Any]? | ||
public let payload : [String: Any] | ||
|
||
public init ( who: MCPeerID, validOn: Date, payload: [String: Any]? ) { | ||
self.who = who | ||
self.validOn = validOn | ||
|
||
self.purpose = "An event that marks the termination of relationship of a game client with the server its connected to." | ||
public init ( _ payload: [String: Any] ) { | ||
self.purpose = "An event that marks the termination of relationship of a game client with the server its connected to" | ||
self.time = .now | ||
self.payload = nil | ||
self.payload = payload | ||
} | ||
|
||
} | ||
|
||
extension GPTerminationEvent : GPEasilyReadableEventPayloadKeys { | ||
|
||
public enum PayloadKeys : String, CaseIterable { | ||
case subject = "subject", | ||
terminationReason = "causeOfTermination", | ||
effectiveTime = "effectiveTime" | ||
} | ||
|
||
public func representation () -> Data { | ||
return ( | ||
try? JSONSerialization.data ( | ||
withJSONObject: [ | ||
"who" : who, | ||
"validOn" : validOn, | ||
"purpose" : purpose, | ||
"time" : time, | ||
"payload" : payload ?? [:] | ||
], | ||
options: .prettyPrinted | ||
) | ||
) ?? Data() | ||
public func value ( for key: PayloadKeys ) -> Any { | ||
return self.payload[key.rawValue]! | ||
} | ||
|
||
} | ||
|
||
extension GPTerminationEvent { | ||
extension GPTerminationEvent : GPRepresentableAsData { | ||
|
||
public enum payloadKeys : String { | ||
case verificationKey = "verificationKey", | ||
terminationReason = "causeOfTermination" | ||
public func representedAsData () -> Data { | ||
return dataFrom { | ||
PayloadKeys.allCases.reduce(into: [String: String]()) { (result, key) in | ||
result[key.rawValue] = String(describing: self.payload[key.rawValue]!) | ||
} | ||
} ?? Data() | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
Sources/GamePantry/Events/Traits/GPEasilyReadablePayloadKeys.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,7 @@ | ||
public protocol GPEasilyReadableEventPayloadKeys { | ||
|
||
associatedtype PayloadKeys : CaseIterable | ||
|
||
func value ( for: PayloadKeys ) -> Any | ||
|
||
} |
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,7 @@ | ||
extension Data { | ||
|
||
public func toString ( encoder: String.Encoding = .utf8 ) -> String? { | ||
String(data: self, encoding: encoder) | ||
} | ||
|
||
} |
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
Oops, something went wrong.