OSDN Git Service

関数名を変更
[kcd/KCD.git] / KCD / MapStartCommand.swift
1 //
2 //  MapStartCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/18.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class MapStartCommand: JSONCommand {
12     
13     private let store = TemporaryDataStore.oneTimeEditor()
14     
15     override class func canExecuteAPI(_ api: API) -> Bool {
16         
17         return api.type == .map
18     }
19     
20     override func execute() {
21         
22         switch api.endpoint {
23         case .start: startBattle()
24             
25         case .next:
26             nextCell()
27             updateBattleCell()
28             
29         default: return Logger.shared.log("Missing API: \(apiResponse.api)")
30         }
31         
32         GuardShelterCommand(apiResponse: apiResponse).execute()
33     }
34     
35     private func startBattle() {
36         
37         store.battles().forEach(store.delete)
38         
39         guard let deckId = parameter["api_deck_id"].int,
40             let mapArea = parameter["api_maparea_id"].int,
41             let mapInfo = parameter["api_mapinfo_no"].int else {
42                 
43                 return Logger.shared.log("startBattle Arguments is wrong")
44         }
45         
46         guard let no = data["api_no"].int else {
47             
48             return Logger.shared.log("startBattle JSON is wrong")
49         }
50         
51         guard let battle = store.createBattle() else {
52             
53             return Logger.shared.log("Can not create Battle")
54         }
55         
56         let kcd = ServerDataStore.default
57         
58         battle.deckId = deckId
59         battle.mapArea = mapArea
60         battle.mapInfo = mapInfo
61         battle.no = no
62         battle.firstFleetShipsCount = kcd.ships(byDeckId: deckId).count
63     }
64     
65     private func nextCell() {
66         
67         guard let cellNumber = data["api_no"].int,
68             let eventId = data["api_event_id"].int else {
69                 
70                 return Logger.shared.log("updateBattleCell JSON is wrong")
71         }
72         
73         guard let battle = store.battle() else { return Logger.shared.log("Battle is invalid.") }
74         
75         battle.no = cellNumber
76         battle.isBossCell = (eventId == 5)
77     }
78     
79     private func updateBattleCell() {
80         
81         store.battle().map { $0.battleCell = nil }
82     }
83 }