forked from aarronc/hutton-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.py
322 lines (229 loc) · 9.58 KB
/
progress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
Track the trucker's progress.
"""
import sys
try:
# for Python2
import Tkinter as tk
except ImportError:
# for python 3
import tkinter as tk
import collections
import datetime
import math
import time
import plugin
import xmit
import myNotebook as nb
COLUMNS = ['day', 'week'] # 'total' also works
ROW_CONFIGURATION = [
("Jumps", 'Jumps', 'ShowExploProgress'),
("Light Years", 'LY', 'ShowExploProgress'),
("Scanned Objects", 'ScannedObjects', 'ShowExploProgress'),
("Data Sold (CR)", 'Exploration', 'ShowExploProgress'),
("Cargo Bought (t)", 'CargoBought', 'ShowCargoProgress'),
("Cargo Sold (t)", 'CargoSold', 'ShowCargoProgress'),
("Cargo Smuggled (t)", 'SmuggledCargo', 'ShowCargoProgress'),
("Missions (points)", 'Mission', 'ShowMissionProgress'),
("Passengers (count)", "PassengersTransported", "ShowMissionProgress"),
("Bounties (CR)", 'Bounty', 'ShowCombatProgress'),
("Combat Bonds (CR)", 'CombatBonds', 'ShowCombatProgress'),
("Thargoids (count)", 'ThargoidsKilled', 'ShowCombatProgress'),
]
def capitalise(word):
"Capitalise the ``word``."
return word[:1].upper() + word[1:]
def render(value):
"Render a ``value`` for display."
if isinstance(value, float):
return '{:,.0f}'.format(value)
elif value is None:
return '-'
else:
return str(value)
def varname(varbase, when):
"Determine a variable name for the JSON."
return '{}{}'.format(varbase, capitalise(when))
def cardinals():
"Nobody expects the Spanish Inquisition!"
delta = (datetime.datetime.utcnow() - datetime.datetime(2015, 11, 23)).total_seconds()
week = int(delta / 604800)
day = int(math.ceil((delta % 604800) / 86400))
return dict(week=week, day=day)
class ProgressDisplay(tk.Frame):
"Displays progress."
def __init__(self, parent, helper=None, **kwargs):
"Initialise the ``ProgressDisplay``."
tk.Frame.__init__(self, parent, relief=tk.SUNKEN, **kwargs)
self.columnconfigure(0, weight=1)
self.helper = helper
self.cmdr = None
self.textvariables = {}
self.heading_labels = list(self.__create_headings())
self.row_labels = list(self.__create_rows())
self.footer_label = self.__create_footer()
self.data = None
self.update()
def __create_headings(self):
"Create labels for the headings."
yield tk.Label(self, text="Progress:", anchor=tk.W)
for when in COLUMNS:
yield tk.Label(self, text=capitalise(when), anchor=tk.E)
def __create_rows(self):
"Create labels for each row."
for title, varbase, _configvar in ROW_CONFIGURATION:
yield list(self.__create_row_cells(title, varbase))
def __create_row_cells(self, title, varbase):
"Create labels for one row."
yield tk.Label(self, text=title, anchor=tk.W)
for when in COLUMNS:
key = varname(varbase, when)
textvariable = self.textvariables[key] = tk.StringVar(value='-')
yield tk.Label(self, textvariable=textvariable, anchor=tk.E)
def __create_footer(self):
"Create our footer."
return tk.Label(self, text="Progress display not ready.", anchor=tk.W)
self.data = None
def update(self, data=None):
"Update our display based on the config."
# This wasn't this complicated until I decided I wanted to show
# truckers each line only if there was data AND they'd made some
# progress AND they were interested in seeing it.
if data:
self.data = data
any_enabled_has_progress = False
sticky = tk.EW
for idx, (_title, varbase, configvar) in enumerate(ROW_CONFIGURATION):
enabled = self.helper.prefs.setdefault(configvar, True)
row_visible = False
for column, when in enumerate(COLUMNS, start=1):
key = varname(varbase, when)
if self.data and key in self.data:
value = float(self.data[key])
has_progress = not not value
else:
value = None
has_progress = False
self.textvariables[key].set(render(value))
if enabled:
any_enabled_has_progress = any_enabled_has_progress or has_progress
if has_progress:
row_visible = True
for column, label in enumerate(self.row_labels[idx]):
if row_visible:
label.grid(row=idx + 1, column=column, sticky=sticky)
else:
label.grid_forget()
self.__display_header_and_footer(any_enabled_has_progress)
return not not self.data
def __display_header_and_footer(self, any_has_progress):
"Display the header and footer."
footer_row = len(ROW_CONFIGURATION) + 1
if any_has_progress:
self.footer_label.grid_forget()
self.heading_labels[0].grid(row=0, column=0, sticky=tk.EW)
card = cardinals()
for column, when in enumerate(COLUMNS, start=1):
label = self.heading_labels[column]
label.grid(row=0, column=column, sticky=tk.EW)
label['text'] = '{} {}'.format(capitalise(when), render(card[when]))
else:
for label in self.heading_labels:
label.grid_forget()
if self.data:
self.footer_label['text'] = "No progress this week. Get truckin'!"
else:
self.footer_label['text'] = "Progress display not ready." # not shown because ``not ready```
self.footer_label.grid(row=footer_row, column=0, columnspan=1 + len(COLUMNS), sticky=tk.EW)
class ProgressPlugin(plugin.HuttonHelperPlugin):
"Provides progress updates."
fetch_events = set([
'FSDJump',
'MarketBuy',
'MarketSell',
'MissionCompleted',
'RedeemVoucher',
'Scan',
'SellExplorationData',
'FactionKillBond',
])
config_intvars = [
# I wanted this to be a dictionary, but couldn't preserve the order without stunt coding
('ShowExploProgress', "Show Exploration Progress"),
('ShowCargoProgress', "Show Cargo Hauling Progress"),
('ShowMissionProgress', "Show Mission Progress"),
('ShowCombatProgress', "Show Combat Progress")
]
def __init__(self, helper):
"Initialise the ``ProgressPlugin``."
plugin.HuttonHelperPlugin.__init__(self, helper)
self.display = None
self.data = None
self.cmdr = None
self.lastfetch = datetime.datetime.now()
self.fetching = False
def __reset(self, cmdr=None):
"Reset our numbers to switch commander."
self.cmdr = cmdr
self.data = None
def journal_entry(self, cmdr, _is_beta, _system, _station, entry, _state):
"Act like a tiny EDMC plugin. Forward events to our ``TRACKERS``."
if self.cmdr != cmdr or entry['event'] == 'ShutDown':
self.__reset(cmdr=cmdr)
if self.data is None or entry['event'] in self.fetch_events:
self.display.after(250, self.__fetch)
def __fetch(self):
"Fetch the data again."
if not self.cmdr:
return
if not datetime.datetime.now() > self.lastfetch + datetime.timedelta(seconds = 5):
#sys.stderr.write("Too soon...\r\n")
return
if self.fetching:
return
try:
self.lastfetch = datetime.datetime.now()
self.fetching = True
self.data = xmit.get('/day-week-stats.json/{}'.format(self.cmdr))
if self.display:
self.ready = self.display.update(self.data)
self.refresh()
finally:
self.fetching = False
def plugin_app(self, parent):
"Called once to get the plugin widget. Return a ``tk.Frame``."
frame = tk.Frame(parent)
frame.columnconfigure(0, weight=1)
tk.Frame(frame, highlightthickness=1).grid(pady=5, sticky=tk.EW) # divider
self.display = ProgressDisplay(frame, helper=self.helper)
self.display.grid(sticky=tk.EW)
self.ready = self.display.update()
self.__initialise_prefs()
return frame
def __initialise_prefs(self):
"Initialise the preference system."
self.prefs_intvars = {}
all_disabled = True
for key, text in self.config_intvars:
enabled = self.helper.prefs.setdefault(key, True)
all_disabled = all_disabled and not enabled
self.prefs_intvars[key] = tk.IntVar(value=1 if enabled else 0)
self.hidden = all_disabled
def plugin_prefs(self, parent, cmdr, is_beta):
"Called each time the user opens EDMC settings. Return an ``nb.Frame``."
frame = nb.Frame(parent)
frame.columnconfigure(0, weight=1)
nb.Label(frame, text="Progress Display Options :-").grid(sticky=tk.W)
for key, text in self.config_intvars:
variable = self.prefs_intvars[key]
nb.Checkbutton(frame, text=text, variable=variable).grid(sticky=tk.W)
return frame
def prefs_changed(self, _cmdr, _is_beta):
"Called when the user clicks OK on the settings dialog."
all_disabled = True
for key, _text in self.config_intvars:
value = self.prefs_intvars[key].get()
all_disabled = all_disabled and not value
self.helper.prefs[key] = bool(value)
self.hidden = all_disabled
self.ready = self.display.update()