OSDN Git Service

バージョンを1.9b33に更新
[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
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 else {
49                 
50                 return Logger.shared.log("startBattle Arguments is wrong")
51         }
52         
53         guard let no = data["api_no"].int else {
54             
55             return Logger.shared.log("startBattle JSON is wrong")
56         }
57         
58         guard let battle = store.createBattle() else {
59             
60             return Logger.shared.log("Can not create Battle")
61         }
62         
63         let kcd = ServerDataStore.default
64         
65         battle.deckId = deckId
66         battle.mapArea = mapArea
67         battle.mapInfo = mapInfo
68         battle.no = no
69         battle.firstFleetShipsCount = kcd.ships(byDeckId: deckId).count
70     }
71     
72     private func nextCell() {
73         
74         guard let cellNumber = data["api_no"].int,
75             let eventId = data["api_event_id"].int else {
76                 
77                 return Logger.shared.log("updateBattleCell JSON is wrong")
78         }
79         
80         guard let battle = store.battle() else { return Logger.shared.log("Battle is invalid.") }
81         
82         battle.no = cellNumber
83         battle.isBossCell = (eventId == 5)
84     }
85     
86     private func updateBattleCell() {
87         
88         store.battle().map { $0.battleCell = nil }
89     }
90 }