/
Primes.py
458 lines (391 loc) · 14.5 KB
/
Primes.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import wx
from wx.lib.masked import NumCtrl
import os
import sys
import math
import Utils
import Model
from ReorderableGrid import ReorderableGrid
from FinishStrip import ShowFinishStrip
from ReadSignOnSheet import ExcelLink
from GetResults import GetResults
from RaceInputState import RaceInputState
def getWinnerInfo( bib ):
race = Model.race
if not race or not bib:
return u''
try:
riderInfo = race.excelLink.read()[bib]
except Exception as e:
return u''
fields = [
u' '.join( f for f in (riderInfo.get('FirstName',u''), riderInfo.get('LastName',u'')) if f ),
riderInfo.get('Team', u''),
]
return u', '.join( f for f in fields if f )
# Do not change the number codes.
with Utils.SuspendTranslation():
EffortChoices = (
(0, _('Pack')),
(1, _('Break')),
(2, _('Chase')),
(-1, _('Custom')), # This one must be last.
)
class Primes( wx.Panel ):
def __init__( self, parent, id=wx.ID_ANY, size=wx.DefaultSize ):
super(Primes, self).__init__( parent, id, size=size )
self.state = RaceInputState()
vsOverall = wx.BoxSizer( wx.VERTICAL )
#---------------------------------------------------------------
self.colNameFields = (
(_('Prime For'), 'effortType', 's'),
(_('or Custom'), 'effortCustom', 's'),
(_('Position'), 'position', 'i'),
(_('Laps\nTo Go'), 'lapsToGo', 'i'),
(_('Sponsor'), 'sponsor', 's'),
(_('Cash'), 'cash', 'f'),
(_('Points'), 'points', 'i'),
(_('Merchandise'), 'merchandise', 's'),
(_('Winner\nBib'), 'winnerBib', 'i'),
(u'', 'winnerInfo', 's'),
)
self.colnames = [colName for colName, fieldName, dataType in self.colNameFields]
self.iCol = dict( (fieldName, i) for i, (colName, fieldName, dataType) in enumerate(self.colNameFields) if fieldName )
self.grid = ReorderableGrid( self )
self.grid.CreateGrid( 0, len(self.colNameFields) )
GetTranslation = _
for col, (colName, fieldName, dataType) in enumerate(self.colNameFields):
self.grid.SetColLabelValue( col, colName )
attr = wx.grid.GridCellAttr()
if fieldName == 'effortType':
attr.SetEditor( wx.grid.GridCellChoiceEditor(choices=[GetTranslation(name) for code, name in EffortChoices]) )
attr.SetAlignment( wx.ALIGN_CENTRE, wx.ALIGN_TOP )
elif fieldName == 'position':
attr.SetAlignment( wx.ALIGN_CENTRE, wx.ALIGN_TOP )
elif fieldName == 'winnerInfo':
attr.SetReadOnly( True )
elif dataType == 'i':
attr.SetAlignment( wx.ALIGN_RIGHT, wx.ALIGN_TOP )
attr.SetEditor( wx.grid.GridCellFloatEditor(precision=0) )
attr.SetRenderer( wx.grid.GridCellFloatRenderer(precision=0) )
elif dataType == 'f':
attr.SetAlignment( wx.ALIGN_RIGHT, wx.ALIGN_TOP )
attr.SetEditor( wx.grid.GridCellFloatEditor(precision=2) )
attr.SetRenderer( wx.grid.GridCellFloatRenderer(precision=2) )
attr.SetAlignment( wx.ALIGN_RIGHT, wx.ALIGN_TOP )
self.grid.SetColAttr( col, attr )
if fieldName == 'lapsToGo':
self.lapsToGoCol = col
self.grid.Bind( wx.grid.EVT_GRID_CELL_CHANGE, self.onCellChange )
self.grid.AutoSizeColumns( False )
self.grid.AutoSizeRows( False )
#---------------------------------------------------------------
self.photosButton = wx.Button( self, label=u'{}...'.format(_('Photos')) )
self.photosButton.Bind( wx.EVT_BUTTON, self.onPhotos )
self.finishStrip = wx.Button( self, label=u'{}...'.format(_('Finish Strip')) )
self.finishStrip.Bind( wx.EVT_BUTTON, self.onFinishStrip )
self.history = wx.Button( self, label=u'{}...'.format(_('History')) )
self.history.Bind( wx.EVT_BUTTON, self.onHistory )
self.newButton = wx.Button( self, id=wx.ID_NEW )
self.newButton.SetToolTip( wx.ToolTip(_('Create a new Prime')) )
self.newButton.Bind( wx.EVT_BUTTON, self.onNew )
self.nextPositionButton = wx.Button( self, label=('Next Position') )
self.nextPositionButton.SetToolTip( wx.ToolTip(_('Create a Prime from an Existing Prime for the Next Position')) )
self.nextPositionButton.Bind( wx.EVT_BUTTON, self.onNextPosition )
self.nextPrimeButton = wx.Button( self, label=('Next Prime') )
self.nextPrimeButton.SetToolTip( wx.ToolTip(_('Create a Prime from an Existing Prime')) )
self.nextPrimeButton.Bind( wx.EVT_BUTTON, self.onNextPrime )
self.deleteButton = wx.Button( self, id=wx.ID_DELETE )
self.deleteButton.SetToolTip( wx.ToolTip(_('Delete a Prime')) )
self.deleteButton.Bind( wx.EVT_BUTTON, self.onDelete )
hsButtons = wx.BoxSizer( wx.HORIZONTAL )
hsButtons.Add( self.photosButton, flag=wx.ALL, border=4 )
hsButtons.Add( self.finishStrip, flag=wx.ALL, border=4 )
hsButtons.Add( self.history, flag=wx.ALL, border=4 )
hsButtons.AddStretchSpacer()
hsButtons.Add( self.newButton, flag=wx.ALL, border=4 )
hsButtons.Add( self.nextPositionButton, flag=wx.ALL, border=4 )
hsButtons.Add( self.nextPrimeButton, flag=wx.ALL, border=4 )
hsButtons.Add( self.deleteButton, flag=wx.ALL, border=4 )
#---------------------------------------------------------------
vsOverall.Add( self.grid, 1, flag=wx.EXPAND|wx.ALL, border=4 )
vsOverall.Add( hsButtons, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=4 )
self.SetSizer( vsOverall )
def onCellChange( self, event ):
row, col = event.GetRow(), event.GetCol()
colName = self.colNameFields[col][1]
GetTranslation = _
if colName == 'effortCustom':
if self.grid.GetCellValue(row, col).strip():
self.grid.SetCellValue( row, col-1, GetTranslation('Custom') )
elif colName == 'effortType':
if self.grid.GetCellValue(row, col) != 'Custom':
self.grid.SetCellValue( row, col+1, u'' )
elif colName == 'winnerBib':
bib = int( u''.join(c for c in self.grid.GetCellValue(row, col) if c.isdigit()) )
self.grid.SetCellValue( row, col+1, getWinnerInfo(bib) )
wx.CallAfter( self.grid.AutoSizeColumns, False )
def getT( self ):
race = Model.race
if not race:
return 0
row = self.grid.GetGridCursorRow()
if row is None or row < 0:
return
lapsToGo = int( u''.join(c for c in self.grid.GetCellValue(row, self.lapsToGoCol) if c.isdigit()) )
tMax = 0.0
for rr in GetResults(None):
try:
tMax = min( tMax, rr.raceTimes[-1-lapsToGo] )
except IndexError:
pass
return tMax
def onPhotos( self, event ):
mainWin = Utils.getMainWin()
if not mainWin:
return
mainWin.photoDialog.SetT( self.getT() )
mainWin.photoDialog.Show()
def onFinishStrip( self, event ):
ShowFinishStrip( self, self.getT() )
def onHistory( self, event ):
mainWin = Utils.getMainWin()
if not mainWin:
return
mainWin.openMenuWindow( 'history' )
def selectGridRow( self, row ):
self.grid.SelectRow( row )
self.grid.SetGridCursor( row, 0 )
self.grid.ShowCellEditControl()
def onNew( self, event ):
race = Model.race
if not race:
Utils.MessageOK( self, _('You must have a Race to create a Prime.'), _('Missing Race') )
return
self.commit()
race.primes = getattr(race, 'primes', [])
rowNew = len( race.primes )
race.primes.append( {} )
self.updateGrid()
self.selectGridRow( rowNew )
def onNextPosition( self, event ):
rowNext = self.grid.GetGridCursorRow()
if rowNext is None or rowNext < 0:
return
self.commit()
race = Model.race
if not race:
Utils.MessageOK( self, _('You must have a Race to create a next Prime.'), _('Missing Race') )
return
nextPrime = race.primes[rowNext].copy()
nextPoints = {
(1, 5): 3,
(2, 3): 2,
(3, 2): 1,
}.get( (nextPrime['position'], nextPrime['points']), None )
if nextPoints is not None:
nextPrime['points'] = nextPoints
try:
nextPrime['position'] += 1
except:
pass
nextPrime['winnerBib'] = None
race.primes = race.primes[:rowNext+1] + [nextPrime] + race.primes[rowNext+1:]
self.updateGrid()
self.selectGridRow( rowNext + 1 )
def onNextPrime( self, event ):
rowNext = self.grid.GetGridCursorRow()
if rowNext is None or rowNext < 0:
return
self.commit()
race = Model.race
if not race:
Utils.MessageOK( self, _('You must have a Race to create a next Prime.'), _('Missing Race') )
return
nextPrime = race.primes[rowNext].copy()
nextPrime['position'] = 1
if nextPrime['points']:
nextPrime['points'] = 5
if nextPrime['lapsToGo'] > 0:
nextPrime['lapsToGo'] -= 1
nextPrime['winnerBib'] = None
race.primes = race.primes[:rowNext+1] + [nextPrime] + race.primes[rowNext+1:]
self.updateGrid()
self.selectGridRow( rowNext + 1 )
def onDelete( self, event ):
rowDelete = self.grid.GetGridCursorRow()
if rowDelete is None or rowDelete < 0:
return
self.commit()
race = Model.race
if race and Utils.MessageOKCancel( self, u'{}: {} ?'.format(_('Delete Prime'), rowDelete+1), _('Confirm Delete Primes') ):
race.primes = getattr(race, 'primes', [])
try:
del race.primes[rowDelete]
except Exception as e:
return
self.updateGrid()
if race.primes:
self.grid.SetGridCursor( rowDelete, 0 )
def getSponsors( self ):
race = Model.race
if not race:
return []
sponsors = [prime.get('sponsor', u'') for prime in getattr(race, 'primes', [])] + [race.organizer]
sponsors = [s for s in sponsors if s]
return sponsors
def getMerchandise( self ):
race = Model.race
if not race:
return []
merchandise = [prime.get('merchandise', u'') for prime in getattr(race, 'primes', [])]
merchandise = [m for m in merchandise if m]
return merchandise
def setRow( self, prime, row, updateGrid=True ):
GetTranslation = _
data = []
for col, (name, attr, dataType) in enumerate(self.colNameFields):
if attr == 'effortType':
effortType = prime.get('effortType', 'Pack')
v = GetTranslation(effortType)
elif attr == 'position':
position = prime.get('position', 1)
v = u'' if position == 0 else unicode(position)
elif attr == 'winnerBib':
winnerBib = prime.get('winnerBib', None)
v = u'' if not winnerBib else unicode(winnerBib)
elif attr == 'winnerInfo':
v = getWinnerInfo(winnerBib)
elif attr == 'cash':
cash = prime.get('cash', 0.0)
v = u'{:.2f}'.format( prime.get('cash', 0.0) ) if cash else u''
else:
v = unicode(prime.get(attr, u''))
if updateGrid:
self.grid.SetCellValue( row, col, v )
data.append( v )
return data
def getRow( self, row ):
values = {}
for col, (name, attr, dataType) in enumerate(self.colNameFields):
v = self.grid.GetCellValue( row, col ).strip()
if dataType == 'i':
v = u''.join( c for c in v if c.isdigit() )
v = int( v or 0 )
elif dataType == 'f':
v = u''.join( c for c in v if c.isdigit() or c == '.')
v = float( v or 0.0 )
values[attr] = v
GetTranslation = _
for code, name in EffortChoices:
if values['effortType'] == GetTranslation(name):
values['effortType'] = name
break
if values['effortCustom']:
values['effortType'] = 'Custom'
return values
def updateGrid( self ):
race = Model.race
if not race or not getattr(race, 'primes', None):
self.grid.ClearGrid()
return
Utils.AdjustGridSize( self.grid, len(race.primes) )
for row, prime in enumerate(race.primes):
self.setRow( prime, row )
self.grid.AutoSizeColumns( False ) # Resize to fit the column name.
self.grid.AutoSizeRows( False )
def refresh( self ):
if self.state.changed():
self.updateGrid()
def commit( self ):
self.grid.SaveEditControlValue() # Make sure the current edit is committed.
self.grid.DisableCellEditControl()
race = Model.race
if not race:
return
race.primes = [self.getRow(row) for row in xrange(self.grid.GetNumberRows())]
def GetGrid():
race = Model.race
if not race:
return {}
primes = getattr( race, 'primes', [] )
if not primes:
return {}
try:
excelLink = race.excelLink
externalFields = set(excelLink.getFields())
externalInfo = excelLink.read()
except:
excelLink = None
externalFields = set()
externalInfo = {}
title = u'\n'.join( [race.name, Utils.formatDate(race.date), u'Primes'] )
rightJustifyCols = set([0, 1])
colnames = [u'Prime', _('Bib'),]
hasName = ('FirstName' in externalFields or 'LastName' in externalFields)
if hasName:
colnames.append( _('Name') )
hasTeam = 'Team' in externalFields
if hasTeam:
colnames.append( _('Team') )
rightJustifyCols.add( len(colnames) )
colnames.append( _('Laps to go') )
colnames.append( _('For') )
hasCash = any( prime['cash'] for prime in primes)
hasMerchandise = any( prime['merchandise'] for prime in primes)
if not hasCash and not hasMerchandise:
hasCash = True
if hasCash:
rightJustifyCols.add( len(colnames) )
colnames.append( _('Cash') )
if hasMerchandise:
colnames.append( _('Merchandise') )
colnames.append( _('Sponsor') )
leftJustifyCols = set( i for i in xrange(len(colnames)) if i not in rightJustifyCols )
data = [[] for c in xrange(len(colnames))] # Column oriented table.
GetTranslation = _
for p, prime in enumerate(primes):
row = []
bib = prime['winnerBib']
info = externalInfo.get( bib, {} )
row.append( p+1 )
row.append( bib or u'' )
if hasName:
row.append( u', '.join( f for f in [info.get('LastName', u''), info.get('FirstName', u'')] if f ) )
if hasTeam:
row.append( info.get('Team', u'') )
row.append( prime['lapsToGo'] )
effortType = prime['effortType']
row.append( GetTranslation(effortType) if effortType != 'Custom' else prime.get('effortCustom',u'') )
if hasCash:
row.append( u'{:.2f}'.format(prime.get('cash',0.0)) )
if hasMerchandise:
row.append( prime.get('merchandise',u'') )
row.append( prime.get('sponsor',u'') )
for c, v in enumerate(row):
data[c].append( unicode(v) )
return {'title':title, 'colnames':colnames, 'data':data, 'leftJustifyCols':leftJustifyCols}
if __name__ == '__main__':
app = wx.App(False)
app.SetAppName("CrossMgr")
Utils.disable_stdout_buffering()
race = Model.newRace()
race._populate()
fnameRiderInfo = os.path.join(Utils.getHomeDir(), 'SimulationRiderData.xlsx')
sheetName = 'RiderData'
race.excelLink = ExcelLink()
race.excelLink.setFileName( fnameRiderInfo )
race.excelLink.setSheetName( sheetName )
race.excelLink.setFieldCol( {'Bib#':0, 'LastName':1, 'FirstName':2, 'Team':3} )
race.primes = [
{'sponsor': u'11111111111111', 'cash': 100, 'merchandise': u'', 'winnerBib': 101, 'lapsToGo': 7 },
{'sponsor': u'22222222222222', 'cash': 200, 'merchandise': u'', 'winnerBib': 110, 'lapsToGo': 6 },
{'sponsor': u'33333333333333', 'cash': 0, 'merchandise': u'Water bottle', 'winnerBib': 115, 'lapsToGo': 5 },
{'sponsor': u'44444444444444', 'cash': 300, 'merchandise': u'', 'winnerBib': 199, 'lapsToGo': 4 },
{'sponsor': u'55555555555555', 'cash': 0.51, 'merchandise': u'New bike', 'winnerBib': 101, 'lapsToGo': 3 },
]
mainWin = wx.Frame(None, title="Primes", size=(800,700) )
primes = Primes( mainWin )
mainWin.Show()
primes.refresh()
app.MainLoop()