TEST: Change Host Identifier to displayName, since the ID is not working for some odd reasons
This commit is contained in:
parent
570099911f
commit
cfe01cde54
@ -45,7 +45,10 @@ class TimerComponent: GKComponent {
|
|||||||
|
|
||||||
if(isFinished()){
|
if(isFinished()){
|
||||||
self.labelNode.text = "Synching"
|
self.labelNode.text = "Synching"
|
||||||
MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves)
|
if !MultiplayerNetwork.sharedInstance.isSending {
|
||||||
|
MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,15 +72,15 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet.
|
Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet.
|
||||||
*/
|
*/
|
||||||
func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
|
func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
|
||||||
viewController.dismiss(animated: true, completion: nil)
|
viewController.dismiss(animated: true, completion: nil)
|
||||||
delegate?.matchEnded()
|
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) {
|
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) {
|
||||||
viewController.dismiss(animated: true, completion: nil)
|
viewController.dismiss(animated: true, completion: nil)
|
||||||
print("Error finding match", error.localizedDescription)
|
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.
|
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
|
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
|
z.B 0 gibt an, dass keine weiteren Spieler benötigt werden um das Match zu starten
|
||||||
*/
|
*/
|
||||||
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
|
func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) {
|
||||||
viewController.dismiss(animated: true, completion: nil)
|
viewController.dismiss(animated: true, completion: nil)
|
||||||
mpMatch = match
|
mpMatch = match
|
||||||
@ -100,33 +100,31 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
|||||||
startMatch()
|
startMatch()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Vom match erhaltene Spielerdaten
|
Vom match erhaltene Spielerdaten
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
|
func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) {
|
||||||
if mpMatch != match { return }
|
if mpMatch != match { return }
|
||||||
|
|
||||||
let jsonDecoder = JSONDecoder()
|
let jsonDecoder = JSONDecoder()
|
||||||
|
|
||||||
if let playerMoves = try? jsonDecoder.decode([PlayerMove].self, from: data) {
|
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 let message = try? jsonDecoder.decode(Host.self, from: data) {
|
||||||
if !self.isServer {
|
DataService.sharedInstance.gameHost = message
|
||||||
DataService.sharedInstance.gameHost = message
|
print("Name: \(player.displayName)")
|
||||||
print("LocalID: \(GKLocalPlayer.local.gamePlayerID)")
|
print("ServerID: \(message.playerID)")
|
||||||
print("ServerID: \(message.playerID)")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
MultiplayerNetwork.sharedInstance.isSending = false
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Verbindung/Matchmaking ist fehlgeschlagen
|
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.
|
Wird beim ändern des States/Zustands des Spielers aufgerufen udn gibt dessen Zustand zurück.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
|
func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) {
|
||||||
if mpMatch != match {
|
if mpMatch != match {
|
||||||
return
|
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() {
|
func startMatch() {
|
||||||
|
|
||||||
mpMatch!.chooseBestHostingPlayer(completionHandler: {
|
mpMatch!.chooseBestHostingPlayer(completionHandler: {
|
||||||
(player) in
|
(player) in
|
||||||
|
|
||||||
self.mpMatchStarted = true
|
self.mpMatchStarted = true
|
||||||
|
|
||||||
if player == GKLocalPlayer.local {
|
if player == GKLocalPlayer.local {
|
||||||
|
print("ich bin host")
|
||||||
self.isServer = true
|
self.isServer = true
|
||||||
self.spieler1 = player
|
self.spieler1 = player
|
||||||
self.nameSpieler1 = self.spieler1!.displayName
|
self.nameSpieler1 = self.spieler1!.displayName
|
||||||
DataService.sharedInstance.setGameHost(host: Host(playerID: player!.gamePlayerID))
|
DataService.sharedInstance.setGameHost(host: Host(playerID: player!.displayName))
|
||||||
} else {
|
} else {
|
||||||
self.isServer = false
|
self.isServer = false
|
||||||
}
|
}
|
||||||
@ -184,7 +183,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
|
|||||||
MultiplayerNetwork.sharedInstance.sendHostIdentifier()
|
MultiplayerNetwork.sharedInstance.sendHostIdentifier()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Trennt die Verbindung vom Match
|
Trennt die Verbindung vom Match
|
||||||
|
@ -8,9 +8,11 @@
|
|||||||
|
|
||||||
import GameplayKit
|
import GameplayKit
|
||||||
import Foundation
|
import Foundation
|
||||||
|
import GameKit
|
||||||
|
|
||||||
class MultiplayerNetwork{
|
class MultiplayerNetwork{
|
||||||
static let sharedInstance = MultiplayerNetwork()
|
static let sharedInstance = MultiplayerNetwork()
|
||||||
|
var isSending = false
|
||||||
|
|
||||||
func sendData(data: Data) {
|
func sendData(data: Data) {
|
||||||
let mmHelper = MatchmakingHelper.sharedInstance
|
let mmHelper = MatchmakingHelper.sharedInstance
|
||||||
@ -25,11 +27,16 @@ class MultiplayerNetwork{
|
|||||||
|
|
||||||
func sendDataToHost(data: Data) {
|
func sendDataToHost(data: Data) {
|
||||||
let mmHelper = MatchmakingHelper.sharedInstance
|
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{
|
if let multiplayerMatch = mmHelper.mpMatch{
|
||||||
do {
|
do {
|
||||||
try multiplayerMatch.send(data, to: hostGKPlayer!, dataMode: .reliable)
|
try multiplayerMatch.send(data, to: [hostGKPlayer!], dataMode: .reliable)
|
||||||
} catch {
|
} catch {
|
||||||
print("Tim war mal wieder am Werk der Krasse")
|
print("Tim war mal wieder am Werk der Krasse")
|
||||||
}
|
}
|
||||||
@ -37,7 +44,8 @@ class MultiplayerNetwork{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendPlayerMoves(playerMoves: [PlayerMove]) {
|
func sendPlayerMoves(playerMoves: [PlayerMove]) {
|
||||||
if !MatchmakingHelper.sharedInstance.isServer {
|
if MatchmakingHelper.sharedInstance.isServer == false {
|
||||||
|
self.isSending = true
|
||||||
let encoder = JSONEncoder()
|
let encoder = JSONEncoder()
|
||||||
let encoded = (try? encoder.encode(playerMoves))!
|
let encoded = (try? encoder.encode(playerMoves))!
|
||||||
sendDataToHost(data: encoded)
|
sendDataToHost(data: encoded)
|
||||||
|
Loading…
Reference in New Issue
Block a user