OSDN Git Service

buildEquipmentEnhancementListをSwift4.0に更新
[kcd/KCD.git] / KCD / MissionStatus.swift
1
2 //
3 //  MissionStatus.swift
4 //  KCD
5 //
6 //  Created by Hori,Masaki on 2017/01/23.
7 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
8 //
9
10 import Cocoa
11
12 private enum State: Int {
13     
14     case none = 0
15     case hasMission = 1
16     case finish = 2
17     case earlyReturn = 3
18 }
19
20 final class MissionStatus: NSObject {
21     
22     private let number: Int
23     private let controller = NSArrayController()
24     private var didNotify = false
25     private var realTime: TimeInterval = 0.0 {
26         
27         didSet { time = realTime as NSNumber }
28     }
29     
30     @objc dynamic var name: String?
31     @objc dynamic var time: NSNumber?
32     @objc dynamic var state: NSNumber?
33     @objc dynamic var missionId: NSNumber? {
34         
35         didSet { updateState() }
36     }
37     @objc dynamic var milliseconds: NSNumber?
38     @objc dynamic var fleetName: String?
39     
40     init?(number: Int) {
41         
42         guard case 2...4 = number else { return nil }
43         
44         self.number = number
45         
46         super.init()
47         
48         controller.managedObjectContext = ServerDataStore.default.context
49         controller.entityName = Deck.entityName
50         controller.fetchPredicate = NSPredicate(#keyPath(Deck.id), equal: number)
51         controller.automaticallyRearrangesObjects = true
52         controller.fetch(nil)
53         
54         bind(NSBindingName(#keyPath(state)), to: controller, withKeyPath: "selection.mission_0")
55         bind(NSBindingName(#keyPath(missionId)), to: controller, withKeyPath: "selection.mission_1")
56         bind(NSBindingName(#keyPath(milliseconds)), to: controller, withKeyPath: "selection.mission_2")
57         bind(NSBindingName(#keyPath(fleetName)), to: controller, withKeyPath: "selection.name")
58     }
59     
60     deinit {
61         
62         unbind(NSBindingName(#keyPath(state)))
63         unbind(NSBindingName(#keyPath(missionId)))
64         unbind(NSBindingName(#keyPath(milliseconds)))
65         unbind(NSBindingName(#keyPath(fleetName)))
66     }
67     
68     private func updateState() {
69         
70         guard let state = state as? Int,
71             let stat = State(rawValue: state) else {
72                 
73                 return Logger.shared.log("unknown State")
74         }
75         
76         if stat == .none || stat == .finish {
77             
78             if stat == .none { didNotify = false }
79             
80             name = nil
81             time = nil
82             
83             return
84         }
85         
86         guard let missionId = self.missionId as? Int else { return }
87         
88         guard let mission = ServerDataStore.default.masterMission(by: missionId) else {
89             
90             name = "Unknown"
91             DispatchQueue(label: "MissionStatus").asyncAfter(deadline: .now() + 0.33) {
92                 
93                 self.updateState()
94             }
95             return
96         }
97         
98         name = mission.name
99     }
100     
101     func update() {
102         
103         if name == nil {
104             
105             time = nil
106             
107             return
108         }
109         
110         guard let milliSeconds = milliseconds as? Int else {
111             
112             name = nil
113             time = nil
114             return
115         }
116         
117         let compTime = TimeInterval(Int(milliSeconds / 1_000))
118         let diff = compTime - Date().timeIntervalSince1970
119         
120         realTime = max(0, diff)
121         
122         if didNotify { return }
123         if diff >= 1 * 60 { return }
124         
125         guard let fleetName = fleetName else { return }
126         
127         let notification = NSUserNotification()
128         let format = LocalizedStrings.missionWillReturnMessage.string
129         notification.title = String(format: format, fleetName)
130         let txtFormat = LocalizedStrings.missionWillReturnInformation.string
131         notification.informativeText = String(format: txtFormat, fleetName, name!)
132         
133         if UserDefaults.standard[.playFinishMissionSound] {
134             
135             notification.soundName = NSUserNotificationDefaultSoundName
136         }
137         
138         NSUserNotificationCenter.default.deliver(notification)
139         
140         didNotify = true
141     }
142 }