From d553b72dafb1c07c3d0146f8bfa33a783cb3e0f8 Mon Sep 17 00:00:00 2001 From: Niko Jochim <82joni1bif@hft-stuttgart.de> Date: Wed, 13 May 2020 14:59:20 +0200 Subject: [PATCH] Change Datamodel to JSON --- GoldWars/GoldWars/MatchmakingHelper.swift | 30 +++++++---------------- GoldWars/GoldWars/MessageProtocol.swift | 11 +++------ 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index bc557c3..26f0c00 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -102,37 +102,24 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } - func decode(data: NSData) -> T { - let pointer = UnsafeMutablePointer.allocate(capacity: MemoryLayout.size) - data.getBytes(pointer, length: MemoryLayout.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.allocate(capacity: MemoryLayout.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 diff --git a/GoldWars/GoldWars/MessageProtocol.swift b/GoldWars/GoldWars/MessageProtocol.swift index 23ef038..3d6d14f 100644 --- a/GoldWars/GoldWars/MessageProtocol.swift +++ b/GoldWars/GoldWars/MessageProtocol.swift @@ -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