diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index a799b69..1a7e678 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -57,7 +57,7 @@ class TimerComponent: GKComponent { self.labelNode.text = "Synching" RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() if !MultiplayerNetwork.sharedInstance.isSending { - MultiplayerNetwork.sharedInstance.sendPlayerMoves(localRoundData: DataService.sharedInstance.localRoundData!) + MultiplayerNetwork.sharedInstance.sendPlayerMoves(localRoundData: DataService.sharedInstance.localRoundData) } if !RoundCalculatorService.sharedInstance.isCalculating && DataService.sharedInstance.didReceiveAllData() diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index 910cb9e..23fe5f2 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -16,6 +16,12 @@ struct LocalRoundData: Codable { var localPlayerMoves: [PlayerMove] var hasAttackBoost: Bool var hasDefenceBoost: Bool + + init() { + localPlayerMoves = [] + hasAttackBoost = false + hasDefenceBoost = false + } } class SnapshotModel: Codable { @@ -40,7 +46,7 @@ class BaseEntityModel: Codable { class DataService { static let sharedInstance = DataService() - var localRoundData: LocalRoundData? + var localRoundData: LocalRoundData = LocalRoundData() var remotePlayerMoves: [String: LocalRoundData] = [:] var snapshotModel: SnapshotModel? var hostingPlayer = GameCenterManager.sharedInstance.hostingPlayer @@ -49,13 +55,13 @@ class DataService { var entityManager = EntityManager.gameEMInstance func addMove(playerMove: PlayerMove) { - var equalMove = localRoundData?.localPlayerMoves.filter { (ele) -> Bool in + var equalMove = localRoundData.localPlayerMoves.filter { (ele) -> Bool in ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase } - if equalMove!.count == 1 { - equalMove![0].unitCount = Int(equalMove![0].unitCount) + Int(playerMove.unitCount) + if equalMove.count == 1 { + equalMove[0].unitCount = Int(equalMove[0].unitCount) + Int(playerMove.unitCount) } else { - self.localRoundData?.localPlayerMoves.append(playerMove) + self.localRoundData.localPlayerMoves.append(playerMove) } } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 633b61b..4229f3f 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -44,14 +44,14 @@ class HUD: GKEntity { text: "Def", isEnabled: true, position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.85, y: EntityManager.gameEMInstance.scene.size.height * 0.1), - onButtonPress: {DataService.sharedInstance.localRoundData?.hasDefenceBoost = true} + onButtonPress: {DataService.sharedInstance.localRoundData.hasDefenceBoost = true} ) atkSkill = SingeClickButtonNode( textureName: "yellow_circle", text: "Atk", isEnabled: true, position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.95, y: EntityManager.gameEMInstance.scene.size.height * 0.1), - onButtonPress: {DataService.sharedInstance.localRoundData?.hasAttackBoost = true} + onButtonPress: {DataService.sharedInstance.localRoundData.hasAttackBoost = true} ) super.init() hostLabel.position = CGPoint(x: size.width * 0.02, y: size.height * 0.95) diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index bd9f9eb..985ec14 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -42,9 +42,9 @@ class MultiplayerNetwork{ let encoder = JSONEncoder() let encoded = (try? encoder.encode(localRoundData))! sendDataToHost(data: encoded) - DataService.sharedInstance.localRoundData!.localPlayerMoves.removeAll() - DataService.sharedInstance.localRoundData!.hasAttackBoost = false - DataService.sharedInstance.localRoundData!.hasDefenceBoost = false + DataService.sharedInstance.localRoundData.localPlayerMoves.removeAll() + DataService.sharedInstance.localRoundData.hasAttackBoost = false + DataService.sharedInstance.localRoundData.hasDefenceBoost = false } } diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index 337af11..5792e59 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -115,9 +115,9 @@ class RoundCalculatorService { } baseSpecificMoves.removeValue(forKey: baseId) } - DataService.sharedInstance.localRoundData?.localPlayerMoves.removeAll() - DataService.sharedInstance.localRoundData?.hasAttackBoost = false - DataService.sharedInstance.localRoundData?.hasDefenceBoost = false + DataService.sharedInstance.localRoundData.localPlayerMoves.removeAll() + DataService.sharedInstance.localRoundData.hasAttackBoost = false + DataService.sharedInstance.localRoundData.hasDefenceBoost = false MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() DataService.sharedInstance.snapshotModel = currentSnapshotModel entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) @@ -136,8 +136,8 @@ class RoundCalculatorService { } boosts[GameCenterManager.sharedInstance.hostingPlayer!.displayName] = ( - DataService.sharedInstance.localRoundData!.hasAttackBoost, - DataService.sharedInstance.localRoundData!.hasDefenceBoost + DataService.sharedInstance.localRoundData.hasAttackBoost, + DataService.sharedInstance.localRoundData.hasDefenceBoost ) boosts[GameCenterManager.sharedInstance.peerPlayer!.displayName] = ( @@ -146,7 +146,7 @@ class RoundCalculatorService { ) // collect moves from local player - allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localRoundData?.localPlayerMoves + allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localRoundData.localPlayerMoves var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:]