diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index b3adc4a..894a16e 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -48,6 +48,9 @@ class TimerComponent: GKComponent { if !MultiplayerNetwork.sharedInstance.isSending { MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) } + if DataService.sharedInstance.didReceiveAllData() { + RoundCalculatorServie.sharedInstance.calculateRound() + } } } diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index 30fdde6..8d2d331 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -29,14 +29,10 @@ class DataService { func addRemotePlayerMoves(playerID: String, playerMoves: [PlayerMove]) { self.remotePlayerMoves[playerID] = playerMoves - - //test - var size = self.remotePlayerMoves.count - if size == 1 { - for (key, value) in remotePlayerMoves { - print("\(key) : \(value)") - } - } + } + + func didReceiveAllData() -> Bool { + return remotePlayerMoves.count == MatchmakingHelper.sharedInstance.mpMatch?.players.count } func setGameHost(host: Host) { diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift index db1ef90..273b2b0 100644 --- a/GoldWars/GoldWars/RoundCalculatorService.swift +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -2,14 +2,43 @@ // RoundCalculatorService.swift // GoldWars // -// Created by student on 13.05.20. +// Created by Aldin Duraki on 13.05.20. // Copyright © 2020 SP2. All rights reserved. // import Foundation +import GameKit +import os class RoundCalculatorServie { + + static let sharedInstance = RoundCalculatorServie() + var allPlayerMoves: [String: [PlayerMove]] = [:] + var baseSpecificMove: [Int: [(String, PlayerMove)]] = [:] + func calculateRound() { + for entry in DataService.sharedInstance.remotePlayerMoves { + addPlayerMove(playerID: entry.key, playerMoves: entry.value) + } + addPlayerMove(playerID: GKLocalPlayer.local.displayName, playerMoves: DataService.sharedInstance.localPlayerMoves) + + // berechnen nach minimal substraction + + for entry in allPlayerMoves { + for move in entry.value { + addFiltedMove(playerName: entry.key, playerMove: move) + } + } + print(baseSpecificMove) + // sende an alle anderen spieler die moves + } + func addPlayerMove(playerID: String, playerMoves: [PlayerMove]) { + self.allPlayerMoves[playerID] = playerMoves + } + + func addFiltedMove(playerName: String, playerMove: PlayerMove) { + self.baseSpecificMove[playerMove.toBase]!.append((playerName, playerMove)) + } }