178 lines
5.7 KiB
Swift
178 lines
5.7 KiB
Swift
//
|
|
// MatchmakingHelper.swift
|
|
// GoldWars
|
|
//
|
|
// Created by Chauntalle Schüle, Ömer Özel, Eray Kör 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
|
|
var isServer: Bool
|
|
var serverPlayer: GKPlayer?
|
|
var menusc: MenuScene?
|
|
let localPlayer: GKLocalPlayer = GKLocalPlayer.local
|
|
|
|
static let sharedInstance = MatchmakingHelper()
|
|
|
|
|
|
static var isAuthenticated: Bool{
|
|
return GKLocalPlayer.local.isAuthenticated
|
|
}
|
|
|
|
override init() {
|
|
mpMatchStarted = false
|
|
isServer = false
|
|
super.init()
|
|
}
|
|
|
|
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()
|
|
|
|
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){
|
|
|
|
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) {
|
|
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()
|
|
}
|
|
|
|
// 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
|
|
//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.
|
|
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("Cant connect to any other player")
|
|
if mpMatch != match {
|
|
return
|
|
}
|
|
mpMatchStarted = false
|
|
//delegate?.matchEnded()
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
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
|
|
print("I'm in 147")
|
|
self.mpMatchStarted = true
|
|
print(self.mpMatchStarted)
|
|
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()
|
|
print("spiel gefunden")
|
|
self.menusc!.loadScene(scene: GameScene(size: self.menusc!.size))
|
|
print("scene müsste gewechselt sein")
|
|
})
|
|
}
|
|
|
|
func disconnect() {
|
|
print("I'm in 164")
|
|
if mpMatch != nil {
|
|
mpMatch?.disconnect()
|
|
}
|
|
}
|
|
}
|