OSDN Git Service

コメントを追加
[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 invalidate() {
69         
70         name = nil
71         time = nil
72     }
73     
74     private func updateState() {
75         
76         guard let state = state as? Int,
77             let stat = State(rawValue: state) else {
78                 
79                 return Logger.shared.log("unknown State")
80         }
81         
82         if stat == .none || stat == .finish {
83             
84             if stat == .none { didNotify = false }
85             
86             invalidate()
87             return
88         }
89         
90         guard let missionId = self.missionId as? Int else { return }
91         
92         guard let mission = ServerDataStore.default.masterMission(by: missionId) else {
93             
94             name = "Unknown"
95             DispatchQueue(label: "MissionStatus").asyncAfter(deadline: .now() + 0.33) {
96                 
97                 self.updateState()
98             }
99             return
100         }
101         
102         name = mission.name
103     }
104     
105     func update() {
106         
107         if name == nil {
108             
109             time = nil
110             return
111         }
112         
113         guard let milliSeconds = milliseconds as? Int else {
114             
115             invalidate()
116             return
117         }
118         
119         let compTime = TimeInterval(Int(milliSeconds / 1_000))
120         let diff = compTime - Date().timeIntervalSince1970
121         
122         realTime = max(0, diff)
123         
124         if didNotify { return }
125         if diff >= 1 * 60 { return }
126         
127         guard let fleetName = fleetName else { return }
128         
129         let notification = NSUserNotification()
130         let format = LocalizedStrings.missionWillReturnMessage.string
131         notification.title = String(format: format, fleetName)
132         let txtFormat = LocalizedStrings.missionWillReturnInformation.string
133         notification.informativeText = String(format: txtFormat, fleetName, name!)
134         
135         if UserDefaults.standard[.playFinishMissionSound] {
136             
137             notification.soundName = NSUserNotificationDefaultSoundName
138         }
139         
140         NSUserNotificationCenter.default.deliver(notification)
141         
142         didNotify = true
143     }
144 }