software-projekt-2-gold-wars/GoldWars/GoldWars/DataService.swift

85 lines
2.4 KiB
Swift

//
// PlayerMovesService.swift
// GoldWars
//
// Created by Tim Herbst on 13.05.20.
// Copyright © 2020 SP2. All rights reserved.
//
struct PlayerMove: Codable {
let fromBase: Int
let toBase: Int
var unitCount: Int
}
struct LocalRoundData: Codable {
var localPlayerMoves: [PlayerMove]
var hasAttackBoost: Bool
var hasDefenceBoost: Bool
init() {
localPlayerMoves = []
hasAttackBoost = false
hasDefenceBoost = false
}
}
class SnapshotModel: Codable {
var baseEntites: [BaseEntityModel]
init(baseEntites: [BaseEntityModel]) {
self.baseEntites = baseEntites
}
}
class BaseEntityModel: Codable {
let baseId: Int
var unitCount: Int
var ownership: String?
init(baseId: Int, unitCount: Int, ownership: String?, hasAttackBoost: Bool, hasDefenceBoost: Bool) {
self.baseId = baseId
self.unitCount = unitCount
self.ownership = ownership
}
}
class DataService {
static let sharedInstance = DataService()
var localRoundData: LocalRoundData = LocalRoundData()
var remotePlayerMoves: [String: LocalRoundData] = [:]
var snapshotModel: SnapshotModel?
var hostingPlayer = GameCenterManager.sharedInstance.hostingPlayer
var mapModel: MapGenerationModel?
var entityManager = EntityManager.gameEMInstance
func addMove(playerMove: PlayerMove) {
var equalMove = localRoundData.localPlayerMoves.filter { (ele) -> Bool in
ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase
}
if equalMove.count == 1 {
equalMove[0].unitCount = Int(equalMove[0].unitCount) + Int(playerMove.unitCount)
} else {
self.localRoundData.localPlayerMoves.append(playerMove)
}
}
func addRemotePlayerMoves(playerName: String, localRoundData: LocalRoundData) {
self.remotePlayerMoves[playerName] = localRoundData
}
func didReceiveAllData() -> Bool {
return remotePlayerMoves.count == GameCenterManager.sharedInstance.myMatch?.players.count
}
func setSnapshotModel(snapshotModel: SnapshotModel) {
self.snapshotModel = snapshotModel
}
func setMapModel(model: MapGenerationModel) {
self.mapModel = model
MapFactory(scene: entityManager.scene, entityManager: entityManager).load(fromModel: DataService.sharedInstance.mapModel!)
}
}