TEST: Change Host Identifier to displayName, since the ID is not working for some odd reasons

This commit is contained in:
Aldin Duraki 2020-05-13 22:32:05 +02:00
parent 570099911f
commit cfe01cde54
3 changed files with 48 additions and 38 deletions

View File

@ -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)
}
}
}

View File

@ -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
@ -104,28 +104,26 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
/*
Vom match erhaltene Spielerdaten
Vom match erhaltene Spielerdaten
*/
*/
func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
if mpMatch != match { return }
func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
if mpMatch != match { return }
let jsonDecoder = JSONDecoder()
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
@ -161,8 +159,8 @@ 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: {
@ -171,10 +169,11 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
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
}

View File

@ -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)