Impl. Heartbeat and DC check

This commit is contained in:
Aldin Duraki 2020-06-25 20:52:48 +02:00
parent 6cfd91b659
commit da4b5cda63
4 changed files with 34 additions and 2 deletions

View File

@ -5,7 +5,11 @@
// Created by Tim Herbst on 13.05.20. // Created by Tim Herbst on 13.05.20.
// Copyright © 2020 SP2. All rights reserved. // Copyright © 2020 SP2. All rights reserved.
// //
import Foundation
struct Heartbeat: Codable {
var date: Date
}
struct NotificationModel: Codable { struct NotificationModel: Codable {
let name: String let name: String

View File

@ -28,7 +28,7 @@ struct State: Codable {
let state: Int let state: Int
} }
final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKGameCenterControllerDelegate ,GKMatchDelegate,GKLocalPlayerListener{ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKGameCenterControllerDelegate, GKMatchDelegate, GKLocalPlayerListener{
static var sharedInstance = GameCenterManager() 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) 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 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 { func initAndSendMap() -> Void {
self.gameScene = GameScene(size: self.menusc!.size) self.gameScene = GameScene(size: self.menusc!.size)
let mapModel = MapFactory(scene: self.gameScene!, entityManager: entityManager).load() let mapModel = MapFactory(scene: self.gameScene!, entityManager: entityManager).load()
@ -246,6 +262,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
} }
return nilGK return nilGK
} }
func sendStateToPeers(state: State){ func sendStateToPeers(state: State){
let encoder = JSONEncoder() let encoder = JSONEncoder()
let encoded = (try? encoder.encode(state))! let encoded = (try? encoder.encode(state))!
@ -264,6 +281,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
matchmakerVC!.matchmakerDelegate = self matchmakerVC!.matchmakerDelegate = self
viewController?.present(matchmakerVC!, animated: true, completion: nil) viewController?.present(matchmakerVC!, animated: true, completion: nil)
} }
func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) { func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController) {
viewController.dismiss(animated: true, completion: nil) viewController.dismiss(animated: true, completion: nil)
} }

View File

@ -71,4 +71,10 @@ class MultiplayerNetwork{
let encoded = (try? encoder.encode(NotificationModel(name: name)))! let encoded = (try? encoder.encode(NotificationModel(name: name)))!
sendData(data: encoded) sendData(data: encoded)
} }
func sendHeartbeatToPlayer() {
let encoder = JSONEncoder()
let encoded = (try? encoder.encode(Heartbeat(date: Date())))!
sendData(data: encoded)
}
} }

View File

@ -12,6 +12,7 @@ class RoundTimer: Timer {
var timer: Timer? var timer: Timer?
var timeLeft: Int = 0 var timeLeft: Int = 0
var isHeartbeatLocked = false
var calculate = false var calculate = false
var roundEnded = "Syncing" var roundEnded = "Syncing"
@ -69,6 +70,9 @@ class RoundTimer: Timer {
} }
} }
if (!isHeartbeatLocked && (timeLeft % 7 == 0)){
MultiplayerNetwork.sharedInstance.sendHeartbeatToPlayer()
isHeartbeatLocked = true;
}
} }
} }