From da4b5cda63389c205166c16b541e0a82bd11b051 Mon Sep 17 00:00:00 2001 From: Aldin Duraki Date: Thu, 25 Jun 2020 20:52:48 +0200 Subject: [PATCH] Impl. Heartbeat and DC check --- GoldWars/GoldWars/DataService.swift | 4 ++++ GoldWars/GoldWars/GameCenterManager.swift | 20 +++++++++++++++++++- GoldWars/GoldWars/MultiplayerNetwork.swift | 6 ++++++ GoldWars/GoldWars/RoundTimer.swift | 6 +++++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index 2e09c43..4c5f63a 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -5,7 +5,11 @@ // Created by Tim Herbst on 13.05.20. // Copyright © 2020 SP2. All rights reserved. // +import Foundation +struct Heartbeat: Codable { + var date: Date +} struct NotificationModel: Codable { let name: String diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index 97f4b03..0b7a26e 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -28,7 +28,7 @@ struct State: Codable { let state: Int } -final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKGameCenterControllerDelegate ,GKMatchDelegate,GKLocalPlayerListener{ +final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKGameCenterControllerDelegate, GKMatchDelegate, GKLocalPlayerListener{ static var sharedInstance = GameCenterManager() @@ -219,9 +219,25 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG os_log("New Scores reported to EloSystem", log: self.LOG, type: .info) }) } + if let heartbeat = try? jsonDecoder.decode(Heartbeat.self, from: data) { + entityManager.getHUD()?.roundTimer.isHeartbeatLocked = false + let df = DateFormatter() + df.dateFormat = "yyyy-MM-dd HH:mm:ss" + let dateString = df.string(from: heartbeat.date) + print("Received last Heartbeat at \(dateString)") + } + MultiplayerNetwork.sharedInstance.isSending = false } + func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState) { + if myMatch != match { return } + if state == GKPlayerConnectionState.disconnected { + self.opponentQuit = true; + gameScene?.gameQuit() + } + } + func initAndSendMap() -> Void { self.gameScene = GameScene(size: self.menusc!.size) let mapModel = MapFactory(scene: self.gameScene!, entityManager: entityManager).load() @@ -246,6 +262,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG } return nilGK } + func sendStateToPeers(state: State){ let encoder = JSONEncoder() let encoded = (try? encoder.encode(state))! @@ -264,6 +281,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG matchmakerVC!.matchmakerDelegate = self viewController?.present(matchmakerVC!, animated: true, completion: nil) } + func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { viewController.dismiss(animated: true, completion: nil) } diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index 596fd1f..07cc2a8 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -71,4 +71,10 @@ class MultiplayerNetwork{ let encoded = (try? encoder.encode(NotificationModel(name: name)))! sendData(data: encoded) } + + func sendHeartbeatToPlayer() { + let encoder = JSONEncoder() + let encoded = (try? encoder.encode(Heartbeat(date: Date())))! + sendData(data: encoded) + } } diff --git a/GoldWars/GoldWars/RoundTimer.swift b/GoldWars/GoldWars/RoundTimer.swift index c8bc801..666eb7a 100644 --- a/GoldWars/GoldWars/RoundTimer.swift +++ b/GoldWars/GoldWars/RoundTimer.swift @@ -12,6 +12,7 @@ class RoundTimer: Timer { var timer: Timer? var timeLeft: Int = 0 + var isHeartbeatLocked = false var calculate = false var roundEnded = "Syncing" @@ -69,6 +70,9 @@ class RoundTimer: Timer { } } - + if (!isHeartbeatLocked && (timeLeft % 7 == 0)){ + MultiplayerNetwork.sharedInstance.sendHeartbeatToPlayer() + isHeartbeatLocked = true; + } } }