diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index c91785c..3d2579c 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -13,6 +13,7 @@ class TimerComponent: GKComponent { let labelNode :SKLabelNode var endTime :Date! var duration :Double + var isRunning = false init(text: String, anchorPoint: CGPoint, duration: TimeInterval) { self.labelNode = SKLabelNode(text: text) @@ -25,16 +26,24 @@ class TimerComponent: GKComponent { } func startWithDuration(duration: TimeInterval){ + isRunning = true endTime = Date().addingTimeInterval(duration) RoundCalculatorService.sharedInstance.isCalculating = false } func timeLeft() -> Int { - let remainingSeconds = Int(endTime.timeIntervalSince(Date())) - if(remainingSeconds < 0 && DataService.sharedInstance.didReceiveAllData()){ - startWithDuration(duration: duration) + if isRunning { + let remainingSeconds = Int(endTime.timeIntervalSince(Date())) + if(remainingSeconds == 0) { + isRunning = false + } + return remainingSeconds } - return remainingSeconds + + // if(remainingSeconds < 0){ + // startWithDuration(duration: duration) + // } + return 0 } func isFinished() -> Bool { diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 264a064..f18dcf4 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -259,4 +259,8 @@ class EntityManager { return SnapshotModel(baseEntites: snapBase) } + + func getTimer() -> TimerComponent { + return entities.filter{$0 is HUD}[0].component(ofType: TimerComponent.self)! + } } diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 5e09ed6..60dc5d2 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -120,6 +120,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { DataService.sharedInstance.snapshotModel = snapshotModel EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel) + EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31) } if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index fb403ac..b9186c6 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -111,6 +111,8 @@ class RoundCalculatorService { MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() DataService.sharedInstance.snapshotModel = currentSnapshotModel EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel!) + sleep(1) + EntityManager.sharedInstance.getTimer().startWithDuration(duration: 31) os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) }