TEST: snapshotModel not updating

This commit is contained in:
Aldin Duraki 2020-05-18 00:27:53 +02:00
parent 1dc97c3bab
commit e08d40dd4a
5 changed files with 84 additions and 25 deletions

View File

@ -13,17 +13,23 @@ struct PlayerMove: Codable{
} }
struct Host: Codable { struct Host: Codable {
let playerID: String let playerName: String
} }
struct SnapshotModel: Codable { struct SnapshotModel: Codable {
var baseEntites: [BaseEntityModel]? var baseEntites: [BaseEntityModel]?
} }
struct BaseEntityModel: Codable { class BaseEntityModel: Codable {
let baseId: Int let baseId: Int
var unitCount: Int var unitCount: Int
var ownership: String var ownership: String?
init(baseId: Int, unitCount: Int, ownership: String?) {
self.baseId = baseId
self.unitCount = unitCount
self.ownership = ownership
}
} }
class DataService { class DataService {
@ -46,8 +52,8 @@ class DataService {
} }
} }
func addRemotePlayerMoves(playerID: String, playerMoves: [PlayerMove]) { func addRemotePlayerMoves(playerName: String, playerMoves: [PlayerMove]) {
self.remotePlayerMoves[playerID] = playerMoves self.remotePlayerMoves[playerName] = playerMoves
} }
func didReceiveAllData() -> Bool { func didReceiveAllData() -> Bool {

View File

@ -119,7 +119,30 @@ class EntityManager {
scene.addChild(base.component(ofType: TeamComponent.self)!.fire) scene.addChild(base.component(ofType: TeamComponent.self)!.fire)
} }
} }
}
func updateSnapshotModel(snapshotModel: SnapshotModel) {
let bases = entities.filter{$0 is Base}
for entity in bases{
let base = entity as! Base
let snapBase = self.getSnapshotBaseById(baseId: base.baseID, snapshotModel: snapshotModel)
print("in updateSnap -> Entity \(base)")
print("in updateSnap -> snapBase \(snapBase)")
var getOwnerBySnapBase: GKPlayer? = nil
base.unitCount = snapBase.unitCount
if snapBase.ownership != nil {
getOwnerBySnapBase = MatchmakingHelper.sharedInstance.getGKPlayerByUsername(displayName: snapBase.ownership!)
}
if getOwnerBySnapBase != nil {
base.ownershipPlayer = getOwnerBySnapBase
}
print("nach updateSnap -> Entity \(base)")
}
}
func getSnapshotBaseById(baseId: Int, snapshotModel: SnapshotModel) -> BaseEntityModel{
return (snapshotModel.baseEntites?.filter { $0.baseId == baseId }[0])!
} }
func getBaseByPlayer(for player: GKPlayer) -> GKEntity? { func getBaseByPlayer(for player: GKPlayer) -> GKEntity? {
@ -200,7 +223,7 @@ class EntityManager {
for entity in bases { for entity in bases {
let base = entity as! Base let base = entity as! Base
snapBase.append(BaseEntityModel(baseId: base.baseID, unitCount: base.unitCount, ownership: base.ownershipPlayer!.displayName)) snapBase.append(BaseEntityModel(baseId: base.baseID, unitCount: base.unitCount, ownership: base.ownershipPlayer?.displayName))
} }
return SnapshotModel(baseEntites: snapBase) return SnapshotModel(baseEntites: snapBase)

View File

@ -110,7 +110,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
let jsonDecoder = JSONDecoder() let jsonDecoder = JSONDecoder()
if let playerMoves = try? jsonDecoder.decode([PlayerMove].self, from: data) { if let playerMoves = try? jsonDecoder.decode([PlayerMove].self, from: data) {
DataService.sharedInstance.addRemotePlayerMoves(playerID: player.displayName, playerMoves: playerMoves) DataService.sharedInstance.addRemotePlayerMoves(playerName: player.displayName, playerMoves: playerMoves)
} }
if let message = try? jsonDecoder.decode(Host.self, from: data) { if let message = try? jsonDecoder.decode(Host.self, from: data) {
@ -118,7 +118,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
} }
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
print("received data package -> \(snapshotModel)")
DataService.sharedInstance.snapshotModel = snapshotModel DataService.sharedInstance.snapshotModel = snapshotModel
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel)
} }
MultiplayerNetwork.sharedInstance.isSending = false MultiplayerNetwork.sharedInstance.isSending = false
@ -170,7 +172,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
self.isServer = true self.isServer = true
self.spieler1 = player self.spieler1 = player
self.nameSpieler1 = self.spieler1!.displayName self.nameSpieler1 = self.spieler1!.displayName
DataService.sharedInstance.setGameHost(host: Host(playerID: player!.displayName)) DataService.sharedInstance.setGameHost(host: Host(playerName: player!.displayName))
} else { } else {
self.isServer = false self.isServer = false
} }
@ -181,6 +183,20 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
}) })
} }
func getGKPlayerByUsername(displayName: String) -> GKPlayer? {
let nilGK : GKPlayer? = nil
if GKLocalPlayer.local.displayName == displayName {
return GKLocalPlayer.local
}
for player in mpMatch!.players {
if player.displayName == displayName {
return player
}
}
return nilGK
}
/* /*
Trennt die Verbindung vom Match Trennt die Verbindung vom Match

View File

@ -30,9 +30,9 @@ class MultiplayerNetwork{
for player in mmHelper.mpMatch!.players { for player in mmHelper.mpMatch!.players {
print(player.displayName) print(player.displayName)
} }
print(DataService.sharedInstance.gameHost!.playerID) print(DataService.sharedInstance.gameHost!.playerName)
let hostGKPlayer = MatchmakingHelper.sharedInstance.mpMatch?.players.filter{ $0.displayName == DataService.sharedInstance.gameHost!.playerID}[0] let hostGKPlayer = MatchmakingHelper.sharedInstance.mpMatch?.players.filter{ $0.displayName == DataService.sharedInstance.gameHost!.playerName}[0]
if let multiplayerMatch = mmHelper.mpMatch{ if let multiplayerMatch = mmHelper.mpMatch{
do { do {
@ -59,8 +59,10 @@ class MultiplayerNetwork{
} }
func sendSnapshotModelToPlayers() { func sendSnapshotModelToPlayers() {
print("sending snapshot -> \(DataService.sharedInstance.snapshotModel)")
let encoder = JSONEncoder() let encoder = JSONEncoder()
let encoded = (try? encoder.encode(DataService.sharedInstance.snapshotModel))! let encoded = (try? encoder.encode(DataService.sharedInstance.snapshotModel))!
print("sending package -> \(encoded)")
sendData(data: encoded) sendData(data: encoded)
} }

View File

@ -21,14 +21,16 @@ class RoundCalculatorServie {
os_log("Started calculating Round", log: OSLog.default, type: .info) os_log("Started calculating Round", log: OSLog.default, type: .info)
let startTime = CFAbsoluteTimeGetCurrent() let startTime = CFAbsoluteTimeGetCurrent()
isCalculating = true isCalculating = true
let currentSnapshotModel = DataService.sharedInstance.snapshotModel.baseEntites
var currentSnapshotModel = DataService.sharedInstance.snapshotModel
// TODO: smarter way? // TODO: smarter way?
for entry in DataService.sharedInstance.remotePlayerMoves { for entry in DataService.sharedInstance.remotePlayerMoves {
addPlayerMove(playerID: entry.key, playerMoves: entry.value) addPlayerMove(playerName: entry.key, playerMoves: entry.value)
} }
addPlayerMove(playerID: GKLocalPlayer.local.displayName, playerMoves: DataService.sharedInstance.localPlayerMoves) addPlayerMove(playerName: GKLocalPlayer.local.displayName, playerMoves: DataService.sharedInstance.localPlayerMoves)
print("allPlayerMoves -> \(allPlayerMoves)")
for entry in allPlayerMoves { for entry in allPlayerMoves {
for move in entry.value { for move in entry.value {
mapPlayerMoveToAttackedBase(playerName: entry.key, playerMove: move) mapPlayerMoveToAttackedBase(playerName: entry.key, playerMove: move)
@ -40,12 +42,12 @@ class RoundCalculatorServie {
let baseId = key let baseId = key
var playerMovesByBase = value var playerMovesByBase = value
let targetBase = currentSnapshotModel?.filter { $0.baseId == baseId }[0] let targetBase = currentSnapshotModel.baseEntites?.filter { $0.baseId == baseId }[0]
let possiblyOwnershipMoves = playerMovesByBase.filter { $0.key == targetBase?.ownership} let possiblyOwnershipMoves = playerMovesByBase.filter { $0.key == targetBase?.ownership}
// spieler verschiebt einheiten beim schieben // spieler verschiebt einheiten beim schieben
for (playerName, playerMove) in possiblyOwnershipMoves { for (playerName, playerMove) in possiblyOwnershipMoves {
for var base in currentSnapshotModel! { for var base in currentSnapshotModel.baseEntites! {
if base.baseId == playerMove.fromBase { if base.baseId == playerMove.fromBase {
base.unitCount -= playerMove.unitCount base.unitCount -= playerMove.unitCount
} }
@ -57,7 +59,7 @@ class RoundCalculatorServie {
} }
for (_, playerMove) in playerMovesByBase { for (_, playerMove) in playerMovesByBase {
for var base in currentSnapshotModel! { for var base in currentSnapshotModel.baseEntites! {
if base.baseId == playerMove.fromBase { if base.baseId == playerMove.fromBase {
base.unitCount -= playerMove.unitCount base.unitCount -= playerMove.unitCount
} }
@ -70,26 +72,36 @@ class RoundCalculatorServie {
} }
var max = sorted[0] var max = sorted[0]
if sorted.count == 2 {
let secMax = sorted[1] let secMax = sorted[1]
max.value.unitCount -= secMax.value.unitCount max.value.unitCount -= secMax.value.unitCount
for var base in currentSnapshotModel! { }
for var base in currentSnapshotModel.baseEntites! {
if base.baseId == max.value.toBase { if base.baseId == max.value.toBase {
base.unitCount -= max.value.unitCount base.unitCount += max.value.unitCount
if max.value.unitCount == 0 {
base.ownership = nil
} else {
base.ownership = max.key base.ownership = max.key
} }
}
} }
baseSpecificMoves.removeValue(forKey: baseId) baseSpecificMoves.removeValue(forKey: baseId)
} }
print(currentSnapshotModel)
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel)
let calcTime = CFAbsoluteTimeGetCurrent() - startTime let calcTime = CFAbsoluteTimeGetCurrent() - startTime
os_log("Finished calculating Round in %{calcTimer}", log: OSLog.default, type: .info, calcTime) os_log("Finished calculating Round in %@", log: OSLog.default, type: .info, calcTime)
} }
func addPlayerMove(playerID: String, playerMoves: [PlayerMove]) { func addPlayerMove(playerName: String, playerMoves: [PlayerMove]) {
self.allPlayerMoves[playerID] = playerMoves self.allPlayerMoves[playerName] = playerMoves
} }
func mapPlayerMoveToAttackedBase(playerName: String, playerMove: PlayerMove) { func mapPlayerMoveToAttackedBase(playerName: String, playerMove: PlayerMove) {