From 54d31d929dcb84466827a13d0abe9ee35c5bb36f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Mon, 4 May 2020 20:34:58 +0200 Subject: [PATCH] Update MatchMakerHelper, added methods --- GoldWars/GoldWars/MatchmakingHelper.swift | 83 +++++++++++++++++------ GoldWars/GoldWars/Scenes/MenuScene.swift | 2 +- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index e38cc92..d546c0e 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -22,6 +22,8 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe var mpMatch: GKMatch? //represents network var viewController: UIViewController? var mpMatchStarted: Bool + var isServer: Bool + var serverPlayer: GKPlayer? let localPlayer: GKLocalPlayer = GKLocalPlayer.local static let sharedInstance = MatchmakingHelper() @@ -33,27 +35,24 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe override init() { mpMatchStarted = false + isServer = false super.init() } func presentMatchmaker() { - // 1 - guard GKLocalPlayer.local.isAuthenticated else { - print("Player ist nicht authentifiziert") - return - } - - // 2 - let request = GKMatchRequest() - - request.minPlayers = 2 - request.maxPlayers = 2 - // 3 - request.inviteMessage = "Willst du GoldWars spielen?" - - // 4 - let vc = GKMatchmakerViewController.init(matchRequest: request) - viewController?.present(vc!, animated: true, completion: nil) + guard GKLocalPlayer.local.isAuthenticated else { + print("Player ist nicht authentifiziert") + return + } + let request = GKMatchRequest() + + request.minPlayers = 2 + request.maxPlayers = 2 + request.inviteMessage = "Willst du GoldWars spielen?" + + let matchmakerVC = GKMatchmakerViewController.init(matchRequest: request) + matchmakerVC!.matchmakerDelegate = self + viewController?.present(matchmakerVC!, animated: true, completion: nil) } func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate){ @@ -94,9 +93,11 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe //expectedPlayerCount : The remaining number of players who have not yet connected to the match if !mpMatchStarted && match.expectedPlayerCount == 0 { print("Bereit das Spiel zu starten!") - //startMatch() + startMatch() } } + + // The match received data sent from the player. private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { if mpMatch != match { return } delegate?.matchReceivedData(match: match, data: data, fromPlayer: @@ -111,7 +112,49 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe delegate?.matchEnded() } - func startMatch(){ - + // The player state changed (eg. connected or disconnected) + func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) { + if mpMatch != match { + return + } + + switch (state) { + case GKPlayerConnectionState.connected: + print("Player connected!") + + if (!mpMatchStarted && match.expectedPlayerCount == 0) { + print("Ready to start match!") + startMatch() + } + case GKPlayerConnectionState.disconnected: + print("Player disconnected!") + mpMatchStarted = false + delegate!.matchEnded() + default: + print("Player unknown status!") + } + } + + func startMatch() { + mpMatch!.chooseBestHostingPlayer(completionHandler: { + (player) in + self.mpMatchStarted = true + if player == GKLocalPlayer.local { + print("I am the server") + self.isServer = true + } else { + print("I am a client") + self.isServer = false + self.serverPlayer = player + + } + self.delegate!.matchStarted() + }) + } + + func disconnect() { + if mpMatch != nil { + mpMatch?.disconnect() + } } } diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index b4ff9db..b4cda64 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -22,7 +22,7 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY), onButtonPress: { MatchmakingHelper.sharedInstance.presentMatchmaker() - // self.loadScene(scene: GameScene(size: self.size)) + self.loadScene(scene: GameScene(size: self.size)) })) entityManager.add(Button(name: "settingsButton",