OSDN Git Service

関数名を変更
[kcd/KCD.git] / KCDTests / NormalBattleTest.swift
1 //
2 //  NormalBattleTest.swift
3 //  KCDTests
4 //
5 //  Created by Hori,Masaki on 2017/10/19.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import XCTest
10
11 @testable import KCD
12
13 import SwiftyJSON
14
15 class NormalBattleTest: XCTestCase {
16     
17     var savedShips: [Ship] = []
18     var shipsHp: [Int] = []
19     var shipEquipments: [NSOrderedSet] = []
20     var shipExSlot: [Int] = []
21     
22     func initBattleFleet(_ fleet: Int, seventh: Bool = false) {
23         
24         savedShips = []
25         shipsHp = []
26         shipEquipments = []
27         shipExSlot = []
28         
29         // 艦隊を設定
30         do {
31             let store = ServerDataStore.oneTimeEditor()
32             
33             guard let deck = store.deck(by: fleet) else { return XCTFail("Can not get Deck.") }
34             
35             let max = seventh ? 6 : 5
36             (0...6).forEach { deck.setShip(id: -1, for: $0) }
37             (0...max).forEach { deck.setShip(id: $0 + 1, for: $0) }
38             
39             store.ships(byDeckId: fleet).forEach {
40                 $0.nowhp = $0.maxhp
41                 
42                 savedShips += [$0]
43                 shipsHp += [$0.nowhp]
44                 shipEquipments += [$0.equippedItem]
45                 shipExSlot += [$0.slot_ex]
46             }
47         }
48         
49         // 出撃艦隊を設定
50         do {
51             let rawValue: [String: Any] = [
52                 "api_result": 1,
53                 "api_data": [
54                     "api_no": 1
55                 ]
56             ]
57             guard let json = JSON(rawValue: rawValue) else { return XCTFail("json is nil") }
58             XCTAssertNotNil(json["api_result"])
59             
60             let paramValue: [String: String] = [
61                 "api_deck_id": "\(fleet)",
62                 "api_maparea_id": "1",
63                 "api_mapinfo_no": "1"
64             ]
65             let param = Parameter(paramValue)
66             XCTAssertEqual(param["api_deck_id"].string, "\(fleet)")
67             
68             let api = APIResponse(api: API(endpointPath: Endpoint.start.rawValue), parameter: param, json: json)
69             XCTAssertEqual(api.json, json)
70             XCTAssertEqual(api.parameter, param)
71             
72             let command = MapStartCommand(apiResponse: api)
73             
74             command.execute()
75         }
76         
77         // battleの生成確認
78         do {
79             let store = TemporaryDataStore.default
80             let battle = store.battle()
81             XCTAssertNotNil(battle)
82             XCTAssertEqual(battle?.deckId, fleet)
83         }
84     }
85     
86     func clear(_ fleet: Int) {
87         
88         do {
89             ResetSortie().reset()
90         }
91         
92         do {
93             let store = ServerDataStore.oneTimeEditor()
94             
95             let ships = store.ships(byDeckId: fleet)
96             
97             zip(ships, shipsHp).forEach { $0.nowhp = $1 }
98             zip(ships, shipEquipments).forEach { $0.equippedItem = $1 }
99             zip(ships, shipExSlot).forEach { $0.slot_ex = $1 }
100             
101             guard let deck = store.deck(by: fleet) else { return XCTFail("Can not get Deck.") }
102             savedShips.enumerated().forEach { deck.setShip(id: $0.element.id, for: $0.offset) }
103             (0...6).forEach { deck.setShip(id: -1, for: $0) }
104         }
105         
106         do {
107             let store = TemporaryDataStore.default
108             let battle = store.battle()
109             XCTAssertNil(battle)
110         }
111     }
112     
113     func normalBattle(_ fleet: Int, seventh: Bool = false) {
114         
115         // 戦闘(昼戦)
116         do {
117             let dfList = seventh ? [[2, 2], [6, 6]] : [[2, 2]]
118             let damages = seventh ? [[0, 3], [0, 7]] : [[0, 3]]
119             let fdam = seventh ? [0, 0, 0, 4, 0, 6, 7] : [0, 0, 0, 4, 0, 6]
120             let rawValue: [String: Any] = [
121                 "api_result": 1,
122                 "api_data": [
123                     "api_kouku": [
124                         "api_stage3": [
125                             "api_fdam": [
126                                 1, 0, 0
127                             ]
128                         ]
129                     ],
130                     "api_opening_atack": [
131                         "api_fdam": [
132                             0, 2
133                         ]
134                     ],
135                     "api_hougeki1": [
136                         "api_df_list": dfList,
137                         "api_damage": damages,
138                         "api_at_eflag": [
139                             1,
140                             1
141                         ]
142                     ],
143                     "api_raigeki": [
144                         "api_fdam": fdam
145                     ]
146                 ]
147             ]
148             guard let json = JSON(rawValue: rawValue) else { return XCTFail("json is nil") }
149             let param = Parameter(["Test": "Test"])
150             let api = APIResponse(api: API(endpointPath: Endpoint.battle.rawValue), parameter: param, json: json)
151             
152             let command = BattleCommand(apiResponse: api)
153             command.execute()
154         }
155         
156         // 戦闘(夜戦)
157         do {
158             let rawValue: [String: Any] = [
159                 "api_result": 1,
160                 "api_data": [
161                     "api_hougeki": [
162                         "api_df_list": [
163                             [4],
164                             [5]
165                         ],
166                         "api_damage": [
167                             [5],
168                             [7]
169                         ],
170                         "api_at_eflag": [
171                             1,
172                             1
173                         ]
174                     ]
175                 ]
176             ]
177             guard let json = JSON(rawValue: rawValue) else { return XCTFail("json is nil") }
178             let param = Parameter(["Test": "Test"])
179             let api = APIResponse(api: API(endpointPath: Endpoint.midnightBattle.rawValue), parameter: param, json: json)
180             
181             let command = BattleCommand(apiResponse: api)
182             command.execute()
183         }
184         
185         // 艦娘HP更新
186         do {
187             let rawValue: [String: Any] = [
188                 "api_result": 1
189             ]
190             guard let json = JSON(rawValue: rawValue) else { return XCTFail("json is nil") }
191             let param = Parameter(["Test": "Test"])
192             let api = APIResponse(api: API(endpointPath: Endpoint.battleResult.rawValue), parameter: param, json: json)
193             
194             let command = BattleCommand(apiResponse: api)
195             command.execute()
196         }
197         
198         // HPチェック
199         do {
200             let store = ServerDataStore.oneTimeEditor()
201             let ships = store.ships(byDeckId: fleet)
202             
203             XCTAssertEqual(ships.count, seventh ? 7 : 6)
204             
205             XCTAssertEqual(ships[0].nowhp, shipsHp[0] - 1)
206             XCTAssertEqual(ships[1].nowhp, shipsHp[1] - 2)
207             XCTAssertEqual(ships[2].nowhp, shipsHp[2] - 3)
208             XCTAssertEqual(ships[3].nowhp, shipsHp[3] - 4)
209             XCTAssertEqual(ships[4].nowhp, shipsHp[4] - 5)
210             XCTAssertEqual(ships[5].nowhp, shipsHp[5] - 6 - 7)
211             if seventh {
212                 XCTAssertEqual(ships[6].nowhp, shipsHp[6] - 7 - 7)
213             }
214         }
215     }
216     
217     func damageControl(_ fleet: Int) {
218         
219         // ダメコンを設定
220         do {
221             let store = ServerDataStore.oneTimeEditor()
222             
223             store.ship(by: 4).flatMap {
224                 $0.nowhp = $0.maxhp
225                 $0.slot_ex = 63765  // 女神
226             }
227             store.ship(by: 5).flatMap {
228                 $0.nowhp = $0.maxhp
229                 // ダメコン
230                 $0.equippedItem = store.slotItem(by: 72418).map { NSOrderedSet(array: [$0]) } ?? []
231             }
232         }
233         
234         // 戦闘(夜戦)
235         do {
236             let rawValue: [String: Any] = [
237                 "api_result": 1,
238                 "api_data": [
239                     "api_hougeki": [
240                         "api_df_list": [
241                             [2],
242                             [3],
243                             [4],
244                             [10]
245                         ],
246                         "api_damage": [
247                             [50],
248                             [50],
249                             [50],
250                             [20]
251                         ],
252                         "api_at_eflag": [
253                             1,
254                             1,
255                             1,
256                             0
257                         ]
258                     ]
259                 ]
260             ]
261             guard let json = JSON(rawValue: rawValue) else { return XCTFail("json is nil") }
262             let param = Parameter(["Test": "Test"])
263             let api = APIResponse(api: API(endpointPath: Endpoint.midnightBattle.rawValue), parameter: param, json: json)
264             
265             let command = BattleCommand(apiResponse: api)
266             command.execute()
267         }
268         
269         // 艦娘HP更新
270         do {
271             let rawValue: [String: Any] = [
272                 "api_result": 1
273             ]
274             guard let json = JSON(rawValue: rawValue) else { return XCTFail("json is nil") }
275             let param = Parameter(["Test": "Test"])
276             let api = APIResponse(api: API(endpointPath: Endpoint.battleResult.rawValue), parameter: param, json: json)
277             
278             let command = BattleCommand(apiResponse: api)
279             command.execute()
280         }
281         
282         // HPチェック
283         do {
284             let store = ServerDataStore.oneTimeEditor()
285             let ships = store.ships(byDeckId: fleet)
286             
287             XCTAssertEqual(ships.count, 6)
288             
289             XCTAssertEqual(ships[0].nowhp, shipsHp[0])
290             XCTAssertEqual(ships[1].nowhp, shipsHp[1])
291             XCTAssertEqual(ships[2].nowhp, 0)
292             XCTAssertEqual(ships[3].nowhp, shipsHp[3])
293             XCTAssertEqual(ships[4].nowhp, Int(Double(shipsHp[4]) * 0.2))
294         }
295     }
296     
297     func testFirstFleet() {
298         
299         initBattleFleet(1)
300         normalBattle(1)
301         initBattleFleet(1)
302         damageControl(1)
303         clear(1)
304     }
305     func testSecondFleet() {
306         
307         initBattleFleet(2)
308         normalBattle(2)
309         initBattleFleet(2)
310         damageControl(2)
311         clear(2)
312     }
313     func testThiredFleet() {
314         
315         initBattleFleet(3)
316         normalBattle(3)
317         initBattleFleet(3)
318         damageControl(3)
319         initBattleFleet(3, seventh: true)
320         normalBattle(3, seventh: true)
321         clear(3)
322     }
323     func testFourthFleet() {
324         
325         initBattleFleet(4)
326         normalBattle(4)
327         initBattleFleet(4)
328         damageControl(4)
329         clear(4)
330     }
331 }