From 71a59f2fa0f0763213439ba0ea2b82910609179b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Mon, 4 May 2020 15:13:04 +0200 Subject: [PATCH 1/9] Add MatchmakingViewController --- GoldWars/GoldWars/GameViewController.swift | 1 + GoldWars/GoldWars/MatchmakingHelper.swift | 117 +++++++++++++++++++++ GoldWars/GoldWars/Scenes/MenuScene.swift | 5 +- 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 GoldWars/GoldWars/MatchmakingHelper.swift diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index 4eed433..e4a2f43 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -24,6 +24,7 @@ class GameViewController: UIViewController { view.showsNodeCount = true GameCenterHelper.helper.viewController = self + MatchmakingHelper.sharedInstance.viewController = self } } diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift new file mode 100644 index 0000000..e38cc92 --- /dev/null +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -0,0 +1,117 @@ +// +// MatchmakingHelper.swift +// GoldWars +// +// Created by Chauntalle Schüle on 03.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import GameKit + +protocol GameKitHelperDelegate { + func matchStarted() + func matchEnded() + func matchReceivedData(match: GKMatch, data: NSData, + fromPlayer player: String) + +} + +class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDelegate { + + var delegate: GameKitHelperDelegate? //will receive all multiplayer events + var mpMatch: GKMatch? //represents network + var viewController: UIViewController? + var mpMatchStarted: Bool + let localPlayer: GKLocalPlayer = GKLocalPlayer.local + + static let sharedInstance = MatchmakingHelper() + + + static var isAuthenticated: Bool{ + return GKLocalPlayer.local.isAuthenticated + } + + override init() { + mpMatchStarted = 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) + } + + func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate){ + + guard GKLocalPlayer.local.isAuthenticated else { + print("Player ist nicht authentifiziert") + return + } + + let request = GKMatchRequest.init() + request.minPlayers = minPlayers + request.maxPlayers = maxPlayers + + let matchmakerVC = GKMatchmakerViewController.init(matchRequest: request) + matchmakerVC!.matchmakerDelegate = self + + viewController.present(matchmakerVC!, animated: true, completion: nil) + } + + //The user has cancelled matchmaking + func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { + viewController.dismiss(animated: true, completion: nil) + delegate?.matchEnded() + } + + //Matchmaking has failed with an error + func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { + viewController.dismiss(animated: true, completion: nil) + print("Error finding match", error.localizedDescription) + delegate?.matchEnded() + } + + // A peer-to-peer match has been found, the game should start + func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { + viewController.dismiss(animated: true, completion: nil) + mpMatch = match + match.delegate = self + //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() + } + } + private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { + if mpMatch != match { return } + delegate?.matchReceivedData(match: match, data: data, fromPlayer: + playerID) + } + + private func match(match: GKMatch!, didFailWithError error: NSError!) { + if mpMatch != match { + return + } + mpMatchStarted = false + delegate?.matchEnded() + } + + func startMatch(){ + + } +} diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 8164439..b4ff9db 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -21,7 +21,9 @@ class MenuScene: SKScene { text: "Start Game", position: CGPoint(x: midX, y: midY), onButtonPress: { - self.loadScene(scene: GameScene(size: self.size)) + MatchmakingHelper.sharedInstance.presentMatchmaker() + // self.loadScene(scene: GameScene(size: self.size)) + })) entityManager.add(Button(name: "settingsButton", iconName: "", @@ -29,6 +31,7 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { //TODO: create Settings Scene + MatchmakingHelper.sharedInstance.presentMatchmaker() })) entityManager.add(Background(size: self.size)) } From 017e99183ba5dfc70931fb0196d255c046ba1c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Mon, 4 May 2020 15:22:48 +0200 Subject: [PATCH 2/9] add File to Project Tree --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index d28b1c8..c24d770 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -42,6 +42,7 @@ AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */; }; AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; + AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE151588245F18EF001D363E /* MatchmakingHelper.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -95,6 +96,7 @@ AB1D759B245DD18100671525 /* TwoPlayerDefaultTestMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TwoPlayerDefaultTestMap.swift; sourceTree = ""; }; AB1D759F245DEC0500671525 /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; + AE151588245F18EF001D363E /* MatchmakingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatchmakingHelper.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -150,6 +152,7 @@ 110360DA244B101A008610AF /* GameViewController.swift */, 110360DF244B101B008610AF /* Assets.xcassets */, 110360E4244B101B008610AF /* Info.plist */, + AE151588245F18EF001D363E /* MatchmakingHelper.swift */, 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */, ); path = GoldWars; @@ -366,6 +369,7 @@ 9E78ACBA245CBDAF00526FF7 /* HUD.swift in Sources */, 11738A3B24508F68004426F1 /* Unit.swift in Sources */, 9E174C86245DD91500209FF0 /* ButtonComponent.swift in Sources */, + AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */, 11036113244B3E30008610AF /* MenuScene.swift in Sources */, 9EA3ABE9245C6DAA006BC61D /* DefaultBaseComponent.swift in Sources */, 9E174C8A245E1A0A00209FF0 /* Background.swift in Sources */, @@ -541,8 +545,8 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = GoldWars/GoldWars.entitlements; - CODE_SIGN_IDENTITY = "Apple Development"; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_IDENTITY = "iPhone Developer"; + CODE_SIGN_STYLE = Manual; CURRENT_PROJECT_VERSION = 2; DEVELOPMENT_TEAM = DDKFQG46BQ; INFOPLIST_FILE = GoldWars/Info.plist; @@ -553,7 +557,7 @@ MARKETING_VERSION = 1.0; PRODUCT_BUNDLE_IDENTIFIER = de.hft.stuttgart.ip2.goldwars; PRODUCT_NAME = "$(TARGET_NAME)"; - PROVISIONING_PROFILE_SPECIFIER = ""; + PROVISIONING_PROFILE_SPECIFIER = "Developer Profile"; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = 2; }; @@ -564,8 +568,8 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = GoldWars/GoldWars.entitlements; - CODE_SIGN_IDENTITY = "Apple Development"; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_IDENTITY = "iPhone Developer"; + CODE_SIGN_STYLE = Manual; CURRENT_PROJECT_VERSION = 2; DEVELOPMENT_TEAM = DDKFQG46BQ; INFOPLIST_FILE = GoldWars/Info.plist; @@ -576,7 +580,7 @@ MARKETING_VERSION = 1.0; PRODUCT_BUNDLE_IDENTIFIER = de.hft.stuttgart.ip2.goldwars; PRODUCT_NAME = "$(TARGET_NAME)"; - PROVISIONING_PROFILE_SPECIFIER = ""; + PROVISIONING_PROFILE_SPECIFIER = "Developer Profile"; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = 2; }; 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 3/9] 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", From e21395d0dbb79d82544e4d89e6b38530fcdf3163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Mon, 4 May 2020 21:44:50 +0200 Subject: [PATCH 4/9] Checken ob match gefunden wurde --- GoldWars/GoldWars/MatchmakingHelper.swift | 16 +++++++++++++--- GoldWars/GoldWars/Scenes/MenuScene.swift | 6 ++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index d546c0e..41b09ac 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -2,7 +2,7 @@ // MatchmakingHelper.swift // GoldWars // -// Created by Chauntalle Schüle on 03.05.20. +// Created by Chauntalle Schüle, Ömer Özel, Eray Kör on 03.05.20. // Copyright © 2020 SP2. All rights reserved. // @@ -40,6 +40,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } func presentMatchmaker() { + print("I'm in 43") guard GKLocalPlayer.local.isAuthenticated else { print("Player ist nicht authentifiziert") return @@ -74,12 +75,14 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe //The user has cancelled matchmaking func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { + print("I'm in 78") viewController.dismiss(animated: true, completion: nil) delegate?.matchEnded() } //Matchmaking has failed with an error func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { + print("I'm in 85") viewController.dismiss(animated: true, completion: nil) print("Error finding match", error.localizedDescription) delegate?.matchEnded() @@ -87,6 +90,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe // A peer-to-peer match has been found, the game should start func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { + print("I'm in 93") viewController.dismiss(animated: true, completion: nil) mpMatch = match match.delegate = self @@ -99,12 +103,14 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe // The match received data sent from the player. private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { + print("I'm in 106") if mpMatch != match { return } delegate?.matchReceivedData(match: match, data: data, fromPlayer: playerID) } private func match(match: GKMatch!, didFailWithError error: NSError!) { + print("I'm in 113") if mpMatch != match { return } @@ -114,6 +120,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe // The player state changed (eg. connected or disconnected) func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) { + print("I'm in 123") if mpMatch != match { return } @@ -129,7 +136,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe case GKPlayerConnectionState.disconnected: print("Player disconnected!") mpMatchStarted = false - delegate!.matchEnded() + delegate?.matchEnded() default: print("Player unknown status!") } @@ -138,7 +145,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe func startMatch() { mpMatch!.chooseBestHostingPlayer(completionHandler: { (player) in + print("I'm in 147") self.mpMatchStarted = true + print(self.mpMatchStarted) if player == GKLocalPlayer.local { print("I am the server") self.isServer = true @@ -148,11 +157,12 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe self.serverPlayer = player } - self.delegate!.matchStarted() + self.delegate?.matchStarted() }) } func disconnect() { + print("I'm in 164") if mpMatch != nil { mpMatch?.disconnect() } diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index b4cda64..e6b80a3 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -22,8 +22,11 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY), onButtonPress: { MatchmakingHelper.sharedInstance.presentMatchmaker() + print(MatchmakingHelper.sharedInstance.mpMatchStarted) + // if MatchmakingHelper.sharedInstance.mpMatchStarted{ self.loadScene(scene: GameScene(size: self.size)) - + print(MatchmakingHelper.sharedInstance.mpMatchStarted) + // } })) entityManager.add(Button(name: "settingsButton", iconName: "", @@ -31,7 +34,6 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY - 80 ), onButtonPress: { //TODO: create Settings Scene - MatchmakingHelper.sharedInstance.presentMatchmaker() })) entityManager.add(Background(size: self.size)) } From e1d5af8a4a01f5d83b7e1ce12616a3500641a48d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Mon, 4 May 2020 23:44:23 +0200 Subject: [PATCH 5/9] matchmaking funktioniert --- GoldWars/GoldWars/MatchmakingHelper.swift | 22 +++++++++++++--------- GoldWars/GoldWars/Scenes/MenuScene.swift | 16 +++++++++++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 41b09ac..8a5ef28 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -10,9 +10,9 @@ import GameKit protocol GameKitHelperDelegate { func matchStarted() - func matchEnded() - func matchReceivedData(match: GKMatch, data: NSData, - fromPlayer player: String) + // func matchEnded() + // func matchReceivedData(match: GKMatch, data: NSData, + // fromPlayer player: String) } @@ -24,6 +24,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe var mpMatchStarted: Bool var isServer: Bool var serverPlayer: GKPlayer? + var menusc: MenuScene? let localPlayer: GKLocalPlayer = GKLocalPlayer.local static let sharedInstance = MatchmakingHelper() @@ -40,6 +41,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } func presentMatchmaker() { + // menusc = scene print("I'm in 43") guard GKLocalPlayer.local.isAuthenticated else { print("Player ist nicht authentifiziert") @@ -77,7 +79,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { print("I'm in 78") viewController.dismiss(animated: true, completion: nil) - delegate?.matchEnded() + // delegate?.matchEnded() } //Matchmaking has failed with an error @@ -85,7 +87,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe print("I'm in 85") viewController.dismiss(animated: true, completion: nil) print("Error finding match", error.localizedDescription) - delegate?.matchEnded() + // delegate?.matchEnded() } // A peer-to-peer match has been found, the game should start @@ -104,9 +106,10 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe // The match received data sent from the player. private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { print("I'm in 106") + if mpMatch != match { return } - delegate?.matchReceivedData(match: match, data: data, fromPlayer: - playerID) + // delegate?.matchReceivedData(match: match, data: data, fromPlayer: + // playerID) } private func match(match: GKMatch!, didFailWithError error: NSError!) { @@ -115,7 +118,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe return } mpMatchStarted = false - delegate?.matchEnded() + //delegate?.matchEnded() } // The player state changed (eg. connected or disconnected) @@ -136,7 +139,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe case GKPlayerConnectionState.disconnected: print("Player disconnected!") mpMatchStarted = false - delegate?.matchEnded() + // delegate?.matchEnded() default: print("Player unknown status!") } @@ -158,6 +161,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe } self.delegate?.matchStarted() + // self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) }) } diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index e6b80a3..10c7a8b 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -22,11 +22,6 @@ class MenuScene: SKScene { position: CGPoint(x: midX, y: midY), onButtonPress: { MatchmakingHelper.sharedInstance.presentMatchmaker() - print(MatchmakingHelper.sharedInstance.mpMatchStarted) - // if MatchmakingHelper.sharedInstance.mpMatchStarted{ - self.loadScene(scene: GameScene(size: self.size)) - print(MatchmakingHelper.sharedInstance.mpMatchStarted) - // } })) entityManager.add(Button(name: "settingsButton", iconName: "", @@ -47,4 +42,15 @@ class MenuScene: SKScene { entityManager.getBackground()!.update(deltaTime: currentTime) entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated } + + +} + +extension MenuScene: GameKitHelperDelegate{ + + + func matchStarted() { + print("halo i bims") + self.loadScene(scene: GameScene(size: self.size)) + } } From b8b37de349f8402bb5005c91d1f43a2067f1309e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=88mer=20O=CC=88zel?= <72oeoe1bif> Date: Tue, 5 May 2020 00:35:27 +0200 Subject: [PATCH 6/9] Push MM --- GoldWars/GoldWars/MatchmakingHelper.swift | 13 ++++++++----- GoldWars/GoldWars/Scenes/MenuScene.swift | 11 ++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 8a5ef28..63f1c89 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -40,8 +40,8 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe super.init() } - func presentMatchmaker() { - // menusc = scene + func presentMatchmaker(scene: MenuScene) { + menusc = scene print("I'm in 43") guard GKLocalPlayer.local.isAuthenticated else { print("Player ist nicht authentifiziert") @@ -110,10 +110,11 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe if mpMatch != match { return } // delegate?.matchReceivedData(match: match, data: data, fromPlayer: // playerID) + } private func match(match: GKMatch!, didFailWithError error: NSError!) { - print("I'm in 113") + print("Cant connect to any other player") if mpMatch != match { return } @@ -160,8 +161,10 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe self.serverPlayer = player } - self.delegate?.matchStarted() - // self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) + /// self.delegate?.matchStarted() + print("spiel gefunden") + self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) + print("scene müsste gewechselt sein") }) } diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 10c7a8b..de73580 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -21,7 +21,9 @@ class MenuScene: SKScene { text: "Start Game", position: CGPoint(x: midX, y: midY), onButtonPress: { - MatchmakingHelper.sharedInstance.presentMatchmaker() + //let mmh = MatchmakingHelper() + //mmh.delegate = self + MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) })) entityManager.add(Button(name: "settingsButton", iconName: "", @@ -47,10 +49,9 @@ class MenuScene: SKScene { } extension MenuScene: GameKitHelperDelegate{ + func matchStarted() { + print("Spiel gestartet") + } - func matchStarted() { - print("halo i bims") - self.loadScene(scene: GameScene(size: self.size)) - } } From 3aef30b4959273dbc92cf1138ecb45844af04b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Tue, 5 May 2020 11:00:11 +0200 Subject: [PATCH 7/9] =?UTF-8?q?Kommentare=20eingefu=CC=88gt=20Spielerzuwei?= =?UTF-8?q?sung=20wird=20getestet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GoldWars/GoldWars/MatchmakingHelper.swift | 137 +++++++++++++--------- 1 file changed, 84 insertions(+), 53 deletions(-) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 63f1c89..0dcc5f1 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -10,20 +10,33 @@ import GameKit protocol GameKitHelperDelegate { func matchStarted() - // func matchEnded() - // func matchReceivedData(match: GKMatch, data: NSData, - // fromPlayer player: String) + func matchEnded() + func matchReceivedData(match: GKMatch, data: NSData, + fromPlayer player: String) } - +/* + Diese Klasse wird für das Matchmaking über das Gamecenter gebraucht + @param: delegate: erhält alle Multiplayerevents + @param: mpMatch: repräsentiert das Netzwerk, alle Mitspieler besitzen ein gemeinsames Spiel + @param: viewController: ist nicht der ViewController des Gamecenters, sondern des Spiels + @param: mpMatchStarted: gibt an, ob ein Match schon gestartet wurde + @param: isServer: gibt an, ob der local player, als der Server fungiert + @param: spieler1: ist immer der Spieler, der der Server ist + @param: spieler2: ist immer der Spieler, der der Client ist + @param: sharedInstance: ist eine geteilte Instanz für alle Spieler + */ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDelegate { - - var delegate: GameKitHelperDelegate? //will receive all multiplayer events - var mpMatch: GKMatch? //represents network + + var delegate: GameKitHelperDelegate? + var mpMatch: GKMatch? var viewController: UIViewController? var mpMatchStarted: Bool var isServer: Bool - var serverPlayer: GKPlayer? + var spieler1: GKPlayer? + var spieler2: GKPlayer? + var nameSpieler1 = "" + var nameSpieler2 = "" var menusc: MenuScene? let localPlayer: GKLocalPlayer = GKLocalPlayer.local @@ -40,6 +53,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe super.init() } + /* + Diese Methode wechselt von der MenuScene zum GameCenter ViewController. Die Spieler werden hier gesucht und gematcht. Mit minplayers/maxplayers kann bestimmt werden, wie viele Spieler insgesamt zusammen miteinander spielen. + */ func presentMatchmaker(scene: MenuScene) { menusc = scene print("I'm in 43") @@ -58,61 +74,56 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe viewController?.present(matchmakerVC!, animated: true, completion: nil) } - func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int, viewController: UIViewController, delegate: GameKitHelperDelegate){ - - guard GKLocalPlayer.local.isAuthenticated else { - print("Player ist nicht authentifiziert") - return - } - - let request = GKMatchRequest.init() - request.minPlayers = minPlayers - request.maxPlayers = maxPlayers - - let matchmakerVC = GKMatchmakerViewController.init(matchRequest: request) - matchmakerVC!.matchmakerDelegate = self - - viewController.present(matchmakerVC!, animated: true, completion: nil) - } - - //The user has cancelled matchmaking + /* + Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet. + */ func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { print("I'm in 78") viewController.dismiss(animated: true, completion: nil) - // delegate?.matchEnded() + // delegate?.matchEnded() } - //Matchmaking has failed with an error + /* + Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed. + */ func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { print("I'm in 85") viewController.dismiss(animated: true, completion: nil) print("Error finding match", error.localizedDescription) - // delegate?.matchEnded() + // delegate?.matchEnded() } - // A peer-to-peer match has been found, the game should start + /* + 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) { print("I'm in 93") viewController.dismiss(animated: true, completion: nil) mpMatch = match match.delegate = self - //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() } } - // The match received data sent from the player. + /* + Vom match erhaltene Spielerdaten + */ private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { print("I'm in 106") if mpMatch != match { return } - // delegate?.matchReceivedData(match: match, data: data, fromPlayer: + // delegate?.matchReceivedData(match: match, data: data, fromPlayer: // playerID) } - + + /* + Verbindung ist fehlgeschlagen + */ private func match(match: GKMatch!, didFailWithError error: NSError!) { print("Cant connect to any other player") if mpMatch != match { @@ -122,52 +133,72 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe //delegate?.matchEnded() } - // The player state changed (eg. connected or disconnected) + /* + Wird beim ändern des States/Zustands des Spielers aufgerufen udn gibt dessen Zustand zurück. + */ func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) { print("I'm in 123") 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.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() + print("Player disconnected!") + mpMatchStarted = false + // delegate?.matchEnded() default: - print("Player unknown status!") + print("Player unknown status!") } } + /* + Ein Spieler wird als Host für das Match gewählt. Dieser ist Spieler 1. Im Anschluss wird die GameScene geladen. + */ func startMatch() { + print("I'm in 166") + let p1 = mpMatch!.players[0] + let p2 = mpMatch!.players[2] + mpMatch!.chooseBestHostingPlayer(completionHandler: { (player) in - print("I'm in 147") + self.mpMatchStarted = true - print(self.mpMatchStarted) + if player == GKLocalPlayer.local { print("I am the server") self.isServer = true + self.spieler1 = player + self.nameSpieler1 = self.spieler1!.displayName + print("I'm in 180") } else { print("I am a client") self.isServer = false - self.serverPlayer = player + if (player == p1){ + self.spieler2 = p1 + }else{ + self.spieler2 = p2 + } + self.nameSpieler2 = self.spieler2!.displayName + print("I'm in 191") } - /// self.delegate?.matchStarted() - print("spiel gefunden") + self.delegate?.matchStarted() self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) - print("scene müsste gewechselt sein") }) } + + /* + Trennt die Verbindung vom Match + */ func disconnect() { print("I'm in 164") if mpMatch != nil { From d97b124b837d2e0c65969e32e6d94318a8c021d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Tue, 5 May 2020 11:44:33 +0200 Subject: [PATCH 8/9] cleaned up code --- GoldWars/GoldWars/MatchmakingHelper.swift | 52 +++++------------------ GoldWars/GoldWars/Scenes/MenuScene.swift | 12 ------ 2 files changed, 11 insertions(+), 53 deletions(-) diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 0dcc5f1..48628ce 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -34,9 +34,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe var mpMatchStarted: Bool var isServer: Bool var spieler1: GKPlayer? - var spieler2: GKPlayer? var nameSpieler1 = "" - var nameSpieler2 = "" var menusc: MenuScene? let localPlayer: GKLocalPlayer = GKLocalPlayer.local @@ -58,9 +56,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe */ func presentMatchmaker(scene: MenuScene) { menusc = scene - print("I'm in 43") guard GKLocalPlayer.local.isAuthenticated else { - print("Player ist nicht authentifiziert") return } let request = GKMatchRequest() @@ -78,19 +74,17 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe Der User hat die Verbindung mit "Abbrechen" unterbrochen. GameCenter MatchMaking wird beendet. */ func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { - print("I'm in 78") viewController.dismiss(animated: true, completion: nil) - // delegate?.matchEnded() + delegate?.matchEnded() } /* Wenn GameCenter kein match erstellen kann, wird der viewcontroller dismissed. */ func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFailWithError error: Error) { - print("I'm in 85") viewController.dismiss(animated: true, completion: nil) print("Error finding match", error.localizedDescription) - // delegate?.matchEnded() + delegate?.matchEnded() } /* @@ -99,12 +93,10 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe z.B 0 gibt an, dass keine weiteren Spieler benötigt werden um das Match zu starten */ func matchmakerViewController(_ viewController: GKMatchmakerViewController, didFind match: GKMatch) { - print("I'm in 93") viewController.dismiss(animated: true, completion: nil) mpMatch = match match.delegate = self if !mpMatchStarted && match.expectedPlayerCount == 0 { - print("Bereit das Spiel zu starten!") startMatch() } } @@ -113,49 +105,40 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe Vom match erhaltene Spielerdaten */ private func match(match: GKMatch!, didReceiveData data: NSData!,fromPlayer playerID: String!) { - print("I'm in 106") - if mpMatch != match { return } - // delegate?.matchReceivedData(match: match, data: data, fromPlayer: - // playerID) + delegate?.matchReceivedData(match: match, data: data, fromPlayer: playerID) } /* - Verbindung ist fehlgeschlagen + Verbindung/Matchmaking ist fehlgeschlagen */ private func match(match: GKMatch!, didFailWithError error: NSError!) { - print("Cant connect to any other player") if mpMatch != match { return } mpMatchStarted = false - //delegate?.matchEnded() + delegate?.matchEnded() } /* Wird beim ändern des States/Zustands des Spielers aufgerufen udn gibt dessen Zustand zurück. */ func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) { - print("I'm in 123") 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() + delegate?.matchEnded() default: - print("Player unknown status!") + delegate?.matchEnded() } } @@ -163,33 +146,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. */ func startMatch() { - print("I'm in 166") - let p1 = mpMatch!.players[0] - let p2 = mpMatch!.players[2] - + mpMatch!.chooseBestHostingPlayer(completionHandler: { (player) in self.mpMatchStarted = true if player == GKLocalPlayer.local { - print("I am the server") self.isServer = true self.spieler1 = player self.nameSpieler1 = self.spieler1!.displayName - print("I'm in 180") - } else { - print("I am a client") - self.isServer = false - if (player == p1){ - self.spieler2 = p1 - }else{ - self.spieler2 = p2 - } - self.nameSpieler2 = self.spieler2!.displayName - print("I'm in 191") + } else { + self.isServer = false } + self.delegate?.matchStarted() self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size)) }) @@ -200,7 +171,6 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe Trennt die Verbindung vom Match */ func disconnect() { - print("I'm in 164") if mpMatch != nil { mpMatch?.disconnect() } diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index de73580..6e1efe9 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -21,8 +21,6 @@ class MenuScene: SKScene { text: "Start Game", position: CGPoint(x: midX, y: midY), onButtonPress: { - //let mmh = MatchmakingHelper() - //mmh.delegate = self MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) })) entityManager.add(Button(name: "settingsButton", @@ -44,14 +42,4 @@ class MenuScene: SKScene { entityManager.getBackground()!.update(deltaTime: currentTime) entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated } - - -} - -extension MenuScene: GameKitHelperDelegate{ - func matchStarted() { - print("Spiel gestartet") - } - - } From 084b79a7c0acaf2d0d51ab23e8db19277e18dd17 Mon Sep 17 00:00:00 2001 From: Simon Kellner Date: Tue, 5 May 2020 18:06:21 +0200 Subject: [PATCH 9/9] Add flag(--no-matchmaking) to prevent matchmaking process and instantly open GameScene --- GoldWars/GoldWars/Scenes/MenuScene.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 6e1efe9..edf6bb9 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -21,7 +21,11 @@ class MenuScene: SKScene { text: "Start Game", position: CGPoint(x: midX, y: midY), onButtonPress: { + if CommandLine.arguments.contains("--no-matchmaking") { + self.loadScene(scene: GameScene(size: self.size)) + } else { MatchmakingHelper.sharedInstance.presentMatchmaker(scene: self) + } })) entityManager.add(Button(name: "settingsButton", iconName: "",