OSDN Git Service

関数を直接引数に渡せる場合はtaling cloureを使わないようにした
[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 final class DestroyItem2Command: JSONCommand {
12     
13     override class func canExecuteAPI(_ api: String) -> Bool {
14         
15         if api == "/kcsapi/api_req_kousyou/destroyitem2" { return true }
16         
17         return false
18     }
19     
20     override func execute() {
21         
22         guard let itemIds = parameter["api_slotitem_ids"]
23             .string?
24             .components(separatedBy: ",")
25             .flatMap({ Int($0) }) else {
26                 
27                 print("api_slotitem_ids is wrong")
28                 return
29         }
30         
31         let store = ServerDataStore.oneTimeEditor()
32         
33         store.slotItems(in: itemIds).forEach(store.delete)
34         
35         guard let material = store.material() else {
36             
37             print("Material is not found")
38             return
39         }
40         guard let gm = data["api_get_material"].arrayObject as? [Int] else {
41             
42             print("api_get_material is wrong")
43             return
44         }
45         
46         let resouces = [#keyPath(Material.fuel), #keyPath(Material.bull), #keyPath(Material.steel), #keyPath(Material.bauxite)]
47         
48         zip(gm, resouces).forEach {
49             
50             if let current = material.value(forKey: $0.1) as? Int {
51                 
52                 material.setValue((current + $0.0) as NSNumber, forKey: $0.1)
53             }
54         }
55         
56     }
57 }