From cfe01cde54f51a876df75ebde87a2dd9cfb2ada9 Mon Sep 17 00:00:00 2001 From: Aldin Duraki Date: Wed, 13 May 2020 22:32:05 +0200 Subject: [PATCH] TEST: Change Host Identifier to displayName, since the ID is not working for some odd reasons --- .../GoldWars/Components/TimerComponent.swift | 5 +- GoldWars/GoldWars/MatchmakingHelper.swift | 65 +++++++++---------- GoldWars/GoldWars/MultiplayerNetwork.swift | 16 +++-- 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index f45026c..b3adc4a 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -45,7 +45,10 @@ class TimerComponent: GKComponent { if(isFinished()){ self.labelNode.text = "Synching" - MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) + if !MultiplayerNetwork.sharedInstance.isSending { + MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) + } + } } diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 0af55c1..4b499d4 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -72,15 +72,15 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe /* Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet. - */ + */ func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { viewController.dismiss(animated: true, completion: nil) delegate?.matchEnded() } /* - Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed. - */ + Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed. + */ func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { viewController.dismiss(animated: true, completion: nil) print("Error finding match", error.localizedDescription) @@ -91,7 +91,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe Gamecenter hat erfolgreich ein Match gefunden, das Spiel kann gestartet werden. expectedPlayerCount: Die verbleibende Anzahl von Spielern, die sich noch nicht mit dem Spiel verbunden haben z.B 0 gibt an, dass keine weiteren Spieler benötigt werden um das Match zu starten - */ + */ func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { viewController.dismiss(animated: true, completion: nil) mpMatch = match @@ -100,33 +100,31 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe startMatch() } } - - - + + + /* - Vom match erhaltene Spielerdaten - - */ - - func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) { - if mpMatch != match { return } - - let jsonDecoder = JSONDecoder() - + Vom match erhaltene Spielerdaten + + */ + + func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) { + if mpMatch != match { return } + + let jsonDecoder = JSONDecoder() + if let playerMoves = try? jsonDecoder.decode([PlayerMove].self, from: data) { - DataService.sharedInstance.addRemotePlayerMoves(playerID: player.gamePlayerID, playerMoves: playerMoves) - } + DataService.sharedInstance.addRemotePlayerMoves(playerID: player.displayName, playerMoves: playerMoves) + } if let message = try? jsonDecoder.decode(Host.self, from: data) { - if !self.isServer { - DataService.sharedInstance.gameHost = message - print("LocalID: \(GKLocalPlayer.local.gamePlayerID)") - print("ServerID: \(message.playerID)") - } - + DataService.sharedInstance.gameHost = message + print("Name: \(player.displayName)") + print("ServerID: \(message.playerID)") } - } - + MultiplayerNetwork.sharedInstance.isSending = false + } + /* Verbindung/Matchmaking ist fehlgeschlagen */ @@ -141,7 +139,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 @@ -161,20 +159,21 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } /* - Ein Spieler wird als Host für das Match gewählt. Dieser ist Spieler 1. Im Anschluss wird die GameScene geladen. - */ + Ein Spieler wird als Host für das Match gewählt. Dieser ist Spieler 1. Im Anschluss wird die GameScene geladen. + */ func startMatch() { - + mpMatch!.chooseBestHostingPlayer(completionHandler: { (player) in self.mpMatchStarted = true - + if player == GKLocalPlayer.local { + print("ich bin host") self.isServer = true self.spieler1 = player self.nameSpieler1 = self.spieler1!.displayName - DataService.sharedInstance.setGameHost(host: Host(playerID: player!.gamePlayerID)) + DataService.sharedInstance.setGameHost(host: Host(playerID: player!.displayName)) } else { self.isServer = false } @@ -184,7 +183,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe MultiplayerNetwork.sharedInstance.sendHostIdentifier() }) } - + /* Trennt die Verbindung vom Match diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index 2cbc4ea..36503ad 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -8,9 +8,11 @@ import GameplayKit import Foundation +import GameKit class MultiplayerNetwork{ - static let sharedInstance = MultiplayerNetwork() + static let sharedInstance = MultiplayerNetwork() + var isSending = false func sendData(data: Data) { let mmHelper = MatchmakingHelper.sharedInstance @@ -25,11 +27,16 @@ class MultiplayerNetwork{ func sendDataToHost(data: Data) { let mmHelper = MatchmakingHelper.sharedInstance - let hostGKPlayer = MatchmakingHelper.sharedInstance.mpMatch?.players.filter{ $0.gamePlayerID == DataService.sharedInstance.gameHost?.playerID} + for player in mmHelper.mpMatch!.players { + print(player.displayName) + } + print(DataService.sharedInstance.gameHost!.playerID) + + let hostGKPlayer = MatchmakingHelper.sharedInstance.mpMatch?.players.filter{ $0.displayName == DataService.sharedInstance.gameHost!.playerID}[0] if let multiplayerMatch = mmHelper.mpMatch{ do { - try multiplayerMatch.send(data, to: hostGKPlayer!, dataMode: .reliable) + try multiplayerMatch.send(data, to: [hostGKPlayer!], dataMode: .reliable) } catch { print("Tim war mal wieder am Werk der Krasse") } @@ -37,7 +44,8 @@ class MultiplayerNetwork{ } func sendPlayerMoves(playerMoves: [PlayerMove]) { - if !MatchmakingHelper.sharedInstance.isServer { + if MatchmakingHelper.sharedInstance.isServer == false { + self.isSending = true let encoder = JSONEncoder() let encoded = (try? encoder.encode(playerMoves))! sendDataToHost(data: encoded)