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 {
let playerID: String
let playerName: String
}
struct SnapshotModel: Codable {
var baseEntites: [BaseEntityModel]?
}
struct BaseEntityModel: Codable {
class BaseEntityModel: Codable {
let baseId: 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 {
@ -46,8 +52,8 @@ class DataService {
}
}
func addRemotePlayerMoves(playerID: String, playerMoves: [PlayerMove]) {
self.remotePlayerMoves[playerID] = playerMoves
func addRemotePlayerMoves(playerName: String, playerMoves: [PlayerMove]) {
self.remotePlayerMoves[playerName] = playerMoves
}
func didReceiveAllData() -> Bool {

View File

@ -119,9 +119,32 @@ class EntityManager {
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? {
for entity in entities {
if let teamComponent = entity.component(ofType: TeamComponent.self),
@ -200,7 +223,7 @@ class EntityManager {
for entity in bases {
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)

View File

@ -110,7 +110,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
let jsonDecoder = JSONDecoder()
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) {
@ -118,7 +118,9 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
}
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
print("received data package -> \(snapshotModel)")
DataService.sharedInstance.snapshotModel = snapshotModel
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: snapshotModel)
}
MultiplayerNetwork.sharedInstance.isSending = false
@ -170,7 +172,7 @@ class MatchmakingHelper: NSObject, GKMatchmakerViewControllerDelegate, GKMatchDe
self.isServer = true
self.spieler1 = player
self.nameSpieler1 = self.spieler1!.displayName
DataService.sharedInstance.setGameHost(host: Host(playerID: player!.displayName))
DataService.sharedInstance.setGameHost(host: Host(playerName: player!.displayName))
} else {
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

View File

@ -30,9 +30,9 @@ class MultiplayerNetwork{
for player in mmHelper.mpMatch!.players {
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{
do {
@ -59,8 +59,10 @@ class MultiplayerNetwork{
}
func sendSnapshotModelToPlayers() {
print("sending snapshot -> \(DataService.sharedInstance.snapshotModel)")
let encoder = JSONEncoder()
let encoded = (try? encoder.encode(DataService.sharedInstance.snapshotModel))!
print("sending package -> \(encoded)")
sendData(data: encoded)
}

View File

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