From 570099911f701ee21affd925e4af21551a0d584a Mon Sep 17 00:00:00 2001 From: Aldin Duraki Date: Wed, 13 May 2020 21:15:44 +0200 Subject: [PATCH] Test: Receiving Data on Hostdevice --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 ++++ .../GoldWars/Components/TimerComponent.swift | 3 +-- GoldWars/GoldWars/DataService.swift | 17 ++++++++++++-- GoldWars/GoldWars/MatchmakingHelper.swift | 5 ++-- GoldWars/GoldWars/MultiplayerNetwork.swift | 23 +++++++++++++++---- .../GoldWars/RoundCalculatorService.swift | 13 +++++++++++ 6 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 GoldWars/GoldWars/RoundCalculatorService.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index abbd510..6e22439 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */; }; 3EBD242E245D9332003CECE7 /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242D245D9332003CECE7 /* Team.swift */; }; 3FE19DB3246C690B004827AB /* RoundSimulatorService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FE19DB2246C690B004827AB /* RoundSimulatorService.swift */; }; + 3FE19DB5246C7A22004827AB /* RoundCalculatorService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */; }; 9E04AFAF245E2B73002D5CFC /* AttackActionComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */; }; 9E11FF79245CD81100EED3BE /* Fire.sks in Resources */ = {isa = PBXBuildFile; fileRef = 9E11FF77245CD81100EED3BE /* Fire.sks */; }; 9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E174C81245DD81D00209FF0 /* ButtonNode.swift */; }; @@ -86,6 +87,7 @@ 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameCenterHelper.swift; sourceTree = ""; }; 3EBD242D245D9332003CECE7 /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = ""; }; 3FE19DB2246C690B004827AB /* RoundSimulatorService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundSimulatorService.swift; sourceTree = ""; }; + 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundCalculatorService.swift; sourceTree = ""; }; 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttackActionComponent.swift; sourceTree = ""; }; 9E11FF77245CD81100EED3BE /* Fire.sks */ = {isa = PBXFileReference; lastKnownFileType = file.sks; path = Fire.sks; sourceTree = ""; }; 9E174C81245DD81D00209FF0 /* ButtonNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ButtonNode.swift; sourceTree = ""; }; @@ -181,6 +183,7 @@ C099579B246C5E5C0016AA22 /* DataService.swift */, 110360E4244B101B008610AF /* Info.plist */, AE151588245F18EF001D363E /* MatchmakingHelper.swift */, + 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */, 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */, C04783EF24685995004961FB /* SettingsScene.swift */, C064E9A7246C0EA50022B228 /* LabelNode.swift */, @@ -394,6 +397,7 @@ files = ( 9E78ACB8245CB75B00526FF7 /* TeamComponent.swift in Sources */, 9EA3ABEF245C834B006BC61D /* ModalContentComponent.swift in Sources */, + 3FE19DB5246C7A22004827AB /* RoundCalculatorService.swift in Sources */, 9EC86BA6245C8AD000796EF3 /* ModalType.swift in Sources */, 9EC239E1246878A900952F74 /* MultiplayerNetwork.swift in Sources */, 9E78ACBE245CC9C000526FF7 /* AtkBoostSkillComponent.swift in Sources */, diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index 9fa31de..f45026c 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -44,9 +44,8 @@ class TimerComponent: GKComponent { self.labelNode.text = String(timeLeft()) if(isFinished()){ - MatchmakingHelper.shar self.labelNode.text = "Synching" - MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.playerMoves) + MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) } } diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index fb3ddb9..30fdde6 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -18,12 +18,25 @@ struct Host: Codable { class DataService { static let sharedInstance = DataService() - var playerMoves: [PlayerMove] = [] + var localPlayerMoves: [PlayerMove] = [] + var remotePlayerMoves: [String: [PlayerMove]] = [:] var gameHost: Host? func addMove(playerMove: PlayerMove) { - self.playerMoves.append(playerMove) + self.localPlayerMoves.append(playerMove) + } + + 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 setGameHost(host: Host) { diff --git a/GoldWars/GoldWars/MatchmakingHelper.swift b/GoldWars/GoldWars/MatchmakingHelper.swift index 272c7ad..0af55c1 100644 --- a/GoldWars/GoldWars/MatchmakingHelper.swift +++ b/GoldWars/GoldWars/MatchmakingHelper.swift @@ -109,13 +109,12 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe */ func match(_ match: GKMatch, didReceive data: Data, fromRemotePlayer player: GKPlayer) { - print("Daten empfangen!") if mpMatch != match { return } let jsonDecoder = JSONDecoder() - if let message = try? jsonDecoder.decode(PlayerMove.self, from: data) { - //TODO: impl logic + if let playerMoves = try? jsonDecoder.decode([PlayerMove].self, from: data) { + DataService.sharedInstance.addRemotePlayerMoves(playerID: player.gamePlayerID, playerMoves: playerMoves) } if let message = try? jsonDecoder.decode(Host.self, from: data) { diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index 75f51f9..2cbc4ea 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -11,7 +11,7 @@ import Foundation class MultiplayerNetwork{ static let sharedInstance = MultiplayerNetwork() - + func sendData(data: Data) { let mmHelper = MatchmakingHelper.sharedInstance if let multiplayerMatch = mmHelper.mpMatch { @@ -22,11 +22,26 @@ class MultiplayerNetwork{ } } } + + func sendDataToHost(data: Data) { + let mmHelper = MatchmakingHelper.sharedInstance + let hostGKPlayer = MatchmakingHelper.sharedInstance.mpMatch?.players.filter{ $0.gamePlayerID == DataService.sharedInstance.gameHost?.playerID} + + if let multiplayerMatch = mmHelper.mpMatch{ + do { + try multiplayerMatch.send(data, to: hostGKPlayer!, dataMode: .reliable) + } catch { + print("Tim war mal wieder am Werk der Krasse") + } + } + } func sendPlayerMoves(playerMoves: [PlayerMove]) { - let encoder = JSONEncoder() - let encoded = (try? encoder.encode(playerMoves))! - sendData(data: encoded) + if !MatchmakingHelper.sharedInstance.isServer { + let encoder = JSONEncoder() + let encoded = (try? encoder.encode(playerMoves))! + sendDataToHost(data: encoded) + } } func sendHostIdentifier() { diff --git a/GoldWars/GoldWars/RoundCalculatorService.swift b/GoldWars/GoldWars/RoundCalculatorService.swift new file mode 100644 index 0000000..b01b676 --- /dev/null +++ b/GoldWars/GoldWars/RoundCalculatorService.swift @@ -0,0 +1,13 @@ +// +// RoundCalculatorService.swift +// GoldWars +// +// Created by student on 13.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation + +class RoundCalculatorServie { + +}