OSDN Git Service

SwiftyJSONを導入
[kcd/KCD.git] / KCD / DestroyItem2Command.swift
1 //
2 //  DestroyItem2Command.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/10.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 class DestroyItem2Command: JSONCommand {
12     override class func canExecuteAPI(_ api: String) -> Bool {
13         if api == "/kcsapi/api_req_kousyou/destroyitem2" { return true }
14         return false
15     }
16     
17     override func execute() {
18         guard let itemIds = arguments["api_slotitem_ids"]?
19             .components(separatedBy: ",")
20             .flatMap({ Int($0) })
21             else { return print("api_slotitem_ids is wrong") }
22         let store = ServerDataStore.oneTimeEditor()
23         store.slotItems(in: itemIds)
24             .forEach { store.delete($0) }
25         
26         guard let material = store.material()
27             else { return print("Material is not found") }
28         guard let gm = data["api_get_material"].arrayObject as? [Int]
29             else { return print("api_get_material is wrong") }
30         let resouces = ["fuel", "bull", "steel", "bauxite"]
31         zip(gm, resouces).forEach {
32             if let current = material.value(forKey: $0.1) as? Int {
33                 material.setValue((current + $0.0) as NSNumber, forKey: $0.1)
34             }
35         }
36     }
37 }