diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index 34cbccd..5cf2937 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -45,8 +45,8 @@ class MultiplayerNetwork{ let encoder = JSONEncoder() let encoded = (try? encoder.encode(playerMoves))! sendDataToHost(data: encoded) + DataService.sharedInstance.localPlayerMoves.removeAll() } - DataService.sharedInstance.localPlayerMoves.removeAll() } func sendHostIdentifier() { diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index 3704ec8..fb403ac 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -54,7 +54,7 @@ class RoundCalculatorService { } playerMovesByBase.removeValue(forKey: playerName) } - + for (_, playerMoves) in playerMovesByBase { for playerMove in playerMoves { for base in currentSnapshotModel!.baseEntites { @@ -84,11 +84,22 @@ class RoundCalculatorService { for base in currentSnapshotModel!.baseEntites { if base.baseId == playerMoveWithMaxUnits.value.toBase { - base.unitCount += playerMoveWithMaxUnits.value.unitCount - if playerMoveWithMaxUnits.value.unitCount == 0 { - base.ownership = nil + if base.ownership == nil { + base.unitCount += playerMoveWithMaxUnits.value.unitCount + if playerMoveWithMaxUnits.value.unitCount == 0 { + base.ownership = nil + } else { + base.ownership = playerMoveWithMaxUnits.key + } } else { - base.ownership = playerMoveWithMaxUnits.key + if base.unitCount < playerMoveWithMaxUnits.value.unitCount { + base.unitCount = playerMoveWithMaxUnits.value.unitCount - base.unitCount + base.ownership = playerMoveWithMaxUnits.key + } else if (base.unitCount == playerMoveWithMaxUnits.value.unitCount) { + base.ownership = nil + } else { + base.unitCount -= playerMoveWithMaxUnits.value.unitCount + } } } } @@ -96,7 +107,9 @@ class RoundCalculatorService { baseSpecificMoves.removeValue(forKey: baseId) } allPlayerMoves.removeAll() + DataService.sharedInstance.localPlayerMoves.removeAll() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() + DataService.sharedInstance.snapshotModel = currentSnapshotModel EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel!) os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 58a3d9d..be67b35 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -26,13 +26,11 @@ class GameScene: SKScene{ } func initMap() { - if (DataService.sharedInstance.gameHost?.playerName == GKLocalPlayer.local.displayName) { let mapModel = MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap() MultiplayerNetwork.sharedInstance.sendMapModelToPlayers(mapModel: mapModel) + DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel()) } - - DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel()) } override func touchesEnded(_ touches: Set, with event: UIEvent?) {