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 enum MapAPI: String {
12     
13     case start = "/kcsapi/api_req_map/start"
14     case next = "/kcsapi/api_req_map/next"
15 }
16
17 final class MapStartCommand: JSONCommand {
18     
19     private let store = TemporaryDataStore.oneTimeEditor()
20     
21     override class func canExecuteAPI(_ api: String) -> Bool {
22         
23         return MapAPI(rawValue: api) != nil ? true : false
24     }
25     
26     override func execute() {
27         
28         MapAPI(rawValue: api).map {
29             
30             switch $0 {
31             case .start: startBattle()
32                 
33             case .next:
34                 nextCell()
35                 updateBattleCell()
36             }
37         }
38         
39         GuardShelterCommand(apiResponse: apiResponse).execute()
40     }
41     
42     private func startBattle() {
43         
44         store.battles().forEach { store.delete($0) }
45         
46         guard let deckId = parameter["api_deck_id"].int,
47             let mapArea = parameter["api_maparea_id"].int,
48             let mapInfo = parameter["api_mapinfo_no"].int
49             else { return print("startBattle Arguments is wrong") }
50         
51         guard let no = data["api_no"].int
52             else { return print("startBattle JSON is wrong") }
53         
54         guard let battle = store.createBattle()
55             else { return print("Can not create Battle") }
56         
57         battle.deckId = deckId
58         battle.mapArea = mapArea
59         battle.mapInfo = mapInfo
60         battle.no = no
61     }
62     
63     private func nextCell() {
64         
65         guard let cellNumber = data["api_no"].int,
66             let eventId = data["api_event_id"].int
67             else { return print("updateBattleCell JSON is wrong") }
68         
69         guard let battle = store.battle()
70             else { return print("Battle is invalid.") }
71         
72         battle.no = cellNumber
73         battle.isBossCell = (eventId == 5)
74     }
75     
76     private func updateBattleCell() {
77         
78         store.battle().map { $0.battleCell = nil }
79     }
80 }