From a4239421824b1fe91c8e1ff5654fb685b702ea05 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sun, 31 May 2020 23:14:06 +0200 Subject: [PATCH] Send boosts with snapshot model, increase unit counts accordingly --- GoldWars/GoldWars/DataService.swift | 8 ++++--- GoldWars/GoldWars/Entities/Base.swift | 5 ++-- .../GoldWars/Entities/EntityManager.swift | 10 +++++++- .../GoldWars/RoundCalculatorService.swift | 24 +++++++++++-------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index f3a5591..93fba09 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -10,8 +10,6 @@ struct PlayerMove: Codable { let fromBase: Int let toBase: Int var unitCount: Int - let hasDefenceBoost: Bool - let hasAttackBoost: Bool } class SnapshotModel: Codable { @@ -26,11 +24,15 @@ class BaseEntityModel: Codable { let baseId: Int var unitCount: Int var ownership: String? + var hasAttackBoost: Bool + var hasDefenceBoost: Bool - init(baseId: Int, unitCount: Int, ownership: String?) { + init(baseId: Int, unitCount: Int, ownership: String?, hasAttackBoost: Bool, hasDefenceBoost: Bool) { self.baseId = baseId self.unitCount = unitCount self.ownership = ownership + self.hasAttackBoost = hasAttackBoost + self.hasDefenceBoost = hasDefenceBoost } } diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 2611b78..1306885 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -53,9 +53,8 @@ class Base: GKEntity{ base.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "\(base.unitCount)" DataService.sharedInstance.addMove(playerMove: PlayerMove(fromBase: self.baseID, toBase: base.baseID, - unitCount: units * playerMoveType.rawValue, - hasDefenceBoost: base.hasAttackBoost, - hasAttackBoost: base.hasDefenseBoost)) + unitCount: units * playerMoveType.rawValue) + ) return [self, base] } diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 6bf1d5b..1e44273 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -249,7 +249,15 @@ class EntityManager { for entity in bases { let base = entity as! Base - snapBase.append(BaseEntityModel(baseId: base.baseID, unitCount: base.unitCount, ownership: base.ownershipPlayer?.displayName)) + snapBase.append( + BaseEntityModel( + baseId: base.baseID, + unitCount: base.unitCount, + ownership: base.ownershipPlayer?.displayName, + hasAttackBoost: base.hasAttackBoost, + hasDefenceBoost: base.hasDefenseBoost + ) + ) } return SnapshotModel(baseEntites: snapBase) diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index 2a75942..60894d4 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -13,6 +13,9 @@ class RoundCalculatorService { static let sharedInstance = RoundCalculatorService() static let LOG = OSLog.init(subsystem: "Round Calculator", category: "RoundCalculatorService") + let ATK_BOOST_MULTIPLICATOR = 1.1 + let DEF_BOOST_MULTIPLICATOR = 1.1 + var entityManager = EntityManager.gameEMInstance var isCalculating = false @@ -62,37 +65,38 @@ class RoundCalculatorService { combinePotentionalForces[playerMoves.key] = PlayerMove( fromBase: playerMoves.value[0].fromBase, toBase: playerMoves.value[0].toBase, - unitCount: 0, - hasDefenceBoost: playerMoves.value[0].hasDefenceBoost, - hasAttackBoost: playerMoves.value[0].hasDefenceBoost + unitCount: 0 ) for move in playerMoves.value { combinePotentionalForces[playerMoves.key]!.unitCount += move.unitCount } } - if(combinePotentionalForces.count > 0) { + if combinePotentionalForces.count > 0 { let sortedPotentionalCombinedForces = combinePotentionalForces.sorted { $0.1.unitCount > $1.1.unitCount } var playerMoveWithMaxUnits = sortedPotentionalCombinedForces[0] + playerMoveWithMaxUnits.value.unitCount = Int(Double(playerMoveWithMaxUnits.value.unitCount) * ((currentSnapshotModel?.baseEntites[playerMoveWithMaxUnits.value.fromBase].hasAttackBoost)! ? ATK_BOOST_MULTIPLICATOR : 1)) if playerMovesByBase.count >= 2 { - let playerMoveWithSecMaxUnits = sortedPotentionalCombinedForces[1] - playerMoveWithMaxUnits.value.unitCount -= playerMoveWithSecMaxUnits.value.unitCount + var playerMoveWithSecMaxUnits = sortedPotentionalCombinedForces[1] + playerMoveWithSecMaxUnits.value.unitCount = Int(Double(playerMoveWithSecMaxUnits.value.unitCount) * ((currentSnapshotModel?.baseEntites[playerMoveWithSecMaxUnits.value.fromBase].hasAttackBoost)! ? ATK_BOOST_MULTIPLICATOR : 1)) + playerMoveWithMaxUnits.value.unitCount -= playerMoveWithSecMaxUnits.value.unitCount // Apply Boost } for base in currentSnapshotModel!.baseEntites { if base.baseId == playerMoveWithMaxUnits.value.toBase { + base.unitCount = Int(Double(base.unitCount) * (base.hasDefenceBoost ? DEF_BOOST_MULTIPLICATOR : 1)) if base.ownership == nil { - base.unitCount += playerMoveWithMaxUnits.value.unitCount + base.unitCount += playerMoveWithMaxUnits.value.unitCount // Apply Boost base.ownership = playerMoveWithMaxUnits.value.unitCount == 0 ? nil : playerMoveWithMaxUnits.key } else { if base.unitCount < playerMoveWithMaxUnits.value.unitCount { - base.unitCount = playerMoveWithMaxUnits.value.unitCount - base.unitCount + base.unitCount = playerMoveWithMaxUnits.value.unitCount - base.unitCount // Apply Boost base.ownership = playerMoveWithMaxUnits.key } else if (base.unitCount == playerMoveWithMaxUnits.value.unitCount) { base.ownership = nil } else { - base.unitCount -= playerMoveWithMaxUnits.value.unitCount + base.unitCount -= playerMoveWithMaxUnits.value.unitCount // Apply Boost } } } @@ -126,7 +130,7 @@ class RoundCalculatorService { for playerMove in allPlayerMoves { for move in playerMove.value { if baseSpecificMoves.keys.contains(move.toBase) { - if (baseSpecificMoves[move.toBase]?.keys.contains(playerMove.key))!{ + 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}