OSDN Git Service

guard の書き方を統一した
[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         controller = NSArrayController()
46         
47         super.init()
48         
49         controller.managedObjectContext = ServerDataStore.default.context
50         controller.entityName = Deck.entityName
51         controller.fetchPredicate = NSPredicate(format: "id = %ld", number)
52         controller.automaticallyRearrangesObjects = true
53         controller.fetch(nil)
54         
55         bind(NSBindingName(#keyPath(state)), to: controller, withKeyPath: "selection.mission_0")
56         bind(NSBindingName(#keyPath(missionId)), to: controller, withKeyPath: "selection.mission_1")
57         bind(NSBindingName(#keyPath(milliseconds)), to: controller, withKeyPath: "selection.mission_2")
58         bind(NSBindingName(#keyPath(fleetName)), to: controller, withKeyPath: "selection.name")
59     }
60     
61     deinit {
62         
63         unbind(NSBindingName(#keyPath(state)))
64         unbind(NSBindingName(#keyPath(missionId)))
65         unbind(NSBindingName(#keyPath(milliseconds)))
66         unbind(NSBindingName(#keyPath(fleetName)))
67     }
68     
69     private func updateState() {
70         
71         guard let state = state as? Int,
72             let stat = State(rawValue: state) else {
73                 
74                 print("unknown State")
75                 return
76         }
77         
78         if stat == .none || stat == .finish {
79             
80             if stat == .none { didNotify = false }
81             
82             name = nil
83             time = nil
84             
85             return
86         }
87         
88         guard let missionId = self.missionId as? Int else { return }
89         
90         guard let mission = ServerDataStore.default.masterMission(by: missionId) else {
91             
92             name = "Unknown"
93             DispatchQueue(label: "MissionStatus")
94                 .asyncAfter(deadline: .now() + 0.33) {
95                     self.updateState()
96             }
97             return
98         }
99         
100         name = mission.name
101     }
102     
103     func update() {
104         
105         if name == nil {
106             
107             time = nil
108             
109             return
110         }
111         
112         guard let milliSeconds = milliseconds as? Int else {
113             
114             name = nil
115             time = nil
116             return
117         }
118         
119         let compTime = TimeInterval(Int(milliSeconds / 1_000))
120         let diff = compTime - Date().timeIntervalSince1970
121         
122         realTime = diff < 0 ? 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 = NSLocalizedString("%@ Will Return From Mission.", comment: "%@ Will Return From Mission.")
131         notification.title = String(format: format, fleetName)
132         let txtFormat = NSLocalizedString("%@ Will Return From %@.", comment: "%@ Will Return From %@.")
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 }