software-projekt-2-gold-wars/GoldWars/GoldWars/RoundCalculatorService.swift
2020-05-18 00:27:53 +02:00

123 lines
4.6 KiB
Swift

//
// RoundCalculatorService.swift
// GoldWars
//
// Created by Aldin Duraki on 13.05.20.
// Copyright © 2020 SP2. All rights reserved.
//
import Foundation
import GameKit
import os
class RoundCalculatorServie {
static let sharedInstance = RoundCalculatorServie()
var isCalculating = false
var allPlayerMoves: [String: [PlayerMove]] = [:]
var baseSpecificMoves: [Int: [String: PlayerMove]] = [:]
func calculateRound() {
os_log("Started calculating Round", log: OSLog.default, type: .info)
let startTime = CFAbsoluteTimeGetCurrent()
isCalculating = true
var currentSnapshotModel = DataService.sharedInstance.snapshotModel
// TODO: smarter way?
for entry in DataService.sharedInstance.remotePlayerMoves {
addPlayerMove(playerName: entry.key, playerMoves: entry.value)
}
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)
}
}
// TODO-END:
for (key, value) in baseSpecificMoves {
let baseId = key
var playerMovesByBase = value
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.baseEntites! {
if base.baseId == playerMove.fromBase {
base.unitCount -= playerMove.unitCount
}
if base.baseId == playerMove.toBase {
base.unitCount += playerMove.unitCount
}
}
playerMovesByBase.removeValue(forKey: playerName)
}
for (_, playerMove) in playerMovesByBase {
for var base in currentSnapshotModel.baseEntites! {
if base.baseId == playerMove.fromBase {
base.unitCount -= playerMove.unitCount
}
}
}
let sorted = playerMovesByBase.sorted { (e1: (key: String, value: PlayerMove), e2: (key: String, value: PlayerMove)) -> Bool in
e1.value.unitCount > e2.value.unitCount
}
var max = sorted[0]
if sorted.count == 2 {
let secMax = sorted[1]
max.value.unitCount -= secMax.value.unitCount
}
for var base in currentSnapshotModel.baseEntites! {
if base.baseId == max.value.toBase {
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 %@", log: OSLog.default, type: .info, calcTime)
}
func addPlayerMove(playerName: String, playerMoves: [PlayerMove]) {
self.allPlayerMoves[playerName] = playerMoves
}
func mapPlayerMoveToAttackedBase(playerName: String, playerMove: PlayerMove) {
if self.baseSpecificMoves.keys.contains(playerMove.toBase) {
var playerMovesForSameBase = self.baseSpecificMoves[playerMove.toBase]!
if playerMovesForSameBase.keys.contains(playerName) {
playerMovesForSameBase[playerName] = playerMove
}
} else {
self.baseSpecificMoves[playerMove.toBase] = [playerName: playerMove]
}
}
func resolvePlayerMove(playerMove: PlayerMove, unitCount: Int, ownership: String?, resolveType: String) {
//outsource playermoves || tnx, remove, add(with calc)
}
}