Change Datamodel to JSON

This commit is contained in:
Niko Jochim 2020-05-13 14:59:20 +02:00
parent fb01faa1e5
commit d553b72daf
2 changed files with 12 additions and 29 deletions

View File

@ -102,37 +102,24 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
}
func decode<T>(data: NSData) -> T {
let pointer = UnsafeMutablePointer<T>.allocate(capacity: MemoryLayout<T>.size)
data.getBytes(pointer, length: MemoryLayout<T>.size)
return pointer.move()
}
/*
Vom match erhaltene Spielerdaten
*/
private func match(match: GKMatch!, didReceiveData data: Data,fromPlayer playerID: String!) {
func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
print("Daten empfangen!")
if mpMatch != match { return }
let jsonDecoder = JSONDecoder()
let pointer = UnsafeMutablePointer<Message>.allocate(capacity: MemoryLayout<Message>.size)
let message = pointer.move()
switch message.messageType{
case MessageType.PlayerMove:
let playerMove = message as! PlayerMove
let count = playerMove.unitCount
print("Angekommen \(count)")
if let message = try? jsonDecoder.decode(PlayerMove.self, from: data) {
let count = message.unitCount
print("Angekommen \(count)")
}
}
//delegate?.matchReceivedData(match: match, data: data, fromPlayer: playerID)
}
/*
Verbindung/Matchmaking ist fehlgeschlagen
*/
@ -147,6 +134,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
/*
Wird beim ändern des States/Zustands des Spielers aufgerufen udn gibt dessen Zustand zurück.
*/
func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
if mpMatch != match {
return

View File

@ -9,22 +9,17 @@
import GameplayKit
import Foundation
protocol MultiplayerProtocol {
}
enum MessageType : Int{
enum MessageType : Int,Codable{
case PlayerMove
}
protocol Message {
protocol Message :Codable{
var messageType:MessageType { get }
}
struct PlayerMove : Message,Codable{
struct PlayerMove : Message{
let messageType: MessageType
// let message: Message