OSDN Git Service

varを使わないようにした
[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
19 extension Notification.Name {
20     
21     static let CombinedDidCange = Notification.Name("com.masakih.KCD.Notification.CombinedDidCange")
22 }
23
24 final class CombinedCommand: JSONCommand {
25     
26     static let userInfoKey = "com.masakih.KCD.Notification.CombinedDidCange.CombinedType"
27     
28     override class func canExecuteAPI(_ api: String) -> Bool {
29         
30         if api == "/kcsapi/api_req_hensei/combined" { return true }
31         
32         return false
33     }
34     
35     override func execute() {
36         
37         if api == "/kcsapi/api_port/port" {
38             
39             if let t = data["api_combined_flag"].int {
40                 
41                 CombineType(rawValue: t).map(postNotification(withType:))
42                 
43             } else {
44                 
45                 postNotification(withType: .cancel)
46             }
47             
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 postNotification(withType type: CombineType) {
58         
59         let userInfo = [CombinedCommand.userInfoKey: type]
60         
61         NotificationCenter.default.post(name: .CombinedDidCange, object: self, userInfo: userInfo)
62     }
63 }