diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index 894a16e..5ad2695 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -48,10 +48,10 @@ class TimerComponent: GKComponent { if !MultiplayerNetwork.sharedInstance.isSending { MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) } - if DataService.sharedInstance.didReceiveAllData() { - RoundCalculatorServie.sharedInstance.calculateRound() + if !RoundCalculatorServie.sharedInstance.isCalculating + && DataService.sharedInstance.didReceiveAllData(){ + RoundCalculatorServie.sharedInstance.calculateRound() } - } } diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index 273b2b0..2032a93 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -13,24 +13,28 @@ import os class RoundCalculatorServie { static let sharedInstance = RoundCalculatorServie() + var allPlayerMoves: [String: [PlayerMove]] = [:] var baseSpecificMove: [Int: [(String, PlayerMove)]] = [:] + var isCalculating = false func calculateRound() { + isCalculating = true for entry in DataService.sharedInstance.remotePlayerMoves { addPlayerMove(playerID: entry.key, playerMoves: entry.value) } addPlayerMove(playerID: GKLocalPlayer.local.displayName, playerMoves: DataService.sharedInstance.localPlayerMoves) - // berechnen nach minimal substraction - for entry in allPlayerMoves { for move in entry.value { addFiltedMove(playerName: entry.key, playerMove: move) } } + // berechnen nach minimal substraction + print(baseSpecificMove) + isCalculating = false // sende an alle anderen spieler die moves } @@ -39,6 +43,6 @@ class RoundCalculatorServie { } func addFiltedMove(playerName: String, playerMove: PlayerMove) { - self.baseSpecificMove[playerMove.toBase]!.append((playerName, playerMove)) + self.baseSpecificMove.merge([playerMove.toBase : [(playerName, playerMove)]], uniquingKeysWith: +) } }