OSDN Git Service

省略できる記法があればそれを用いるようにした
[kcd/KCD.git] / KCD / StoreCreateSlotItemHistoryCommand.swift
1 //
2 //  StoreCreateSlotItemHistoryCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/11.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 class StoreCreateSlotItemHistoryCommand: JSONCommand {
12     override func execute() {
13         guard let fuel = arguments["api_item1"].flatMap({ Int($0) }),
14             let bull = arguments["api_item2"].flatMap({ Int($0) }),
15             let steel = arguments["api_item3"].flatMap({ Int($0) }),
16             let bauxite = arguments["api_item4"].flatMap({ Int($0) })
17             else { return print("Parameter is Wrong") }
18         
19         guard let data = json[dataKey] as? [String: Any],
20             let success = data["api_create_flag"] as? Bool
21             else { return print("api_create_flag is wrong") }
22         let name = masterSlotItemName(sccess: success, data: data)
23         let numberOfUsedKaihatuSizai = success ? 1 : 0
24         
25         let store = ServerDataStore.default
26         guard let ship0 = store.deck(byId: 1)?.ship_0,
27             let flagShip = store.ship(byId: ship0)
28             else { return print("Flagship is not found") }
29         
30         guard let basic = store.basic()
31             else { return print("Basic is wrong") }
32         
33         let localStore = LocalDataStore.oneTimeEditor()
34         guard let newHistory = localStore.createKaihatuHistory()
35             else { return print("Can not create new KaihatuHistory entry") }
36         newHistory.name = name
37         newHistory.fuel = fuel
38         newHistory.bull = bull
39         newHistory.steel = steel
40         newHistory.bauxite = bauxite
41         newHistory.kaihatusizai = numberOfUsedKaihatuSizai
42         newHistory.flagShipLv = flagShip.lv
43         newHistory.flagShipName = flagShip.name
44         newHistory.commanderLv = basic.level
45         newHistory.date = Date()
46     }
47     
48     private func masterSlotItemName(sccess: Bool, data: [String: Any]) -> String {
49         if !sccess {
50             return NSLocalizedString("fail to develop", comment: "fail to develop")
51         }
52         guard let si = data["api_slot_item"] as? [String: Any],
53             let slotItemId = si["api_slotitem_id"] as? Int
54             else {
55                 print("api_slotitem_id is wrong")
56                 return ""
57         }
58         
59         return ServerDataStore.default.masterSlotItem(by: slotItemId)?.name ?? ""
60     }
61 }