Add localRoundData
Send boosts
This commit is contained in:
parent
a423942182
commit
4c9d83c7cb
@ -57,7 +57,7 @@ class TimerComponent: GKComponent {
|
|||||||
self.labelNode.text = "Synching"
|
self.labelNode.text = "Synching"
|
||||||
RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats()
|
RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats()
|
||||||
if !MultiplayerNetwork.sharedInstance.isSending {
|
if !MultiplayerNetwork.sharedInstance.isSending {
|
||||||
MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves)
|
MultiplayerNetwork.sharedInstance.sendPlayerMoves(localRoundData: DataService.sharedInstance.localRoundData!)
|
||||||
}
|
}
|
||||||
if !RoundCalculatorService.sharedInstance.isCalculating
|
if !RoundCalculatorService.sharedInstance.isCalculating
|
||||||
&& DataService.sharedInstance.didReceiveAllData()
|
&& DataService.sharedInstance.didReceiveAllData()
|
||||||
|
@ -12,6 +12,12 @@ struct PlayerMove: Codable {
|
|||||||
var unitCount: Int
|
var unitCount: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct LocalRoundData: Codable {
|
||||||
|
var localPlayerMoves: [PlayerMove]
|
||||||
|
var hasAttackBoost: Bool
|
||||||
|
var hasDefenceBoost: Bool
|
||||||
|
}
|
||||||
|
|
||||||
class SnapshotModel: Codable {
|
class SnapshotModel: Codable {
|
||||||
var baseEntites: [BaseEntityModel]
|
var baseEntites: [BaseEntityModel]
|
||||||
|
|
||||||
@ -24,22 +30,18 @@ class BaseEntityModel: Codable {
|
|||||||
let baseId: Int
|
let baseId: Int
|
||||||
var unitCount: Int
|
var unitCount: Int
|
||||||
var ownership: String?
|
var ownership: String?
|
||||||
var hasAttackBoost: Bool
|
|
||||||
var hasDefenceBoost: Bool
|
|
||||||
|
|
||||||
init(baseId: Int, unitCount: Int, ownership: String?, hasAttackBoost: Bool, hasDefenceBoost: Bool) {
|
init(baseId: Int, unitCount: Int, ownership: String?, hasAttackBoost: Bool, hasDefenceBoost: Bool) {
|
||||||
self.baseId = baseId
|
self.baseId = baseId
|
||||||
self.unitCount = unitCount
|
self.unitCount = unitCount
|
||||||
self.ownership = ownership
|
self.ownership = ownership
|
||||||
self.hasAttackBoost = hasAttackBoost
|
|
||||||
self.hasDefenceBoost = hasDefenceBoost
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DataService {
|
class DataService {
|
||||||
static let sharedInstance = DataService()
|
static let sharedInstance = DataService()
|
||||||
var localPlayerMoves: [PlayerMove] = []
|
var localRoundData: LocalRoundData?
|
||||||
var remotePlayerMoves: [String: [PlayerMove]] = [:]
|
var remotePlayerMoves: [String: LocalRoundData] = [:]
|
||||||
var snapshotModel: SnapshotModel?
|
var snapshotModel: SnapshotModel?
|
||||||
var hostingPlayer = GameCenterManager.sharedInstance.hostingPlayer
|
var hostingPlayer = GameCenterManager.sharedInstance.hostingPlayer
|
||||||
|
|
||||||
@ -47,18 +49,18 @@ class DataService {
|
|||||||
var entityManager = EntityManager.gameEMInstance
|
var entityManager = EntityManager.gameEMInstance
|
||||||
|
|
||||||
func addMove(playerMove: PlayerMove) {
|
func addMove(playerMove: PlayerMove) {
|
||||||
var equalMove = localPlayerMoves.filter { (ele) -> Bool in
|
var equalMove = localRoundData?.localPlayerMoves.filter { (ele) -> Bool in
|
||||||
ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase
|
ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase
|
||||||
}
|
}
|
||||||
if equalMove.count == 1 {
|
if equalMove!.count == 1 {
|
||||||
equalMove[0].unitCount = Int(equalMove[0].unitCount) + Int(playerMove.unitCount)
|
equalMove![0].unitCount = Int(equalMove![0].unitCount) + Int(playerMove.unitCount)
|
||||||
} else {
|
} else {
|
||||||
self.localPlayerMoves.append(playerMove)
|
self.localRoundData?.localPlayerMoves.append(playerMove)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func addRemotePlayerMoves(playerName: String, playerMoves: [PlayerMove]) {
|
func addRemotePlayerMoves(playerName: String, localRoundData: LocalRoundData) {
|
||||||
self.remotePlayerMoves[playerName] = playerMoves
|
self.remotePlayerMoves[playerName] = localRoundData
|
||||||
}
|
}
|
||||||
|
|
||||||
func didReceiveAllData() -> Bool {
|
func didReceiveAllData() -> Bool {
|
||||||
|
@ -44,14 +44,14 @@ class HUD: GKEntity {
|
|||||||
text: "Def",
|
text: "Def",
|
||||||
isEnabled: true,
|
isEnabled: true,
|
||||||
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.85, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
|
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.85, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
|
||||||
onButtonPress: {EntityManager.gameEMInstance.getBasesByTeam(for: .team2).forEach({base in base.hasDefenseBoost = true})}
|
onButtonPress: {DataService.sharedInstance.localRoundData?.hasDefenceBoost = true}
|
||||||
)
|
)
|
||||||
atkSkill = SingeClickButtonNode(
|
atkSkill = SingeClickButtonNode(
|
||||||
textureName: "yellow_circle",
|
textureName: "yellow_circle",
|
||||||
text: "Atk",
|
text: "Atk",
|
||||||
isEnabled: true,
|
isEnabled: true,
|
||||||
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.95, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
|
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.95, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
|
||||||
onButtonPress: {EntityManager.gameEMInstance.getBasesByTeam(for: .team2).forEach({base in base.hasAttackBoost = true})}
|
onButtonPress: {DataService.sharedInstance.localRoundData?.hasAttackBoost = true}
|
||||||
)
|
)
|
||||||
super.init()
|
super.init()
|
||||||
hostLabel.position = CGPoint(x: size.width * 0.02, y: size.height * 0.95)
|
hostLabel.position = CGPoint(x: size.width * 0.02, y: size.height * 0.95)
|
||||||
|
@ -122,8 +122,8 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let playerMoves = try? jsonDecoder.decode([PlayerMove].self, from: data) {
|
if let roundData = try? jsonDecoder.decode(LocalRoundData.self, from: data) {
|
||||||
DataService.sharedInstance.addRemotePlayerMoves(playerName: player.displayName, playerMoves: playerMoves)
|
DataService.sharedInstance.addRemotePlayerMoves(playerName: player.displayName, localRoundData: roundData)
|
||||||
}
|
}
|
||||||
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
|
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
|
||||||
DataService.sharedInstance.snapshotModel = snapshotModel
|
DataService.sharedInstance.snapshotModel = snapshotModel
|
||||||
|
@ -36,13 +36,15 @@ class MultiplayerNetwork{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendPlayerMoves(playerMoves: [PlayerMove]) {
|
func sendPlayerMoves(localRoundData: LocalRoundData) {
|
||||||
if GameCenterManager.sharedInstance.isServer == false {
|
if GameCenterManager.sharedInstance.isServer == false {
|
||||||
self.isSending = true
|
self.isSending = true
|
||||||
let encoder = JSONEncoder()
|
let encoder = JSONEncoder()
|
||||||
let encoded = (try? encoder.encode(playerMoves))!
|
let encoded = (try? encoder.encode(localRoundData))!
|
||||||
sendDataToHost(data: encoded)
|
sendDataToHost(data: encoded)
|
||||||
DataService.sharedInstance.localPlayerMoves.removeAll()
|
DataService.sharedInstance.localRoundData!.localPlayerMoves.removeAll()
|
||||||
|
DataService.sharedInstance.localRoundData!.hasAttackBoost = false
|
||||||
|
DataService.sharedInstance.localRoundData!.hasDefenceBoost = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,9 @@ class RoundCalculatorService {
|
|||||||
let ATK_BOOST_MULTIPLICATOR = 1.1
|
let ATK_BOOST_MULTIPLICATOR = 1.1
|
||||||
let DEF_BOOST_MULTIPLICATOR = 1.1
|
let DEF_BOOST_MULTIPLICATOR = 1.1
|
||||||
|
|
||||||
|
// TODO: Better data structure
|
||||||
|
var boosts: [String: (Bool, Bool)] = [:] // First bool is atk boost, second is def boost
|
||||||
|
|
||||||
var entityManager = EntityManager.gameEMInstance
|
var entityManager = EntityManager.gameEMInstance
|
||||||
|
|
||||||
var isCalculating = false
|
var isCalculating = false
|
||||||
@ -76,27 +79,35 @@ class RoundCalculatorService {
|
|||||||
let sortedPotentionalCombinedForces = combinePotentionalForces.sorted { $0.1.unitCount > $1.1.unitCount }
|
let sortedPotentionalCombinedForces = combinePotentionalForces.sorted { $0.1.unitCount > $1.1.unitCount }
|
||||||
|
|
||||||
var playerMoveWithMaxUnits = sortedPotentionalCombinedForces[0]
|
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 {
|
if playerMovesByBase.count >= 2 {
|
||||||
var playerMoveWithSecMaxUnits = sortedPotentionalCombinedForces[1]
|
var playerMoveWithSecMaxUnits = sortedPotentionalCombinedForces[1]
|
||||||
playerMoveWithSecMaxUnits.value.unitCount = Int(Double(playerMoveWithSecMaxUnits.value.unitCount) * ((currentSnapshotModel?.baseEntites[playerMoveWithSecMaxUnits.value.fromBase].hasAttackBoost)! ? ATK_BOOST_MULTIPLICATOR : 1))
|
if boosts[playerMoveWithMaxUnits.key]!.0 {
|
||||||
playerMoveWithMaxUnits.value.unitCount -= playerMoveWithSecMaxUnits.value.unitCount // Apply Boost
|
playerMoveWithMaxUnits.value.unitCount = Int(Double(playerMoveWithMaxUnits.value.unitCount) * ATK_BOOST_MULTIPLICATOR)
|
||||||
|
}
|
||||||
|
if boosts[playerMoveWithSecMaxUnits.key]!.0 {
|
||||||
|
playerMoveWithSecMaxUnits.value.unitCount = Int(Double(playerMoveWithSecMaxUnits.value.unitCount) * ATK_BOOST_MULTIPLICATOR)
|
||||||
|
}
|
||||||
|
playerMoveWithMaxUnits.value.unitCount -= playerMoveWithSecMaxUnits.value.unitCount
|
||||||
}
|
}
|
||||||
|
|
||||||
for base in currentSnapshotModel!.baseEntites {
|
for base in currentSnapshotModel!.baseEntites {
|
||||||
if base.baseId == playerMoveWithMaxUnits.value.toBase {
|
if base.baseId == playerMoveWithMaxUnits.value.toBase {
|
||||||
base.unitCount = Int(Double(base.unitCount) * (base.hasDefenceBoost ? DEF_BOOST_MULTIPLICATOR : 1))
|
|
||||||
if base.ownership == nil {
|
if base.ownership == nil {
|
||||||
base.unitCount += playerMoveWithMaxUnits.value.unitCount // Apply Boost
|
base.unitCount += playerMoveWithMaxUnits.value.unitCount
|
||||||
base.ownership = playerMoveWithMaxUnits.value.unitCount == 0 ? nil : playerMoveWithMaxUnits.key
|
base.ownership = playerMoveWithMaxUnits.value.unitCount == 0 ? nil : playerMoveWithMaxUnits.key
|
||||||
} else {
|
} else {
|
||||||
|
if boosts[base.ownership!]!.1 {
|
||||||
|
base.unitCount = Int(Double(base.unitCount) * DEF_BOOST_MULTIPLICATOR)
|
||||||
|
}
|
||||||
if base.unitCount < playerMoveWithMaxUnits.value.unitCount {
|
if base.unitCount < playerMoveWithMaxUnits.value.unitCount {
|
||||||
base.unitCount = playerMoveWithMaxUnits.value.unitCount - base.unitCount // Apply Boost
|
base.unitCount = playerMoveWithMaxUnits.value.unitCount - base.unitCount
|
||||||
base.ownership = playerMoveWithMaxUnits.key
|
base.ownership = playerMoveWithMaxUnits.key
|
||||||
} else if (base.unitCount == playerMoveWithMaxUnits.value.unitCount) {
|
} else if (base.unitCount == playerMoveWithMaxUnits.value.unitCount) {
|
||||||
base.ownership = nil
|
base.ownership = nil
|
||||||
|
base.unitCount = 0
|
||||||
} else {
|
} else {
|
||||||
base.unitCount -= playerMoveWithMaxUnits.value.unitCount // Apply Boost
|
base.unitCount -= playerMoveWithMaxUnits.value.unitCount
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,7 +115,9 @@ class RoundCalculatorService {
|
|||||||
}
|
}
|
||||||
baseSpecificMoves.removeValue(forKey: baseId)
|
baseSpecificMoves.removeValue(forKey: baseId)
|
||||||
}
|
}
|
||||||
DataService.sharedInstance.localPlayerMoves.removeAll()
|
DataService.sharedInstance.localRoundData?.localPlayerMoves.removeAll()
|
||||||
|
DataService.sharedInstance.localRoundData?.hasAttackBoost = false
|
||||||
|
DataService.sharedInstance.localRoundData?.hasDefenceBoost = false
|
||||||
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
|
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
|
||||||
DataService.sharedInstance.snapshotModel = currentSnapshotModel
|
DataService.sharedInstance.snapshotModel = currentSnapshotModel
|
||||||
entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
|
entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
|
||||||
@ -119,11 +132,21 @@ class RoundCalculatorService {
|
|||||||
|
|
||||||
// collect moves from remote player
|
// collect moves from remote player
|
||||||
for playerMove in DataService.sharedInstance.remotePlayerMoves {
|
for playerMove in DataService.sharedInstance.remotePlayerMoves {
|
||||||
allPlayerMoves[playerMove.key] = playerMove.value
|
allPlayerMoves[playerMove.key] = playerMove.value.localPlayerMoves
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boosts[GameCenterManager.sharedInstance.hostingPlayer!.displayName] = (
|
||||||
|
DataService.sharedInstance.localRoundData!.hasAttackBoost,
|
||||||
|
DataService.sharedInstance.localRoundData!.hasDefenceBoost
|
||||||
|
)
|
||||||
|
|
||||||
|
boosts[GameCenterManager.sharedInstance.peerPlayer!.displayName] = (
|
||||||
|
DataService.sharedInstance.remotePlayerMoves[GameCenterManager.sharedInstance.peerPlayer!.displayName]!.hasAttackBoost,
|
||||||
|
DataService.sharedInstance.remotePlayerMoves[GameCenterManager.sharedInstance.peerPlayer!.displayName]!.hasDefenceBoost
|
||||||
|
)
|
||||||
|
|
||||||
// collect moves from local player
|
// collect moves from local player
|
||||||
allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localPlayerMoves
|
allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localRoundData?.localPlayerMoves
|
||||||
|
|
||||||
var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:]
|
var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:]
|
||||||
|
|
||||||
@ -141,6 +164,8 @@ class RoundCalculatorService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DataService.sharedInstance.remotePlayerMoves.removeAll()
|
||||||
|
|
||||||
return baseSpecificMoves
|
return baseSpecificMoves
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user