OSDN Git Service

関数名を変更
[kcd/KCD.git] / KCD / CalculateDamageCommand.swift
1 //
2 //  CalculateDamageCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/15.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10 import SwiftyJSON
11
12 enum BattleType {
13     
14     case normal
15     case combinedAir
16     case combinedWater
17     case eachCombinedAir
18     case eachCombinedWater
19     case enemyCombined
20 }
21
22 enum DamageControlID: Int {
23     
24     case damageControl = 42
25     case goddes = 43
26 }
27
28 final class CalculateDamageCommand: JSONCommand {
29     
30     override func execute() {
31         
32         switch api.endpoint {
33             
34         case .battle, .airBattle, .ldAirBattle:
35             normalBattle(battleType: .normal)
36             
37         case .combinedEcBattle:
38             enemyCombinedBattle(battleType: .enemyCombined)
39             
40         case .combinedBattle, .combinedAirBattle:
41             combinedAirBattle(battleType: .combinedAir)
42             
43         case .combinedBattleWater, .combinedLdAirBattle:
44             normalBattle(battleType: .combinedWater)
45             
46         case .combinedEachBattle:
47             eachAirBattle(battleType: .eachCombinedAir)
48             
49         case .combinedEachBattleWater:
50             normalBattle(battleType: .eachCombinedWater)
51             
52         case .combinedEachNightToDay:
53             eachNightToDayBattle(battleType: .normal)
54             
55         case .midnightBattle, .midnightSpMidnight:
56             midnightBattle(battleType: .normal)
57             
58         case .combinedMidnightBattle, .combinedSpMidnight:
59             midnightBattle(battleType: .combinedAir)
60             
61         case .combinedEcMidnightBattle:
62             midnightBattle(battleType: .eachCombinedAir)
63             
64         case .battleResult, .combinedBattleResult:
65             applyDamage()
66             resetDamage()
67             
68         default: return Logger.shared.log("Missing API: \(apiResponse.api)")
69         }
70     }
71     
72     func normalBattle(battleType: BattleType) {
73         
74         updateBattleCell()
75         DamageCalculator(json, battleType).calculateBattle()
76     }
77     
78     func combinedAirBattle(battleType: BattleType) {
79         
80         updateBattleCell()
81         DamageCalculator(json, battleType).calcCombinedBattleAir()
82     }
83     
84     func eachAirBattle(battleType: BattleType) {
85         
86         updateBattleCell()
87         DamageCalculator(json, battleType).calcEachBattleAir()
88     }
89     
90     func eachNightToDayBattle(battleType: BattleType) {
91         
92         updateBattleCell()
93         DamageCalculator(json, battleType).calcEachNightToDay()
94     }
95     
96     func enemyCombinedBattle(battleType: BattleType) {
97         
98         updateBattleCell()
99         DamageCalculator(json, battleType).calcEnemyCombinedBattle()
100     }
101     
102     func midnightBattle(battleType: BattleType) {
103         
104         DamageCalculator(json, battleType).calcMidnight()
105     }
106 }
107
108 extension CalculateDamageCommand {
109     
110     func resetDamage() {
111         
112         let store = TemporaryDataStore.oneTimeEditor()
113         
114         store.damages().forEach(store.delete)
115     }
116     
117     func applyDamage() {
118         
119         let store = TemporaryDataStore.oneTimeEditor()
120         
121         let totalDamages = store.sortedDamagesById()
122         
123         let aStore = ServerDataStore.oneTimeEditor()
124         
125         Debug.excute(level: .debug) {
126             
127             print("-------")
128             
129             totalDamages.forEach {
130                 
131                 guard let ship = aStore.ship(by: $0.shipID) else { return }
132                 
133                 if ship.nowhp != $0.hp {
134                     
135                     print("\(ship.name)(\(ship.id)),HP \(ship.nowhp) -> \($0.hp)")
136                 }
137             }
138             
139             
140             print("------- End Battle")
141         }
142         
143         // 第二艦隊単独出撃で正しくデータが反映されるように逆順にして計算
144         totalDamages.reversed().forEach {
145             
146             guard let ship = aStore.ship(by: $0.shipID) else { return }
147             
148             ship.nowhp = $0.hp
149             
150             if $0.useDamageControl { removeFirstDamageControl(of: ship) }
151         }
152         
153     }
154     
155     func updateBattleCell() {
156         
157         let store = TemporaryDataStore.default
158         
159         guard let battle = store.battle() else { return Logger.shared.log("Battle is invalid.") }
160         
161         battle.battleCell = (battle.no == 0 ? nil : battle.no as NSNumber)
162         
163         Debug.excute(level: .debug) {
164             
165             print("Enter Cell ------- ")
166             
167             if let seiku = json["api_data"]["api_kouku"]["api_stage1"]["api_disp_seiku"].int {
168                 
169                 switch seiku {
170                 case 0: print("制空権 均衡")
171                 case 1: print("制空権 確保")
172                 case 2: print("制空権 優勢")
173                 case 3: print("制空権 劣勢")
174                 case 4: print("制空権 喪失")
175                 default: break
176                 }
177             }
178             
179             if let intercept = json["api_data"]["api_formation"][2].int {
180                 
181                 switch intercept {
182                 case 1: print("交戦形態 同航戦")
183                 case 2: print("交戦形態 反航戦")
184                 case 3: print("交戦形態 T字戦有利")
185                 case 4: print("交戦形態 T字戦不利")
186                 default: break
187                 }
188             }
189         }
190     }
191     
192     func removeFirstDamageControl(of ship: Ship) {
193         
194         let store = ServerDataStore.default
195         
196         let (item, damageControl) = ship
197             .equippedItem
198             .lazy
199             .flatMap { $0 as? SlotItem }
200             .map { ($0, store.masterSlotItemID(by: $0.id)) }
201             .map { ($0.0, DamageControlID(rawValue: $0.1)) }
202             .filter { $0.1 != nil }
203             .first ?? (nil, nil)
204         
205         if let validDamageControl = damageControl {
206             
207             switch validDamageControl {
208             case .damageControl: break
209                 
210             case .goddes:
211                 ship.fuel = ship.maxFuel
212                 ship.bull = ship.maxBull
213             }
214             
215             guard let equiped = ship.equippedItem.array as? [SlotItem] else { return }
216                 
217             ship.equippedItem = NSOrderedSet(array: equiped.filter { $0 != item })
218             
219             return
220         }
221         
222         // check extra slot
223         let exItemId = store.masterSlotItemID(by: ship.slot_ex)
224         
225         guard let exType = DamageControlID(rawValue: exItemId) else { return }
226         
227         switch exType {
228         case .damageControl: break
229             
230         case .goddes:
231             ship.fuel = ship.maxFuel
232             ship.bull = ship.maxBull
233         }
234         
235         ship.slot_ex = -1
236     }
237 }