OSDN Git Service

洋上補給の補強増設用のショートネームをつけた
[kcd/KCD.git] / KCD / CombinedCommand.swift
1 //
2 //  CombinedCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/09.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 enum CombineType: Int {
12     
13     case cancel
14     case maneuver
15     case water
16     case transportation
17     
18     func displayName() -> String {
19         
20         switch self {
21         case .cancel: return LocalizedStrings.uncombined.string
22         case .maneuver: return LocalizedStrings.maneuver.string
23         case .water: return LocalizedStrings.water.string
24         case .transportation: return LocalizedStrings.transportation.string
25         }
26     }
27 }
28
29 extension Notification.Name {
30     
31     static let CombinedDidCange = Notification.Name("com.masakih.KCD.Notification.CombinedDidCange")
32 }
33
34 final class CombinedCommand: JSONCommand {
35     
36     static let userInfoKey = "com.masakih.KCD.Notification.CombinedDidCange.CombinedType"
37     
38     override class func canExecuteAPI(_ api: API) -> Bool {
39         
40         return api.endpoint == .henseiCombined
41     }
42     
43     override func execute() {
44         
45         if api.endpoint == .port {
46             
47             handlePort()
48             return
49         }
50         
51         parameter["api_combined_type"]
52             .int
53             .flatMap { CombineType(rawValue: $0) }
54             .map(postNotification(withType:))
55     }
56     
57     private func handlePort() {
58         
59         if let t = data["api_combined_flag"].int {
60             
61             CombineType(rawValue: t).map(postNotification(withType:))
62             
63         } else {
64             
65             postNotification(withType: .cancel)
66         }
67     }
68     
69     private func postNotification(withType type: CombineType) {
70         
71         let userInfo = [CombinedCommand.userInfoKey: type]
72         
73         NotificationCenter.default.post(name: .CombinedDidCange, object: self, userInfo: userInfo)
74     }
75 }