Remove optionals on localRoundData

This commit is contained in:
Marcel Schwarz 2020-06-01 17:37:43 +02:00
parent 4c9d83c7cb
commit 2820c7357a
5 changed files with 23 additions and 17 deletions

View File

@ -57,7 +57,7 @@ class TimerComponent: GKComponent {
self.labelNode.text = "Synching" self.labelNode.text = "Synching"
RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats()
if !MultiplayerNetwork.sharedInstance.isSending { if !MultiplayerNetwork.sharedInstance.isSending {
MultiplayerNetwork.sharedInstance.sendPlayerMoves(localRoundData: DataService.sharedInstance.localRoundData!) MultiplayerNetwork.sharedInstance.sendPlayerMoves(localRoundData: DataService.sharedInstance.localRoundData)
} }
if !RoundCalculatorService.sharedInstance.isCalculating if !RoundCalculatorService.sharedInstance.isCalculating
&& DataService.sharedInstance.didReceiveAllData() && DataService.sharedInstance.didReceiveAllData()

View File

@ -16,6 +16,12 @@ struct LocalRoundData: Codable {
var localPlayerMoves: [PlayerMove] var localPlayerMoves: [PlayerMove]
var hasAttackBoost: Bool var hasAttackBoost: Bool
var hasDefenceBoost: Bool var hasDefenceBoost: Bool
init() {
localPlayerMoves = []
hasAttackBoost = false
hasDefenceBoost = false
}
} }
class SnapshotModel: Codable { class SnapshotModel: Codable {
@ -40,7 +46,7 @@ class BaseEntityModel: Codable {
class DataService { class DataService {
static let sharedInstance = DataService() static let sharedInstance = DataService()
var localRoundData: LocalRoundData? var localRoundData: LocalRoundData = LocalRoundData()
var remotePlayerMoves: [String: LocalRoundData] = [:] var remotePlayerMoves: [String: LocalRoundData] = [:]
var snapshotModel: SnapshotModel? var snapshotModel: SnapshotModel?
var hostingPlayer = GameCenterManager.sharedInstance.hostingPlayer var hostingPlayer = GameCenterManager.sharedInstance.hostingPlayer
@ -49,13 +55,13 @@ class DataService {
var entityManager = EntityManager.gameEMInstance var entityManager = EntityManager.gameEMInstance
func addMove(playerMove: PlayerMove) { func addMove(playerMove: PlayerMove) {
var equalMove = localRoundData?.localPlayerMoves.filter { (ele) -> Bool in var equalMove = localRoundData.localPlayerMoves.filter { (ele) -> Bool in
ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase
} }
if equalMove!.count == 1 { if equalMove.count == 1 {
equalMove![0].unitCount = Int(equalMove![0].unitCount) + Int(playerMove.unitCount) equalMove[0].unitCount = Int(equalMove[0].unitCount) + Int(playerMove.unitCount)
} else { } else {
self.localRoundData?.localPlayerMoves.append(playerMove) self.localRoundData.localPlayerMoves.append(playerMove)
} }
} }

View File

@ -44,14 +44,14 @@ class HUD: GKEntity {
text: "Def", text: "Def",
isEnabled: true, isEnabled: true,
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.85, y: EntityManager.gameEMInstance.scene.size.height * 0.1), position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.85, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
onButtonPress: {DataService.sharedInstance.localRoundData?.hasDefenceBoost = true} onButtonPress: {DataService.sharedInstance.localRoundData.hasDefenceBoost = true}
) )
atkSkill = SingeClickButtonNode( atkSkill = SingeClickButtonNode(
textureName: "yellow_circle", textureName: "yellow_circle",
text: "Atk", text: "Atk",
isEnabled: true, isEnabled: true,
position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.95, y: EntityManager.gameEMInstance.scene.size.height * 0.1), position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.95, y: EntityManager.gameEMInstance.scene.size.height * 0.1),
onButtonPress: {DataService.sharedInstance.localRoundData?.hasAttackBoost = true} onButtonPress: {DataService.sharedInstance.localRoundData.hasAttackBoost = true}
) )
super.init() super.init()
hostLabel.position = CGPoint(x: size.width * 0.02, y: size.height * 0.95) hostLabel.position = CGPoint(x: size.width * 0.02, y: size.height * 0.95)

View File

@ -42,9 +42,9 @@ class MultiplayerNetwork{
let encoder = JSONEncoder() let encoder = JSONEncoder()
let encoded = (try? encoder.encode(localRoundData))! let encoded = (try? encoder.encode(localRoundData))!
sendDataToHost(data: encoded) sendDataToHost(data: encoded)
DataService.sharedInstance.localRoundData!.localPlayerMoves.removeAll() DataService.sharedInstance.localRoundData.localPlayerMoves.removeAll()
DataService.sharedInstance.localRoundData!.hasAttackBoost = false DataService.sharedInstance.localRoundData.hasAttackBoost = false
DataService.sharedInstance.localRoundData!.hasDefenceBoost = false DataService.sharedInstance.localRoundData.hasDefenceBoost = false
} }
} }

View File

@ -115,9 +115,9 @@ class RoundCalculatorService {
} }
baseSpecificMoves.removeValue(forKey: baseId) baseSpecificMoves.removeValue(forKey: baseId)
} }
DataService.sharedInstance.localRoundData?.localPlayerMoves.removeAll() DataService.sharedInstance.localRoundData.localPlayerMoves.removeAll()
DataService.sharedInstance.localRoundData?.hasAttackBoost = false DataService.sharedInstance.localRoundData.hasAttackBoost = false
DataService.sharedInstance.localRoundData?.hasDefenceBoost = false DataService.sharedInstance.localRoundData.hasDefenceBoost = false
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
DataService.sharedInstance.snapshotModel = currentSnapshotModel DataService.sharedInstance.snapshotModel = currentSnapshotModel
entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
@ -136,8 +136,8 @@ class RoundCalculatorService {
} }
boosts[GameCenterManager.sharedInstance.hostingPlayer!.displayName] = ( boosts[GameCenterManager.sharedInstance.hostingPlayer!.displayName] = (
DataService.sharedInstance.localRoundData!.hasAttackBoost, DataService.sharedInstance.localRoundData.hasAttackBoost,
DataService.sharedInstance.localRoundData!.hasDefenceBoost DataService.sharedInstance.localRoundData.hasDefenceBoost
) )
boosts[GameCenterManager.sharedInstance.peerPlayer!.displayName] = ( boosts[GameCenterManager.sharedInstance.peerPlayer!.displayName] = (
@ -146,7 +146,7 @@ class RoundCalculatorService {
) )
// collect moves from local player // collect moves from local player
allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localRoundData?.localPlayerMoves allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localRoundData.localPlayerMoves
var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:] var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:]