Collect baseSpecificMoves in separate function

This commit is contained in:
Marcel Schwarz 2020-05-31 14:45:57 +02:00
parent ad861f63c4
commit 0d20cc6716

View File

@ -13,8 +13,6 @@ class RoundCalculatorService {
static let sharedInstance = RoundCalculatorService() static let sharedInstance = RoundCalculatorService()
static let LOG = OSLog.init(subsystem: "Round Calculator", category: "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 entityManager = EntityManager.gameEMInstance
var isCalculating = false var isCalculating = false
@ -27,21 +25,10 @@ class RoundCalculatorService {
isCalculating = true isCalculating = true
let currentSnapshotModel = DataService.sharedInstance.snapshotModel let currentSnapshotModel = DataService.sharedInstance.snapshotModel
for playerMove in DataService.sharedInstance.remotePlayerMoves { var baseSpecificMoves = collectBaseSpecificMoves()
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)
}
}
// TODO: Refactor to a less complex way // TODO: Refactor to a less complex way
for (key, value) in baseSpecificMoves { for (baseId, var playerMovesByBase) in baseSpecificMoves {
let baseId = key
var playerMovesByBase = value
let targetBase = currentSnapshotModel?.baseEntites.filter { $0.baseId == baseId }[0] let targetBase = currentSnapshotModel?.baseEntites.filter { $0.baseId == baseId }[0]
let possiblyOwnershipMoves = playerMovesByBase.filter { $0.key == targetBase?.ownership} let possiblyOwnershipMoves = playerMovesByBase.filter { $0.key == targetBase?.ownership}
@ -110,7 +97,6 @@ class RoundCalculatorService {
} }
baseSpecificMoves.removeValue(forKey: baseId) baseSpecificMoves.removeValue(forKey: baseId)
} }
allPlayerMoves.removeAll()
DataService.sharedInstance.localPlayerMoves.removeAll() DataService.sharedInstance.localPlayerMoves.removeAll()
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
DataService.sharedInstance.snapshotModel = currentSnapshotModel DataService.sharedInstance.snapshotModel = currentSnapshotModel
@ -120,20 +106,35 @@ class RoundCalculatorService {
os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info)
} }
func addPlayerMove(playerName: String, playerMoves: [PlayerMove]) { func collectBaseSpecificMoves() -> [Int: [String: [PlayerMove]]] {
self.allPlayerMoves[playerName] = playerMoves
}
func mapPlayerMoveToAttackedBase(playerName: String, playerMove: PlayerMove) { var allPlayerMoves: [String: [PlayerMove]] = [:]
if self.baseSpecificMoves.keys.contains(playerMove.toBase) {
if (self.baseSpecificMoves[playerMove.toBase]?.keys.contains(playerName))!{ // collect moves from remote player
self.baseSpecificMoves[playerMove.toBase]?[playerName]?.append(playerMove) for playerMove in DataService.sharedInstance.remotePlayerMoves {
} else { allPlayerMoves[playerMove.key] = playerMove.value
self.baseSpecificMoves[playerMove.toBase]?.merge([playerName: [playerMove]]){(current, _) in current}
}
} else {
self.baseSpecificMoves[playerMove.toBase] = [playerName: [playerMove]]
} }
// 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) { func resolvePlayerMove(playerMove: PlayerMove, unitCount: Int, ownership: String?, resolveType: String) {