OSDN Git Service

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