diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index 0b61efc..596fd7b 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -9,11 +9,11 @@ import GameplayKit class TimerComponent: GKComponent { - + let labelNode :SKLabelNode var endTime :Date! var duration :Double - + init(text: String, anchorPoint: CGPoint, duration: TimeInterval) { self.labelNode = SKLabelNode(text: text) self.labelNode.fontColor = UIColor.black @@ -49,7 +49,8 @@ class TimerComponent: GKComponent { MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) } if !RoundCalculatorServie.sharedInstance.isCalculating - && DataService.sharedInstance.didReceiveAllData(){ + && DataService.sharedInstance.didReceiveAllData() + && MatchmakingHelper.sharedInstance.isServer { RoundCalculatorServie.sharedInstance.calculateRound() } } @@ -58,6 +59,6 @@ class TimerComponent: GKComponent { required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + } diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index 44dd5fe..452e65f 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -16,8 +16,16 @@ struct Host: Codable { let playerName: String } -struct SnapshotModel: Codable { - var baseEntites: [BaseEntityModel]? +class SnapshotModel: Codable { + var baseEntites: [BaseEntityModel] +// +// init() { +// self.baseEntites = [] +// } +// + init(baseEntites: [BaseEntityModel]) { + self.baseEntites = baseEntites + } } class BaseEntityModel: Codable { @@ -36,7 +44,7 @@ class DataService { static let sharedInstance = DataService() var localPlayerMoves: [PlayerMove] = [] var remotePlayerMoves: [String: [PlayerMove]] = [:] - var snapshotModel = SnapshotModel() + var snapshotModel: SnapshotModel? var gameHost: Host? diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index fe32c99..1dbda90 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -142,7 +142,7 @@ class EntityManager { } func getSnapshotBaseById(baseId: Int, snapshotModel: SnapshotModel) -> BaseEntityModel{ - return (snapshotModel.baseEntites?.filter { $0.baseId == baseId }[0])! + return snapshotModel.baseEntites.filter { $0.baseId == baseId }[0] } func getBaseByPlayer(for player: GKPlayer) -> GKEntity? { diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index a8e08bb..b8fde43 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -42,12 +42,12 @@ class RoundCalculatorServie { 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} // spieler verschiebt einheiten beim schieben for (playerName, playerMove) in possiblyOwnershipMoves { - for var base in currentSnapshotModel.baseEntites! { + for var base in currentSnapshotModel!.baseEntites { if base.baseId == playerMove.fromBase { base.unitCount -= playerMove.unitCount } @@ -59,7 +59,7 @@ class RoundCalculatorServie { } for (_, playerMove) in playerMovesByBase { - for var base in currentSnapshotModel.baseEntites! { + for var base in currentSnapshotModel!.baseEntites { if base.baseId == playerMove.fromBase { base.unitCount -= playerMove.unitCount } @@ -78,7 +78,7 @@ class RoundCalculatorServie { } - for var base in currentSnapshotModel.baseEntites! { + for var base in currentSnapshotModel!.baseEntites { if base.baseId == max.value.toBase { base.unitCount += max.value.unitCount if max.value.unitCount == 0 { @@ -95,9 +95,9 @@ class RoundCalculatorServie { print(currentSnapshotModel) MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() - EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel) + EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel!) let calcTime = CFAbsoluteTimeGetCurrent() - startTime - os_log("Finished calculating Round in %@", log: OSLog.default, type: .info, calcTime) + os_log("Finished calculating Round in %@", log: OSLog.default, type: .info) } func addPlayerMove(playerName: String, playerMoves: [PlayerMove]) {