From 0d20cc6716e4b46ea78bc8c509665d17a009e61e Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sun, 31 May 2020 14:45:57 +0200 Subject: [PATCH] Collect baseSpecificMoves in separate function --- .../GoldWars/RoundCalculatorService.swift | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index 8e61759..d495681 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -13,8 +13,6 @@ class RoundCalculatorService { static let sharedInstance = RoundCalculatorService() static let LOG = OSLog.init(subsystem: "Round Calculator", category: "RoundCalculatorService") - var allPlayerMoves: [String: [PlayerMove]] = [:] - var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:] var entityManager = EntityManager.gameEMInstance var isCalculating = false @@ -27,21 +25,10 @@ class RoundCalculatorService { isCalculating = true let currentSnapshotModel = DataService.sharedInstance.snapshotModel - for playerMove in DataService.sharedInstance.remotePlayerMoves { - addPlayerMove(playerName: playerMove.key, playerMoves: playerMove.value) - } - addPlayerMove(playerName: GKLocalPlayer.local.displayName, playerMoves: DataService.sharedInstance.localPlayerMoves) - - for playerMove in allPlayerMoves { - for move in playerMove.value { - mapPlayerMoveToAttackedBase(playerName: playerMove.key, playerMove: move) - } - } + var baseSpecificMoves = collectBaseSpecificMoves() // TODO: Refactor to a less complex way - for (key, value) in baseSpecificMoves { - let baseId = key - var playerMovesByBase = value + for (baseId, var playerMovesByBase) in baseSpecificMoves { let targetBase = currentSnapshotModel?.baseEntites.filter { $0.baseId == baseId }[0] let possiblyOwnershipMoves = playerMovesByBase.filter { $0.key == targetBase?.ownership} @@ -58,7 +45,7 @@ class RoundCalculatorService { } playerMovesByBase.removeValue(forKey: playerName) } - + for (_, playerMoves) in playerMovesByBase { for playerMove in playerMoves { for base in currentSnapshotModel!.baseEntites { @@ -110,7 +97,6 @@ class RoundCalculatorService { } baseSpecificMoves.removeValue(forKey: baseId) } - allPlayerMoves.removeAll() DataService.sharedInstance.localPlayerMoves.removeAll() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() DataService.sharedInstance.snapshotModel = currentSnapshotModel @@ -120,20 +106,35 @@ class RoundCalculatorService { os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) } - func addPlayerMove(playerName: String, playerMoves: [PlayerMove]) { - self.allPlayerMoves[playerName] = playerMoves - } - - func mapPlayerMoveToAttackedBase(playerName: String, playerMove: PlayerMove) { - if self.baseSpecificMoves.keys.contains(playerMove.toBase) { - if (self.baseSpecificMoves[playerMove.toBase]?.keys.contains(playerName))!{ - self.baseSpecificMoves[playerMove.toBase]?[playerName]?.append(playerMove) - } else { - self.baseSpecificMoves[playerMove.toBase]?.merge([playerName: [playerMove]]){(current, _) in current} - } - } else { - self.baseSpecificMoves[playerMove.toBase] = [playerName: [playerMove]] + func collectBaseSpecificMoves() -> [Int: [String: [PlayerMove]]] { + + var allPlayerMoves: [String: [PlayerMove]] = [:] + + // collect moves from remote player + for playerMove in DataService.sharedInstance.remotePlayerMoves { + allPlayerMoves[playerMove.key] = playerMove.value } + + // collect moves from local player + allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localPlayerMoves + + var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:] + + for playerMove in allPlayerMoves { + for move in playerMove.value { + if baseSpecificMoves.keys.contains(move.toBase) { + if (baseSpecificMoves[move.toBase]?.keys.contains(playerMove.key))!{ + baseSpecificMoves[move.toBase]?[playerMove.key]?.append(move) + } else { + baseSpecificMoves[move.toBase]?.merge([playerMove.key: [move]]){(current, _) in current} + } + } else { + baseSpecificMoves[move.toBase] = [playerMove.key: [move]] + } + } + } + + return baseSpecificMoves } func resolvePlayerMove(playerMove: PlayerMove, unitCount: Int, ownership: String?, resolveType: String) {