94 lines
2.5 KiB
Swift
Raw Normal View History

2020-05-13 20:22:23 +02:00
//
// PlayerMovesService.swift
// GoldWars
//
// Created by Tim Herbst on 13.05.20.
// Copyright © 2020 SP2. All rights reserved.
//
2020-06-06 18:59:26 +02:00
struct NotificationModel: Codable {
let name: String
init(name: String) {
self.name = name
}
}
struct PlayerMove: Codable {
2020-05-13 20:22:23 +02:00
let fromBase: Int
let toBase: Int
var unitCount: Int
2020-05-13 20:22:23 +02:00
}
2020-06-01 17:27:13 +02:00
struct LocalRoundData: Codable {
var localPlayerMoves: [PlayerMove]
var hasAttackBoost: Bool
var hasDefenceBoost: Bool
2020-06-01 17:37:43 +02:00
init() {
localPlayerMoves = []
hasAttackBoost = false
hasDefenceBoost = false
}
2020-06-01 17:27:13 +02:00
}
class SnapshotModel: Codable {
var baseEntites: [BaseEntityModel]
init(baseEntites: [BaseEntityModel]) {
self.baseEntites = baseEntites
}
}
2020-05-18 00:27:53 +02:00
class BaseEntityModel: Codable {
let baseId: Int
var unitCount: Int
2020-05-18 00:27:53 +02:00
var ownership: String?
init(baseId: Int, unitCount: Int, ownership: String?, hasAttackBoost: Bool, hasDefenceBoost: Bool) {
2020-05-18 00:27:53 +02:00
self.baseId = baseId
self.unitCount = unitCount
self.ownership = ownership
}
}
2020-05-13 20:22:23 +02:00
class DataService {
static let sharedInstance = DataService()
2020-06-01 17:37:43 +02:00
var localRoundData: LocalRoundData = LocalRoundData()
2020-06-01 17:27:13 +02:00
var remotePlayerMoves: [String: LocalRoundData] = [:]
var snapshotModel: SnapshotModel?
var hostingPlayer = GameCenterManager.sharedInstance.hostingPlayer
var mapModel: MapGenerationModel?
var entityManager = EntityManager.gameEMInstance
2020-05-13 20:22:23 +02:00
func addMove(playerMove: PlayerMove) {
2020-06-01 17:37:43 +02:00
var equalMove = localRoundData.localPlayerMoves.filter { (ele) -> Bool in
ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase
}
2020-06-01 17:37:43 +02:00
if equalMove.count == 1 {
equalMove[0].unitCount = Int(equalMove[0].unitCount) + Int(playerMove.unitCount)
} else {
2020-06-01 17:37:43 +02:00
self.localRoundData.localPlayerMoves.append(playerMove)
}
2020-05-13 21:15:44 +02:00
}
2020-06-01 17:27:13 +02:00
func addRemotePlayerMoves(playerName: String, localRoundData: LocalRoundData) {
self.remotePlayerMoves[playerName] = localRoundData
}
func didReceiveAllData() -> Bool {
return remotePlayerMoves.count == GameCenterManager.sharedInstance.myMatch?.players.count
2020-05-13 20:22:23 +02:00
}
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!)
}
2020-05-13 20:22:23 +02:00
}