Send boosts with snapshot model, increase unit counts accordingly

This commit is contained in:
Marcel Schwarz 2020-05-31 23:14:06 +02:00
parent 6b1d0eac12
commit a423942182
4 changed files with 30 additions and 17 deletions

View File

@ -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
}
}

View File

@ -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]
}

View File

@ -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)

View File

@ -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
}
}
}